}
namespace {
+ static llvm::cl::opt<bool>
+ DisableLoopExtraction("disable-loop-extraction",
+ cl::desc("Don't extract loops when searching for miscompilations"),
+ cl::init(false));
+
class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> {
BugDriver &BD;
public:
// 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::global_iterator I = M->global_begin(), E = M->global_end(); I != E; ++I)
+ // Agree with the CBE on symbol naming
+ Mang.markCharUnacceptable('.');
+ Mang.setPreserveAsmNames(true);
+ for (Module::global_iterator I = M->global_begin(), E = M->global_end();
+ 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));
std::vector<Function*> &MiscompiledFunctions) {
bool MadeChange = false;
while (1) {
+ if (BugpointIsInterrupted) return MadeChange;
+
Module *ToNotOptimize = CloneModule(BD.getProgram());
Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
MiscompiledFunctions);
std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions;
for (Module::iterator I = ToOptimizeLoopExtracted->begin(),
E = ToOptimizeLoopExtracted->end(); I != E; ++I)
- if (!I->isExternal())
+ if (!I->isDeclaration())
MisCompFunctions.push_back(std::make_pair(I->getName(),
I->getFunctionType()));
// optimized and loop extracted module.
MiscompiledFunctions.clear();
for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
- Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first,
- MisCompFunctions[i].second);
+ Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first);
+
assert(NewF && "Function not found??");
+ assert(NewF->getFunctionType() == MisCompFunctions[i].second &&
+ "found wrong function type?");
MiscompiledFunctions.push_back(NewF);
}
static bool ExtractBlocks(BugDriver &BD,
bool (*TestFn)(BugDriver &, Module *, Module *),
std::vector<Function*> &MiscompiledFunctions) {
+ if (BugpointIsInterrupted) return false;
+
std::vector<BasicBlock*> Blocks;
for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
for (Function::iterator I = MiscompiledFunctions[i]->begin(),
std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions;
for (Module::iterator I = Extracted->begin(), E = Extracted->end();
I != E; ++I)
- if (!I->isExternal())
+ if (!I->isDeclaration())
MisCompFunctions.push_back(std::make_pair(I->getName(),
I->getFunctionType()));
MiscompiledFunctions.clear();
for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
- Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first,
- MisCompFunctions[i].second);
+ Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first);
assert(NewF && "Function not found??");
+ assert(NewF->getFunctionType() == MisCompFunctions[i].second &&
+ "Function has wrong type??");
MiscompiledFunctions.push_back(NewF);
}
std::vector<Function*> MiscompiledFunctions;
Module *Prog = BD.getProgram();
for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I)
- if (!I->isExternal())
+ if (!I->isDeclaration())
MiscompiledFunctions.push_back(I);
// Do the reduction...
- ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
+ if (!BugpointIsInterrupted)
+ ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
std::cout << "\n*** The following function"
<< (MiscompiledFunctions.size() == 1 ? " is" : "s are")
// See if we can rip any loops out of the miscompiled functions and still
// trigger the problem.
- if (ExtractLoops(BD, TestFn, MiscompiledFunctions)) {
+
+ if (!BugpointIsInterrupted && !DisableLoopExtraction &&
+ ExtractLoops(BD, TestFn, MiscompiledFunctions)) {
// Okay, we extracted some loops and the problem still appears. See if we
// can eliminate some of the created functions from being candidates.
DisambiguateGlobalSymbols(BD.getProgram());
// Do the reduction...
- ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
+ if (!BugpointIsInterrupted)
+ ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
std::cout << "\n*** The following function"
<< (MiscompiledFunctions.size() == 1 ? " is" : "s are")
std::cout << '\n';
}
- if (ExtractBlocks(BD, TestFn, MiscompiledFunctions)) {
+ if (!BugpointIsInterrupted &&
+ 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.
///
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;
- }
+ if (!BugpointIsInterrupted)
+ 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") << ": "
// 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()) {
+ if (Function *oldMain = Safe->getFunction("main"))
+ if (!oldMain->isDeclaration()) {
// Rename it
oldMain->setName("llvm_bugpoint_old_main");
// Create a NEW `main' function with same type in the test module.
// Call the old main function and return its result
BasicBlock *BB = new BasicBlock("entry", newMain);
- CallInst *call = new CallInst(oldMainProto, args, "", BB);
+ CallInst *call = new CallInst(oldMainProto, &args[0], args.size(),
+ "", BB);
// If the type of old function wasn't void, return value of call
new ReturnInst(call, BB);
// Add the resolver to the Safe module.
// Prototype: void *getPointerToNamedFunction(const char* Name)
- Function *resolverFunc =
+ Constant *resolverFunc =
Safe->getOrInsertFunction("getPointerToNamedFunction",
- PointerType::get(Type::SByteTy),
- PointerType::get(Type::SByteTy), 0);
+ PointerType::get(Type::Int8Ty),
+ PointerType::get(Type::Int8Ty), (Type *)0);
// Use the function we just added to get addresses of functions we need.
for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
- if (F->isExternal() && !F->use_empty() && &*F != resolverFunc &&
+ if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc &&
F->getIntrinsicID() == 0 /* ignore intrinsics */) {
- Function *TestFn = Test->getFunction(F->getName(), F->getFunctionType());
+ Function *TestFn = Test->getFunction(F->getName());
// Don't forward functions which are external in the test module too.
- if (TestFn && !TestFn->isExternal()) {
+ if (TestFn && !TestFn->isDeclaration()) {
// 1. Add a string constant with its name to the global file
Constant *InitArray = ConstantArray::get(F->getName());
GlobalVariable *funcName =
// 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 =
- ConstantExpr::getGetElementPtr(funcName, GEPargs);
+ std::vector<Constant*> GEPargs(2,Constant::getNullValue(Type::Int32Ty));
+ Value *GEP = ConstantExpr::getGetElementPtr(funcName, &GEPargs[0], 2);
std::vector<Value*> ResolverArgs;
ResolverArgs.push_back(GEP);
// Check to see if we already looked up the value.
Value *CachedVal = new LoadInst(Cache, "fpcache", EntryBB);
- Value *IsNull = new SetCondInst(Instruction::SetEQ, CachedVal,
- NullPtr, "isNull", EntryBB);
+ Value *IsNull = new ICmpInst(ICmpInst::ICMP_EQ, CachedVal,
+ NullPtr, "isNull", EntryBB);
new BranchInst(LookupBB, DoCallBB, IsNull, EntryBB);
// Resolve the call to function F via the JIT API:
//
// call resolver(GetElementPtr...)
- CallInst *Resolver = new CallInst(resolverFunc, ResolverArgs,
+ CallInst *Resolver = new CallInst(resolverFunc, &ResolverArgs[0],
+ ResolverArgs.size(),
"resolver", LookupBB);
// cast the result from the resolver to correctly-typed function
- CastInst *CastedResolver =
- new CastInst(Resolver, PointerType::get(F->getFunctionType()),
- "resolverCast", LookupBB);
+ CastInst *CastedResolver = new BitCastInst(Resolver,
+ PointerType::get(F->getFunctionType()), "resolverCast", LookupBB);
+
// Save the value in our cache.
new StoreInst(CastedResolver, Cache, LookupBB);
new BranchInst(DoCallBB, LookupBB);
// Pass on the arguments to the real function, return its result
if (F->getReturnType() == Type::VoidTy) {
- CallInst *Call = new CallInst(FuncPtr, Args, "", DoCallBB);
+ new CallInst(FuncPtr, &Args[0], Args.size(), "", DoCallBB);
new ReturnInst(DoCallBB);
} else {
- CallInst *Call = new CallInst(FuncPtr, Args, "retval", DoCallBB);
+ CallInst *Call = new CallInst(FuncPtr, &Args[0], Args.size(),
+ "retval", DoCallBB);
new ReturnInst(Call, DoCallBB);
}
CleanupAndPrepareModules(BD, Test, Safe);
sys::Path TestModuleBC("bugpoint.test.bc");
- TestModuleBC.makeUnique();
+ std::string ErrMsg;
+ if (TestModuleBC.makeUnique(true, &ErrMsg)) {
+ std::cerr << BD.getToolName() << "Error making unique filename: "
+ << ErrMsg << "\n";
+ exit(1);
+ }
if (BD.writeProgramToFile(TestModuleBC.toString(), Test)) {
std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting.";
exit(1);
// Make the shared library
sys::Path SafeModuleBC("bugpoint.safe.bc");
- SafeModuleBC.makeUnique();
+ if (SafeModuleBC.makeUnique(true, &ErrMsg)) {
+ std::cerr << BD.getToolName() << "Error making unique filename: "
+ << ErrMsg << "\n";
+ exit(1);
+ }
if (BD.writeProgramToFile(SafeModuleBC.toString(), Safe)) {
std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen);
sys::Path TestModuleBC("bugpoint.test.bc");
- TestModuleBC.makeUnique();
+ std::string ErrMsg;
+ if (TestModuleBC.makeUnique(true, &ErrMsg)) {
+ std::cerr << getToolName() << "Error making unique filename: "
+ << ErrMsg << "\n";
+ exit(1);
+ }
if (writeProgramToFile(TestModuleBC.toString(), ToCodeGen)) {
std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting.";
// Make the shared library
sys::Path SafeModuleBC("bugpoint.safe.bc");
- SafeModuleBC.makeUnique();
+ if (SafeModuleBC.makeUnique(true, &ErrMsg)) {
+ std::cerr << getToolName() << "Error making unique filename: "
+ << ErrMsg << "\n";
+ exit(1);
+ }
if (writeProgramToFile(SafeModuleBC.toString(), ToNotCodeGen)) {
std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
std::cout << " gcc " << SharedObject << " " << TestModuleBC
<< ".s -o " << TestModuleBC << ".exe";
#if defined (HAVE_LINK_R)
- std::cout << "-Wl,-R.";
+ std::cout << " -Wl,-R.";
#endif
std::cout << "\n";
std::cout << " " << TestModuleBC << ".exe";