Chris Lattner | 22ee3eb | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 1 | //===- 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 Lattner | 22ee3eb | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 16 | #include "llvm/SymbolTable.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Pass.h" |
| 19 | #include "llvm/iOther.h" |
Chris Lattner | abe6c3d | 2002-05-24 21:33:26 +0000 | [diff] [blame] | 20 | #include "llvm/Constant.h" |
Chris Lattner | 22ee3eb | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 21 | #include "Support/StatisticReporter.h" |
| 22 | #include <iostream> |
| 23 | #include <algorithm> |
| 24 | |
| 25 | using std::vector; |
| 26 | using std::string; |
| 27 | using std::cerr; |
| 28 | |
| 29 | namespace { |
| 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 Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 35 | bool run(Module &M); |
Chris Lattner | 22ee3eb | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 36 | }; |
| 37 | } |
| 38 | |
| 39 | Pass *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 | // |
| 46 | static void ConvertCallTo(CallInst *CI, Function *Dest) { |
| 47 | const FunctionType::ParamTypes &ParamTys = |
| 48 | Dest->getFunctionType()->getParamTypes(); |
| 49 | BasicBlock *BB = CI->getParent(); |
| 50 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 51 | // Keep an iterator to where we want to insert cast instructions if the |
Chris Lattner | 22ee3eb | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 52 | // argument types don't agree. |
| 53 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 54 | BasicBlock::iterator BBI = CI; |
Chris Lattner | abe6c3d | 2002-05-24 21:33:26 +0000 | [diff] [blame] | 55 | assert(CI->getNumOperands()-1 == ParamTys.size() && |
Chris Lattner | 22ee3eb | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 56 | "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 Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 67 | BBI = ++BB->getInstList().insert(BBI, Cast); |
Chris Lattner | 22ee3eb | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 68 | V = Cast; |
| 69 | } |
| 70 | |
| 71 | Params.push_back(V); |
| 72 | } |
| 73 | |
Chris Lattner | abe6c3d | 2002-05-24 21:33:26 +0000 | [diff] [blame] | 74 | Instruction *NewCall = new CallInst(Dest, Params); |
| 75 | |
Chris Lattner | 22ee3eb | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 76 | // Replace the old call instruction with a new call instruction that calls |
| 77 | // the real function. |
| 78 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 79 | BBI = ++BB->getInstList().insert(BBI, NewCall); |
Chris Lattner | abe6c3d | 2002-05-24 21:33:26 +0000 | [diff] [blame] | 80 | |
| 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 Lattner | 22ee3eb | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 109 | bool FunctionResolvingPass::run(Module &M) { |
| 110 | SymbolTable *ST = M.getSymbolTable(); |
Chris Lattner | 22ee3eb | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 111 | 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 Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 150 | if (Functions[i]->use_empty()) { |
| 151 | M.getFunctionList().erase(Functions[i]); |
Chris Lattner | 22ee3eb | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 152 | 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 Lattner | abe6c3d | 2002-05-24 21:33:26 +0000 | [diff] [blame] | 190 | assert((Old->getReturnType() == Concrete->getReturnType() || |
| 191 | Concrete->getReturnType() == Type::VoidTy || |
| 192 | Old->getReturnType() == Type::VoidTy) && |
Chris Lattner | 22ee3eb | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 193 | "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 Lattner | abe6c3d | 2002-05-24 21:33:26 +0000 | [diff] [blame] | 211 | // 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 Lattner | 22ee3eb | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 213 | // 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 | } |