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