2002-12-23 23:50:16 +00:00
|
|
|
//===- Miscompilation.cpp - Debug program miscompilations -----------------===//
|
2003-10-20 17:47:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-23 23:50:16 +00:00
|
|
|
//
|
2004-04-05 22:58:16 +00:00
|
|
|
// This file implements optimizer and code generation miscompilation debugging
|
|
|
|
// support.
|
2002-12-23 23:50:16 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "BugDriver.h"
|
2003-04-24 20:16:29 +00:00
|
|
|
#include "ListReducer.h"
|
2004-04-05 22:58:16 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2002-12-23 23:50:16 +00:00
|
|
|
#include "llvm/Module.h"
|
2003-08-07 21:19:30 +00:00
|
|
|
#include "llvm/Pass.h"
|
2004-04-05 22:58:16 +00:00
|
|
|
#include "llvm/Analysis/Verifier.h"
|
|
|
|
#include "llvm/Support/Mangler.h"
|
2003-04-24 17:02:17 +00:00
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
2004-06-23 17:33:09 +00:00
|
|
|
#include "llvm/Support/Linker.h"
|
2004-04-05 22:58:16 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2003-08-07 21:42:28 +00:00
|
|
|
#include "Support/FileUtilities.h"
|
2004-01-14 03:38:37 +00:00
|
|
|
using namespace llvm;
|
2002-12-23 23:50:16 +00:00
|
|
|
|
2004-04-05 22:58:16 +00:00
|
|
|
namespace llvm {
|
|
|
|
extern cl::list<std::string> InputArgv;
|
|
|
|
}
|
|
|
|
|
2004-03-14 20:50:42 +00:00
|
|
|
namespace {
|
2004-01-14 03:38:37 +00:00
|
|
|
class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> {
|
|
|
|
BugDriver &BD;
|
|
|
|
public:
|
|
|
|
ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
|
|
|
|
|
|
|
|
virtual TestResult doTest(std::vector<const PassInfo*> &Prefix,
|
|
|
|
std::vector<const PassInfo*> &Suffix);
|
|
|
|
};
|
|
|
|
}
|
2003-04-24 17:02:17 +00:00
|
|
|
|
2004-04-21 18:36:43 +00:00
|
|
|
/// TestResult - After passes have been split into a test group and a control
|
|
|
|
/// group, see if they still break the program.
|
|
|
|
///
|
2003-04-24 17:02:17 +00:00
|
|
|
ReduceMiscompilingPasses::TestResult
|
2003-04-24 22:24:22 +00:00
|
|
|
ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
|
2003-04-25 03:16:05 +00:00
|
|
|
std::vector<const PassInfo*> &Suffix) {
|
|
|
|
// First, run the program with just the Suffix passes. If it is still broken
|
2003-04-24 17:02:17 +00:00
|
|
|
// with JUST the kept passes, discard the prefix passes.
|
2003-04-25 03:16:05 +00:00
|
|
|
std::cout << "Checking to see if '" << getPassesString(Suffix)
|
2003-04-24 17:02:17 +00:00
|
|
|
<< "' compile correctly: ";
|
|
|
|
|
|
|
|
std::string BytecodeResult;
|
2003-04-25 03:16:05 +00:00
|
|
|
if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
|
2003-10-18 00:05:05 +00:00
|
|
|
std::cerr << " Error running this sequence of passes"
|
2003-04-24 17:02:17 +00:00
|
|
|
<< " on the input program!\n";
|
2003-10-17 23:07:47 +00:00
|
|
|
BD.setPassesToRun(Suffix);
|
|
|
|
BD.EmitProgressBytecode("pass-error", false);
|
2004-02-18 21:02:04 +00:00
|
|
|
exit(BD.debugOptimizerCrash());
|
2003-04-24 17:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if the finished program matches the reference output...
|
Major addition to bugpoint: ability to debug code generators (LLC and LLI).
The C backend is assumed correct and is used to generate shared objects to be
loaded by the other two code generators.
LLC debugging should be functional now, LLI needs a few more additions to work,
the major one is renaming of external functions to call the JIT lazy function
resolver.
Bugpoint now has a command-line switch -mode with options 'compile' and
'codegen' to debug appropriate portions of tools.
ExecutionDriver.cpp: Added implementations of AbstractInterpreter for LLC and
GCC, broke out common code within other tools, and added ability to generate C
code with CBE individually, without executing the program, and the GCC tool can
generate executables shared objects or executables.
If no reference output is specified to Bugpoint, it will be generated with CBE,
because it is already assumed to be correct for the purposes of debugging using
this method. As a result, many functions now accept as an optional parameter a
shared object to be loaded in, if specified.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@7293 91177308-0d34-0410-b5e6-96231b3b80d8
2003-07-24 18:17:43 +00:00
|
|
|
if (BD.diffProgram(BytecodeResult, "", true /*delete bytecode*/)) {
|
2004-04-22 20:02:09 +00:00
|
|
|
std::cout << " nope.\n";
|
|
|
|
return KeepSuffix; // Miscompilation detected!
|
2003-04-24 17:02:17 +00:00
|
|
|
}
|
2004-04-22 20:02:09 +00:00
|
|
|
std::cout << " yup.\n"; // No miscompilation!
|
2003-04-24 17:02:17 +00:00
|
|
|
|
|
|
|
if (Prefix.empty()) return NoFailure;
|
|
|
|
|
2003-04-25 03:16:05 +00:00
|
|
|
// Next, see if the program is broken if we run the "prefix" passes first,
|
2003-07-14 17:20:40 +00:00
|
|
|
// then separately run the "kept" passes.
|
2003-04-24 17:02:17 +00:00
|
|
|
std::cout << "Checking to see if '" << getPassesString(Prefix)
|
|
|
|
<< "' compile correctly: ";
|
|
|
|
|
|
|
|
// If it is not broken with the kept passes, it's possible that the prefix
|
|
|
|
// passes must be run before the kept passes to break it. If the program
|
|
|
|
// WORKS after the prefix passes, but then fails if running the prefix AND
|
|
|
|
// kept passes, we can update our bytecode file to include the result of the
|
|
|
|
// prefix passes, then discard the prefix passes.
|
|
|
|
//
|
|
|
|
if (BD.runPasses(Prefix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
|
2003-10-18 00:05:05 +00:00
|
|
|
std::cerr << " Error running this sequence of passes"
|
2003-04-24 17:02:17 +00:00
|
|
|
<< " on the input program!\n";
|
2003-10-17 23:03:16 +00:00
|
|
|
BD.setPassesToRun(Prefix);
|
|
|
|
BD.EmitProgressBytecode("pass-error", false);
|
2004-02-18 21:02:04 +00:00
|
|
|
exit(BD.debugOptimizerCrash());
|
2003-04-24 17:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the prefix maintains the predicate by itself, only keep the prefix!
|
Major addition to bugpoint: ability to debug code generators (LLC and LLI).
The C backend is assumed correct and is used to generate shared objects to be
loaded by the other two code generators.
LLC debugging should be functional now, LLI needs a few more additions to work,
the major one is renaming of external functions to call the JIT lazy function
resolver.
Bugpoint now has a command-line switch -mode with options 'compile' and
'codegen' to debug appropriate portions of tools.
ExecutionDriver.cpp: Added implementations of AbstractInterpreter for LLC and
GCC, broke out common code within other tools, and added ability to generate C
code with CBE individually, without executing the program, and the GCC tool can
generate executables shared objects or executables.
If no reference output is specified to Bugpoint, it will be generated with CBE,
because it is already assumed to be correct for the purposes of debugging using
this method. As a result, many functions now accept as an optional parameter a
shared object to be loaded in, if specified.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@7293 91177308-0d34-0410-b5e6-96231b3b80d8
2003-07-24 18:17:43 +00:00
|
|
|
if (BD.diffProgram(BytecodeResult)) {
|
2004-04-22 20:02:09 +00:00
|
|
|
std::cout << " nope.\n";
|
2003-04-24 17:02:17 +00:00
|
|
|
removeFile(BytecodeResult);
|
|
|
|
return KeepPrefix;
|
|
|
|
}
|
2004-04-22 20:02:09 +00:00
|
|
|
std::cout << " yup.\n"; // No miscompilation!
|
2003-04-24 17:02:17 +00:00
|
|
|
|
|
|
|
// Ok, so now we know that the prefix passes work, try running the suffix
|
|
|
|
// passes on the result of the prefix passes.
|
|
|
|
//
|
2004-03-14 20:50:42 +00:00
|
|
|
Module *PrefixOutput = ParseInputFile(BytecodeResult);
|
2003-04-24 17:02:17 +00:00
|
|
|
if (PrefixOutput == 0) {
|
|
|
|
std::cerr << BD.getToolName() << ": Error reading bytecode file '"
|
|
|
|
<< BytecodeResult << "'!\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
removeFile(BytecodeResult); // No longer need the file on disk
|
2004-04-23 20:36:51 +00:00
|
|
|
|
|
|
|
// Don't check if there are no passes in the suffix.
|
|
|
|
if (Suffix.empty())
|
|
|
|
return NoFailure;
|
|
|
|
|
2003-04-25 03:16:05 +00:00
|
|
|
std::cout << "Checking to see if '" << getPassesString(Suffix)
|
2003-04-24 17:02:17 +00:00
|
|
|
<< "' passes compile correctly after the '"
|
|
|
|
<< getPassesString(Prefix) << "' passes: ";
|
|
|
|
|
2004-03-14 20:50:42 +00:00
|
|
|
Module *OriginalInput = BD.swapProgramIn(PrefixOutput);
|
2003-04-25 03:16:05 +00:00
|
|
|
if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
|
2003-10-18 00:05:05 +00:00
|
|
|
std::cerr << " Error running this sequence of passes"
|
2003-04-24 17:02:17 +00:00
|
|
|
<< " on the input program!\n";
|
2003-10-17 23:07:47 +00:00
|
|
|
BD.setPassesToRun(Suffix);
|
2003-10-17 23:03:16 +00:00
|
|
|
BD.EmitProgressBytecode("pass-error", false);
|
2004-02-18 21:02:04 +00:00
|
|
|
exit(BD.debugOptimizerCrash());
|
2003-04-24 17:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the result...
|
Major addition to bugpoint: ability to debug code generators (LLC and LLI).
The C backend is assumed correct and is used to generate shared objects to be
loaded by the other two code generators.
LLC debugging should be functional now, LLI needs a few more additions to work,
the major one is renaming of external functions to call the JIT lazy function
resolver.
Bugpoint now has a command-line switch -mode with options 'compile' and
'codegen' to debug appropriate portions of tools.
ExecutionDriver.cpp: Added implementations of AbstractInterpreter for LLC and
GCC, broke out common code within other tools, and added ability to generate C
code with CBE individually, without executing the program, and the GCC tool can
generate executables shared objects or executables.
If no reference output is specified to Bugpoint, it will be generated with CBE,
because it is already assumed to be correct for the purposes of debugging using
this method. As a result, many functions now accept as an optional parameter a
shared object to be loaded in, if specified.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@7293 91177308-0d34-0410-b5e6-96231b3b80d8
2003-07-24 18:17:43 +00:00
|
|
|
if (BD.diffProgram(BytecodeResult, "", true/*delete bytecode*/)) {
|
2004-04-22 20:02:09 +00:00
|
|
|
std::cout << " nope.\n";
|
2003-04-24 17:02:17 +00:00
|
|
|
delete OriginalInput; // We pruned down the original input...
|
2003-04-25 03:16:05 +00:00
|
|
|
return KeepSuffix;
|
2003-04-24 17:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we must not be running the bad pass anymore.
|
2004-04-22 20:02:09 +00:00
|
|
|
std::cout << " yup.\n"; // No miscompilation!
|
2004-03-14 20:50:42 +00:00
|
|
|
delete BD.swapProgramIn(OriginalInput); // Restore orig program & free test
|
2003-04-24 17:02:17 +00:00
|
|
|
return NoFailure;
|
|
|
|
}
|
|
|
|
|
2004-03-14 20:50:42 +00:00
|
|
|
namespace {
|
2004-01-14 03:38:37 +00:00
|
|
|
class ReduceMiscompilingFunctions : public ListReducer<Function*> {
|
|
|
|
BugDriver &BD;
|
2004-04-05 21:37:38 +00:00
|
|
|
bool (*TestFn)(BugDriver &, Module *, Module *);
|
2004-01-14 03:38:37 +00:00
|
|
|
public:
|
2004-04-05 21:37:38 +00:00
|
|
|
ReduceMiscompilingFunctions(BugDriver &bd,
|
|
|
|
bool (*F)(BugDriver &, Module *, Module *))
|
|
|
|
: BD(bd), TestFn(F) {}
|
2004-01-14 03:38:37 +00:00
|
|
|
|
|
|
|
virtual TestResult doTest(std::vector<Function*> &Prefix,
|
|
|
|
std::vector<Function*> &Suffix) {
|
2004-03-14 19:27:19 +00:00
|
|
|
if (!Suffix.empty() && TestFuncs(Suffix))
|
2004-01-14 03:38:37 +00:00
|
|
|
return KeepSuffix;
|
2004-03-14 19:27:19 +00:00
|
|
|
if (!Prefix.empty() && TestFuncs(Prefix))
|
2004-01-14 03:38:37 +00:00
|
|
|
return KeepPrefix;
|
|
|
|
return NoFailure;
|
|
|
|
}
|
|
|
|
|
2004-03-14 19:27:19 +00:00
|
|
|
bool TestFuncs(const std::vector<Function*> &Prefix);
|
2004-01-14 03:38:37 +00:00
|
|
|
};
|
|
|
|
}
|
2003-04-24 17:02:17 +00:00
|
|
|
|
2004-03-14 20:50:42 +00:00
|
|
|
/// TestMergedProgram - Given two modules, link them together and run the
|
|
|
|
/// program, checking to see if the program matches the diff. If the diff
|
2004-03-14 22:08:00 +00:00
|
|
|
/// matches, return false, otherwise return true. If the DeleteInputs argument
|
|
|
|
/// is set to true then this function deletes both input modules before it
|
|
|
|
/// returns.
|
2004-04-21 18:36:43 +00:00
|
|
|
///
|
2004-03-14 22:08:00 +00:00
|
|
|
static bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2,
|
|
|
|
bool DeleteInputs) {
|
2004-03-14 20:50:42 +00:00
|
|
|
// Link the two portions of the program back to together.
|
|
|
|
std::string ErrorMsg;
|
2004-03-14 22:08:00 +00:00
|
|
|
if (!DeleteInputs) M1 = CloneModule(M1);
|
2004-03-14 20:50:42 +00:00
|
|
|
if (LinkModules(M1, M2, &ErrorMsg)) {
|
|
|
|
std::cerr << BD.getToolName() << ": Error linking modules together:"
|
2004-07-23 01:30:49 +00:00
|
|
|
<< ErrorMsg << '\n';
|
2004-03-14 20:50:42 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2004-03-14 22:08:00 +00:00
|
|
|
if (DeleteInputs) delete M2; // We are done with this module...
|
2004-03-14 20:50:42 +00:00
|
|
|
|
|
|
|
Module *OldProgram = BD.swapProgramIn(M1);
|
|
|
|
|
|
|
|
// Execute the program. If it does not match the expected output, we must
|
|
|
|
// return true.
|
|
|
|
bool Broken = BD.diffProgram();
|
|
|
|
|
|
|
|
// Delete the linked module & restore the original
|
2004-03-14 22:08:00 +00:00
|
|
|
BD.swapProgramIn(OldProgram);
|
2004-04-02 06:32:17 +00:00
|
|
|
delete M1;
|
2004-03-14 20:50:42 +00:00
|
|
|
return Broken;
|
|
|
|
}
|
|
|
|
|
2004-04-21 18:36:43 +00:00
|
|
|
/// TestFuncs - split functions in a Module into two groups: those that are
|
|
|
|
/// under consideration for miscompilation vs. those that are not, and test
|
|
|
|
/// accordingly. Each group of functions becomes a separate Module.
|
|
|
|
///
|
2004-03-14 19:27:19 +00:00
|
|
|
bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*>&Funcs){
|
2003-04-24 17:02:17 +00:00
|
|
|
// Test to see if the function is misoptimized if we ONLY run it on the
|
|
|
|
// functions listed in Funcs.
|
2004-03-14 19:27:19 +00:00
|
|
|
std::cout << "Checking to see if the program is misoptimized when "
|
|
|
|
<< (Funcs.size()==1 ? "this function is" : "these functions are")
|
|
|
|
<< " run through the pass"
|
2004-03-14 22:08:00 +00:00
|
|
|
<< (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";
|
2004-03-14 20:50:42 +00:00
|
|
|
PrintFunctionList(Funcs);
|
2004-07-23 01:30:49 +00:00
|
|
|
std::cout << '\n';
|
2003-04-24 17:02:17 +00:00
|
|
|
|
2004-03-14 19:27:19 +00:00
|
|
|
// Split the module into the two halves of the program we want.
|
2004-03-14 20:50:42 +00:00
|
|
|
Module *ToNotOptimize = CloneModule(BD.getProgram());
|
|
|
|
Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, Funcs);
|
2003-04-24 17:02:17 +00:00
|
|
|
|
2004-04-05 21:37:38 +00:00
|
|
|
// Run the predicate, not that the predicate will delete both input modules.
|
|
|
|
return TestFn(BD, ToOptimize, ToNotOptimize);
|
2003-04-24 17:02:17 +00:00
|
|
|
}
|
|
|
|
|
2004-04-21 18:36:43 +00:00
|
|
|
/// DisambiguateGlobalSymbols - Mangle symbols to guarantee uniqueness by
|
|
|
|
/// modifying predominantly internal symbols rather than external ones.
|
|
|
|
///
|
2004-04-11 23:52:35 +00:00
|
|
|
static void DisambiguateGlobalSymbols(Module *M) {
|
|
|
|
// Try not to cause collisions by minimizing chances of renaming an
|
|
|
|
// already-external symbol, so take in external globals and functions as-is.
|
|
|
|
// The code should work correctly without disambiguation (assuming the same
|
|
|
|
// mangler is used by the two code generators), but having symbols with the
|
|
|
|
// same name causes warnings to be emitted by the code generator.
|
|
|
|
Mangler Mang(*M);
|
|
|
|
for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
|
|
|
|
I->setName(Mang.getValueName(I));
|
|
|
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
I->setName(Mang.getValueName(I));
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:08:00 +00:00
|
|
|
/// ExtractLoops - Given a reduced list of functions that still exposed the bug,
|
|
|
|
/// check to see if we can extract the loops in the region without obscuring the
|
|
|
|
/// bug. If so, it reduces the amount of code identified.
|
2004-04-21 18:36:43 +00:00
|
|
|
///
|
2004-04-05 21:37:38 +00:00
|
|
|
static bool ExtractLoops(BugDriver &BD,
|
|
|
|
bool (*TestFn)(BugDriver &, Module *, Module *),
|
2004-03-14 22:08:00 +00:00
|
|
|
std::vector<Function*> &MiscompiledFunctions) {
|
|
|
|
bool MadeChange = false;
|
|
|
|
while (1) {
|
|
|
|
Module *ToNotOptimize = CloneModule(BD.getProgram());
|
|
|
|
Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
|
|
|
|
MiscompiledFunctions);
|
|
|
|
Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize);
|
|
|
|
if (!ToOptimizeLoopExtracted) {
|
|
|
|
// If the loop extractor crashed or if there were no extractible loops,
|
|
|
|
// then this chapter of our odyssey is over with.
|
|
|
|
delete ToNotOptimize;
|
|
|
|
delete ToOptimize;
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "Extracted a loop from the breaking portion of the program.\n";
|
|
|
|
delete ToOptimize;
|
|
|
|
|
|
|
|
// Bugpoint is intentionally not very trusting of LLVM transformations. In
|
|
|
|
// particular, we're not going to assume that the loop extractor works, so
|
|
|
|
// we're going to test the newly loop extracted program to make sure nothing
|
|
|
|
// has broken. If something broke, then we'll inform the user and stop
|
|
|
|
// extraction.
|
2004-04-05 22:58:16 +00:00
|
|
|
AbstractInterpreter *AI = BD.switchToCBE();
|
2004-03-14 22:08:00 +00:00
|
|
|
if (TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize, false)) {
|
2004-04-05 22:58:16 +00:00
|
|
|
BD.switchToInterpreter(AI);
|
|
|
|
|
2004-03-14 22:08:00 +00:00
|
|
|
// Merged program doesn't work anymore!
|
|
|
|
std::cerr << " *** ERROR: Loop extraction broke the program. :("
|
|
|
|
<< " Please report a bug!\n";
|
|
|
|
std::cerr << " Continuing on with un-loop-extracted version.\n";
|
|
|
|
delete ToNotOptimize;
|
|
|
|
delete ToOptimizeLoopExtracted;
|
|
|
|
return MadeChange;
|
|
|
|
}
|
2004-04-05 22:58:16 +00:00
|
|
|
BD.switchToInterpreter(AI);
|
2004-03-14 22:08:00 +00:00
|
|
|
|
2004-04-05 21:37:38 +00:00
|
|
|
std::cout << " Testing after loop extraction:\n";
|
|
|
|
// Clone modules, the tester function will free them.
|
|
|
|
Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted);
|
|
|
|
Module *TNOBackup = CloneModule(ToNotOptimize);
|
|
|
|
if (!TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize)) {
|
|
|
|
std::cout << "*** Loop extraction masked the problem. Undoing.\n";
|
2004-03-14 22:08:00 +00:00
|
|
|
// If the program is not still broken, then loop extraction did something
|
|
|
|
// that masked the error. Stop loop extraction now.
|
2004-04-05 21:37:38 +00:00
|
|
|
delete TOLEBackup;
|
|
|
|
delete TNOBackup;
|
2004-03-14 22:08:00 +00:00
|
|
|
return MadeChange;
|
|
|
|
}
|
2004-04-05 21:37:38 +00:00
|
|
|
ToOptimizeLoopExtracted = TOLEBackup;
|
|
|
|
ToNotOptimize = TNOBackup;
|
|
|
|
|
|
|
|
std::cout << "*** Loop extraction successful!\n";
|
2004-03-14 22:08:00 +00:00
|
|
|
|
|
|
|
// Okay, great! Now we know that we extracted a loop and that loop
|
|
|
|
// extraction both didn't break the program, and didn't mask the problem.
|
|
|
|
// Replace the current program with the loop extracted version, and try to
|
|
|
|
// extract another loop.
|
|
|
|
std::string ErrorMsg;
|
|
|
|
if (LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)) {
|
|
|
|
std::cerr << BD.getToolName() << ": Error linking modules together:"
|
2004-07-23 01:30:49 +00:00
|
|
|
<< ErrorMsg << '\n';
|
2004-03-14 22:08:00 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2004-03-17 17:42:09 +00:00
|
|
|
|
|
|
|
// All of the Function*'s in the MiscompiledFunctions list are in the old
|
2004-04-02 06:32:17 +00:00
|
|
|
// module. Update this list to include all of the functions in the
|
|
|
|
// optimized and loop extracted module.
|
|
|
|
MiscompiledFunctions.clear();
|
|
|
|
for (Module::iterator I = ToOptimizeLoopExtracted->begin(),
|
|
|
|
E = ToOptimizeLoopExtracted->end(); I != E; ++I) {
|
|
|
|
if (!I->isExternal()) {
|
2004-04-02 06:32:45 +00:00
|
|
|
Function *NewF = ToNotOptimize->getFunction(I->getName(),
|
|
|
|
I->getFunctionType());
|
2004-04-02 06:32:17 +00:00
|
|
|
assert(NewF && "Function not found??");
|
|
|
|
MiscompiledFunctions.push_back(NewF);
|
|
|
|
}
|
2004-03-17 17:42:09 +00:00
|
|
|
}
|
2004-04-02 06:32:17 +00:00
|
|
|
delete ToOptimizeLoopExtracted;
|
2004-03-17 17:42:09 +00:00
|
|
|
|
2004-03-14 22:08:00 +00:00
|
|
|
BD.setNewProgram(ToNotOptimize);
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-11 21:54:13 +00:00
|
|
|
namespace {
|
|
|
|
class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> {
|
|
|
|
BugDriver &BD;
|
|
|
|
bool (*TestFn)(BugDriver &, Module *, Module *);
|
|
|
|
std::vector<Function*> FunctionsBeingTested;
|
|
|
|
public:
|
|
|
|
ReduceMiscompiledBlocks(BugDriver &bd,
|
|
|
|
bool (*F)(BugDriver &, Module *, Module *),
|
|
|
|
const std::vector<Function*> &Fns)
|
|
|
|
: BD(bd), TestFn(F), FunctionsBeingTested(Fns) {}
|
|
|
|
|
|
|
|
virtual TestResult doTest(std::vector<BasicBlock*> &Prefix,
|
|
|
|
std::vector<BasicBlock*> &Suffix) {
|
|
|
|
if (!Suffix.empty() && TestFuncs(Suffix))
|
|
|
|
return KeepSuffix;
|
|
|
|
if (TestFuncs(Prefix))
|
|
|
|
return KeepPrefix;
|
|
|
|
return NoFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestFuncs(const std::vector<BasicBlock*> &Prefix);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// TestFuncs - Extract all blocks for the miscompiled functions except for the
|
|
|
|
/// specified blocks. If the problem still exists, return true.
|
|
|
|
///
|
|
|
|
bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs) {
|
|
|
|
// Test to see if the function is misoptimized if we ONLY run it on the
|
|
|
|
// functions listed in Funcs.
|
2004-05-12 16:08:01 +00:00
|
|
|
std::cout << "Checking to see if the program is misoptimized when all ";
|
|
|
|
if (!BBs.empty()) {
|
|
|
|
std::cout << "but these " << BBs.size() << " blocks are extracted: ";
|
|
|
|
for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i)
|
|
|
|
std::cout << BBs[i]->getName() << " ";
|
|
|
|
if (BBs.size() > 10) std::cout << "...";
|
|
|
|
} else {
|
|
|
|
std::cout << "blocks are extracted.";
|
|
|
|
}
|
2004-07-23 01:30:49 +00:00
|
|
|
std::cout << '\n';
|
2004-05-11 21:54:13 +00:00
|
|
|
|
|
|
|
// Split the module into the two halves of the program we want.
|
|
|
|
Module *ToNotOptimize = CloneModule(BD.getProgram());
|
|
|
|
Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
|
|
|
|
FunctionsBeingTested);
|
|
|
|
|
|
|
|
// Try the extraction. If it doesn't work, then the block extractor crashed
|
|
|
|
// or something, in which case bugpoint can't chase down this possibility.
|
|
|
|
if (Module *New = BD.ExtractMappedBlocksFromModule(BBs, ToOptimize)) {
|
|
|
|
delete ToOptimize;
|
|
|
|
// Run the predicate, not that the predicate will delete both input modules.
|
|
|
|
return TestFn(BD, New, ToNotOptimize);
|
|
|
|
}
|
|
|
|
delete ToOptimize;
|
|
|
|
delete ToNotOptimize;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ExtractBlocks - Given a reduced list of functions that still expose the bug,
|
|
|
|
/// extract as many basic blocks from the region as possible without obscuring
|
|
|
|
/// the bug.
|
|
|
|
///
|
|
|
|
static bool ExtractBlocks(BugDriver &BD,
|
|
|
|
bool (*TestFn)(BugDriver &, Module *, Module *),
|
|
|
|
std::vector<Function*> &MiscompiledFunctions) {
|
|
|
|
std::vector<BasicBlock*> Blocks;
|
|
|
|
for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
|
|
|
|
for (Function::iterator I = MiscompiledFunctions[i]->begin(),
|
|
|
|
E = MiscompiledFunctions[i]->end(); I != E; ++I)
|
|
|
|
Blocks.push_back(I);
|
|
|
|
|
|
|
|
// Use the list reducer to identify blocks that can be extracted without
|
|
|
|
// obscuring the bug. The Blocks list will end up containing blocks that must
|
|
|
|
// be retained from the original program.
|
|
|
|
unsigned OldSize = Blocks.size();
|
2004-05-12 16:08:01 +00:00
|
|
|
|
|
|
|
// Check to see if all blocks are extractible first.
|
|
|
|
if (ReduceMiscompiledBlocks(BD, TestFn,
|
|
|
|
MiscompiledFunctions).TestFuncs(std::vector<BasicBlock*>())) {
|
|
|
|
Blocks.clear();
|
|
|
|
} else {
|
|
|
|
ReduceMiscompiledBlocks(BD, TestFn,MiscompiledFunctions).reduceList(Blocks);
|
|
|
|
if (Blocks.size() == OldSize)
|
|
|
|
return false;
|
|
|
|
}
|
2004-05-11 21:54:13 +00:00
|
|
|
|
2004-05-12 02:43:24 +00:00
|
|
|
Module *ProgClone = CloneModule(BD.getProgram());
|
|
|
|
Module *ToExtract = SplitFunctionsOutOfModule(ProgClone,
|
|
|
|
MiscompiledFunctions);
|
|
|
|
Module *Extracted = BD.ExtractMappedBlocksFromModule(Blocks, ToExtract);
|
|
|
|
if (Extracted == 0) {
|
|
|
|
// Wierd, extraction should have worked.
|
|
|
|
std::cerr << "Nondeterministic problem extracting blocks??\n";
|
|
|
|
delete ProgClone;
|
|
|
|
delete ToExtract;
|
|
|
|
return false;
|
|
|
|
}
|
2004-05-11 21:54:13 +00:00
|
|
|
|
2004-05-12 02:43:24 +00:00
|
|
|
// Otherwise, block extraction succeeded. Link the two program fragments back
|
|
|
|
// together.
|
|
|
|
delete ToExtract;
|
2004-05-11 21:54:13 +00:00
|
|
|
|
2004-05-12 02:43:24 +00:00
|
|
|
std::string ErrorMsg;
|
|
|
|
if (LinkModules(ProgClone, Extracted, &ErrorMsg)) {
|
|
|
|
std::cerr << BD.getToolName() << ": Error linking modules together:"
|
2004-07-23 01:30:49 +00:00
|
|
|
<< ErrorMsg << '\n';
|
2004-05-12 02:43:24 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2004-05-11 21:54:13 +00:00
|
|
|
|
2004-05-12 02:43:24 +00:00
|
|
|
// Set the new program and delete the old one.
|
|
|
|
BD.setNewProgram(ProgClone);
|
2004-05-11 21:54:13 +00:00
|
|
|
|
2004-05-12 02:43:24 +00:00
|
|
|
// Update the list of miscompiled functions.
|
|
|
|
MiscompiledFunctions.clear();
|
2004-05-11 21:54:13 +00:00
|
|
|
|
2004-05-12 02:43:24 +00:00
|
|
|
for (Module::iterator I = Extracted->begin(), E = Extracted->end(); I != E;
|
|
|
|
++I)
|
|
|
|
if (!I->isExternal()) {
|
|
|
|
Function *NF = ProgClone->getFunction(I->getName(), I->getFunctionType());
|
|
|
|
assert(NF && "Mapped function not found!");
|
|
|
|
MiscompiledFunctions.push_back(NF);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete Extracted;
|
|
|
|
|
|
|
|
return true;
|
2004-05-11 21:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-05 21:37:38 +00:00
|
|
|
/// DebugAMiscompilation - This is a generic driver to narrow down
|
|
|
|
/// miscompilations, either in an optimization or a code generator.
|
2004-04-21 18:36:43 +00:00
|
|
|
///
|
2004-04-05 21:37:38 +00:00
|
|
|
static std::vector<Function*>
|
|
|
|
DebugAMiscompilation(BugDriver &BD,
|
|
|
|
bool (*TestFn)(BugDriver &, Module *, Module *)) {
|
2003-04-24 17:02:17 +00:00
|
|
|
// Okay, now that we have reduced the list of passes which are causing the
|
|
|
|
// failure, see if we can pin down which functions are being
|
|
|
|
// miscompiled... first build a list of all of the non-external functions in
|
|
|
|
// the program.
|
|
|
|
std::vector<Function*> MiscompiledFunctions;
|
2004-04-05 21:37:38 +00:00
|
|
|
Module *Prog = BD.getProgram();
|
|
|
|
for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I)
|
2003-04-24 17:02:17 +00:00
|
|
|
if (!I->isExternal())
|
|
|
|
MiscompiledFunctions.push_back(I);
|
2002-12-23 23:50:16 +00:00
|
|
|
|
2003-04-24 17:02:17 +00:00
|
|
|
// Do the reduction...
|
2004-04-05 21:37:38 +00:00
|
|
|
ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
|
2003-04-24 17:02:17 +00:00
|
|
|
|
2003-12-07 02:43:09 +00:00
|
|
|
std::cout << "\n*** The following function"
|
|
|
|
<< (MiscompiledFunctions.size() == 1 ? " is" : "s are")
|
|
|
|
<< " being miscompiled: ";
|
2003-04-24 17:02:17 +00:00
|
|
|
PrintFunctionList(MiscompiledFunctions);
|
2004-07-23 01:30:49 +00:00
|
|
|
std::cout << '\n';
|
2002-12-23 23:50:16 +00:00
|
|
|
|
2004-03-14 22:08:00 +00:00
|
|
|
// See if we can rip any loops out of the miscompiled functions and still
|
|
|
|
// trigger the problem.
|
2004-04-05 21:37:38 +00:00
|
|
|
if (ExtractLoops(BD, TestFn, MiscompiledFunctions)) {
|
2004-03-14 22:08:00 +00:00
|
|
|
// Okay, we extracted some loops and the problem still appears. See if we
|
|
|
|
// can eliminate some of the created functions from being candidates.
|
|
|
|
|
2004-04-11 23:52:35 +00:00
|
|
|
// Loop extraction can introduce functions with the same name (foo_code).
|
|
|
|
// Make sure to disambiguate the symbols so that when the program is split
|
2004-05-11 21:54:13 +00:00
|
|
|
// apart that we can link it back together again.
|
|
|
|
DisambiguateGlobalSymbols(BD.getProgram());
|
|
|
|
|
|
|
|
// Do the reduction...
|
|
|
|
ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
|
|
|
|
|
|
|
|
std::cout << "\n*** The following function"
|
|
|
|
<< (MiscompiledFunctions.size() == 1 ? " is" : "s are")
|
|
|
|
<< " being miscompiled: ";
|
|
|
|
PrintFunctionList(MiscompiledFunctions);
|
2004-07-23 01:30:49 +00:00
|
|
|
std::cout << '\n';
|
2004-05-11 21:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ExtractBlocks(BD, TestFn, MiscompiledFunctions)) {
|
|
|
|
// Okay, we extracted some blocks and the problem still appears. See if we
|
|
|
|
// can eliminate some of the created functions from being candidates.
|
|
|
|
|
|
|
|
// Block extraction can introduce functions with the same name (foo_code).
|
|
|
|
// Make sure to disambiguate the symbols so that when the program is split
|
2004-04-11 23:52:35 +00:00
|
|
|
// apart that we can link it back together again.
|
|
|
|
DisambiguateGlobalSymbols(BD.getProgram());
|
|
|
|
|
2004-03-14 22:08:00 +00:00
|
|
|
// Do the reduction...
|
2004-04-05 21:37:38 +00:00
|
|
|
ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
|
2004-03-14 22:08:00 +00:00
|
|
|
|
|
|
|
std::cout << "\n*** The following function"
|
|
|
|
<< (MiscompiledFunctions.size() == 1 ? " is" : "s are")
|
|
|
|
<< " being miscompiled: ";
|
|
|
|
PrintFunctionList(MiscompiledFunctions);
|
2004-07-23 01:30:49 +00:00
|
|
|
std::cout << '\n';
|
2004-03-14 22:08:00 +00:00
|
|
|
}
|
|
|
|
|
2004-04-05 21:37:38 +00:00
|
|
|
return MiscompiledFunctions;
|
|
|
|
}
|
|
|
|
|
2004-04-05 22:58:16 +00:00
|
|
|
/// TestOptimizer - This is the predicate function used to check to see if the
|
|
|
|
/// "Test" portion of the program is misoptimized. If so, return true. In any
|
|
|
|
/// case, both module arguments are deleted.
|
2004-04-21 18:36:43 +00:00
|
|
|
///
|
2004-04-05 21:37:38 +00:00
|
|
|
static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe) {
|
|
|
|
// Run the optimization passes on ToOptimize, producing a transformed version
|
|
|
|
// of the functions being tested.
|
|
|
|
std::cout << " Optimizing functions being tested: ";
|
|
|
|
Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(),
|
|
|
|
/*AutoDebugCrashes*/true);
|
|
|
|
std::cout << "done.\n";
|
|
|
|
delete Test;
|
|
|
|
|
|
|
|
std::cout << " Checking to see if the merged program executes correctly: ";
|
2004-04-09 22:28:33 +00:00
|
|
|
bool Broken = TestMergedProgram(BD, Optimized, Safe, true);
|
2004-04-05 21:37:38 +00:00
|
|
|
std::cout << (Broken ? " nope.\n" : " yup.\n");
|
|
|
|
return Broken;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// debugMiscompilation - This method is used when the passes selected are not
|
|
|
|
/// crashing, but the generated output is semantically different from the
|
|
|
|
/// input.
|
|
|
|
///
|
|
|
|
bool BugDriver::debugMiscompilation() {
|
|
|
|
// Make sure something was miscompiled...
|
|
|
|
if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) {
|
|
|
|
std::cerr << "*** Optimized program matches reference output! No problem "
|
|
|
|
<< "detected...\nbugpoint can't help you with your problem!\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "\n*** Found miscompiling pass"
|
|
|
|
<< (getPassesToRun().size() == 1 ? "" : "es") << ": "
|
2004-07-23 01:30:49 +00:00
|
|
|
<< getPassesString(getPassesToRun()) << '\n';
|
2004-04-05 21:37:38 +00:00
|
|
|
EmitProgressBytecode("passinput");
|
|
|
|
|
|
|
|
std::vector<Function*> MiscompiledFunctions =
|
|
|
|
DebugAMiscompilation(*this, TestOptimizer);
|
|
|
|
|
2003-04-24 17:02:17 +00:00
|
|
|
// Output a bunch of bytecode files for the user...
|
2004-03-14 19:27:19 +00:00
|
|
|
std::cout << "Outputting reduced bytecode files which expose the problem:\n";
|
2004-03-14 20:50:42 +00:00
|
|
|
Module *ToNotOptimize = CloneModule(getProgram());
|
|
|
|
Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
|
|
|
|
MiscompiledFunctions);
|
2004-03-14 19:27:19 +00:00
|
|
|
|
|
|
|
std::cout << " Non-optimized portion: ";
|
2004-04-05 21:37:38 +00:00
|
|
|
ToNotOptimize = swapProgramIn(ToNotOptimize);
|
2004-03-14 19:27:19 +00:00
|
|
|
EmitProgressBytecode("tonotoptimize", true);
|
|
|
|
setNewProgram(ToNotOptimize); // Delete hacked module.
|
|
|
|
|
|
|
|
std::cout << " Portion that is input to optimizer: ";
|
2004-04-05 21:37:38 +00:00
|
|
|
ToOptimize = swapProgramIn(ToOptimize);
|
2004-03-14 19:27:19 +00:00
|
|
|
EmitProgressBytecode("tooptimize");
|
|
|
|
setNewProgram(ToOptimize); // Delete hacked module.
|
2002-12-23 23:50:16 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-04-05 22:58:16 +00:00
|
|
|
/// CleanupAndPrepareModules - Get the specified modules ready for code
|
|
|
|
/// generator testing.
|
2004-04-21 18:36:43 +00:00
|
|
|
///
|
2004-04-05 22:58:16 +00:00
|
|
|
static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
|
|
|
|
Module *Safe) {
|
|
|
|
// Clean up the modules, removing extra cruft that we don't need anymore...
|
|
|
|
Test = BD.performFinalCleanups(Test);
|
|
|
|
|
|
|
|
// If we are executing the JIT, we have several nasty issues to take care of.
|
|
|
|
if (!BD.isExecutingJIT()) return;
|
|
|
|
|
|
|
|
// First, if the main function is in the Safe module, we must add a stub to
|
|
|
|
// the Test module to call into it. Thus, we create a new function `main'
|
|
|
|
// which just calls the old one.
|
|
|
|
if (Function *oldMain = Safe->getNamedFunction("main"))
|
|
|
|
if (!oldMain->isExternal()) {
|
|
|
|
// Rename it
|
|
|
|
oldMain->setName("llvm_bugpoint_old_main");
|
|
|
|
// Create a NEW `main' function with same type in the test module.
|
|
|
|
Function *newMain = new Function(oldMain->getFunctionType(),
|
|
|
|
GlobalValue::ExternalLinkage,
|
|
|
|
"main", Test);
|
|
|
|
// Create an `oldmain' prototype in the test module, which will
|
|
|
|
// corresponds to the real main function in the same module.
|
|
|
|
Function *oldMainProto = new Function(oldMain->getFunctionType(),
|
|
|
|
GlobalValue::ExternalLinkage,
|
|
|
|
oldMain->getName(), Test);
|
|
|
|
// Set up and remember the argument list for the main function.
|
|
|
|
std::vector<Value*> args;
|
|
|
|
for (Function::aiterator I = newMain->abegin(), E = newMain->aend(),
|
|
|
|
OI = oldMain->abegin(); I != E; ++I, ++OI) {
|
|
|
|
I->setName(OI->getName()); // Copy argument names from oldMain
|
|
|
|
args.push_back(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the old main function and return its result
|
|
|
|
BasicBlock *BB = new BasicBlock("entry", newMain);
|
|
|
|
CallInst *call = new CallInst(oldMainProto, args);
|
|
|
|
BB->getInstList().push_back(call);
|
|
|
|
|
|
|
|
// If the type of old function wasn't void, return value of call
|
|
|
|
new ReturnInst(oldMain->getReturnType() != Type::VoidTy ? call : 0, BB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The second nasty issue we must deal with in the JIT is that the Safe
|
|
|
|
// module cannot directly reference any functions defined in the test
|
|
|
|
// module. Instead, we use a JIT API call to dynamically resolve the
|
|
|
|
// symbol.
|
|
|
|
|
|
|
|
// Add the resolver to the Safe module.
|
|
|
|
// Prototype: void *getPointerToNamedFunction(const char* Name)
|
|
|
|
Function *resolverFunc =
|
|
|
|
Safe->getOrInsertFunction("getPointerToNamedFunction",
|
|
|
|
PointerType::get(Type::SByteTy),
|
|
|
|
PointerType::get(Type::SByteTy), 0);
|
|
|
|
|
|
|
|
// Use the function we just added to get addresses of functions we need.
|
2004-04-19 01:12:01 +00:00
|
|
|
for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
|
2004-04-05 22:58:16 +00:00
|
|
|
if (F->isExternal() && !F->use_empty() && &*F != resolverFunc &&
|
|
|
|
F->getIntrinsicID() == 0 /* ignore intrinsics */) {
|
2004-04-19 01:12:01 +00:00
|
|
|
Function *TestFn = Test->getFunction(F->getName(), F->getFunctionType());
|
2004-04-05 22:58:16 +00:00
|
|
|
|
|
|
|
// Don't forward functions which are external in the test module too.
|
|
|
|
if (TestFn && !TestFn->isExternal()) {
|
|
|
|
// 1. Add a string constant with its name to the global file
|
|
|
|
Constant *InitArray = ConstantArray::get(F->getName());
|
|
|
|
GlobalVariable *funcName =
|
|
|
|
new GlobalVariable(InitArray->getType(), true /*isConstant*/,
|
|
|
|
GlobalValue::InternalLinkage, InitArray,
|
|
|
|
F->getName() + "_name", Safe);
|
|
|
|
|
|
|
|
// 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
|
|
|
|
// sbyte* so it matches the signature of the resolver function.
|
|
|
|
|
|
|
|
// GetElementPtr *funcName, ulong 0, ulong 0
|
|
|
|
std::vector<Constant*> GEPargs(2,Constant::getNullValue(Type::IntTy));
|
|
|
|
Value *GEP =
|
2004-07-18 00:44:37 +00:00
|
|
|
ConstantExpr::getGetElementPtr(funcName, GEPargs);
|
2004-04-05 22:58:16 +00:00
|
|
|
std::vector<Value*> ResolverArgs;
|
|
|
|
ResolverArgs.push_back(GEP);
|
|
|
|
|
2004-04-19 03:36:47 +00:00
|
|
|
// Rewrite uses of F in global initializers, etc. to uses of a wrapper
|
|
|
|
// function that dynamically resolves the calls to F via our JIT API
|
|
|
|
if (F->use_begin() != F->use_end()) {
|
|
|
|
// Construct a new stub function that will re-route calls to F
|
2004-04-19 01:12:01 +00:00
|
|
|
const FunctionType *FuncTy = F->getFunctionType();
|
2004-04-19 03:36:47 +00:00
|
|
|
Function *FuncWrapper = new Function(FuncTy,
|
|
|
|
GlobalValue::InternalLinkage,
|
2004-04-19 01:12:01 +00:00
|
|
|
F->getName() + "_wrapper",
|
|
|
|
F->getParent());
|
|
|
|
BasicBlock *Header = new BasicBlock("header", FuncWrapper);
|
|
|
|
|
2004-04-19 03:36:47 +00:00
|
|
|
// Resolve the call to function F via the JIT API:
|
|
|
|
//
|
|
|
|
// call resolver(GetElementPtr...)
|
|
|
|
CallInst *resolve = new CallInst(resolverFunc, ResolverArgs,
|
|
|
|
"resolver");
|
|
|
|
Header->getInstList().push_back(resolve);
|
|
|
|
// cast the result from the resolver to correctly-typed function
|
|
|
|
CastInst *castResolver =
|
|
|
|
new CastInst(resolve, PointerType::get(F->getFunctionType()),
|
|
|
|
"resolverCast");
|
|
|
|
Header->getInstList().push_back(castResolver);
|
|
|
|
|
2004-04-19 01:12:01 +00:00
|
|
|
// Save the argument list
|
|
|
|
std::vector<Value*> Args;
|
|
|
|
for (Function::aiterator i = FuncWrapper->abegin(),
|
|
|
|
e = FuncWrapper->aend(); i != e; ++i)
|
|
|
|
Args.push_back(i);
|
|
|
|
|
|
|
|
// Pass on the arguments to the real function, return its result
|
|
|
|
if (F->getReturnType() == Type::VoidTy) {
|
2004-04-19 03:36:47 +00:00
|
|
|
CallInst *Call = new CallInst(castResolver, Args);
|
2004-04-19 01:12:01 +00:00
|
|
|
Header->getInstList().push_back(Call);
|
|
|
|
ReturnInst *Ret = new ReturnInst();
|
|
|
|
Header->getInstList().push_back(Ret);
|
|
|
|
} else {
|
2004-04-19 03:36:47 +00:00
|
|
|
CallInst *Call = new CallInst(castResolver, Args, "redir");
|
2004-04-19 01:12:01 +00:00
|
|
|
Header->getInstList().push_back(Call);
|
|
|
|
ReturnInst *Ret = new ReturnInst(Call);
|
|
|
|
Header->getInstList().push_back(Ret);
|
|
|
|
}
|
|
|
|
|
2004-04-19 03:36:47 +00:00
|
|
|
// Use the wrapper function instead of the old function
|
|
|
|
F->replaceAllUsesWith(FuncWrapper);
|
2004-04-19 01:12:01 +00:00
|
|
|
}
|
2004-04-05 22:58:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verifyModule(*Test) || verifyModule(*Safe)) {
|
|
|
|
std::cerr << "Bugpoint has a bug, which corrupted a module!!\n";
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// TestCodeGenerator - This is the predicate function used to check to see if
|
|
|
|
/// the "Test" portion of the program is miscompiled by the code generator under
|
|
|
|
/// test. If so, return true. In any case, both module arguments are deleted.
|
2004-04-21 18:36:43 +00:00
|
|
|
///
|
2004-04-05 22:58:16 +00:00
|
|
|
static bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe) {
|
|
|
|
CleanupAndPrepareModules(BD, Test, Safe);
|
|
|
|
|
|
|
|
std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
|
|
|
|
if (BD.writeProgramToFile(TestModuleBC, Test)) {
|
|
|
|
std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting.";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
delete Test;
|
|
|
|
|
|
|
|
// Make the shared library
|
|
|
|
std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
|
|
|
|
|
|
|
|
if (BD.writeProgramToFile(SafeModuleBC, Safe)) {
|
|
|
|
std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
std::string SharedObject = BD.compileSharedObject(SafeModuleBC);
|
|
|
|
delete Safe;
|
|
|
|
|
|
|
|
// Run the code generator on the `Test' code, loading the shared library.
|
|
|
|
// The function returns whether or not the new output differs from reference.
|
|
|
|
int Result = BD.diffProgram(TestModuleBC, SharedObject, false);
|
|
|
|
|
|
|
|
if (Result)
|
|
|
|
std::cerr << ": still failing!\n";
|
|
|
|
else
|
|
|
|
std::cerr << ": didn't fail.\n";
|
|
|
|
removeFile(TestModuleBC);
|
|
|
|
removeFile(SafeModuleBC);
|
|
|
|
removeFile(SharedObject);
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-21 18:36:43 +00:00
|
|
|
/// debugCodeGenerator - debug errors in LLC, LLI, or CBE.
|
|
|
|
///
|
2004-04-05 22:58:16 +00:00
|
|
|
bool BugDriver::debugCodeGenerator() {
|
|
|
|
if ((void*)cbe == (void*)Interpreter) {
|
|
|
|
std::string Result = executeProgramWithCBE("bugpoint.cbe.out");
|
|
|
|
std::cout << "\n*** The C backend cannot match the reference diff, but it "
|
|
|
|
<< "is used as the 'known good'\n code generator, so I can't"
|
|
|
|
<< " debug it. Perhaps you have a front-end problem?\n As a"
|
|
|
|
<< " sanity check, I left the result of executing the program "
|
|
|
|
<< "with the C backend\n in this file for you: '"
|
|
|
|
<< Result << "'.\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DisambiguateGlobalSymbols(Program);
|
|
|
|
|
|
|
|
std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator);
|
|
|
|
|
|
|
|
// Split the module into the two halves of the program we want.
|
|
|
|
Module *ToNotCodeGen = CloneModule(getProgram());
|
|
|
|
Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs);
|
|
|
|
|
|
|
|
// Condition the modules
|
|
|
|
CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen);
|
|
|
|
|
|
|
|
std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
|
|
|
|
if (writeProgramToFile(TestModuleBC, ToCodeGen)) {
|
|
|
|
std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting.";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
delete ToCodeGen;
|
|
|
|
|
|
|
|
// Make the shared library
|
|
|
|
std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
|
|
|
|
if (writeProgramToFile(SafeModuleBC, ToNotCodeGen)) {
|
|
|
|
std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
std::string SharedObject = compileSharedObject(SafeModuleBC);
|
|
|
|
delete ToNotCodeGen;
|
|
|
|
|
|
|
|
std::cout << "You can reproduce the problem with the command line: \n";
|
|
|
|
if (isExecutingJIT()) {
|
|
|
|
std::cout << " lli -load " << SharedObject << " " << TestModuleBC;
|
|
|
|
} else {
|
|
|
|
std::cout << " llc " << TestModuleBC << " -o " << TestModuleBC << ".s\n";
|
|
|
|
std::cout << " gcc " << SharedObject << " " << TestModuleBC
|
|
|
|
<< ".s -o " << TestModuleBC << ".exe -Wl,-R.\n";
|
|
|
|
std::cout << " " << TestModuleBC << ".exe";
|
|
|
|
}
|
|
|
|
for (unsigned i=0, e = InputArgv.size(); i != e; ++i)
|
|
|
|
std::cout << " " << InputArgv[i];
|
2004-07-23 01:30:49 +00:00
|
|
|
std::cout << '\n';
|
2004-04-05 22:58:16 +00:00
|
|
|
std::cout << "The shared object was created with:\n llc -march=c "
|
|
|
|
<< SafeModuleBC << " -o temporary.c\n"
|
|
|
|
<< " gcc -xc temporary.c -O2 -o " << SharedObject
|
|
|
|
#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
|
|
|
|
<< " -G" // Compile a shared library, `-G' for Sparc
|
|
|
|
#else
|
|
|
|
<< " -shared" // `-shared' for Linux/X86, maybe others
|
|
|
|
#endif
|
|
|
|
<< " -fno-strict-aliasing\n";
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|