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