blob: 683512689e31466e80d4362d665b621abf0135d2 [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) {
Chris Lattner7f20ea72002-07-18 03:01:24 +0000125 Function *F = cast<Function>(PI->second);
126 assert(PI->first == F->getName() &&
127 "Function name and symbol table do not agree!");
128 if (F->hasExternalLinkage()) // Only resolve decls to external fns
129 Functions[PI->first].push_back(F);
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000130 }
131 }
132
133 bool Changed = false;
134
135 // Now we have a list of all functions with a particular name. If there is
136 // more than one entry in a list, merge the functions together.
137 //
138 for (std::map<string, vector<Function*> >::iterator I = Functions.begin(),
139 E = Functions.end(); I != E; ++I) {
140 vector<Function*> &Functions = I->second;
141 Function *Implementation = 0; // Find the implementation
142 Function *Concrete = 0;
143 for (unsigned i = 0; i < Functions.size(); ) {
144 if (!Functions[i]->isExternal()) { // Found an implementation
Chris Lattner7f20ea72002-07-18 03:01:24 +0000145 if (Implementation != 0)
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000146 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 //
Chris Lattner7e708292002-06-25 16:13:24 +0000154 if (Functions[i]->use_empty()) {
155 M.getFunctionList().erase(Functions[i]);
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000156 Functions.erase(Functions.begin()+i);
157 Changed = true;
158 ++NumResolved;
159 continue;
160 }
161 }
162
163 if (Functions[i] && (!Functions[i]->getFunctionType()->isVarArg())) {
164 if (Concrete) { // Found two different functions types. Can't choose
165 Concrete = 0;
166 break;
167 }
168 Concrete = Functions[i];
169 }
170 ++i;
171 }
172
173 if (Functions.size() > 1) { // Found a multiply defined function...
174 // We should find exactly one non-vararg function definition, which is
175 // probably the implementation. Change all of the function definitions
176 // and uses to use it instead.
177 //
178 if (!Concrete) {
179 cerr << "Warning: Found functions types that are not compatible:\n";
180 for (unsigned i = 0; i < Functions.size(); ++i) {
181 cerr << "\t" << Functions[i]->getType()->getDescription() << " %"
182 << Functions[i]->getName() << "\n";
183 }
184 cerr << " No linkage of functions named '" << Functions[0]->getName()
185 << "' performed!\n";
186 } else {
187 for (unsigned i = 0; i < Functions.size(); ++i)
188 if (Functions[i] != Concrete) {
189 Function *Old = Functions[i];
190 const FunctionType *OldMT = Old->getFunctionType();
191 const FunctionType *ConcreteMT = Concrete->getFunctionType();
192 bool Broken = false;
193
Chris Lattnerabe6c3d2002-05-24 21:33:26 +0000194 assert((Old->getReturnType() == Concrete->getReturnType() ||
195 Concrete->getReturnType() == Type::VoidTy ||
196 Old->getReturnType() == Type::VoidTy) &&
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000197 "Differing return types not handled yet!");
198 assert(OldMT->getParamTypes().size() <=
199 ConcreteMT->getParamTypes().size() &&
200 "Concrete type must have more specified parameters!");
201
202 // Check to make sure that if there are specified types, that they
203 // match...
204 //
205 for (unsigned i = 0; i < OldMT->getParamTypes().size(); ++i)
206 if (OldMT->getParamTypes()[i] != ConcreteMT->getParamTypes()[i]) {
207 cerr << "Parameter types conflict for" << OldMT
208 << " and " << ConcreteMT;
209 Broken = true;
210 }
211 if (Broken) break; // Can't process this one!
212
213
214 // Attempt to convert all of the uses of the old function to the
Chris Lattnerabe6c3d2002-05-24 21:33:26 +0000215 // concrete form of the function. If there is a use of the fn that
216 // we don't understand here we punt to avoid making a bad
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000217 // transformation.
218 //
219 // At this point, we know that the return values are the same for
220 // our two functions and that the Old function has no varargs fns
221 // specified. In otherwords it's just <retty> (...)
222 //
223 for (unsigned i = 0; i < Old->use_size(); ) {
224 User *U = *(Old->use_begin()+i);
225 if (CastInst *CI = dyn_cast<CastInst>(U)) {
226 // Convert casts directly
227 assert(CI->getOperand(0) == Old);
228 CI->setOperand(0, Concrete);
229 Changed = true;
230 ++NumResolved;
231 } else if (CallInst *CI = dyn_cast<CallInst>(U)) {
232 // Can only fix up calls TO the argument, not args passed in.
233 if (CI->getCalledValue() == Old) {
234 ConvertCallTo(CI, Concrete);
235 Changed = true;
236 ++NumResolved;
237 } else {
238 cerr << "Couldn't cleanup this function call, must be an"
239 << " argument or something!" << CI;
240 ++i;
241 }
242 } else {
243 cerr << "Cannot convert use of function: " << U << "\n";
244 ++i;
245 }
246 }
247 }
248 }
249 }
250 }
251
252 return Changed;
253}