blob: 844bc0027d1b09e8df64b159057ea25e6ca7fb0c [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
Chris Lattner5aa9e3e2002-05-24 20:42:13 +000032namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000033 Statistic<>NumResolved("funcresolve", "Number of varargs functions resolved");
Chris Lattner013eca002002-10-09 21:10:06 +000034 Statistic<> NumGlobals("funcresolve", "Number of global variables resolved");
Chris Lattner5aa9e3e2002-05-24 20:42:13 +000035
36 struct FunctionResolvingPass : public Pass {
Chris Lattner4cd99ff2003-10-22 03:35:34 +000037 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
38 AU.addRequired<TargetData>();
39 }
40
Chris Lattner113f4f42002-06-25 16:13:24 +000041 bool run(Module &M);
Chris Lattner5aa9e3e2002-05-24 20:42:13 +000042 };
Chris Lattnera2c09852002-07-26 21:12:44 +000043 RegisterOpt<FunctionResolvingPass> X("funcresolve", "Resolve Functions");
Chris Lattner5aa9e3e2002-05-24 20:42:13 +000044}
45
46Pass *createFunctionResolvingPass() {
47 return new FunctionResolvingPass();
48}
49
Chris Lattnerb2d9f7d2003-01-30 18:22:32 +000050static bool ResolveFunctions(Module &M, std::vector<GlobalValue*> &Globals,
Chris Lattner013eca002002-10-09 21:10:06 +000051 Function *Concrete) {
52 bool Changed = false;
53 for (unsigned i = 0; i != Globals.size(); ++i)
54 if (Globals[i] != Concrete) {
55 Function *Old = cast<Function>(Globals[i]);
56 const FunctionType *OldMT = Old->getFunctionType();
57 const FunctionType *ConcreteMT = Concrete->getFunctionType();
58
Chris Lattner9810b942003-04-28 01:23:29 +000059 if (OldMT->getParamTypes().size() > ConcreteMT->getParamTypes().size() &&
Chris Lattner50cbb902003-03-03 19:57:46 +000060 !ConcreteMT->isVarArg())
Chris Lattnerdbb05b02003-02-27 20:55:48 +000061 if (!Old->use_empty()) {
62 std::cerr << "WARNING: Linking function '" << Old->getName()
63 << "' is causing arguments to be dropped.\n";
64 std::cerr << "WARNING: Prototype: ";
65 WriteAsOperand(std::cerr, Old);
66 std::cerr << " resolved to ";
67 WriteAsOperand(std::cerr, Concrete);
68 std::cerr << "\n";
69 }
Chris Lattner013eca002002-10-09 21:10:06 +000070
71 // Check to make sure that if there are specified types, that they
72 // match...
73 //
Chris Lattnerdbb05b02003-02-27 20:55:48 +000074 unsigned NumArguments = std::min(OldMT->getParamTypes().size(),
75 ConcreteMT->getParamTypes().size());
76
Chris Lattner50cbb902003-03-03 19:57:46 +000077 if (!Old->use_empty() && !Concrete->use_empty())
78 for (unsigned i = 0; i < NumArguments; ++i)
Chris Lattner6fc0ee92003-08-23 20:03:05 +000079 if (OldMT->getParamTypes()[i] != ConcreteMT->getParamTypes()[i])
Chris Lattner015d98e2003-08-20 23:50:38 +000080 if (OldMT->getParamTypes()[i]->getPrimitiveID() !=
Chris Lattner6fc0ee92003-08-23 20:03:05 +000081 ConcreteMT->getParamTypes()[i]->getPrimitiveID()) {
82 std::cerr << "WARNING: Function [" << Old->getName()
83 << "]: Parameter types conflict for: '" << OldMT
84 << "' and '" << ConcreteMT << "'\n";
Chris Lattner015d98e2003-08-20 23:50:38 +000085 return Changed;
Chris Lattner6fc0ee92003-08-23 20:03:05 +000086 }
Chris Lattner013eca002002-10-09 21:10:06 +000087
Chris Lattner9810b942003-04-28 01:23:29 +000088 // Attempt to convert all of the uses of the old function to the concrete
89 // form of the function. If there is a use of the fn that we don't
90 // understand here we punt to avoid making a bad transformation.
Chris Lattner013eca002002-10-09 21:10:06 +000091 //
Chris Lattner9810b942003-04-28 01:23:29 +000092 // At this point, we know that the return values are the same for our two
93 // functions and that the Old function has no varargs fns specified. In
94 // otherwords it's just <retty> (...)
Chris Lattner013eca002002-10-09 21:10:06 +000095 //
Chris Lattnere9340222003-07-23 22:03:18 +000096 if (!Old->use_empty()) { // Avoid making the CPR unless we really need it
97 Value *Replacement = Concrete;
98 if (Concrete->getType() != Old->getType())
99 Replacement = ConstantExpr::getCast(ConstantPointerRef::get(Concrete),
100 Old->getType());
101 NumResolved += Old->use_size();
102 Old->replaceAllUsesWith(Replacement);
103 }
Chris Lattner08043682003-05-31 21:08:45 +0000104
105 // Since there are no uses of Old anymore, remove it from the module.
106 M.getFunctionList().erase(Old);
Chris Lattner013eca002002-10-09 21:10:06 +0000107 }
108 return Changed;
109}
110
111
Chris Lattnerb2d9f7d2003-01-30 18:22:32 +0000112static bool ResolveGlobalVariables(Module &M,
113 std::vector<GlobalValue*> &Globals,
Chris Lattner013eca002002-10-09 21:10:06 +0000114 GlobalVariable *Concrete) {
115 bool Changed = false;
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000116 Constant *CCPR = ConstantPointerRef::get(Concrete);
117
Chris Lattner013eca002002-10-09 21:10:06 +0000118 for (unsigned i = 0; i != Globals.size(); ++i)
119 if (Globals[i] != Concrete) {
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000120 Constant *Cast = ConstantExpr::getCast(CCPR, Globals[i]->getType());
121 Globals[i]->replaceAllUsesWith(Cast);
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000122
Chris Lattner013eca002002-10-09 21:10:06 +0000123 // Since there are no uses of Old anymore, remove it from the module.
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000124 M.getGlobalList().erase(cast<GlobalVariable>(Globals[i]));
Chris Lattner013eca002002-10-09 21:10:06 +0000125
126 ++NumGlobals;
127 Changed = true;
128 }
129 return Changed;
130}
131
Chris Lattner4cd99ff2003-10-22 03:35:34 +0000132static bool ProcessGlobalsWithSameName(Module &M, TargetData &TD,
Chris Lattnerb2d9f7d2003-01-30 18:22:32 +0000133 std::vector<GlobalValue*> &Globals) {
Chris Lattner013eca002002-10-09 21:10:06 +0000134 assert(!Globals.empty() && "Globals list shouldn't be empty here!");
135
136 bool isFunction = isa<Function>(Globals[0]); // Is this group all functions?
Chris Lattner013eca002002-10-09 21:10:06 +0000137 GlobalValue *Concrete = 0; // The most concrete implementation to resolve to
138
Chris Lattner013eca002002-10-09 21:10:06 +0000139 for (unsigned i = 0; i != Globals.size(); ) {
140 if (isa<Function>(Globals[i]) != isFunction) {
141 std::cerr << "WARNING: Found function and global variable with the "
142 << "same name: '" << Globals[i]->getName() << "'.\n";
143 return false; // Don't know how to handle this, bail out!
144 }
145
Chris Lattner52b8fc02002-11-10 03:36:55 +0000146 if (isFunction) {
Chris Lattner013eca002002-10-09 21:10:06 +0000147 // For functions, we look to merge functions definitions of "int (...)"
148 // to 'int (int)' or 'int ()' or whatever else is not completely generic.
149 //
150 Function *F = cast<Function>(Globals[i]);
Chris Lattner5997c3d2002-11-08 00:38:20 +0000151 if (!F->isExternal()) {
Chris Lattner52b8fc02002-11-10 03:36:55 +0000152 if (Concrete && !Concrete->isExternal())
Chris Lattner013eca002002-10-09 21:10:06 +0000153 return false; // Found two different functions types. Can't choose!
154
155 Concrete = Globals[i];
Chris Lattner52b8fc02002-11-10 03:36:55 +0000156 } else if (Concrete) {
157 if (Concrete->isExternal()) // If we have multiple external symbols...x
158 if (F->getFunctionType()->getNumParams() >
159 cast<Function>(Concrete)->getFunctionType()->getNumParams())
160 Concrete = F; // We are more concrete than "Concrete"!
161
162 } else {
163 Concrete = F;
Chris Lattner013eca002002-10-09 21:10:06 +0000164 }
Chris Lattner013eca002002-10-09 21:10:06 +0000165 } else {
Chris Lattner013eca002002-10-09 21:10:06 +0000166 GlobalVariable *GV = cast<GlobalVariable>(Globals[i]);
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000167 if (!GV->isExternal()) {
168 if (Concrete) {
169 std::cerr << "WARNING: Two global variables with external linkage"
170 << " exist with the same name: '" << GV->getName()
171 << "'!\n";
172 return false;
Chris Lattner013eca002002-10-09 21:10:06 +0000173 }
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000174 Concrete = GV;
Chris Lattner013eca002002-10-09 21:10:06 +0000175 }
Chris Lattner013eca002002-10-09 21:10:06 +0000176 }
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000177 ++i;
Chris Lattner013eca002002-10-09 21:10:06 +0000178 }
179
180 if (Globals.size() > 1) { // Found a multiply defined global...
Chris Lattner2b132962003-05-31 21:57:06 +0000181 // If there are no external declarations, and there is at most one
182 // externally visible instance of the global, then there is nothing to do.
183 //
184 bool HasExternal = false;
185 unsigned NumInstancesWithExternalLinkage = 0;
186
187 for (unsigned i = 0, e = Globals.size(); i != e; ++i) {
188 if (Globals[i]->isExternal())
189 HasExternal = true;
190 else if (!Globals[i]->hasInternalLinkage())
191 NumInstancesWithExternalLinkage++;
192 }
193
194 if (!HasExternal && NumInstancesWithExternalLinkage <= 1)
195 return false; // Nothing to do? Must have multiple internal definitions.
196
197
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000198 std::cerr << "WARNING: Found global types that are not compatible:\n";
199 for (unsigned i = 0; i < Globals.size(); ++i) {
200 std::cerr << "\t" << *Globals[i]->getType() << " %"
201 << Globals[i]->getName() << "\n";
Chris Lattner013eca002002-10-09 21:10:06 +0000202 }
203
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000204 if (!Concrete)
205 Concrete = Globals[0];
Chris Lattner4cd99ff2003-10-22 03:35:34 +0000206 else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Concrete)) {
207 // Handle special case hack to change globals if it will make their types
208 // happier in the long run. The situation we do this is intentionally
209 // extremely limited.
210 if (GV->use_empty() && GV->hasInitializer() &&
211 GV->getInitializer()->isNullValue()) {
212 // Check to see if there is another (external) global with the same size
213 // and a non-empty use-list. If so, we will make IT be the real
214 // implementation.
215 unsigned TS = TD.getTypeSize(Concrete->getType()->getElementType());
216 for (unsigned i = 0, e = Globals.size(); i != e; ++i)
217 if (Globals[i] != Concrete && !Globals[i]->use_empty() &&
218 isa<GlobalVariable>(Globals[i]) &&
219 TD.getTypeSize(Globals[i]->getType()->getElementType()) == TS) {
220 // At this point we want to replace Concrete with Globals[i]. Make
221 // concrete external, and Globals[i] have an initializer.
222 GlobalVariable *NGV = cast<GlobalVariable>(Globals[i]);
223 const Type *ElTy = NGV->getType()->getElementType();
224 NGV->setInitializer(Constant::getNullValue(ElTy));
225 cast<GlobalVariable>(Concrete)->setInitializer(0);
226 Concrete = NGV;
227 break;
228 }
229 }
230 }
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000231
Chris Lattner013eca002002-10-09 21:10:06 +0000232 if (isFunction)
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000233 return ResolveFunctions(M, Globals, cast<Function>(Concrete));
Chris Lattner013eca002002-10-09 21:10:06 +0000234 else
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000235 return ResolveGlobalVariables(M, Globals,
236 cast<GlobalVariable>(Concrete));
Chris Lattner013eca002002-10-09 21:10:06 +0000237 }
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000238 return false;
Chris Lattner013eca002002-10-09 21:10:06 +0000239}
240
Chris Lattner113f4f42002-06-25 16:13:24 +0000241bool FunctionResolvingPass::run(Module &M) {
Chris Lattnerb2d9f7d2003-01-30 18:22:32 +0000242 std::map<std::string, std::vector<GlobalValue*> > Globals;
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000243
Chris Lattner12b35932003-10-22 04:42:20 +0000244 // Loop over the globals, adding them to the Globals map. We use a two pass
245 // algorithm here to avoid problems with iterators getting invalidated if we
246 // did a one pass scheme.
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000247 //
Chris Lattner12b35932003-10-22 04:42:20 +0000248 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
249 Function *F = I++;
250 if (F->use_empty() && F->isExternal())
251 M.getFunctionList().erase(F);
252 else if (!F->hasInternalLinkage() && !F->getName().empty())
253 Globals[F->getName()].push_back(F);
254 }
255
256 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ) {
257 GlobalVariable *GV = I++;
258 if (GV->use_empty() && GV->isExternal())
259 M.getGlobalList().erase(GV);
260 else if (!GV->hasInternalLinkage() && !GV->getName().empty())
261 Globals[GV->getName()].push_back(GV);
262 }
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000263
264 bool Changed = false;
265
Chris Lattner4cd99ff2003-10-22 03:35:34 +0000266 TargetData &TD = getAnalysis<TargetData>();
267
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000268 // Now we have a list of all functions with a particular name. If there is
269 // more than one entry in a list, merge the functions together.
270 //
Chris Lattnerb2d9f7d2003-01-30 18:22:32 +0000271 for (std::map<std::string, std::vector<GlobalValue*> >::iterator
272 I = Globals.begin(), E = Globals.end(); I != E; ++I)
Chris Lattner4cd99ff2003-10-22 03:35:34 +0000273 Changed |= ProcessGlobalsWithSameName(M, TD, I->second);
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000274
Chris Lattner52b8fc02002-11-10 03:36:55 +0000275 // Now loop over all of the globals, checking to see if any are trivially
276 // dead. If so, remove them now.
277
278 for (Module::iterator I = M.begin(), E = M.end(); I != E; )
279 if (I->isExternal() && I->use_empty()) {
280 Function *F = I;
281 ++I;
282 M.getFunctionList().erase(F);
283 ++NumResolved;
284 Changed = true;
285 } else {
286 ++I;
287 }
288
289 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; )
290 if (I->isExternal() && I->use_empty()) {
291 GlobalVariable *GV = I;
292 ++I;
293 M.getGlobalList().erase(GV);
294 ++NumGlobals;
295 Changed = true;
296 } else {
297 ++I;
298 }
299
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000300 return Changed;
301}