blob: 9eb6e54eecc7340ea7e0010b517bfdf2c1b16a39 [file] [log] [blame]
Chris Lattner22ee3eb2002-05-24 20:42:13 +00001//===- FunctionResolution.cpp - Resolve declarations to implementations ---===//
2//
3// Loop over the functions that are in the module and look for functions that
4// have the same name. More often than not, there will be things like:
5//
6// declare void %foo(...)
7// void %foo(int, int) { ... }
8//
9// because of the way things are declared in C. If this is the case, patch
10// things up.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Transforms/CleanupGCCOutput.h"
15#include "llvm/Module.h"
16#include "llvm/Function.h"
Chris Lattnerabe6c3d2002-05-24 21:33:26 +000017#include "llvm/BasicBlock.h"
Chris Lattner22ee3eb2002-05-24 20:42:13 +000018#include "llvm/SymbolTable.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Pass.h"
21#include "llvm/iOther.h"
Chris Lattnerabe6c3d2002-05-24 21:33:26 +000022#include "llvm/Constant.h"
Chris Lattner22ee3eb2002-05-24 20:42:13 +000023#include "Support/StatisticReporter.h"
24#include <iostream>
25#include <algorithm>
26
27using std::vector;
28using std::string;
29using std::cerr;
30
31namespace {
32 Statistic<>NumResolved("funcresolve\t- Number of varargs functions resolved");
33
34 struct FunctionResolvingPass : public Pass {
35 const char *getPassName() const { return "Resolve Functions"; }
36
37 bool run(Module *M);
38 };
39}
40
41Pass *createFunctionResolvingPass() {
42 return new FunctionResolvingPass();
43}
44
45// ConvertCallTo - Convert a call to a varargs function with no arg types
46// specified to a concrete nonvarargs function.
47//
48static void ConvertCallTo(CallInst *CI, Function *Dest) {
49 const FunctionType::ParamTypes &ParamTys =
50 Dest->getFunctionType()->getParamTypes();
51 BasicBlock *BB = CI->getParent();
52
53 // Get an iterator to where we want to insert cast instructions if the
54 // argument types don't agree.
55 //
56 BasicBlock::iterator BBI = find(BB->begin(), BB->end(), CI);
57 assert(BBI != BB->end() && "CallInst not in parent block?");
58
Chris Lattnerabe6c3d2002-05-24 21:33:26 +000059 assert(CI->getNumOperands()-1 == ParamTys.size() &&
Chris Lattner22ee3eb2002-05-24 20:42:13 +000060 "Function calls resolved funny somehow, incompatible number of args");
61
62 vector<Value*> Params;
63
64 // Convert all of the call arguments over... inserting cast instructions if
65 // the types are not compatible.
66 for (unsigned i = 1; i < CI->getNumOperands(); ++i) {
67 Value *V = CI->getOperand(i);
68
69 if (V->getType() != ParamTys[i-1]) { // Must insert a cast...
70 Instruction *Cast = new CastInst(V, ParamTys[i-1]);
71 BBI = BB->getInstList().insert(BBI, Cast)+1;
72 V = Cast;
73 }
74
75 Params.push_back(V);
76 }
77
Chris Lattnerabe6c3d2002-05-24 21:33:26 +000078 Instruction *NewCall = new CallInst(Dest, Params);
79
Chris Lattner22ee3eb2002-05-24 20:42:13 +000080 // Replace the old call instruction with a new call instruction that calls
81 // the real function.
82 //
Chris Lattnerabe6c3d2002-05-24 21:33:26 +000083 BBI = BB->getInstList().insert(BBI, NewCall)+1;
84
85 // Remove the old call instruction from the program...
86 BB->getInstList().remove(BBI);
87
88 // Replace uses of the old instruction with the appropriate values...
89 //
90 if (NewCall->getType() == CI->getType()) {
91 CI->replaceAllUsesWith(NewCall);
92 NewCall->setName(CI->getName());
93
94 } else if (NewCall->getType() == Type::VoidTy) {
95 // Resolved function does not return a value but the prototype does. This
96 // often occurs because undefined functions default to returning integers.
97 // Just replace uses of the call (which are broken anyway) with dummy
98 // values.
99 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
100 } else if (CI->getType() == Type::VoidTy) {
101 // If we are gaining a new return value, we don't have to do anything
102 // special.
103 } else {
104 assert(0 && "This should have been checked before!");
105 abort();
106 }
107
108 // The old instruction is no longer needed, destroy it!
109 delete CI;
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000110}
111
112
113bool FunctionResolvingPass::run(Module *M) {
114 SymbolTable *ST = M->getSymbolTable();
115 if (!ST) return false;
116
117 std::map<string, vector<Function*> > Functions;
118
119 // Loop over the entries in the symbol table. If an entry is a func pointer,
120 // then add it to the Functions map. We do a two pass algorithm here to avoid
121 // problems with iterators getting invalidated if we did a one pass scheme.
122 //
123 for (SymbolTable::iterator I = ST->begin(), E = ST->end(); I != E; ++I)
124 if (const PointerType *PT = dyn_cast<PointerType>(I->first))
125 if (isa<FunctionType>(PT->getElementType())) {
126 SymbolTable::VarMap &Plane = I->second;
127 for (SymbolTable::type_iterator PI = Plane.begin(), PE = Plane.end();
128 PI != PE; ++PI) {
129 const string &Name = PI->first;
130 Functions[Name].push_back(cast<Function>(PI->second));
131 }
132 }
133
134 bool Changed = false;
135
136 // Now we have a list of all functions with a particular name. If there is
137 // more than one entry in a list, merge the functions together.
138 //
139 for (std::map<string, vector<Function*> >::iterator I = Functions.begin(),
140 E = Functions.end(); I != E; ++I) {
141 vector<Function*> &Functions = I->second;
142 Function *Implementation = 0; // Find the implementation
143 Function *Concrete = 0;
144 for (unsigned i = 0; i < Functions.size(); ) {
145 if (!Functions[i]->isExternal()) { // Found an implementation
146 assert(Implementation == 0 && "Multiple definitions of the same"
147 " function. Case not handled yet!");
148 Implementation = Functions[i];
149 } else {
150 // Ignore functions that are never used so they don't cause spurious
151 // warnings... here we will actually DCE the function so that it isn't
152 // used later.
153 //
154 if (Functions[i]->use_size() == 0) {
155 M->getFunctionList().remove(Functions[i]);
156 delete Functions[i];
157 Functions.erase(Functions.begin()+i);
158 Changed = true;
159 ++NumResolved;
160 continue;
161 }
162 }
163
164 if (Functions[i] && (!Functions[i]->getFunctionType()->isVarArg())) {
165 if (Concrete) { // Found two different functions types. Can't choose
166 Concrete = 0;
167 break;
168 }
169 Concrete = Functions[i];
170 }
171 ++i;
172 }
173
174 if (Functions.size() > 1) { // Found a multiply defined function...
175 // We should find exactly one non-vararg function definition, which is
176 // probably the implementation. Change all of the function definitions
177 // and uses to use it instead.
178 //
179 if (!Concrete) {
180 cerr << "Warning: Found functions types that are not compatible:\n";
181 for (unsigned i = 0; i < Functions.size(); ++i) {
182 cerr << "\t" << Functions[i]->getType()->getDescription() << " %"
183 << Functions[i]->getName() << "\n";
184 }
185 cerr << " No linkage of functions named '" << Functions[0]->getName()
186 << "' performed!\n";
187 } else {
188 for (unsigned i = 0; i < Functions.size(); ++i)
189 if (Functions[i] != Concrete) {
190 Function *Old = Functions[i];
191 const FunctionType *OldMT = Old->getFunctionType();
192 const FunctionType *ConcreteMT = Concrete->getFunctionType();
193 bool Broken = false;
194
Chris Lattnerabe6c3d2002-05-24 21:33:26 +0000195 assert((Old->getReturnType() == Concrete->getReturnType() ||
196 Concrete->getReturnType() == Type::VoidTy ||
197 Old->getReturnType() == Type::VoidTy) &&
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000198 "Differing return types not handled yet!");
199 assert(OldMT->getParamTypes().size() <=
200 ConcreteMT->getParamTypes().size() &&
201 "Concrete type must have more specified parameters!");
202
203 // Check to make sure that if there are specified types, that they
204 // match...
205 //
206 for (unsigned i = 0; i < OldMT->getParamTypes().size(); ++i)
207 if (OldMT->getParamTypes()[i] != ConcreteMT->getParamTypes()[i]) {
208 cerr << "Parameter types conflict for" << OldMT
209 << " and " << ConcreteMT;
210 Broken = true;
211 }
212 if (Broken) break; // Can't process this one!
213
214
215 // Attempt to convert all of the uses of the old function to the
Chris Lattnerabe6c3d2002-05-24 21:33:26 +0000216 // concrete form of the function. If there is a use of the fn that
217 // we don't understand here we punt to avoid making a bad
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000218 // transformation.
219 //
220 // At this point, we know that the return values are the same for
221 // our two functions and that the Old function has no varargs fns
222 // specified. In otherwords it's just <retty> (...)
223 //
224 for (unsigned i = 0; i < Old->use_size(); ) {
225 User *U = *(Old->use_begin()+i);
226 if (CastInst *CI = dyn_cast<CastInst>(U)) {
227 // Convert casts directly
228 assert(CI->getOperand(0) == Old);
229 CI->setOperand(0, Concrete);
230 Changed = true;
231 ++NumResolved;
232 } else if (CallInst *CI = dyn_cast<CallInst>(U)) {
233 // Can only fix up calls TO the argument, not args passed in.
234 if (CI->getCalledValue() == Old) {
235 ConvertCallTo(CI, Concrete);
236 Changed = true;
237 ++NumResolved;
238 } else {
239 cerr << "Couldn't cleanup this function call, must be an"
240 << " argument or something!" << CI;
241 ++i;
242 }
243 } else {
244 cerr << "Cannot convert use of function: " << U << "\n";
245 ++i;
246 }
247 }
248 }
249 }
250 }
251 }
252
253 return Changed;
254}