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