blob: e2165c77cb74cf7b2e61903ad1303fdd48678d10 [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
Chris Lattnerff1be262002-07-23 22:04:02 +000014#include "llvm/Transforms/IPO.h"
Chris Lattner22ee3eb2002-05-24 20:42:13 +000015#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 {
Chris Lattner7e708292002-06-25 16:13:24 +000033 bool run(Module &M);
Chris Lattner22ee3eb2002-05-24 20:42:13 +000034 };
Chris Lattner1e435162002-07-26 21:12:44 +000035 RegisterOpt<FunctionResolvingPass> X("funcresolve", "Resolve Functions");
Chris Lattner22ee3eb2002-05-24 20:42:13 +000036}
37
38Pass *createFunctionResolvingPass() {
39 return new FunctionResolvingPass();
40}
41
42// ConvertCallTo - Convert a call to a varargs function with no arg types
43// specified to a concrete nonvarargs function.
44//
45static void ConvertCallTo(CallInst *CI, Function *Dest) {
46 const FunctionType::ParamTypes &ParamTys =
47 Dest->getFunctionType()->getParamTypes();
48 BasicBlock *BB = CI->getParent();
49
Chris Lattner7e708292002-06-25 16:13:24 +000050 // Keep an iterator to where we want to insert cast instructions if the
Chris Lattner22ee3eb2002-05-24 20:42:13 +000051 // argument types don't agree.
52 //
Chris Lattner7e708292002-06-25 16:13:24 +000053 BasicBlock::iterator BBI = CI;
Chris Lattnerabe6c3d2002-05-24 21:33:26 +000054 assert(CI->getNumOperands()-1 == ParamTys.size() &&
Chris Lattner22ee3eb2002-05-24 20:42:13 +000055 "Function calls resolved funny somehow, incompatible number of args");
56
57 vector<Value*> Params;
58
59 // Convert all of the call arguments over... inserting cast instructions if
60 // the types are not compatible.
61 for (unsigned i = 1; i < CI->getNumOperands(); ++i) {
62 Value *V = CI->getOperand(i);
63
64 if (V->getType() != ParamTys[i-1]) { // Must insert a cast...
65 Instruction *Cast = new CastInst(V, ParamTys[i-1]);
Chris Lattner7e708292002-06-25 16:13:24 +000066 BBI = ++BB->getInstList().insert(BBI, Cast);
Chris Lattner22ee3eb2002-05-24 20:42:13 +000067 V = Cast;
68 }
69
70 Params.push_back(V);
71 }
72
Chris Lattnerabe6c3d2002-05-24 21:33:26 +000073 Instruction *NewCall = new CallInst(Dest, Params);
74
Chris Lattner22ee3eb2002-05-24 20:42:13 +000075 // Replace the old call instruction with a new call instruction that calls
76 // the real function.
77 //
Chris Lattner7e708292002-06-25 16:13:24 +000078 BBI = ++BB->getInstList().insert(BBI, NewCall);
Chris Lattnerabe6c3d2002-05-24 21:33:26 +000079
80 // Remove the old call instruction from the program...
81 BB->getInstList().remove(BBI);
82
Chris Lattner9cf307f2002-07-30 00:50:49 +000083 // Transfer the name over...
Chris Lattnere902bda2002-07-30 02:42:49 +000084 if (NewCall->getType() != Type::VoidTy)
85 NewCall->setName(CI->getName());
Chris Lattner9cf307f2002-07-30 00:50:49 +000086
Chris Lattnerabe6c3d2002-05-24 21:33:26 +000087 // Replace uses of the old instruction with the appropriate values...
88 //
89 if (NewCall->getType() == CI->getType()) {
90 CI->replaceAllUsesWith(NewCall);
91 NewCall->setName(CI->getName());
92
93 } else if (NewCall->getType() == Type::VoidTy) {
94 // Resolved function does not return a value but the prototype does. This
95 // often occurs because undefined functions default to returning integers.
96 // Just replace uses of the call (which are broken anyway) with dummy
97 // values.
98 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
99 } else if (CI->getType() == Type::VoidTy) {
100 // If we are gaining a new return value, we don't have to do anything
Chris Lattner9cf307f2002-07-30 00:50:49 +0000101 // special here, because it will automatically be ignored.
Chris Lattnerabe6c3d2002-05-24 21:33:26 +0000102 } else {
Chris Lattner9cf307f2002-07-30 00:50:49 +0000103 // Insert a cast instruction to convert the return value of the function
104 // into it's new type. Of course we only need to do this if the return
105 // value of the function is actually USED.
106 //
107 if (!CI->use_empty()) {
108 CastInst *NewCast = new CastInst(NewCall, CI->getType(),
109 NewCall->getName());
110 CI->replaceAllUsesWith(NewCast);
111 // Insert the new cast instruction...
112 BB->getInstList().insert(BBI, NewCast);
113 }
Chris Lattnerabe6c3d2002-05-24 21:33:26 +0000114 }
115
116 // The old instruction is no longer needed, destroy it!
117 delete CI;
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000118}
119
120
Chris Lattner7e708292002-06-25 16:13:24 +0000121bool FunctionResolvingPass::run(Module &M) {
122 SymbolTable *ST = M.getSymbolTable();
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000123 if (!ST) return false;
124
125 std::map<string, vector<Function*> > Functions;
126
127 // Loop over the entries in the symbol table. If an entry is a func pointer,
128 // then add it to the Functions map. We do a two pass algorithm here to avoid
129 // problems with iterators getting invalidated if we did a one pass scheme.
130 //
131 for (SymbolTable::iterator I = ST->begin(), E = ST->end(); I != E; ++I)
132 if (const PointerType *PT = dyn_cast<PointerType>(I->first))
133 if (isa<FunctionType>(PT->getElementType())) {
134 SymbolTable::VarMap &Plane = I->second;
135 for (SymbolTable::type_iterator PI = Plane.begin(), PE = Plane.end();
136 PI != PE; ++PI) {
Chris Lattner7f20ea72002-07-18 03:01:24 +0000137 Function *F = cast<Function>(PI->second);
138 assert(PI->first == F->getName() &&
139 "Function name and symbol table do not agree!");
140 if (F->hasExternalLinkage()) // Only resolve decls to external fns
141 Functions[PI->first].push_back(F);
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000142 }
143 }
144
145 bool Changed = false;
146
147 // Now we have a list of all functions with a particular name. If there is
148 // more than one entry in a list, merge the functions together.
149 //
150 for (std::map<string, vector<Function*> >::iterator I = Functions.begin(),
151 E = Functions.end(); I != E; ++I) {
152 vector<Function*> &Functions = I->second;
153 Function *Implementation = 0; // Find the implementation
154 Function *Concrete = 0;
155 for (unsigned i = 0; i < Functions.size(); ) {
156 if (!Functions[i]->isExternal()) { // Found an implementation
Chris Lattner7f20ea72002-07-18 03:01:24 +0000157 if (Implementation != 0)
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000158 assert(Implementation == 0 && "Multiple definitions of the same"
159 " function. Case not handled yet!");
160 Implementation = Functions[i];
161 } else {
162 // Ignore functions that are never used so they don't cause spurious
163 // warnings... here we will actually DCE the function so that it isn't
164 // used later.
165 //
Chris Lattner7e708292002-06-25 16:13:24 +0000166 if (Functions[i]->use_empty()) {
167 M.getFunctionList().erase(Functions[i]);
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000168 Functions.erase(Functions.begin()+i);
169 Changed = true;
170 ++NumResolved;
171 continue;
172 }
173 }
174
175 if (Functions[i] && (!Functions[i]->getFunctionType()->isVarArg())) {
176 if (Concrete) { // Found two different functions types. Can't choose
177 Concrete = 0;
178 break;
179 }
180 Concrete = Functions[i];
181 }
182 ++i;
183 }
184
185 if (Functions.size() > 1) { // Found a multiply defined function...
186 // We should find exactly one non-vararg function definition, which is
187 // probably the implementation. Change all of the function definitions
188 // and uses to use it instead.
189 //
190 if (!Concrete) {
191 cerr << "Warning: Found functions types that are not compatible:\n";
192 for (unsigned i = 0; i < Functions.size(); ++i) {
193 cerr << "\t" << Functions[i]->getType()->getDescription() << " %"
194 << Functions[i]->getName() << "\n";
195 }
196 cerr << " No linkage of functions named '" << Functions[0]->getName()
197 << "' performed!\n";
198 } else {
199 for (unsigned i = 0; i < Functions.size(); ++i)
200 if (Functions[i] != Concrete) {
201 Function *Old = Functions[i];
202 const FunctionType *OldMT = Old->getFunctionType();
203 const FunctionType *ConcreteMT = Concrete->getFunctionType();
204 bool Broken = false;
205
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000206 assert(OldMT->getParamTypes().size() <=
207 ConcreteMT->getParamTypes().size() &&
208 "Concrete type must have more specified parameters!");
209
210 // Check to make sure that if there are specified types, that they
211 // match...
212 //
213 for (unsigned i = 0; i < OldMT->getParamTypes().size(); ++i)
214 if (OldMT->getParamTypes()[i] != ConcreteMT->getParamTypes()[i]) {
215 cerr << "Parameter types conflict for" << OldMT
216 << " and " << ConcreteMT;
217 Broken = true;
218 }
219 if (Broken) break; // Can't process this one!
220
221
222 // Attempt to convert all of the uses of the old function to the
Chris Lattnerabe6c3d2002-05-24 21:33:26 +0000223 // concrete form of the function. If there is a use of the fn that
224 // we don't understand here we punt to avoid making a bad
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000225 // transformation.
226 //
227 // At this point, we know that the return values are the same for
228 // our two functions and that the Old function has no varargs fns
229 // specified. In otherwords it's just <retty> (...)
230 //
231 for (unsigned i = 0; i < Old->use_size(); ) {
232 User *U = *(Old->use_begin()+i);
233 if (CastInst *CI = dyn_cast<CastInst>(U)) {
234 // Convert casts directly
235 assert(CI->getOperand(0) == Old);
236 CI->setOperand(0, Concrete);
237 Changed = true;
238 ++NumResolved;
239 } else if (CallInst *CI = dyn_cast<CallInst>(U)) {
240 // Can only fix up calls TO the argument, not args passed in.
241 if (CI->getCalledValue() == Old) {
242 ConvertCallTo(CI, Concrete);
243 Changed = true;
244 ++NumResolved;
245 } else {
246 cerr << "Couldn't cleanup this function call, must be an"
247 << " argument or something!" << CI;
248 ++i;
249 }
250 } else {
251 cerr << "Cannot convert use of function: " << U << "\n";
252 ++i;
253 }
254 }
255 }
256 }
257 }
258 }
259
260 return Changed;
261}