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" |
| 8 | #include "SystemUtils.h" |
| 9 | #include "ListReducer.h" |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 10 | #include "llvm/Constants.h" |
| 11 | #include "llvm/DerivedTypes.h" |
| 12 | #include "llvm/GlobalValue.h" |
| 13 | #include "llvm/iMemory.h" |
| 14 | #include "llvm/iTerminators.h" |
| 15 | #include "llvm/iOther.h" |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 17 | #include "llvm/Pass.h" |
| 18 | #include "llvm/Analysis/Verifier.h" |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Mangler.h" |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 20 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/Utils/Cloning.h" |
| 22 | #include "llvm/Transforms/Utils/Linker.h" |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame^] | 23 | #include "Support/CommandLine.h" |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 24 | #include "Support/Statistic.h" |
| 25 | #include "Support/StringExtras.h" |
| 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, |
| 51 | bool KeepFiles) |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 52 | { |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame^] | 53 | std::cout << "Testing functions: "; |
| 54 | BD.PrintFunctionList(Funcs); |
| 55 | std::cout << "\t"; |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 56 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 57 | // Clone the module for the two halves of the program we want. |
| 58 | Module *SafeModule = CloneModule(BD.Program); |
| 59 | |
| 60 | // Make sure functions & globals are all external so that linkage |
| 61 | // between the two modules will work. |
| 62 | for (Module::iterator I = SafeModule->begin(), E = SafeModule->end();I!=E;++I) |
| 63 | I->setLinkage(GlobalValue::ExternalLinkage); |
| 64 | for (Module::giterator I=SafeModule->gbegin(),E = SafeModule->gend();I!=E;++I) |
| 65 | I->setLinkage(GlobalValue::ExternalLinkage); |
| 66 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 67 | Module *TestModule = CloneModule(SafeModule); |
| 68 | |
| 69 | // Make sure global initializers exist only in the safe module (CBE->.so) |
| 70 | for (Module::giterator I=TestModule->gbegin(),E = TestModule->gend();I!=E;++I) |
| 71 | I->setInitializer(0); // Delete the initializer to make it external |
| 72 | |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 73 | // Remove the Test functions from the Safe module |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 74 | for (unsigned i = 0, e = Funcs.size(); i != e; ++i) { |
| 75 | Function *TNOF = SafeModule->getFunction(Funcs[i]->getName(), |
| 76 | Funcs[i]->getFunctionType()); |
Misha Brukman | de9720f | 2003-07-29 16:02:28 +0000 | [diff] [blame] | 77 | DEBUG(std::cerr << "Removing function " << Funcs[i]->getName() << "\n"); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 78 | assert(TNOF && "Function doesn't exist in module!"); |
| 79 | DeleteFunctionBody(TNOF); // Function is now external in this module! |
| 80 | } |
| 81 | |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 82 | // Remove the Safe functions from the Test module |
| 83 | for (Module::iterator I=TestModule->begin(),E=TestModule->end(); I!=E; ++I) { |
| 84 | bool funcFound = false; |
| 85 | for (std::vector<Function*>::const_iterator F=Funcs.begin(),Fe=Funcs.end(); |
| 86 | F != Fe; ++F) |
| 87 | if (I->getName() == (*F)->getName()) funcFound = true; |
| 88 | |
| 89 | if (!funcFound && !(BD.isExecutingJIT() && I->getName() == "main")) |
| 90 | DeleteFunctionBody(I); |
| 91 | } |
| 92 | |
| 93 | // This is only applicable if we are debugging the JIT: |
| 94 | // Find all external functions in the Safe modules that are actually used |
| 95 | // (called or taken address of), and make them call the JIT wrapper instead |
| 96 | if (BD.isExecutingJIT()) { |
| 97 | // Must delete `main' from Safe module if it has it |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 98 | Function *safeMain = SafeModule->getNamedFunction("main"); |
Misha Brukman | de9720f | 2003-07-29 16:02:28 +0000 | [diff] [blame] | 99 | assert(safeMain && "`main' function not found in safe module!"); |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 100 | DeleteFunctionBody(safeMain); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 101 | |
| 102 | // Add an external function "getPointerToNamedFunction" that JIT provides |
| 103 | // Prototype: void *getPointerToNamedFunction(const char* Name) |
| 104 | std::vector<const Type*> Params; |
| 105 | Params.push_back(PointerType::get(Type::SByteTy)); // std::string& |
| 106 | FunctionType *resolverTy = FunctionType::get(PointerType::get(Type::VoidTy), |
| 107 | Params, false /* isVarArg */); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 108 | Function *resolverFunc = new Function(resolverTy, |
| 109 | GlobalValue::ExternalLinkage, |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 110 | "getPointerToNamedFunction", |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 111 | SafeModule); |
| 112 | |
| 113 | // Use the function we just added to get addresses of functions we need |
| 114 | // Iterate over the global declarations in the Safe module |
| 115 | for (Module::iterator F=SafeModule->begin(),E=SafeModule->end(); F!=E; ++F){ |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 116 | if (F->isExternal() && !F->use_empty() && &(*F) != resolverFunc) { |
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 | |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 169 | DEBUG(std::cerr << "Safe module:\n"; |
| 170 | typedef Module::iterator MI; |
| 171 | typedef Module::giterator MGI; |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 172 | |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 173 | for (MI I = SafeModule->begin(), E = SafeModule->end(); I != E; ++I) |
| 174 | if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n"; |
| 175 | for (MGI I = SafeModule->gbegin(), E = SafeModule->gend(); I!=E; ++I) |
| 176 | if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n"; |
| 177 | |
| 178 | std::cerr << "Test module:\n"; |
| 179 | for (MI I = TestModule->begin(), E = TestModule->end(); I != E; ++I) |
| 180 | if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n"; |
| 181 | for (MGI I=TestModule->gbegin(),E = TestModule->gend(); I!= E; ++I) |
| 182 | if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n"; |
| 183 | ); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 184 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 185 | // Write out the bytecode to be sent to CBE |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 186 | std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc"); |
| 187 | if (verifyModule(*SafeModule)) { |
| 188 | std::cerr << "Bytecode file corrupted!\n"; |
| 189 | exit(1); |
| 190 | } |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 191 | if (BD.writeProgramToFile(SafeModuleBC, SafeModule)) { |
| 192 | std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting."; |
| 193 | exit(1); |
| 194 | } |
| 195 | |
| 196 | // Make a shared library |
| 197 | std::string SharedObject; |
| 198 | BD.compileSharedObject(SafeModuleBC, SharedObject); |
| 199 | |
| 200 | // Remove all functions from the Test module EXCEPT for the ones specified in |
| 201 | // Funcs. We know which ones these are because they are non-external in |
| 202 | // ToOptimize, but external in ToNotOptimize. |
| 203 | // |
| 204 | for (Module::iterator I = TestModule->begin(), E = TestModule->end();I!=E;++I) |
| 205 | if (!I->isExternal()) { |
| 206 | Function *TNOF = SafeModule->getFunction(I->getName(), |
| 207 | I->getFunctionType()); |
| 208 | assert(TNOF && "Function doesn't exist in ToNotOptimize module??"); |
| 209 | if (!TNOF->isExternal()) |
| 210 | DeleteFunctionBody(I); |
| 211 | } |
| 212 | |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 213 | std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc"); |
| 214 | if (verifyModule(*TestModule)) { |
| 215 | std::cerr << "Bytecode file corrupted!\n"; |
| 216 | exit(1); |
| 217 | } |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 218 | if (BD.writeProgramToFile(TestModuleBC, TestModule)) { |
| 219 | std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting."; |
| 220 | exit(1); |
| 221 | } |
| 222 | |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 223 | delete SafeModule; |
| 224 | delete TestModule; |
| 225 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 226 | // Run the code generator on the `Test' code, loading the shared library. |
| 227 | // The function returns whether or not the new output differs from reference. |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 228 | int Result = BD.diffProgram(TestModuleBC, SharedObject, false); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 229 | if (KeepFiles) { |
| 230 | std::cout << "You can reproduce the problem with the command line: \n" |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 231 | << (BD.isExecutingJIT() ? "lli" : "llc") |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame^] | 232 | << " -load " << SharedObject << " " << TestModuleBC; |
| 233 | for (unsigned i=0, e = InputArgv.size(); i != e; ++i) |
| 234 | std::cout << " " << InputArgv[i]; |
| 235 | std::cout << "\n"; |
| 236 | std::cout << "The shared object " << SharedObject << " was created from " |
| 237 | << SafeModuleBC << ", using `dis -c'.\n"; |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 238 | } else { |
| 239 | removeFile(TestModuleBC); |
| 240 | removeFile(SafeModuleBC); |
| 241 | removeFile(SharedObject); |
| 242 | } |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 243 | return Result; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | namespace { |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 247 | struct Disambiguator { |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 248 | std::set<std::string> SymbolNames; |
| 249 | std::set<GlobalValue*> Symbols; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 250 | uint64_t uniqueCounter; |
| 251 | bool externalOnly; |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 252 | public: |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 253 | Disambiguator() : uniqueCounter(0), externalOnly(true) {} |
| 254 | void setExternalOnly(bool value) { externalOnly = value; } |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 255 | void add(GlobalValue &V) { |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 256 | // If we're only processing externals and this isn't external, bail |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 257 | if (externalOnly && !V.isExternal()) return; |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 258 | // If we're already processed this symbol, don't add it again |
| 259 | if (Symbols.count(&V) != 0) return; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 260 | |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 261 | std::string SymName = V.getName(); |
| 262 | |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 263 | // Use the Mangler facility to make symbol names that will be valid in |
| 264 | // shared objects. |
| 265 | SymName = Mangler::makeNameProper(SymName); |
| 266 | V.setName(SymName); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 267 | |
| 268 | if (SymbolNames.count(SymName) == 0) { |
| 269 | DEBUG(std::cerr << "Disambiguator: adding " << SymName |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 270 | << ", no conflicts.\n"); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 271 | SymbolNames.insert(SymName); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 272 | } else { |
| 273 | // Mangle name before adding |
| 274 | std::string newName; |
| 275 | do { |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 276 | newName = SymName + "_" + utostr(uniqueCounter); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 277 | if (SymbolNames.count(newName) == 0) break; |
| 278 | else ++uniqueCounter; |
| 279 | } while (1); |
| 280 | //while (SymbolNames.count(V->getName()+utostr(uniqueCounter++))==0); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 281 | DEBUG(std::cerr << "Disambiguator: conflict: " << SymName |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 282 | << ", adding: " << newName << "\n"); |
| 283 | V.setName(newName); |
| 284 | SymbolNames.insert(newName); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 285 | } |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 286 | Symbols.insert(&V); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 287 | } |
| 288 | }; |
| 289 | } |
| 290 | |
Misha Brukman | de9720f | 2003-07-29 16:02:28 +0000 | [diff] [blame] | 291 | void DisambiguateGlobalSymbols(Module *M) { |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 292 | // First, try not to cause collisions by minimizing chances of renaming an |
| 293 | // already-external symbol, so take in external globals and functions as-is. |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 294 | Disambiguator D; |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 295 | DEBUG(std::cerr << "Disambiguating globals (external-only)\n"); |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 296 | 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] | 297 | DEBUG(std::cerr << "Disambiguating functions (external-only)\n"); |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 298 | 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] | 299 | |
| 300 | // Now just rename functions and globals as necessary, keeping what's already |
| 301 | // in the set unique. |
| 302 | D.setExternalOnly(false); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 303 | DEBUG(std::cerr << "Disambiguating globals\n"); |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 304 | 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] | 305 | DEBUG(std::cerr << "Disambiguating globals\n"); |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 306 | 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] | 307 | } |
| 308 | |
| 309 | |
| 310 | bool BugDriver::debugCodeGenerator() { |
| 311 | // See if we can pin down which functions are being miscompiled... |
| 312 | //First, build a list of all of the non-external functions in the program. |
| 313 | std::vector<Function*> MisCodegenFunctions; |
| 314 | for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I) |
| 315 | if (!I->isExternal()) |
| 316 | MisCodegenFunctions.push_back(I); |
| 317 | |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 318 | // If we are executing the JIT, we *must* keep the function `main' in the |
| 319 | // module that is passed in, and not the shared library. However, we still |
| 320 | // want to be able to debug the `main' function alone. Thus, we create a new |
| 321 | // function `main' which just calls the old one. |
| 322 | if (isExecutingJIT()) { |
| 323 | // Get the `main' function |
| 324 | Function *oldMain = Program->getNamedFunction("main"); |
Misha Brukman | de9720f | 2003-07-29 16:02:28 +0000 | [diff] [blame] | 325 | assert(oldMain && "`main' function not found in program!"); |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 326 | // Rename it |
| 327 | oldMain->setName("old_main"); |
| 328 | // Create a NEW `main' function with same type |
| 329 | Function *newMain = new Function(oldMain->getFunctionType(), |
Misha Brukman | c1e39ee | 2003-07-28 21:07:39 +0000 | [diff] [blame] | 330 | GlobalValue::ExternalLinkage, |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 331 | "main", Program); |
| 332 | // Call the old main function and return its result |
| 333 | BasicBlock *BB = new BasicBlock("entry", newMain); |
| 334 | std::vector<Value*> args; |
| 335 | for (Function::aiterator I=newMain->abegin(), E=newMain->aend(); I!=E; ++I) |
| 336 | args.push_back(I); |
| 337 | CallInst *call = new CallInst(oldMain, args); |
| 338 | BB->getInstList().push_back(call); |
| 339 | |
| 340 | // if the type of old function wasn't void, return value of call |
| 341 | ReturnInst *ret; |
| 342 | if (oldMain->getReturnType() != Type::VoidTy) { |
| 343 | ret = new ReturnInst(call); |
| 344 | } else { |
| 345 | ret = new ReturnInst(); |
| 346 | } |
| 347 | |
| 348 | // Add the return instruction to the BasicBlock |
| 349 | BB->getInstList().push_back(ret); |
| 350 | } |
| 351 | |
Misha Brukman | de9720f | 2003-07-29 16:02:28 +0000 | [diff] [blame] | 352 | DisambiguateGlobalSymbols(Program); |
| 353 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 354 | // Do the reduction... |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame^] | 355 | if (!ReduceMisCodegenFunctions(*this).reduceList(MisCodegenFunctions)) { |
| 356 | std::cerr << "*** Execution matches reference output! No problem " |
| 357 | << "detected...\nbugpoint can't help you with your problem!\n"; |
| 358 | return false; |
| 359 | } |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 360 | |
| 361 | std::cout << "\n*** The following functions are being miscompiled: "; |
| 362 | PrintFunctionList(MisCodegenFunctions); |
| 363 | std::cout << "\n"; |
| 364 | |
| 365 | // Output a bunch of bytecode files for the user... |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 366 | ReduceMisCodegenFunctions(*this).TestFuncs(MisCodegenFunctions, true); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 367 | |
| 368 | return false; |
| 369 | } |