Chris Lattner | 5aa9e3e | 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 | |
Chris Lattner | 5afe2f2 | 2002-07-23 22:04:02 +0000 | [diff] [blame] | 14 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 15 | #include "llvm/Module.h" |
Chris Lattner | 5aa9e3e | 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 | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 21 | #include "llvm/Assembly/Writer.h" // FIXME: remove when varargs implemented |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 22 | #include "Support/Statistic.h" |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 25 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 26 | Statistic<>NumResolved("funcresolve", "Number of varargs functions resolved"); |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 27 | Statistic<> NumGlobals("funcresolve", "Number of global variables resolved"); |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 28 | |
| 29 | struct FunctionResolvingPass : public Pass { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 30 | bool run(Module &M); |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 31 | }; |
Chris Lattner | a2c0985 | 2002-07-26 21:12:44 +0000 | [diff] [blame] | 32 | RegisterOpt<FunctionResolvingPass> X("funcresolve", "Resolve Functions"); |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | Pass *createFunctionResolvingPass() { |
| 36 | return new FunctionResolvingPass(); |
| 37 | } |
| 38 | |
| 39 | // ConvertCallTo - Convert a call to a varargs function with no arg types |
| 40 | // specified to a concrete nonvarargs function. |
| 41 | // |
| 42 | static void ConvertCallTo(CallInst *CI, Function *Dest) { |
| 43 | const FunctionType::ParamTypes &ParamTys = |
| 44 | Dest->getFunctionType()->getParamTypes(); |
| 45 | BasicBlock *BB = CI->getParent(); |
| 46 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 47 | // Keep an iterator to where we want to insert cast instructions if the |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 48 | // argument types don't agree. |
| 49 | // |
Chris Lattner | 68f63f7 | 2003-01-30 22:38:44 +0000 | [diff] [blame] | 50 | unsigned NumArgsToCopy = CI->getNumOperands()-1; |
Chris Lattner | 04c9274 | 2003-02-14 19:12:29 +0000 | [diff] [blame] | 51 | if (NumArgsToCopy != ParamTys.size() && |
| 52 | !(NumArgsToCopy > ParamTys.size() && |
Chris Lattner | 68f63f7 | 2003-01-30 22:38:44 +0000 | [diff] [blame] | 53 | Dest->getFunctionType()->isVarArg())) { |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 54 | std::cerr << "WARNING: Call arguments do not match expected number of" |
| 55 | << " parameters.\n"; |
| 56 | std::cerr << "WARNING: In function '" |
| 57 | << CI->getParent()->getParent()->getName() << "': call: " << *CI; |
| 58 | std::cerr << "Function resolved to: "; |
| 59 | WriteAsOperand(std::cerr, Dest); |
| 60 | std::cerr << "\n"; |
Chris Lattner | 04c9274 | 2003-02-14 19:12:29 +0000 | [diff] [blame] | 61 | if (NumArgsToCopy > ParamTys.size()) |
| 62 | NumArgsToCopy = ParamTys.size(); |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 63 | } |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 64 | |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 65 | std::vector<Value*> Params; |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 66 | |
| 67 | // Convert all of the call arguments over... inserting cast instructions if |
| 68 | // the types are not compatible. |
Chris Lattner | 68f63f7 | 2003-01-30 22:38:44 +0000 | [diff] [blame] | 69 | for (unsigned i = 1; i <= NumArgsToCopy; ++i) { |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 70 | Value *V = CI->getOperand(i); |
| 71 | |
Chris Lattner | 68f63f7 | 2003-01-30 22:38:44 +0000 | [diff] [blame] | 72 | if (i-1 < ParamTys.size() && V->getType() != ParamTys[i-1]) { |
| 73 | // Must insert a cast... |
Chris Lattner | dbb05b0 | 2003-02-27 20:55:48 +0000 | [diff] [blame^] | 74 | V = new CastInst(V, ParamTys[i-1], "argcast", CI); |
Chris Lattner | 68f63f7 | 2003-01-30 22:38:44 +0000 | [diff] [blame] | 75 | } |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 76 | |
| 77 | Params.push_back(V); |
| 78 | } |
| 79 | |
| 80 | // Replace the old call instruction with a new call instruction that calls |
| 81 | // the real function. |
| 82 | // |
Chris Lattner | dbb05b0 | 2003-02-27 20:55:48 +0000 | [diff] [blame^] | 83 | Instruction *NewCall = new CallInst(Dest, Params, "", CI); |
| 84 | std::string Name = CI->getName(); CI->setName(""); |
Chris Lattner | 1eb9e6c | 2002-05-24 21:33:26 +0000 | [diff] [blame] | 85 | |
Chris Lattner | aacadd1 | 2002-07-30 00:50:49 +0000 | [diff] [blame] | 86 | // Transfer the name over... |
Chris Lattner | ed37856 | 2002-07-30 02:42:49 +0000 | [diff] [blame] | 87 | if (NewCall->getType() != Type::VoidTy) |
Chris Lattner | dbb05b0 | 2003-02-27 20:55:48 +0000 | [diff] [blame^] | 88 | NewCall->setName(Name); |
Chris Lattner | aacadd1 | 2002-07-30 00:50:49 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 1eb9e6c | 2002-05-24 21:33:26 +0000 | [diff] [blame] | 90 | // Replace uses of the old instruction with the appropriate values... |
| 91 | // |
| 92 | if (NewCall->getType() == CI->getType()) { |
| 93 | CI->replaceAllUsesWith(NewCall); |
Chris Lattner | dbb05b0 | 2003-02-27 20:55:48 +0000 | [diff] [blame^] | 94 | NewCall->setName(Name); |
Chris Lattner | 1eb9e6c | 2002-05-24 21:33:26 +0000 | [diff] [blame] | 95 | |
| 96 | } else if (NewCall->getType() == Type::VoidTy) { |
| 97 | // Resolved function does not return a value but the prototype does. This |
| 98 | // often occurs because undefined functions default to returning integers. |
| 99 | // Just replace uses of the call (which are broken anyway) with dummy |
| 100 | // values. |
| 101 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 102 | } else if (CI->getType() == Type::VoidTy) { |
| 103 | // If we are gaining a new return value, we don't have to do anything |
Chris Lattner | aacadd1 | 2002-07-30 00:50:49 +0000 | [diff] [blame] | 104 | // special here, because it will automatically be ignored. |
Chris Lattner | 1eb9e6c | 2002-05-24 21:33:26 +0000 | [diff] [blame] | 105 | } else { |
Chris Lattner | aacadd1 | 2002-07-30 00:50:49 +0000 | [diff] [blame] | 106 | // Insert a cast instruction to convert the return value of the function |
| 107 | // into it's new type. Of course we only need to do this if the return |
| 108 | // value of the function is actually USED. |
| 109 | // |
| 110 | if (!CI->use_empty()) { |
Chris Lattner | aacadd1 | 2002-07-30 00:50:49 +0000 | [diff] [blame] | 111 | // Insert the new cast instruction... |
Chris Lattner | dbb05b0 | 2003-02-27 20:55:48 +0000 | [diff] [blame^] | 112 | CastInst *NewCast = new CastInst(NewCall, CI->getType(), Name, CI); |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 113 | CI->replaceAllUsesWith(NewCast); |
Chris Lattner | aacadd1 | 2002-07-30 00:50:49 +0000 | [diff] [blame] | 114 | } |
Chris Lattner | 1eb9e6c | 2002-05-24 21:33:26 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // The old instruction is no longer needed, destroy it! |
Chris Lattner | dbb05b0 | 2003-02-27 20:55:48 +0000 | [diff] [blame^] | 118 | BB->getInstList().erase(CI); |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 122 | static bool ResolveFunctions(Module &M, std::vector<GlobalValue*> &Globals, |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 123 | Function *Concrete) { |
| 124 | bool Changed = false; |
| 125 | for (unsigned i = 0; i != Globals.size(); ++i) |
| 126 | if (Globals[i] != Concrete) { |
| 127 | Function *Old = cast<Function>(Globals[i]); |
| 128 | const FunctionType *OldMT = Old->getFunctionType(); |
| 129 | const FunctionType *ConcreteMT = Concrete->getFunctionType(); |
| 130 | |
Chris Lattner | dbb05b0 | 2003-02-27 20:55:48 +0000 | [diff] [blame^] | 131 | if (OldMT->getParamTypes().size() <= ConcreteMT->getParamTypes().size()) |
| 132 | if (!Old->use_empty()) { |
| 133 | std::cerr << "WARNING: Linking function '" << Old->getName() |
| 134 | << "' is causing arguments to be dropped.\n"; |
| 135 | std::cerr << "WARNING: Prototype: "; |
| 136 | WriteAsOperand(std::cerr, Old); |
| 137 | std::cerr << " resolved to "; |
| 138 | WriteAsOperand(std::cerr, Concrete); |
| 139 | std::cerr << "\n"; |
| 140 | } |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 141 | |
| 142 | // Check to make sure that if there are specified types, that they |
| 143 | // match... |
| 144 | // |
Chris Lattner | dbb05b0 | 2003-02-27 20:55:48 +0000 | [diff] [blame^] | 145 | unsigned NumArguments = std::min(OldMT->getParamTypes().size(), |
| 146 | ConcreteMT->getParamTypes().size()); |
| 147 | |
| 148 | for (unsigned i = 0; i < NumArguments; ++i) |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 149 | if (OldMT->getParamTypes()[i] != ConcreteMT->getParamTypes()[i]) { |
Chris Lattner | 4794895 | 2003-01-30 21:33:07 +0000 | [diff] [blame] | 150 | std::cerr << "funcresolve: Function [" << Old->getName() |
| 151 | << "]: Parameter types conflict for: '" << OldMT |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 152 | << "' and '" << ConcreteMT << "'\n"; |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 153 | return Changed; |
| 154 | } |
| 155 | |
| 156 | // Attempt to convert all of the uses of the old function to the |
| 157 | // concrete form of the function. If there is a use of the fn that |
| 158 | // we don't understand here we punt to avoid making a bad |
| 159 | // transformation. |
| 160 | // |
| 161 | // At this point, we know that the return values are the same for |
| 162 | // our two functions and that the Old function has no varargs fns |
| 163 | // specified. In otherwords it's just <retty> (...) |
| 164 | // |
| 165 | for (unsigned i = 0; i < Old->use_size(); ) { |
| 166 | User *U = *(Old->use_begin()+i); |
| 167 | if (CastInst *CI = dyn_cast<CastInst>(U)) { |
| 168 | // Convert casts directly |
| 169 | assert(CI->getOperand(0) == Old); |
| 170 | CI->setOperand(0, Concrete); |
| 171 | Changed = true; |
| 172 | ++NumResolved; |
| 173 | } else if (CallInst *CI = dyn_cast<CallInst>(U)) { |
| 174 | // Can only fix up calls TO the argument, not args passed in. |
| 175 | if (CI->getCalledValue() == Old) { |
| 176 | ConvertCallTo(CI, Concrete); |
| 177 | Changed = true; |
| 178 | ++NumResolved; |
| 179 | } else { |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 180 | std::cerr << "Couldn't cleanup this function call, must be an" |
| 181 | << " argument or something!" << CI; |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 182 | ++i; |
| 183 | } |
| 184 | } else { |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 185 | std::cerr << "Cannot convert use of function: " << U << "\n"; |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 186 | ++i; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | return Changed; |
| 191 | } |
| 192 | |
| 193 | |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 194 | static bool ResolveGlobalVariables(Module &M, |
| 195 | std::vector<GlobalValue*> &Globals, |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 196 | GlobalVariable *Concrete) { |
| 197 | bool Changed = false; |
| 198 | assert(isa<ArrayType>(Concrete->getType()->getElementType()) && |
| 199 | "Concrete version should be an array type!"); |
| 200 | |
| 201 | // Get the type of the things that may be resolved to us... |
| 202 | const Type *AETy = |
| 203 | cast<ArrayType>(Concrete->getType()->getElementType())->getElementType(); |
| 204 | |
| 205 | std::vector<Constant*> Args; |
| 206 | Args.push_back(Constant::getNullValue(Type::LongTy)); |
| 207 | Args.push_back(Constant::getNullValue(Type::LongTy)); |
| 208 | ConstantExpr *Replacement = |
| 209 | ConstantExpr::getGetElementPtr(ConstantPointerRef::get(Concrete), Args); |
| 210 | |
| 211 | for (unsigned i = 0; i != Globals.size(); ++i) |
| 212 | if (Globals[i] != Concrete) { |
| 213 | GlobalVariable *Old = cast<GlobalVariable>(Globals[i]); |
| 214 | if (Old->getType()->getElementType() != AETy) { |
| 215 | std::cerr << "WARNING: Two global variables exist with the same name " |
| 216 | << "that cannot be resolved!\n"; |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | // In this case, Old is a pointer to T, Concrete is a pointer to array of |
| 221 | // T. Because of this, replace all uses of Old with a constantexpr |
| 222 | // getelementptr that returns the address of the first element of the |
| 223 | // array. |
| 224 | // |
| 225 | Old->replaceAllUsesWith(Replacement); |
| 226 | // Since there are no uses of Old anymore, remove it from the module. |
| 227 | M.getGlobalList().erase(Old); |
| 228 | |
| 229 | ++NumGlobals; |
| 230 | Changed = true; |
| 231 | } |
| 232 | return Changed; |
| 233 | } |
| 234 | |
| 235 | static bool ProcessGlobalsWithSameName(Module &M, |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 236 | std::vector<GlobalValue*> &Globals) { |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 237 | assert(!Globals.empty() && "Globals list shouldn't be empty here!"); |
| 238 | |
| 239 | bool isFunction = isa<Function>(Globals[0]); // Is this group all functions? |
| 240 | bool Changed = false; |
| 241 | GlobalValue *Concrete = 0; // The most concrete implementation to resolve to |
| 242 | |
| 243 | assert((isFunction ^ isa<GlobalVariable>(Globals[0])) && |
| 244 | "Should either be function or gvar!"); |
| 245 | |
| 246 | for (unsigned i = 0; i != Globals.size(); ) { |
| 247 | if (isa<Function>(Globals[i]) != isFunction) { |
| 248 | std::cerr << "WARNING: Found function and global variable with the " |
| 249 | << "same name: '" << Globals[i]->getName() << "'.\n"; |
| 250 | return false; // Don't know how to handle this, bail out! |
| 251 | } |
| 252 | |
Chris Lattner | 52b8fc0 | 2002-11-10 03:36:55 +0000 | [diff] [blame] | 253 | if (isFunction) { |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 254 | // For functions, we look to merge functions definitions of "int (...)" |
| 255 | // to 'int (int)' or 'int ()' or whatever else is not completely generic. |
| 256 | // |
| 257 | Function *F = cast<Function>(Globals[i]); |
Chris Lattner | 5997c3d | 2002-11-08 00:38:20 +0000 | [diff] [blame] | 258 | if (!F->isExternal()) { |
Chris Lattner | 52b8fc0 | 2002-11-10 03:36:55 +0000 | [diff] [blame] | 259 | if (Concrete && !Concrete->isExternal()) |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 260 | return false; // Found two different functions types. Can't choose! |
| 261 | |
| 262 | Concrete = Globals[i]; |
Chris Lattner | 52b8fc0 | 2002-11-10 03:36:55 +0000 | [diff] [blame] | 263 | } else if (Concrete) { |
| 264 | if (Concrete->isExternal()) // If we have multiple external symbols...x |
| 265 | if (F->getFunctionType()->getNumParams() > |
| 266 | cast<Function>(Concrete)->getFunctionType()->getNumParams()) |
| 267 | Concrete = F; // We are more concrete than "Concrete"! |
| 268 | |
| 269 | } else { |
| 270 | Concrete = F; |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 271 | } |
| 272 | ++i; |
| 273 | } else { |
| 274 | // For global variables, we have to merge C definitions int A[][4] with |
| 275 | // int[6][4] |
| 276 | GlobalVariable *GV = cast<GlobalVariable>(Globals[i]); |
| 277 | if (Concrete == 0) { |
| 278 | if (isa<ArrayType>(GV->getType()->getElementType())) |
| 279 | Concrete = GV; |
| 280 | } else { // Must have different types... one is an array of the other? |
| 281 | const ArrayType *AT = |
| 282 | dyn_cast<ArrayType>(GV->getType()->getElementType()); |
| 283 | |
| 284 | // If GV is an array of Concrete, then GV is the array. |
| 285 | if (AT && AT->getElementType() == Concrete->getType()->getElementType()) |
| 286 | Concrete = GV; |
| 287 | else { |
| 288 | // Concrete must be an array type, check to see if the element type of |
| 289 | // concrete is already GV. |
| 290 | AT = cast<ArrayType>(Concrete->getType()->getElementType()); |
| 291 | if (AT->getElementType() != GV->getType()->getElementType()) |
| 292 | Concrete = 0; // Don't know how to handle it! |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | ++i; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | if (Globals.size() > 1) { // Found a multiply defined global... |
| 301 | // We should find exactly one concrete function definition, which is |
| 302 | // probably the implementation. Change all of the function definitions and |
| 303 | // uses to use it instead. |
| 304 | // |
| 305 | if (!Concrete) { |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 306 | std::cerr << "WARNING: Found function types that are not compatible:\n"; |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 307 | for (unsigned i = 0; i < Globals.size(); ++i) { |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 308 | std::cerr << "\t" << Globals[i]->getType()->getDescription() << " %" |
| 309 | << Globals[i]->getName() << "\n"; |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 310 | } |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 311 | std::cerr << " No linkage of globals named '" << Globals[0]->getName() |
| 312 | << "' performed!\n"; |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 313 | return Changed; |
| 314 | } |
| 315 | |
| 316 | if (isFunction) |
| 317 | return Changed | ResolveFunctions(M, Globals, cast<Function>(Concrete)); |
| 318 | else |
| 319 | return Changed | ResolveGlobalVariables(M, Globals, |
| 320 | cast<GlobalVariable>(Concrete)); |
| 321 | } |
| 322 | return Changed; |
| 323 | } |
| 324 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 325 | bool FunctionResolvingPass::run(Module &M) { |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 326 | SymbolTable &ST = M.getSymbolTable(); |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 327 | |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 328 | std::map<std::string, std::vector<GlobalValue*> > Globals; |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 329 | |
| 330 | // Loop over the entries in the symbol table. If an entry is a func pointer, |
| 331 | // then add it to the Functions map. We do a two pass algorithm here to avoid |
| 332 | // problems with iterators getting invalidated if we did a one pass scheme. |
| 333 | // |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 334 | for (SymbolTable::iterator I = ST.begin(), E = ST.end(); I != E; ++I) |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 335 | if (const PointerType *PT = dyn_cast<PointerType>(I->first)) { |
| 336 | SymbolTable::VarMap &Plane = I->second; |
| 337 | for (SymbolTable::type_iterator PI = Plane.begin(), PE = Plane.end(); |
| 338 | PI != PE; ++PI) { |
| 339 | GlobalValue *GV = cast<GlobalValue>(PI->second); |
| 340 | assert(PI->first == GV->getName() && |
| 341 | "Global name and symbol table do not agree!"); |
| 342 | if (GV->hasExternalLinkage()) // Only resolve decls to external fns |
| 343 | Globals[PI->first].push_back(GV); |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 344 | } |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 345 | } |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 346 | |
| 347 | bool Changed = false; |
| 348 | |
| 349 | // Now we have a list of all functions with a particular name. If there is |
| 350 | // more than one entry in a list, merge the functions together. |
| 351 | // |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 352 | for (std::map<std::string, std::vector<GlobalValue*> >::iterator |
| 353 | I = Globals.begin(), E = Globals.end(); I != E; ++I) |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 354 | Changed |= ProcessGlobalsWithSameName(M, I->second); |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 355 | |
Chris Lattner | 52b8fc0 | 2002-11-10 03:36:55 +0000 | [diff] [blame] | 356 | // Now loop over all of the globals, checking to see if any are trivially |
| 357 | // dead. If so, remove them now. |
| 358 | |
| 359 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ) |
| 360 | if (I->isExternal() && I->use_empty()) { |
| 361 | Function *F = I; |
| 362 | ++I; |
| 363 | M.getFunctionList().erase(F); |
| 364 | ++NumResolved; |
| 365 | Changed = true; |
| 366 | } else { |
| 367 | ++I; |
| 368 | } |
| 369 | |
| 370 | for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ) |
| 371 | if (I->isExternal() && I->use_empty()) { |
| 372 | GlobalVariable *GV = I; |
| 373 | ++I; |
| 374 | M.getGlobalList().erase(GV); |
| 375 | ++NumGlobals; |
| 376 | Changed = true; |
| 377 | } else { |
| 378 | ++I; |
| 379 | } |
| 380 | |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 381 | return Changed; |
| 382 | } |