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