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