Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 1 | //===- CodeGeneratorBug.cpp - Debug code generation bugs ------------------===// |
| 2 | // |
| 3 | // This file implements program code generation debugging support. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #include "BugDriver.h" |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 8 | #include "ListReducer.h" |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 9 | #include "llvm/Constants.h" |
| 10 | #include "llvm/DerivedTypes.h" |
| 11 | #include "llvm/GlobalValue.h" |
| 12 | #include "llvm/iMemory.h" |
| 13 | #include "llvm/iTerminators.h" |
| 14 | #include "llvm/iOther.h" |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 15 | #include "llvm/Module.h" |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 16 | #include "llvm/Pass.h" |
| 17 | #include "llvm/Analysis/Verifier.h" |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Mangler.h" |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 20 | #include "llvm/Transforms/Utils/Cloning.h" |
| 21 | #include "llvm/Transforms/Utils/Linker.h" |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 22 | #include "Support/CommandLine.h" |
Chris Lattner | c648dab | 2003-08-01 22:13:59 +0000 | [diff] [blame] | 23 | #include "Support/Debug.h" |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 24 | #include "Support/StringExtras.h" |
Misha Brukman | 3d9cafa | 2003-08-07 21:42:28 +0000 | [diff] [blame] | 25 | #include "Support/FileUtilities.h" |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 26 | #include <algorithm> |
| 27 | #include <set> |
| 28 | |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 29 | extern cl::list<std::string> InputArgv; |
| 30 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 31 | class ReduceMisCodegenFunctions : public ListReducer<Function*> { |
| 32 | BugDriver &BD; |
| 33 | public: |
| 34 | ReduceMisCodegenFunctions(BugDriver &bd) : BD(bd) {} |
| 35 | |
| 36 | virtual TestResult doTest(std::vector<Function*> &Prefix, |
| 37 | std::vector<Function*> &Suffix) { |
| 38 | if (!Prefix.empty() && TestFuncs(Prefix)) |
| 39 | return KeepPrefix; |
| 40 | if (!Suffix.empty() && TestFuncs(Suffix)) |
| 41 | return KeepSuffix; |
| 42 | return NoFailure; |
| 43 | } |
| 44 | |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 45 | bool TestFuncs(const std::vector<Function*> &CodegenTest, |
| 46 | bool KeepFiles = false); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 50 | bool ReduceMisCodegenFunctions::TestFuncs(const std::vector<Function*> &Funcs, |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 51 | bool KeepFiles) { |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 52 | std::cout << "Testing functions: "; |
| 53 | BD.PrintFunctionList(Funcs); |
| 54 | std::cout << "\t"; |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 55 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 56 | // Clone the module for the two halves of the program we want. |
| 57 | Module *SafeModule = CloneModule(BD.Program); |
| 58 | |
| 59 | // Make sure functions & globals are all external so that linkage |
| 60 | // between the two modules will work. |
| 61 | for (Module::iterator I = SafeModule->begin(), E = SafeModule->end();I!=E;++I) |
| 62 | I->setLinkage(GlobalValue::ExternalLinkage); |
| 63 | for (Module::giterator I=SafeModule->gbegin(),E = SafeModule->gend();I!=E;++I) |
| 64 | I->setLinkage(GlobalValue::ExternalLinkage); |
| 65 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 66 | Module *TestModule = CloneModule(SafeModule); |
| 67 | |
| 68 | // Make sure global initializers exist only in the safe module (CBE->.so) |
| 69 | for (Module::giterator I=TestModule->gbegin(),E = TestModule->gend();I!=E;++I) |
| 70 | I->setInitializer(0); // Delete the initializer to make it external |
| 71 | |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 72 | // Remove the Test functions from the Safe module |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 73 | for (unsigned i = 0, e = Funcs.size(); i != e; ++i) { |
| 74 | Function *TNOF = SafeModule->getFunction(Funcs[i]->getName(), |
| 75 | Funcs[i]->getFunctionType()); |
Misha Brukman | de9720f | 2003-07-29 16:02:28 +0000 | [diff] [blame] | 76 | DEBUG(std::cerr << "Removing function " << Funcs[i]->getName() << "\n"); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 77 | assert(TNOF && "Function doesn't exist in module!"); |
| 78 | DeleteFunctionBody(TNOF); // Function is now external in this module! |
| 79 | } |
| 80 | |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 81 | // Remove the Safe functions from the Test module |
| 82 | for (Module::iterator I=TestModule->begin(),E=TestModule->end(); I!=E; ++I) { |
| 83 | bool funcFound = false; |
| 84 | for (std::vector<Function*>::const_iterator F=Funcs.begin(),Fe=Funcs.end(); |
| 85 | F != Fe; ++F) |
| 86 | if (I->getName() == (*F)->getName()) funcFound = true; |
| 87 | |
| 88 | if (!funcFound && !(BD.isExecutingJIT() && I->getName() == "main")) |
| 89 | DeleteFunctionBody(I); |
| 90 | } |
| 91 | |
| 92 | // This is only applicable if we are debugging the JIT: |
| 93 | // Find all external functions in the Safe modules that are actually used |
| 94 | // (called or taken address of), and make them call the JIT wrapper instead |
| 95 | if (BD.isExecutingJIT()) { |
| 96 | // Must delete `main' from Safe module if it has it |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 97 | Function *safeMain = SafeModule->getNamedFunction("main"); |
Misha Brukman | de9720f | 2003-07-29 16:02:28 +0000 | [diff] [blame] | 98 | assert(safeMain && "`main' function not found in safe module!"); |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 99 | DeleteFunctionBody(safeMain); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 100 | |
| 101 | // Add an external function "getPointerToNamedFunction" that JIT provides |
| 102 | // Prototype: void *getPointerToNamedFunction(const char* Name) |
| 103 | std::vector<const Type*> Params; |
| 104 | Params.push_back(PointerType::get(Type::SByteTy)); // std::string& |
| 105 | FunctionType *resolverTy = FunctionType::get(PointerType::get(Type::VoidTy), |
| 106 | Params, false /* isVarArg */); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 107 | Function *resolverFunc = new Function(resolverTy, |
| 108 | GlobalValue::ExternalLinkage, |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 109 | "getPointerToNamedFunction", |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 110 | SafeModule); |
| 111 | |
| 112 | // Use the function we just added to get addresses of functions we need |
| 113 | // Iterate over the global declarations in the Safe module |
| 114 | for (Module::iterator F=SafeModule->begin(),E=SafeModule->end(); F!=E; ++F){ |
Misha Brukman | 3b62462 | 2003-07-30 21:45:20 +0000 | [diff] [blame] | 115 | if (F->isExternal() && !F->use_empty() && &(*F) != resolverFunc && |
| 116 | F->getIntrinsicID() == 0 /* ignore intrinsics */) { |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 117 | // If it has a non-zero use list, |
| 118 | // 1. Add a string constant with its name to the global file |
| 119 | // The correct type is `const [ NUM x sbyte ]' where NUM is length of |
| 120 | // function name + 1 |
| 121 | const std::string &Name = F->getName(); |
| 122 | GlobalVariable *funcName = |
| 123 | new GlobalVariable(ArrayType::get(Type::SByteTy, Name.length()+1), |
| 124 | true /* isConstant */, |
| 125 | GlobalValue::InternalLinkage, |
| 126 | ConstantArray::get(Name), |
| 127 | Name + "_name", |
| 128 | SafeModule); |
| 129 | |
| 130 | // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an |
| 131 | // sbyte* so it matches the signature of the resolver function. |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 132 | std::vector<Constant*> GEPargs(2, Constant::getNullValue(Type::LongTy)); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 133 | |
| 134 | // 3. Replace all uses of `func' with calls to resolver by: |
| 135 | // (a) Iterating through the list of uses of this function |
| 136 | // (b) Insert a cast instruction in front of each use |
| 137 | // (c) Replace use of old call with new call |
| 138 | |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 139 | // GetElementPtr *funcName, ulong 0, ulong 0 |
| 140 | Value *GEP = |
| 141 | ConstantExpr::getGetElementPtr(ConstantPointerRef::get(funcName), |
| 142 | GEPargs); |
| 143 | std::vector<Value*> ResolverArgs; |
| 144 | ResolverArgs.push_back(GEP); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 145 | |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 146 | // Insert code at the beginning of the function |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 147 | for (Value::use_iterator i=F->use_begin(), e=F->use_end(); i!=e; ++i) { |
| 148 | if (Instruction* Inst = dyn_cast<Instruction>(*i)) { |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 149 | // call resolver(GetElementPtr...) |
| 150 | CallInst *resolve = new CallInst(resolverFunc, ResolverArgs, |
| 151 | "resolver", Inst); |
| 152 | // cast the result from the resolver to correctly-typed function |
| 153 | CastInst *castResolver = |
| 154 | new CastInst(resolve, PointerType::get(F->getFunctionType()), |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 155 | "resolverCast", Inst); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 156 | // actually use the resolved function |
| 157 | Inst->replaceUsesOfWith(F, castResolver); |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 158 | } else { |
| 159 | // FIXME: need to take care of cases where a function is used that |
| 160 | // is not an instruction, e.g. global variable initializer... |
| 161 | std::cerr << "Non-instruction is using an external function!\n"; |
| 162 | abort(); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
Chris Lattner | 10b9fa8 | 2003-08-01 16:14:33 +0000 | [diff] [blame] | 169 | if (verifyModule(*SafeModule) || verifyModule(*TestModule)) { |
| 170 | std::cerr << "Bugpoint has a bug, an corrupted a module!!\n"; |
| 171 | abort(); |
| 172 | } |
| 173 | |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 174 | DEBUG(std::cerr << "Safe module:\n"; |
| 175 | typedef Module::iterator MI; |
| 176 | typedef Module::giterator MGI; |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 177 | |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 178 | for (MI I = SafeModule->begin(), E = SafeModule->end(); I != E; ++I) |
| 179 | if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n"; |
| 180 | for (MGI I = SafeModule->gbegin(), E = SafeModule->gend(); I!=E; ++I) |
| 181 | if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n"; |
| 182 | |
| 183 | std::cerr << "Test module:\n"; |
| 184 | for (MI I = TestModule->begin(), E = TestModule->end(); I != E; ++I) |
| 185 | if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n"; |
| 186 | for (MGI I=TestModule->gbegin(),E = TestModule->gend(); I!= E; ++I) |
| 187 | if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n"; |
| 188 | ); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 189 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 190 | // Write out the bytecode to be sent to CBE |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 191 | std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc"); |
Chris Lattner | 10b9fa8 | 2003-08-01 16:14:33 +0000 | [diff] [blame] | 192 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 193 | if (BD.writeProgramToFile(SafeModuleBC, SafeModule)) { |
| 194 | std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting."; |
| 195 | exit(1); |
| 196 | } |
| 197 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 198 | // Remove all functions from the Test module EXCEPT for the ones specified in |
| 199 | // Funcs. We know which ones these are because they are non-external in |
| 200 | // ToOptimize, but external in ToNotOptimize. |
| 201 | // |
| 202 | for (Module::iterator I = TestModule->begin(), E = TestModule->end();I!=E;++I) |
| 203 | if (!I->isExternal()) { |
| 204 | Function *TNOF = SafeModule->getFunction(I->getName(), |
| 205 | I->getFunctionType()); |
| 206 | assert(TNOF && "Function doesn't exist in ToNotOptimize module??"); |
| 207 | if (!TNOF->isExternal()) |
| 208 | DeleteFunctionBody(I); |
| 209 | } |
| 210 | |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 211 | std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc"); |
| 212 | if (verifyModule(*TestModule)) { |
| 213 | std::cerr << "Bytecode file corrupted!\n"; |
| 214 | exit(1); |
| 215 | } |
Chris Lattner | 15d1127 | 2003-08-03 22:29:43 +0000 | [diff] [blame] | 216 | |
| 217 | // Clean up the modules, removing extra cruft that we don't need anymore... |
| 218 | SafeModule = BD.performFinalCleanups(SafeModule); |
| 219 | TestModule = BD.performFinalCleanups(TestModule); |
| 220 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 221 | if (BD.writeProgramToFile(TestModuleBC, TestModule)) { |
| 222 | std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting."; |
| 223 | exit(1); |
| 224 | } |
| 225 | |
Chris Lattner | 15d1127 | 2003-08-03 22:29:43 +0000 | [diff] [blame] | 226 | // Make a shared library |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 227 | std::string SharedObject = BD.compileSharedObject(SafeModuleBC); |
Chris Lattner | 15d1127 | 2003-08-03 22:29:43 +0000 | [diff] [blame] | 228 | |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 229 | delete SafeModule; |
| 230 | delete TestModule; |
| 231 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 232 | // Run the code generator on the `Test' code, loading the shared library. |
| 233 | // The function returns whether or not the new output differs from reference. |
Chris Lattner | 6730c81 | 2003-08-04 00:56:27 +0000 | [diff] [blame] | 234 | int Result = BD.diffProgram(TestModuleBC, SharedObject, false); |
| 235 | |
| 236 | if (Result) |
Misha Brukman | 11c6592 | 2003-08-07 21:05:13 +0000 | [diff] [blame] | 237 | std::cerr << ": still failing!\n"; |
Chris Lattner | 6730c81 | 2003-08-04 00:56:27 +0000 | [diff] [blame] | 238 | else |
| 239 | std::cerr << ": didn't fail.\n"; |
| 240 | |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 241 | if (KeepFiles) { |
Chris Lattner | 6730c81 | 2003-08-04 00:56:27 +0000 | [diff] [blame] | 242 | std::cout << "You can reproduce the problem with the command line: \n"; |
| 243 | if (BD.isExecutingJIT()) { |
| 244 | std::cout << " lli -load " << SharedObject << " " << TestModuleBC; |
| 245 | } else { |
Chris Lattner | 6730c81 | 2003-08-04 00:56:27 +0000 | [diff] [blame] | 246 | std::cout << " llc " << TestModuleBC << " -o " << TestModuleBC << ".s\n"; |
| 247 | std::cout << " gcc " << SharedObject << " " << TestModuleBC |
| 248 | << ".s -o " << TestModuleBC << ".exe\n"; |
| 249 | std::cout << " " << TestModuleBC << ".exe"; |
| 250 | } |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 251 | for (unsigned i=0, e = InputArgv.size(); i != e; ++i) |
| 252 | std::cout << " " << InputArgv[i]; |
| 253 | std::cout << "\n"; |
Misha Brukman | 85544ba | 2003-08-28 22:14:16 +0000 | [diff] [blame] | 254 | std::cout << "The shared object was created with:\n llvm-dis -c " |
Chris Lattner | e533cef | 2003-08-17 22:08:25 +0000 | [diff] [blame] | 255 | << SafeModuleBC << " -o temporary.c\n" |
Chris Lattner | 57c6941 | 2003-08-17 23:38:53 +0000 | [diff] [blame] | 256 | << " gcc -xc temporary.c -O2 -o " << SharedObject |
| 257 | #if defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
Chris Lattner | 182a21e | 2003-10-14 20:55:56 +0000 | [diff] [blame] | 258 | << " -G" // Compile a shared library, `-G' for Sparc |
Chris Lattner | 8deb581 | 2003-10-14 21:01:51 +0000 | [diff] [blame] | 259 | #else |
| 260 | << " -shared" // `-shared' for Linux/X86, maybe others |
Chris Lattner | 57c6941 | 2003-08-17 23:38:53 +0000 | [diff] [blame] | 261 | #endif |
Chris Lattner | b715497 | 2003-10-18 21:08:57 +0000 | [diff] [blame^] | 262 | << " -fno-strict-aliasing\n"; |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 263 | } else { |
| 264 | removeFile(TestModuleBC); |
| 265 | removeFile(SafeModuleBC); |
| 266 | removeFile(SharedObject); |
| 267 | } |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 268 | return Result; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | namespace { |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 272 | struct Disambiguator { |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 273 | std::set<std::string> SymbolNames; |
| 274 | std::set<GlobalValue*> Symbols; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 275 | uint64_t uniqueCounter; |
| 276 | bool externalOnly; |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 277 | public: |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 278 | Disambiguator() : uniqueCounter(0), externalOnly(true) {} |
| 279 | void setExternalOnly(bool value) { externalOnly = value; } |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 280 | void add(GlobalValue &V) { |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 281 | // If we're only processing externals and this isn't external, bail |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 282 | if (externalOnly && !V.isExternal()) return; |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 283 | // If we're already processed this symbol, don't add it again |
| 284 | if (Symbols.count(&V) != 0) return; |
Misha Brukman | 3b62462 | 2003-07-30 21:45:20 +0000 | [diff] [blame] | 285 | // Ignore intrinsic functions |
| 286 | if (Function *F = dyn_cast<Function>(&V)) |
| 287 | if (F->getIntrinsicID() != 0) |
| 288 | return; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 289 | |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 290 | std::string SymName = V.getName(); |
| 291 | |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 292 | // Use the Mangler facility to make symbol names that will be valid in |
| 293 | // shared objects. |
| 294 | SymName = Mangler::makeNameProper(SymName); |
| 295 | V.setName(SymName); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 296 | |
| 297 | if (SymbolNames.count(SymName) == 0) { |
| 298 | DEBUG(std::cerr << "Disambiguator: adding " << SymName |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 299 | << ", no conflicts.\n"); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 300 | SymbolNames.insert(SymName); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 301 | } else { |
| 302 | // Mangle name before adding |
| 303 | std::string newName; |
| 304 | do { |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 305 | newName = SymName + "_" + utostr(uniqueCounter); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 306 | if (SymbolNames.count(newName) == 0) break; |
| 307 | else ++uniqueCounter; |
| 308 | } while (1); |
| 309 | //while (SymbolNames.count(V->getName()+utostr(uniqueCounter++))==0); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 310 | DEBUG(std::cerr << "Disambiguator: conflict: " << SymName |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 311 | << ", adding: " << newName << "\n"); |
| 312 | V.setName(newName); |
| 313 | SymbolNames.insert(newName); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 314 | } |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 315 | Symbols.insert(&V); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 316 | } |
| 317 | }; |
| 318 | } |
| 319 | |
Misha Brukman | de9720f | 2003-07-29 16:02:28 +0000 | [diff] [blame] | 320 | void DisambiguateGlobalSymbols(Module *M) { |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 321 | // First, try not to cause collisions by minimizing chances of renaming an |
| 322 | // already-external symbol, so take in external globals and functions as-is. |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 323 | Disambiguator D; |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 324 | DEBUG(std::cerr << "Disambiguating globals (external-only)\n"); |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 325 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) D.add(*I); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 326 | DEBUG(std::cerr << "Disambiguating functions (external-only)\n"); |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 327 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) D.add(*I); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 328 | |
| 329 | // Now just rename functions and globals as necessary, keeping what's already |
| 330 | // in the set unique. |
| 331 | D.setExternalOnly(false); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 332 | DEBUG(std::cerr << "Disambiguating globals\n"); |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 333 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) D.add(*I); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 334 | DEBUG(std::cerr << "Disambiguating globals\n"); |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 335 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) D.add(*I); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | |
| 339 | bool BugDriver::debugCodeGenerator() { |
| 340 | // See if we can pin down which functions are being miscompiled... |
| 341 | //First, build a list of all of the non-external functions in the program. |
| 342 | std::vector<Function*> MisCodegenFunctions; |
| 343 | for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I) |
| 344 | if (!I->isExternal()) |
| 345 | MisCodegenFunctions.push_back(I); |
| 346 | |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 347 | // If we are executing the JIT, we *must* keep the function `main' in the |
| 348 | // module that is passed in, and not the shared library. However, we still |
| 349 | // want to be able to debug the `main' function alone. Thus, we create a new |
| 350 | // function `main' which just calls the old one. |
| 351 | if (isExecutingJIT()) { |
| 352 | // Get the `main' function |
| 353 | Function *oldMain = Program->getNamedFunction("main"); |
Misha Brukman | de9720f | 2003-07-29 16:02:28 +0000 | [diff] [blame] | 354 | assert(oldMain && "`main' function not found in program!"); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 355 | // Rename it |
Misha Brukman | 11c6592 | 2003-08-07 21:05:13 +0000 | [diff] [blame] | 356 | oldMain->setName("llvm_old_main"); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 357 | // Create a NEW `main' function with same type |
| 358 | Function *newMain = new Function(oldMain->getFunctionType(), |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 359 | GlobalValue::ExternalLinkage, |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 360 | "main", Program); |
| 361 | // Call the old main function and return its result |
| 362 | BasicBlock *BB = new BasicBlock("entry", newMain); |
| 363 | std::vector<Value*> args; |
Chris Lattner | 57e5a70 | 2003-08-17 22:14:20 +0000 | [diff] [blame] | 364 | for (Function::aiterator I = newMain->abegin(), E = newMain->aend(), |
| 365 | OI = oldMain->abegin(); I != E; ++I, ++OI) { |
| 366 | I->setName(OI->getName()); // Copy argument names from oldMain |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 367 | args.push_back(I); |
Chris Lattner | 57e5a70 | 2003-08-17 22:14:20 +0000 | [diff] [blame] | 368 | } |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 369 | CallInst *call = new CallInst(oldMain, args); |
| 370 | BB->getInstList().push_back(call); |
| 371 | |
| 372 | // if the type of old function wasn't void, return value of call |
| 373 | ReturnInst *ret; |
| 374 | if (oldMain->getReturnType() != Type::VoidTy) { |
| 375 | ret = new ReturnInst(call); |
| 376 | } else { |
| 377 | ret = new ReturnInst(); |
| 378 | } |
| 379 | |
| 380 | // Add the return instruction to the BasicBlock |
| 381 | BB->getInstList().push_back(ret); |
| 382 | } |
| 383 | |
Misha Brukman | de9720f | 2003-07-29 16:02:28 +0000 | [diff] [blame] | 384 | DisambiguateGlobalSymbols(Program); |
| 385 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 386 | // Do the reduction... |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 387 | if (!ReduceMisCodegenFunctions(*this).reduceList(MisCodegenFunctions)) { |
Chris Lattner | 6730c81 | 2003-08-04 00:56:27 +0000 | [diff] [blame] | 388 | std::cerr << "*** Execution matches reference output! " |
| 389 | << "bugpoint can't help you with your problem!\n"; |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 390 | return false; |
| 391 | } |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 392 | |
| 393 | std::cout << "\n*** The following functions are being miscompiled: "; |
| 394 | PrintFunctionList(MisCodegenFunctions); |
| 395 | std::cout << "\n"; |
| 396 | |
| 397 | // Output a bunch of bytecode files for the user... |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 398 | ReduceMisCodegenFunctions(*this).TestFuncs(MisCodegenFunctions, true); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 399 | |
| 400 | return false; |
| 401 | } |