blob: 02ef5e3459a5010ae4652d062c7cf65f8fbeb31a [file] [log] [blame]
Misha Brukman50733362003-07-24 18:17:43 +00001//===- 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 Brukman91eabc12003-07-28 19:16:14 +000010#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 Brukman50733362003-07-24 18:17:43 +000016#include "llvm/Module.h"
Misha Brukman91eabc12003-07-28 19:16:14 +000017#include "llvm/Pass.h"
18#include "llvm/Analysis/Verifier.h"
Misha Brukmanc1e39ee2003-07-28 21:07:39 +000019#include "llvm/Support/Mangler.h"
Misha Brukman91eabc12003-07-28 19:16:14 +000020#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Misha Brukman50733362003-07-24 18:17:43 +000021#include "llvm/Transforms/Utils/Cloning.h"
22#include "llvm/Transforms/Utils/Linker.h"
Misha Brukmanbe6bf562003-07-30 20:15:56 +000023#include "Support/CommandLine.h"
Misha Brukman50733362003-07-24 18:17:43 +000024#include "Support/Statistic.h"
25#include "Support/StringExtras.h"
26#include <algorithm>
27#include <set>
28
Misha Brukmanbe6bf562003-07-30 20:15:56 +000029extern cl::list<std::string> InputArgv;
30
Misha Brukman50733362003-07-24 18:17:43 +000031class ReduceMisCodegenFunctions : public ListReducer<Function*> {
32 BugDriver &BD;
33public:
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 Brukman91eabc12003-07-28 19:16:14 +000045 bool TestFuncs(const std::vector<Function*> &CodegenTest,
46 bool KeepFiles = false);
Misha Brukman50733362003-07-24 18:17:43 +000047};
48
49
Misha Brukman91eabc12003-07-28 19:16:14 +000050bool ReduceMisCodegenFunctions::TestFuncs(const std::vector<Function*> &Funcs,
51 bool KeepFiles)
Misha Brukman50733362003-07-24 18:17:43 +000052{
Misha Brukmanbe6bf562003-07-30 20:15:56 +000053 std::cout << "Testing functions: ";
54 BD.PrintFunctionList(Funcs);
55 std::cout << "\t";
Misha Brukman91eabc12003-07-28 19:16:14 +000056
Misha Brukman50733362003-07-24 18:17:43 +000057 // 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 Brukman50733362003-07-24 18:17:43 +000067 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 Brukmana259c9b2003-07-24 21:59:10 +000073 // Remove the Test functions from the Safe module
Misha Brukman50733362003-07-24 18:17:43 +000074 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
75 Function *TNOF = SafeModule->getFunction(Funcs[i]->getName(),
76 Funcs[i]->getFunctionType());
Misha Brukmande9720f2003-07-29 16:02:28 +000077 DEBUG(std::cerr << "Removing function " << Funcs[i]->getName() << "\n");
Misha Brukman50733362003-07-24 18:17:43 +000078 assert(TNOF && "Function doesn't exist in module!");
79 DeleteFunctionBody(TNOF); // Function is now external in this module!
80 }
81
Misha Brukman91eabc12003-07-28 19:16:14 +000082 // 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 Brukmanc1e39ee2003-07-28 21:07:39 +000098 Function *safeMain = SafeModule->getNamedFunction("main");
Misha Brukmande9720f2003-07-29 16:02:28 +000099 assert(safeMain && "`main' function not found in safe module!");
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000100 DeleteFunctionBody(safeMain);
Misha Brukman91eabc12003-07-28 19:16:14 +0000101
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 Brukman91eabc12003-07-28 19:16:14 +0000108 Function *resolverFunc = new Function(resolverTy,
109 GlobalValue::ExternalLinkage,
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000110 "getPointerToNamedFunction",
Misha Brukman91eabc12003-07-28 19:16:14 +0000111 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 Brukman3b624622003-07-30 21:45:20 +0000116 if (F->isExternal() && !F->use_empty() && &(*F) != resolverFunc &&
117 F->getIntrinsicID() == 0 /* ignore intrinsics */) {
Misha Brukman91eabc12003-07-28 19:16:14 +0000118 // If it has a non-zero use list,
119 // 1. Add a string constant with its name to the global file
120 // The correct type is `const [ NUM x sbyte ]' where NUM is length of
121 // function name + 1
122 const std::string &Name = F->getName();
123 GlobalVariable *funcName =
124 new GlobalVariable(ArrayType::get(Type::SByteTy, Name.length()+1),
125 true /* isConstant */,
126 GlobalValue::InternalLinkage,
127 ConstantArray::get(Name),
128 Name + "_name",
129 SafeModule);
130
131 // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
132 // sbyte* so it matches the signature of the resolver function.
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000133 std::vector<Constant*> GEPargs(2, Constant::getNullValue(Type::LongTy));
Misha Brukman91eabc12003-07-28 19:16:14 +0000134
135 // 3. Replace all uses of `func' with calls to resolver by:
136 // (a) Iterating through the list of uses of this function
137 // (b) Insert a cast instruction in front of each use
138 // (c) Replace use of old call with new call
139
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000140 // GetElementPtr *funcName, ulong 0, ulong 0
141 Value *GEP =
142 ConstantExpr::getGetElementPtr(ConstantPointerRef::get(funcName),
143 GEPargs);
144 std::vector<Value*> ResolverArgs;
145 ResolverArgs.push_back(GEP);
Misha Brukman91eabc12003-07-28 19:16:14 +0000146
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000147 // Insert code at the beginning of the function
Misha Brukman91eabc12003-07-28 19:16:14 +0000148 for (Value::use_iterator i=F->use_begin(), e=F->use_end(); i!=e; ++i) {
149 if (Instruction* Inst = dyn_cast<Instruction>(*i)) {
Misha Brukman91eabc12003-07-28 19:16:14 +0000150 // call resolver(GetElementPtr...)
151 CallInst *resolve = new CallInst(resolverFunc, ResolverArgs,
152 "resolver", Inst);
153 // cast the result from the resolver to correctly-typed function
154 CastInst *castResolver =
155 new CastInst(resolve, PointerType::get(F->getFunctionType()),
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000156 "resolverCast", Inst);
Misha Brukman91eabc12003-07-28 19:16:14 +0000157 // actually use the resolved function
158 Inst->replaceUsesOfWith(F, castResolver);
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000159 } else {
160 // FIXME: need to take care of cases where a function is used that
161 // is not an instruction, e.g. global variable initializer...
162 std::cerr << "Non-instruction is using an external function!\n";
163 abort();
Misha Brukman91eabc12003-07-28 19:16:14 +0000164 }
165 }
166 }
167 }
168 }
169
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000170 DEBUG(std::cerr << "Safe module:\n";
171 typedef Module::iterator MI;
172 typedef Module::giterator MGI;
Misha Brukman91eabc12003-07-28 19:16:14 +0000173
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000174 for (MI I = SafeModule->begin(), E = SafeModule->end(); I != E; ++I)
175 if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n";
176 for (MGI I = SafeModule->gbegin(), E = SafeModule->gend(); I!=E; ++I)
177 if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n";
178
179 std::cerr << "Test module:\n";
180 for (MI I = TestModule->begin(), E = TestModule->end(); I != E; ++I)
181 if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n";
182 for (MGI I=TestModule->gbegin(),E = TestModule->gend(); I!= E; ++I)
183 if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n";
184 );
Misha Brukman91eabc12003-07-28 19:16:14 +0000185
Misha Brukman50733362003-07-24 18:17:43 +0000186 // Write out the bytecode to be sent to CBE
Misha Brukman91eabc12003-07-28 19:16:14 +0000187 std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
188 if (verifyModule(*SafeModule)) {
189 std::cerr << "Bytecode file corrupted!\n";
190 exit(1);
191 }
Misha Brukman50733362003-07-24 18:17:43 +0000192 if (BD.writeProgramToFile(SafeModuleBC, SafeModule)) {
193 std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
194 exit(1);
195 }
196
197 // Make a shared library
198 std::string SharedObject;
199 BD.compileSharedObject(SafeModuleBC, SharedObject);
200
201 // Remove all functions from the Test module EXCEPT for the ones specified in
202 // Funcs. We know which ones these are because they are non-external in
203 // ToOptimize, but external in ToNotOptimize.
204 //
205 for (Module::iterator I = TestModule->begin(), E = TestModule->end();I!=E;++I)
206 if (!I->isExternal()) {
207 Function *TNOF = SafeModule->getFunction(I->getName(),
208 I->getFunctionType());
209 assert(TNOF && "Function doesn't exist in ToNotOptimize module??");
210 if (!TNOF->isExternal())
211 DeleteFunctionBody(I);
212 }
213
Misha Brukman91eabc12003-07-28 19:16:14 +0000214 std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
215 if (verifyModule(*TestModule)) {
216 std::cerr << "Bytecode file corrupted!\n";
217 exit(1);
218 }
Misha Brukman50733362003-07-24 18:17:43 +0000219 if (BD.writeProgramToFile(TestModuleBC, TestModule)) {
220 std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
221 exit(1);
222 }
223
Misha Brukman91eabc12003-07-28 19:16:14 +0000224 delete SafeModule;
225 delete TestModule;
226
Misha Brukman50733362003-07-24 18:17:43 +0000227 // Run the code generator on the `Test' code, loading the shared library.
228 // The function returns whether or not the new output differs from reference.
Misha Brukmana259c9b2003-07-24 21:59:10 +0000229 int Result = BD.diffProgram(TestModuleBC, SharedObject, false);
Misha Brukman91eabc12003-07-28 19:16:14 +0000230 if (KeepFiles) {
231 std::cout << "You can reproduce the problem with the command line: \n"
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000232 << (BD.isExecutingJIT() ? "lli" : "llc")
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000233 << " -load " << SharedObject << " " << TestModuleBC;
234 for (unsigned i=0, e = InputArgv.size(); i != e; ++i)
235 std::cout << " " << InputArgv[i];
236 std::cout << "\n";
237 std::cout << "The shared object " << SharedObject << " was created from "
238 << SafeModuleBC << ", using `dis -c'.\n";
Misha Brukman91eabc12003-07-28 19:16:14 +0000239 } else {
240 removeFile(TestModuleBC);
241 removeFile(SafeModuleBC);
242 removeFile(SharedObject);
243 }
Misha Brukmana259c9b2003-07-24 21:59:10 +0000244 return Result;
Misha Brukman50733362003-07-24 18:17:43 +0000245}
246
247namespace {
Misha Brukmana259c9b2003-07-24 21:59:10 +0000248 struct Disambiguator {
Misha Brukman91eabc12003-07-28 19:16:14 +0000249 std::set<std::string> SymbolNames;
250 std::set<GlobalValue*> Symbols;
Misha Brukman50733362003-07-24 18:17:43 +0000251 uint64_t uniqueCounter;
252 bool externalOnly;
Misha Brukmana259c9b2003-07-24 21:59:10 +0000253 public:
Misha Brukman50733362003-07-24 18:17:43 +0000254 Disambiguator() : uniqueCounter(0), externalOnly(true) {}
255 void setExternalOnly(bool value) { externalOnly = value; }
Misha Brukmana259c9b2003-07-24 21:59:10 +0000256 void add(GlobalValue &V) {
Misha Brukman91eabc12003-07-28 19:16:14 +0000257 // If we're only processing externals and this isn't external, bail
Misha Brukman50733362003-07-24 18:17:43 +0000258 if (externalOnly && !V.isExternal()) return;
Misha Brukman91eabc12003-07-28 19:16:14 +0000259 // If we're already processed this symbol, don't add it again
260 if (Symbols.count(&V) != 0) return;
Misha Brukman3b624622003-07-30 21:45:20 +0000261 // Ignore intrinsic functions
262 if (Function *F = dyn_cast<Function>(&V))
263 if (F->getIntrinsicID() != 0)
264 return;
Misha Brukman50733362003-07-24 18:17:43 +0000265
Misha Brukman91eabc12003-07-28 19:16:14 +0000266 std::string SymName = V.getName();
267
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000268 // Use the Mangler facility to make symbol names that will be valid in
269 // shared objects.
270 SymName = Mangler::makeNameProper(SymName);
271 V.setName(SymName);
Misha Brukman91eabc12003-07-28 19:16:14 +0000272
273 if (SymbolNames.count(SymName) == 0) {
274 DEBUG(std::cerr << "Disambiguator: adding " << SymName
Misha Brukman50733362003-07-24 18:17:43 +0000275 << ", no conflicts.\n");
Misha Brukman91eabc12003-07-28 19:16:14 +0000276 SymbolNames.insert(SymName);
Misha Brukman50733362003-07-24 18:17:43 +0000277 } else {
278 // Mangle name before adding
279 std::string newName;
280 do {
Misha Brukman91eabc12003-07-28 19:16:14 +0000281 newName = SymName + "_" + utostr(uniqueCounter);
Misha Brukman50733362003-07-24 18:17:43 +0000282 if (SymbolNames.count(newName) == 0) break;
283 else ++uniqueCounter;
284 } while (1);
285 //while (SymbolNames.count(V->getName()+utostr(uniqueCounter++))==0);
Misha Brukman91eabc12003-07-28 19:16:14 +0000286 DEBUG(std::cerr << "Disambiguator: conflict: " << SymName
Misha Brukman50733362003-07-24 18:17:43 +0000287 << ", adding: " << newName << "\n");
288 V.setName(newName);
289 SymbolNames.insert(newName);
Misha Brukman50733362003-07-24 18:17:43 +0000290 }
Misha Brukman91eabc12003-07-28 19:16:14 +0000291 Symbols.insert(&V);
Misha Brukman50733362003-07-24 18:17:43 +0000292 }
293 };
294}
295
Misha Brukmande9720f2003-07-29 16:02:28 +0000296void DisambiguateGlobalSymbols(Module *M) {
Misha Brukman50733362003-07-24 18:17:43 +0000297 // First, try not to cause collisions by minimizing chances of renaming an
298 // already-external symbol, so take in external globals and functions as-is.
Misha Brukmana259c9b2003-07-24 21:59:10 +0000299 Disambiguator D;
Misha Brukman91eabc12003-07-28 19:16:14 +0000300 DEBUG(std::cerr << "Disambiguating globals (external-only)\n");
Misha Brukmana259c9b2003-07-24 21:59:10 +0000301 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) D.add(*I);
Misha Brukman91eabc12003-07-28 19:16:14 +0000302 DEBUG(std::cerr << "Disambiguating functions (external-only)\n");
Misha Brukmana259c9b2003-07-24 21:59:10 +0000303 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) D.add(*I);
Misha Brukman50733362003-07-24 18:17:43 +0000304
305 // Now just rename functions and globals as necessary, keeping what's already
306 // in the set unique.
307 D.setExternalOnly(false);
Misha Brukman91eabc12003-07-28 19:16:14 +0000308 DEBUG(std::cerr << "Disambiguating globals\n");
Misha Brukmana259c9b2003-07-24 21:59:10 +0000309 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) D.add(*I);
Misha Brukman91eabc12003-07-28 19:16:14 +0000310 DEBUG(std::cerr << "Disambiguating globals\n");
Misha Brukmana259c9b2003-07-24 21:59:10 +0000311 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) D.add(*I);
Misha Brukman50733362003-07-24 18:17:43 +0000312}
313
314
315bool BugDriver::debugCodeGenerator() {
316 // See if we can pin down which functions are being miscompiled...
317 //First, build a list of all of the non-external functions in the program.
318 std::vector<Function*> MisCodegenFunctions;
319 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
320 if (!I->isExternal())
321 MisCodegenFunctions.push_back(I);
322
Misha Brukman91eabc12003-07-28 19:16:14 +0000323 // If we are executing the JIT, we *must* keep the function `main' in the
324 // module that is passed in, and not the shared library. However, we still
325 // want to be able to debug the `main' function alone. Thus, we create a new
326 // function `main' which just calls the old one.
327 if (isExecutingJIT()) {
328 // Get the `main' function
329 Function *oldMain = Program->getNamedFunction("main");
Misha Brukmande9720f2003-07-29 16:02:28 +0000330 assert(oldMain && "`main' function not found in program!");
Misha Brukman91eabc12003-07-28 19:16:14 +0000331 // Rename it
332 oldMain->setName("old_main");
333 // Create a NEW `main' function with same type
334 Function *newMain = new Function(oldMain->getFunctionType(),
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000335 GlobalValue::ExternalLinkage,
Misha Brukman91eabc12003-07-28 19:16:14 +0000336 "main", Program);
337 // Call the old main function and return its result
338 BasicBlock *BB = new BasicBlock("entry", newMain);
339 std::vector<Value*> args;
340 for (Function::aiterator I=newMain->abegin(), E=newMain->aend(); I!=E; ++I)
341 args.push_back(I);
342 CallInst *call = new CallInst(oldMain, args);
343 BB->getInstList().push_back(call);
344
345 // if the type of old function wasn't void, return value of call
346 ReturnInst *ret;
347 if (oldMain->getReturnType() != Type::VoidTy) {
348 ret = new ReturnInst(call);
349 } else {
350 ret = new ReturnInst();
351 }
352
353 // Add the return instruction to the BasicBlock
354 BB->getInstList().push_back(ret);
355 }
356
Misha Brukmande9720f2003-07-29 16:02:28 +0000357 DisambiguateGlobalSymbols(Program);
358
Misha Brukman50733362003-07-24 18:17:43 +0000359 // Do the reduction...
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000360 if (!ReduceMisCodegenFunctions(*this).reduceList(MisCodegenFunctions)) {
361 std::cerr << "*** Execution matches reference output! No problem "
362 << "detected...\nbugpoint can't help you with your problem!\n";
363 return false;
364 }
Misha Brukman50733362003-07-24 18:17:43 +0000365
366 std::cout << "\n*** The following functions are being miscompiled: ";
367 PrintFunctionList(MisCodegenFunctions);
368 std::cout << "\n";
369
370 // Output a bunch of bytecode files for the user...
Misha Brukman91eabc12003-07-28 19:16:14 +0000371 ReduceMisCodegenFunctions(*this).TestFuncs(MisCodegenFunctions, true);
Misha Brukman50733362003-07-24 18:17:43 +0000372
373 return false;
374}