Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 1 | //===- FunctionResolution.cpp - Resolve declarations to implementations ---===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 9 | // |
| 10 | // Loop over the functions that are in the module and look for functions that |
| 11 | // have the same name. More often than not, there will be things like: |
| 12 | // |
| 13 | // declare void %foo(...) |
| 14 | // void %foo(int, int) { ... } |
| 15 | // |
| 16 | // because of the way things are declared in C. If this is the case, patch |
| 17 | // things up. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
Chris Lattner | 5afe2f2 | 2002-07-23 22:04:02 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 22 | #include "llvm/Module.h" |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 23 | #include "llvm/DerivedTypes.h" |
| 24 | #include "llvm/Pass.h" |
| 25 | #include "llvm/iOther.h" |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 26 | #include "llvm/Constants.h" |
Chris Lattner | 4cd99ff | 2003-10-22 03:35:34 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Chris Lattner | cbf0839 | 2003-08-13 22:15:04 +0000 | [diff] [blame] | 28 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 29 | #include "Support/Statistic.h" |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 30 | #include <algorithm> |
| 31 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame^] | 32 | namespace llvm { |
| 33 | |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 34 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 35 | Statistic<>NumResolved("funcresolve", "Number of varargs functions resolved"); |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 36 | Statistic<> NumGlobals("funcresolve", "Number of global variables resolved"); |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 37 | |
| 38 | struct FunctionResolvingPass : public Pass { |
Chris Lattner | 4cd99ff | 2003-10-22 03:35:34 +0000 | [diff] [blame] | 39 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 40 | AU.addRequired<TargetData>(); |
| 41 | } |
| 42 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 43 | bool run(Module &M); |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 44 | }; |
Chris Lattner | a2c0985 | 2002-07-26 21:12:44 +0000 | [diff] [blame] | 45 | RegisterOpt<FunctionResolvingPass> X("funcresolve", "Resolve Functions"); |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | Pass *createFunctionResolvingPass() { |
| 49 | return new FunctionResolvingPass(); |
| 50 | } |
| 51 | |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 52 | static bool ResolveFunctions(Module &M, std::vector<GlobalValue*> &Globals, |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 53 | Function *Concrete) { |
| 54 | bool Changed = false; |
| 55 | for (unsigned i = 0; i != Globals.size(); ++i) |
| 56 | if (Globals[i] != Concrete) { |
| 57 | Function *Old = cast<Function>(Globals[i]); |
| 58 | const FunctionType *OldMT = Old->getFunctionType(); |
| 59 | const FunctionType *ConcreteMT = Concrete->getFunctionType(); |
| 60 | |
Chris Lattner | 9810b94 | 2003-04-28 01:23:29 +0000 | [diff] [blame] | 61 | if (OldMT->getParamTypes().size() > ConcreteMT->getParamTypes().size() && |
Chris Lattner | 50cbb90 | 2003-03-03 19:57:46 +0000 | [diff] [blame] | 62 | !ConcreteMT->isVarArg()) |
Chris Lattner | dbb05b0 | 2003-02-27 20:55:48 +0000 | [diff] [blame] | 63 | if (!Old->use_empty()) { |
| 64 | std::cerr << "WARNING: Linking function '" << Old->getName() |
| 65 | << "' is causing arguments to be dropped.\n"; |
| 66 | std::cerr << "WARNING: Prototype: "; |
| 67 | WriteAsOperand(std::cerr, Old); |
| 68 | std::cerr << " resolved to "; |
| 69 | WriteAsOperand(std::cerr, Concrete); |
| 70 | std::cerr << "\n"; |
| 71 | } |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 72 | |
| 73 | // Check to make sure that if there are specified types, that they |
| 74 | // match... |
| 75 | // |
Chris Lattner | dbb05b0 | 2003-02-27 20:55:48 +0000 | [diff] [blame] | 76 | unsigned NumArguments = std::min(OldMT->getParamTypes().size(), |
| 77 | ConcreteMT->getParamTypes().size()); |
| 78 | |
Chris Lattner | 50cbb90 | 2003-03-03 19:57:46 +0000 | [diff] [blame] | 79 | if (!Old->use_empty() && !Concrete->use_empty()) |
| 80 | for (unsigned i = 0; i < NumArguments; ++i) |
Chris Lattner | 6fc0ee9 | 2003-08-23 20:03:05 +0000 | [diff] [blame] | 81 | if (OldMT->getParamTypes()[i] != ConcreteMT->getParamTypes()[i]) |
Chris Lattner | 015d98e | 2003-08-20 23:50:38 +0000 | [diff] [blame] | 82 | if (OldMT->getParamTypes()[i]->getPrimitiveID() != |
Chris Lattner | 6fc0ee9 | 2003-08-23 20:03:05 +0000 | [diff] [blame] | 83 | ConcreteMT->getParamTypes()[i]->getPrimitiveID()) { |
| 84 | std::cerr << "WARNING: Function [" << Old->getName() |
| 85 | << "]: Parameter types conflict for: '" << OldMT |
| 86 | << "' and '" << ConcreteMT << "'\n"; |
Chris Lattner | 015d98e | 2003-08-20 23:50:38 +0000 | [diff] [blame] | 87 | return Changed; |
Chris Lattner | 6fc0ee9 | 2003-08-23 20:03:05 +0000 | [diff] [blame] | 88 | } |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 9810b94 | 2003-04-28 01:23:29 +0000 | [diff] [blame] | 90 | // Attempt to convert all of the uses of the old function to the concrete |
| 91 | // form of the function. If there is a use of the fn that we don't |
| 92 | // understand here we punt to avoid making a bad transformation. |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 93 | // |
Chris Lattner | 9810b94 | 2003-04-28 01:23:29 +0000 | [diff] [blame] | 94 | // At this point, we know that the return values are the same for our two |
| 95 | // functions and that the Old function has no varargs fns specified. In |
| 96 | // otherwords it's just <retty> (...) |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 97 | // |
Chris Lattner | e934022 | 2003-07-23 22:03:18 +0000 | [diff] [blame] | 98 | if (!Old->use_empty()) { // Avoid making the CPR unless we really need it |
| 99 | Value *Replacement = Concrete; |
| 100 | if (Concrete->getType() != Old->getType()) |
| 101 | Replacement = ConstantExpr::getCast(ConstantPointerRef::get(Concrete), |
| 102 | Old->getType()); |
| 103 | NumResolved += Old->use_size(); |
| 104 | Old->replaceAllUsesWith(Replacement); |
| 105 | } |
Chris Lattner | 0804368 | 2003-05-31 21:08:45 +0000 | [diff] [blame] | 106 | |
| 107 | // Since there are no uses of Old anymore, remove it from the module. |
| 108 | M.getFunctionList().erase(Old); |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 109 | } |
| 110 | return Changed; |
| 111 | } |
| 112 | |
| 113 | |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 114 | static bool ResolveGlobalVariables(Module &M, |
| 115 | std::vector<GlobalValue*> &Globals, |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 116 | GlobalVariable *Concrete) { |
| 117 | bool Changed = false; |
Chris Lattner | defe5c7 | 2003-04-19 00:15:27 +0000 | [diff] [blame] | 118 | Constant *CCPR = ConstantPointerRef::get(Concrete); |
| 119 | |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 120 | for (unsigned i = 0; i != Globals.size(); ++i) |
| 121 | if (Globals[i] != Concrete) { |
Chris Lattner | af2c00b | 2003-10-21 23:17:56 +0000 | [diff] [blame] | 122 | Constant *Cast = ConstantExpr::getCast(CCPR, Globals[i]->getType()); |
| 123 | Globals[i]->replaceAllUsesWith(Cast); |
Chris Lattner | defe5c7 | 2003-04-19 00:15:27 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 125 | // Since there are no uses of Old anymore, remove it from the module. |
Chris Lattner | af2c00b | 2003-10-21 23:17:56 +0000 | [diff] [blame] | 126 | M.getGlobalList().erase(cast<GlobalVariable>(Globals[i])); |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 127 | |
| 128 | ++NumGlobals; |
| 129 | Changed = true; |
| 130 | } |
| 131 | return Changed; |
| 132 | } |
| 133 | |
Chris Lattner | 4cd99ff | 2003-10-22 03:35:34 +0000 | [diff] [blame] | 134 | static bool ProcessGlobalsWithSameName(Module &M, TargetData &TD, |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 135 | std::vector<GlobalValue*> &Globals) { |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 136 | assert(!Globals.empty() && "Globals list shouldn't be empty here!"); |
| 137 | |
| 138 | bool isFunction = isa<Function>(Globals[0]); // Is this group all functions? |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 139 | GlobalValue *Concrete = 0; // The most concrete implementation to resolve to |
| 140 | |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 141 | for (unsigned i = 0; i != Globals.size(); ) { |
| 142 | if (isa<Function>(Globals[i]) != isFunction) { |
| 143 | std::cerr << "WARNING: Found function and global variable with the " |
| 144 | << "same name: '" << Globals[i]->getName() << "'.\n"; |
| 145 | return false; // Don't know how to handle this, bail out! |
| 146 | } |
| 147 | |
Chris Lattner | 52b8fc0 | 2002-11-10 03:36:55 +0000 | [diff] [blame] | 148 | if (isFunction) { |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 149 | // For functions, we look to merge functions definitions of "int (...)" |
| 150 | // to 'int (int)' or 'int ()' or whatever else is not completely generic. |
| 151 | // |
| 152 | Function *F = cast<Function>(Globals[i]); |
Chris Lattner | 5997c3d | 2002-11-08 00:38:20 +0000 | [diff] [blame] | 153 | if (!F->isExternal()) { |
Chris Lattner | 52b8fc0 | 2002-11-10 03:36:55 +0000 | [diff] [blame] | 154 | if (Concrete && !Concrete->isExternal()) |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 155 | return false; // Found two different functions types. Can't choose! |
| 156 | |
| 157 | Concrete = Globals[i]; |
Chris Lattner | 52b8fc0 | 2002-11-10 03:36:55 +0000 | [diff] [blame] | 158 | } else if (Concrete) { |
| 159 | if (Concrete->isExternal()) // If we have multiple external symbols...x |
| 160 | if (F->getFunctionType()->getNumParams() > |
| 161 | cast<Function>(Concrete)->getFunctionType()->getNumParams()) |
| 162 | Concrete = F; // We are more concrete than "Concrete"! |
| 163 | |
| 164 | } else { |
| 165 | Concrete = F; |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 166 | } |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 167 | } else { |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 168 | GlobalVariable *GV = cast<GlobalVariable>(Globals[i]); |
Chris Lattner | af2c00b | 2003-10-21 23:17:56 +0000 | [diff] [blame] | 169 | if (!GV->isExternal()) { |
| 170 | if (Concrete) { |
| 171 | std::cerr << "WARNING: Two global variables with external linkage" |
| 172 | << " exist with the same name: '" << GV->getName() |
| 173 | << "'!\n"; |
| 174 | return false; |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 175 | } |
Chris Lattner | af2c00b | 2003-10-21 23:17:56 +0000 | [diff] [blame] | 176 | Concrete = GV; |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 177 | } |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 178 | } |
Chris Lattner | defe5c7 | 2003-04-19 00:15:27 +0000 | [diff] [blame] | 179 | ++i; |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | if (Globals.size() > 1) { // Found a multiply defined global... |
Chris Lattner | 2b13296 | 2003-05-31 21:57:06 +0000 | [diff] [blame] | 183 | // If there are no external declarations, and there is at most one |
| 184 | // externally visible instance of the global, then there is nothing to do. |
| 185 | // |
| 186 | bool HasExternal = false; |
| 187 | unsigned NumInstancesWithExternalLinkage = 0; |
| 188 | |
| 189 | for (unsigned i = 0, e = Globals.size(); i != e; ++i) { |
| 190 | if (Globals[i]->isExternal()) |
| 191 | HasExternal = true; |
| 192 | else if (!Globals[i]->hasInternalLinkage()) |
| 193 | NumInstancesWithExternalLinkage++; |
| 194 | } |
| 195 | |
| 196 | if (!HasExternal && NumInstancesWithExternalLinkage <= 1) |
| 197 | return false; // Nothing to do? Must have multiple internal definitions. |
| 198 | |
Chris Lattner | 9e8fe49 | 2003-10-22 23:03:38 +0000 | [diff] [blame] | 199 | // There are a couple of special cases we don't want to print the warning |
| 200 | // for, check them now. |
| 201 | bool DontPrintWarning = false; |
| 202 | if (Concrete && Globals.size() == 2) { |
| 203 | GlobalValue *Other = Globals[Globals[0] == Concrete]; |
| 204 | // If the non-concrete global is a function which takes (...) arguments, |
| 205 | // and the return values match, do not warn. |
| 206 | if (Function *ConcreteF = dyn_cast<Function>(Concrete)) |
| 207 | if (Function *OtherF = dyn_cast<Function>(Other)) |
| 208 | if (ConcreteF->getReturnType() == OtherF->getReturnType() && |
| 209 | OtherF->getFunctionType()->isVarArg() && |
| 210 | OtherF->getFunctionType()->getParamTypes().empty()) |
| 211 | DontPrintWarning = true; |
| 212 | |
| 213 | // Otherwise, if the non-concrete global is a global array variable with a |
| 214 | // size of 0, and the concrete global is an array with a real size, don't |
| 215 | // warn. This occurs due to declaring 'extern int A[];'. |
| 216 | if (GlobalVariable *ConcreteGV = dyn_cast<GlobalVariable>(Concrete)) |
| 217 | if (GlobalVariable *OtherGV = dyn_cast<GlobalVariable>(Other)) |
| 218 | if (const ArrayType *OtherAT = |
| 219 | dyn_cast<ArrayType>(OtherGV->getType()->getElementType())) |
| 220 | if (const ArrayType *ConcreteAT = |
| 221 | dyn_cast<ArrayType>(ConcreteGV->getType()->getElementType())) |
| 222 | if (OtherAT->getElementType() == ConcreteAT->getElementType() && |
| 223 | OtherAT->getNumElements() == 0) |
| 224 | DontPrintWarning = true; |
| 225 | } |
Chris Lattner | 2b13296 | 2003-05-31 21:57:06 +0000 | [diff] [blame] | 226 | |
Chris Lattner | 9e8fe49 | 2003-10-22 23:03:38 +0000 | [diff] [blame] | 227 | if (!DontPrintWarning) { |
| 228 | std::cerr << "WARNING: Found global types that are not compatible:\n"; |
| 229 | for (unsigned i = 0; i < Globals.size(); ++i) { |
| 230 | std::cerr << "\t" << *Globals[i]->getType() << " %" |
| 231 | << Globals[i]->getName() << "\n"; |
| 232 | } |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Chris Lattner | af2c00b | 2003-10-21 23:17:56 +0000 | [diff] [blame] | 235 | if (!Concrete) |
| 236 | Concrete = Globals[0]; |
Chris Lattner | 4cd99ff | 2003-10-22 03:35:34 +0000 | [diff] [blame] | 237 | else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Concrete)) { |
| 238 | // Handle special case hack to change globals if it will make their types |
| 239 | // happier in the long run. The situation we do this is intentionally |
| 240 | // extremely limited. |
| 241 | if (GV->use_empty() && GV->hasInitializer() && |
| 242 | GV->getInitializer()->isNullValue()) { |
| 243 | // Check to see if there is another (external) global with the same size |
| 244 | // and a non-empty use-list. If so, we will make IT be the real |
| 245 | // implementation. |
| 246 | unsigned TS = TD.getTypeSize(Concrete->getType()->getElementType()); |
| 247 | for (unsigned i = 0, e = Globals.size(); i != e; ++i) |
| 248 | if (Globals[i] != Concrete && !Globals[i]->use_empty() && |
| 249 | isa<GlobalVariable>(Globals[i]) && |
| 250 | TD.getTypeSize(Globals[i]->getType()->getElementType()) == TS) { |
| 251 | // At this point we want to replace Concrete with Globals[i]. Make |
| 252 | // concrete external, and Globals[i] have an initializer. |
| 253 | GlobalVariable *NGV = cast<GlobalVariable>(Globals[i]); |
| 254 | const Type *ElTy = NGV->getType()->getElementType(); |
| 255 | NGV->setInitializer(Constant::getNullValue(ElTy)); |
| 256 | cast<GlobalVariable>(Concrete)->setInitializer(0); |
| 257 | Concrete = NGV; |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | } |
Chris Lattner | af2c00b | 2003-10-21 23:17:56 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 263 | if (isFunction) |
Chris Lattner | defe5c7 | 2003-04-19 00:15:27 +0000 | [diff] [blame] | 264 | return ResolveFunctions(M, Globals, cast<Function>(Concrete)); |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 265 | else |
Chris Lattner | defe5c7 | 2003-04-19 00:15:27 +0000 | [diff] [blame] | 266 | return ResolveGlobalVariables(M, Globals, |
| 267 | cast<GlobalVariable>(Concrete)); |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 268 | } |
Chris Lattner | defe5c7 | 2003-04-19 00:15:27 +0000 | [diff] [blame] | 269 | return false; |
Chris Lattner | 013eca00 | 2002-10-09 21:10:06 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 272 | bool FunctionResolvingPass::run(Module &M) { |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 273 | std::map<std::string, std::vector<GlobalValue*> > Globals; |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 274 | |
Chris Lattner | 12b3593 | 2003-10-22 04:42:20 +0000 | [diff] [blame] | 275 | // Loop over the globals, adding them to the Globals map. We use a two pass |
| 276 | // algorithm here to avoid problems with iterators getting invalidated if we |
| 277 | // did a one pass scheme. |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 278 | // |
Chris Lattner | 6d3425e | 2003-10-22 04:43:18 +0000 | [diff] [blame] | 279 | bool Changed = false; |
Chris Lattner | 12b3593 | 2003-10-22 04:42:20 +0000 | [diff] [blame] | 280 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ) { |
| 281 | Function *F = I++; |
Chris Lattner | 6d3425e | 2003-10-22 04:43:18 +0000 | [diff] [blame] | 282 | if (F->use_empty() && F->isExternal()) { |
Chris Lattner | 12b3593 | 2003-10-22 04:42:20 +0000 | [diff] [blame] | 283 | M.getFunctionList().erase(F); |
Chris Lattner | 6d3425e | 2003-10-22 04:43:18 +0000 | [diff] [blame] | 284 | Changed = true; |
| 285 | } else if (!F->hasInternalLinkage() && !F->getName().empty()) |
Chris Lattner | 12b3593 | 2003-10-22 04:42:20 +0000 | [diff] [blame] | 286 | Globals[F->getName()].push_back(F); |
| 287 | } |
| 288 | |
| 289 | for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ) { |
| 290 | GlobalVariable *GV = I++; |
Chris Lattner | 6d3425e | 2003-10-22 04:43:18 +0000 | [diff] [blame] | 291 | if (GV->use_empty() && GV->isExternal()) { |
Chris Lattner | 12b3593 | 2003-10-22 04:42:20 +0000 | [diff] [blame] | 292 | M.getGlobalList().erase(GV); |
Chris Lattner | 6d3425e | 2003-10-22 04:43:18 +0000 | [diff] [blame] | 293 | Changed = true; |
| 294 | } else if (!GV->hasInternalLinkage() && !GV->getName().empty()) |
Chris Lattner | 12b3593 | 2003-10-22 04:42:20 +0000 | [diff] [blame] | 295 | Globals[GV->getName()].push_back(GV); |
| 296 | } |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 297 | |
Chris Lattner | 4cd99ff | 2003-10-22 03:35:34 +0000 | [diff] [blame] | 298 | TargetData &TD = getAnalysis<TargetData>(); |
| 299 | |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 300 | // Now we have a list of all functions with a particular name. If there is |
| 301 | // more than one entry in a list, merge the functions together. |
| 302 | // |
Chris Lattner | b2d9f7d | 2003-01-30 18:22:32 +0000 | [diff] [blame] | 303 | for (std::map<std::string, std::vector<GlobalValue*> >::iterator |
| 304 | I = Globals.begin(), E = Globals.end(); I != E; ++I) |
Chris Lattner | 4cd99ff | 2003-10-22 03:35:34 +0000 | [diff] [blame] | 305 | Changed |= ProcessGlobalsWithSameName(M, TD, I->second); |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 306 | |
Chris Lattner | 52b8fc0 | 2002-11-10 03:36:55 +0000 | [diff] [blame] | 307 | // Now loop over all of the globals, checking to see if any are trivially |
| 308 | // dead. If so, remove them now. |
| 309 | |
| 310 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ) |
| 311 | if (I->isExternal() && I->use_empty()) { |
| 312 | Function *F = I; |
| 313 | ++I; |
| 314 | M.getFunctionList().erase(F); |
| 315 | ++NumResolved; |
| 316 | Changed = true; |
| 317 | } else { |
| 318 | ++I; |
| 319 | } |
| 320 | |
| 321 | for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ) |
| 322 | if (I->isExternal() && I->use_empty()) { |
| 323 | GlobalVariable *GV = I; |
| 324 | ++I; |
| 325 | M.getGlobalList().erase(GV); |
| 326 | ++NumGlobals; |
| 327 | Changed = true; |
| 328 | } else { |
| 329 | ++I; |
| 330 | } |
| 331 | |
Chris Lattner | 5aa9e3e | 2002-05-24 20:42:13 +0000 | [diff] [blame] | 332 | return Changed; |
| 333 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame^] | 334 | |
| 335 | } // End llvm namespace |