1 //===- FunctionResolution.cpp - Resolve declarations to implementations ---===//
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:
6 // declare void %foo(...)
7 // void %foo(int, int) { ... }
9 // because of the way things are declared in C. If this is the case, patch
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/CleanupGCCOutput.h"
15 #include "llvm/Module.h"
16 #include "llvm/Function.h"
17 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
18 #include "llvm/SymbolTable.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Pass.h"
21 #include "llvm/iOther.h"
22 #include "Support/StatisticReporter.h"
31 Statistic<>NumResolved("funcresolve\t- Number of varargs functions resolved");
33 struct FunctionResolvingPass : public Pass {
34 const char *getPassName() const { return "Resolve Functions"; }
40 Pass *createFunctionResolvingPass() {
41 return new FunctionResolvingPass();
44 // ConvertCallTo - Convert a call to a varargs function with no arg types
45 // specified to a concrete nonvarargs function.
47 static void ConvertCallTo(CallInst *CI, Function *Dest) {
48 const FunctionType::ParamTypes &ParamTys =
49 Dest->getFunctionType()->getParamTypes();
50 BasicBlock *BB = CI->getParent();
52 // Get an iterator to where we want to insert cast instructions if the
53 // argument types don't agree.
55 BasicBlock::iterator BBI = find(BB->begin(), BB->end(), CI);
56 assert(BBI != BB->end() && "CallInst not in parent block?");
58 assert(CI->getNumOperands()-1 == ParamTys.size()&&
59 "Function calls resolved funny somehow, incompatible number of args");
61 vector<Value*> Params;
63 // Convert all of the call arguments over... inserting cast instructions if
64 // the types are not compatible.
65 for (unsigned i = 1; i < CI->getNumOperands(); ++i) {
66 Value *V = CI->getOperand(i);
68 if (V->getType() != ParamTys[i-1]) { // Must insert a cast...
69 Instruction *Cast = new CastInst(V, ParamTys[i-1]);
70 BBI = BB->getInstList().insert(BBI, Cast)+1;
77 // Replace the old call instruction with a new call instruction that calls
80 ReplaceInstWithInst(BB->getInstList(), BBI, new CallInst(Dest, Params));
84 bool FunctionResolvingPass::run(Module *M) {
85 SymbolTable *ST = M->getSymbolTable();
86 if (!ST) return false;
88 std::map<string, vector<Function*> > Functions;
90 // Loop over the entries in the symbol table. If an entry is a func pointer,
91 // then add it to the Functions map. We do a two pass algorithm here to avoid
92 // problems with iterators getting invalidated if we did a one pass scheme.
94 for (SymbolTable::iterator I = ST->begin(), E = ST->end(); I != E; ++I)
95 if (const PointerType *PT = dyn_cast<PointerType>(I->first))
96 if (isa<FunctionType>(PT->getElementType())) {
97 SymbolTable::VarMap &Plane = I->second;
98 for (SymbolTable::type_iterator PI = Plane.begin(), PE = Plane.end();
100 const string &Name = PI->first;
101 Functions[Name].push_back(cast<Function>(PI->second));
105 bool Changed = false;
107 // Now we have a list of all functions with a particular name. If there is
108 // more than one entry in a list, merge the functions together.
110 for (std::map<string, vector<Function*> >::iterator I = Functions.begin(),
111 E = Functions.end(); I != E; ++I) {
112 vector<Function*> &Functions = I->second;
113 Function *Implementation = 0; // Find the implementation
114 Function *Concrete = 0;
115 for (unsigned i = 0; i < Functions.size(); ) {
116 if (!Functions[i]->isExternal()) { // Found an implementation
117 assert(Implementation == 0 && "Multiple definitions of the same"
118 " function. Case not handled yet!");
119 Implementation = Functions[i];
121 // Ignore functions that are never used so they don't cause spurious
122 // warnings... here we will actually DCE the function so that it isn't
125 if (Functions[i]->use_size() == 0) {
126 M->getFunctionList().remove(Functions[i]);
128 Functions.erase(Functions.begin()+i);
135 if (Functions[i] && (!Functions[i]->getFunctionType()->isVarArg())) {
136 if (Concrete) { // Found two different functions types. Can't choose
140 Concrete = Functions[i];
145 if (Functions.size() > 1) { // Found a multiply defined function...
146 // We should find exactly one non-vararg function definition, which is
147 // probably the implementation. Change all of the function definitions
148 // and uses to use it instead.
151 cerr << "Warning: Found functions types that are not compatible:\n";
152 for (unsigned i = 0; i < Functions.size(); ++i) {
153 cerr << "\t" << Functions[i]->getType()->getDescription() << " %"
154 << Functions[i]->getName() << "\n";
156 cerr << " No linkage of functions named '" << Functions[0]->getName()
159 for (unsigned i = 0; i < Functions.size(); ++i)
160 if (Functions[i] != Concrete) {
161 Function *Old = Functions[i];
162 const FunctionType *OldMT = Old->getFunctionType();
163 const FunctionType *ConcreteMT = Concrete->getFunctionType();
166 assert(Old->getReturnType() == Concrete->getReturnType() &&
167 "Differing return types not handled yet!");
168 assert(OldMT->getParamTypes().size() <=
169 ConcreteMT->getParamTypes().size() &&
170 "Concrete type must have more specified parameters!");
172 // Check to make sure that if there are specified types, that they
175 for (unsigned i = 0; i < OldMT->getParamTypes().size(); ++i)
176 if (OldMT->getParamTypes()[i] != ConcreteMT->getParamTypes()[i]) {
177 cerr << "Parameter types conflict for" << OldMT
178 << " and " << ConcreteMT;
181 if (Broken) break; // Can't process this one!
184 // Attempt to convert all of the uses of the old function to the
185 // concrete form of the function. If there is a use of the fn
186 // that we don't understand here we punt to avoid making a bad
189 // At this point, we know that the return values are the same for
190 // our two functions and that the Old function has no varargs fns
191 // specified. In otherwords it's just <retty> (...)
193 for (unsigned i = 0; i < Old->use_size(); ) {
194 User *U = *(Old->use_begin()+i);
195 if (CastInst *CI = dyn_cast<CastInst>(U)) {
196 // Convert casts directly
197 assert(CI->getOperand(0) == Old);
198 CI->setOperand(0, Concrete);
201 } else if (CallInst *CI = dyn_cast<CallInst>(U)) {
202 // Can only fix up calls TO the argument, not args passed in.
203 if (CI->getCalledValue() == Old) {
204 ConvertCallTo(CI, Concrete);
208 cerr << "Couldn't cleanup this function call, must be an"
209 << " argument or something!" << CI;
213 cerr << "Cannot convert use of function: " << U << "\n";