blob: 0aea9b031dad6a3961ba1d46d4754e0399551e45 [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"
Misha Brukman50733362003-07-24 18:17:43 +00008#include "ListReducer.h"
Misha Brukman91eabc12003-07-28 19:16:14 +00009#include "llvm/Constants.h"
10#include "llvm/DerivedTypes.h"
11#include "llvm/GlobalValue.h"
12#include "llvm/iMemory.h"
13#include "llvm/iTerminators.h"
14#include "llvm/iOther.h"
Misha Brukman50733362003-07-24 18:17:43 +000015#include "llvm/Module.h"
Misha Brukman91eabc12003-07-28 19:16:14 +000016#include "llvm/Pass.h"
17#include "llvm/Analysis/Verifier.h"
Misha Brukmanc1e39ee2003-07-28 21:07:39 +000018#include "llvm/Support/Mangler.h"
Misha Brukman91eabc12003-07-28 19:16:14 +000019#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Misha Brukman50733362003-07-24 18:17:43 +000020#include "llvm/Transforms/Utils/Cloning.h"
21#include "llvm/Transforms/Utils/Linker.h"
Misha Brukmanbe6bf562003-07-30 20:15:56 +000022#include "Support/CommandLine.h"
Chris Lattnerc648dab2003-08-01 22:13:59 +000023#include "Support/Debug.h"
Misha Brukman50733362003-07-24 18:17:43 +000024#include "Support/StringExtras.h"
Misha Brukman3d9cafa2003-08-07 21:42:28 +000025#include "Support/FileUtilities.h"
Misha Brukman50733362003-07-24 18:17:43 +000026#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,
Chris Lattner769f1fe2003-10-14 21:59:36 +000051 bool KeepFiles) {
Misha Brukmanbe6bf562003-07-30 20:15:56 +000052 std::cout << "Testing functions: ";
53 BD.PrintFunctionList(Funcs);
54 std::cout << "\t";
Misha Brukman91eabc12003-07-28 19:16:14 +000055
Misha Brukman50733362003-07-24 18:17:43 +000056 // Clone the module for the two halves of the program we want.
57 Module *SafeModule = CloneModule(BD.Program);
58
59 // Make sure functions & globals are all external so that linkage
60 // between the two modules will work.
61 for (Module::iterator I = SafeModule->begin(), E = SafeModule->end();I!=E;++I)
62 I->setLinkage(GlobalValue::ExternalLinkage);
63 for (Module::giterator I=SafeModule->gbegin(),E = SafeModule->gend();I!=E;++I)
64 I->setLinkage(GlobalValue::ExternalLinkage);
65
Misha Brukman50733362003-07-24 18:17:43 +000066 Module *TestModule = CloneModule(SafeModule);
67
68 // Make sure global initializers exist only in the safe module (CBE->.so)
69 for (Module::giterator I=TestModule->gbegin(),E = TestModule->gend();I!=E;++I)
70 I->setInitializer(0); // Delete the initializer to make it external
71
Misha Brukmana259c9b2003-07-24 21:59:10 +000072 // Remove the Test functions from the Safe module
Misha Brukman50733362003-07-24 18:17:43 +000073 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
74 Function *TNOF = SafeModule->getFunction(Funcs[i]->getName(),
75 Funcs[i]->getFunctionType());
Misha Brukmande9720f2003-07-29 16:02:28 +000076 DEBUG(std::cerr << "Removing function " << Funcs[i]->getName() << "\n");
Misha Brukman50733362003-07-24 18:17:43 +000077 assert(TNOF && "Function doesn't exist in module!");
78 DeleteFunctionBody(TNOF); // Function is now external in this module!
79 }
80
Misha Brukman91eabc12003-07-28 19:16:14 +000081 // Remove the Safe functions from the Test module
82 for (Module::iterator I=TestModule->begin(),E=TestModule->end(); I!=E; ++I) {
83 bool funcFound = false;
84 for (std::vector<Function*>::const_iterator F=Funcs.begin(),Fe=Funcs.end();
85 F != Fe; ++F)
86 if (I->getName() == (*F)->getName()) funcFound = true;
87
88 if (!funcFound && !(BD.isExecutingJIT() && I->getName() == "main"))
89 DeleteFunctionBody(I);
90 }
91
92 // This is only applicable if we are debugging the JIT:
93 // Find all external functions in the Safe modules that are actually used
94 // (called or taken address of), and make them call the JIT wrapper instead
95 if (BD.isExecutingJIT()) {
96 // Must delete `main' from Safe module if it has it
Misha Brukmanc1e39ee2003-07-28 21:07:39 +000097 Function *safeMain = SafeModule->getNamedFunction("main");
Misha Brukmande9720f2003-07-29 16:02:28 +000098 assert(safeMain && "`main' function not found in safe module!");
Misha Brukmanc1e39ee2003-07-28 21:07:39 +000099 DeleteFunctionBody(safeMain);
Misha Brukman91eabc12003-07-28 19:16:14 +0000100
101 // Add an external function "getPointerToNamedFunction" that JIT provides
102 // Prototype: void *getPointerToNamedFunction(const char* Name)
103 std::vector<const Type*> Params;
104 Params.push_back(PointerType::get(Type::SByteTy)); // std::string&
105 FunctionType *resolverTy = FunctionType::get(PointerType::get(Type::VoidTy),
106 Params, false /* isVarArg */);
Misha Brukman91eabc12003-07-28 19:16:14 +0000107 Function *resolverFunc = new Function(resolverTy,
108 GlobalValue::ExternalLinkage,
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000109 "getPointerToNamedFunction",
Misha Brukman91eabc12003-07-28 19:16:14 +0000110 SafeModule);
111
112 // Use the function we just added to get addresses of functions we need
113 // Iterate over the global declarations in the Safe module
114 for (Module::iterator F=SafeModule->begin(),E=SafeModule->end(); F!=E; ++F){
Misha Brukman3b624622003-07-30 21:45:20 +0000115 if (F->isExternal() && !F->use_empty() && &(*F) != resolverFunc &&
116 F->getIntrinsicID() == 0 /* ignore intrinsics */) {
Misha Brukman91eabc12003-07-28 19:16:14 +0000117 // 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 Brukmanc1e39ee2003-07-28 21:07:39 +0000132 std::vector<Constant*> GEPargs(2, Constant::getNullValue(Type::LongTy));
Misha Brukman91eabc12003-07-28 19:16:14 +0000133
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 Brukmanc1e39ee2003-07-28 21:07:39 +0000139 // 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 Brukman91eabc12003-07-28 19:16:14 +0000145
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000146 // Insert code at the beginning of the function
Chris Lattnerb656c202003-10-19 21:48:27 +0000147 while (!F->use_empty())
148 if (Instruction *Inst = dyn_cast<Instruction>(F->use_back())) {
Misha Brukman91eabc12003-07-28 19:16:14 +0000149 // 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 Brukmanbe6bf562003-07-30 20:15:56 +0000155 "resolverCast", Inst);
Misha Brukman91eabc12003-07-28 19:16:14 +0000156 // actually use the resolved function
157 Inst->replaceUsesOfWith(F, castResolver);
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000158 } 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 Brukman91eabc12003-07-28 19:16:14 +0000163 }
Misha Brukman91eabc12003-07-28 19:16:14 +0000164 }
165 }
166 }
167
Chris Lattner10b9fa82003-08-01 16:14:33 +0000168 if (verifyModule(*SafeModule) || verifyModule(*TestModule)) {
169 std::cerr << "Bugpoint has a bug, an corrupted a module!!\n";
170 abort();
171 }
172
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000173 DEBUG(std::cerr << "Safe module:\n";
174 typedef Module::iterator MI;
175 typedef Module::giterator MGI;
Misha Brukman91eabc12003-07-28 19:16:14 +0000176
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000177 for (MI I = SafeModule->begin(), E = SafeModule->end(); I != E; ++I)
178 if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n";
179 for (MGI I = SafeModule->gbegin(), E = SafeModule->gend(); I!=E; ++I)
180 if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n";
181
182 std::cerr << "Test module:\n";
183 for (MI I = TestModule->begin(), E = TestModule->end(); I != E; ++I)
184 if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n";
185 for (MGI I=TestModule->gbegin(),E = TestModule->gend(); I!= E; ++I)
186 if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n";
187 );
Misha Brukman91eabc12003-07-28 19:16:14 +0000188
Misha Brukman50733362003-07-24 18:17:43 +0000189 // Write out the bytecode to be sent to CBE
Misha Brukman91eabc12003-07-28 19:16:14 +0000190 std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
Chris Lattner10b9fa82003-08-01 16:14:33 +0000191
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
Misha Brukman50733362003-07-24 18:17:43 +0000197 // Remove all functions from the Test module EXCEPT for the ones specified in
198 // Funcs. We know which ones these are because they are non-external in
199 // ToOptimize, but external in ToNotOptimize.
200 //
201 for (Module::iterator I = TestModule->begin(), E = TestModule->end();I!=E;++I)
202 if (!I->isExternal()) {
203 Function *TNOF = SafeModule->getFunction(I->getName(),
204 I->getFunctionType());
205 assert(TNOF && "Function doesn't exist in ToNotOptimize module??");
206 if (!TNOF->isExternal())
207 DeleteFunctionBody(I);
208 }
209
Misha Brukman91eabc12003-07-28 19:16:14 +0000210 std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
211 if (verifyModule(*TestModule)) {
212 std::cerr << "Bytecode file corrupted!\n";
213 exit(1);
214 }
Chris Lattner15d11272003-08-03 22:29:43 +0000215
216 // Clean up the modules, removing extra cruft that we don't need anymore...
217 SafeModule = BD.performFinalCleanups(SafeModule);
218 TestModule = BD.performFinalCleanups(TestModule);
219
Misha Brukman50733362003-07-24 18:17:43 +0000220 if (BD.writeProgramToFile(TestModuleBC, TestModule)) {
221 std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
222 exit(1);
223 }
224
Chris Lattner15d11272003-08-03 22:29:43 +0000225 // Make a shared library
Chris Lattner769f1fe2003-10-14 21:59:36 +0000226 std::string SharedObject = BD.compileSharedObject(SafeModuleBC);
Chris Lattner15d11272003-08-03 22:29:43 +0000227
Misha Brukman91eabc12003-07-28 19:16:14 +0000228 delete SafeModule;
229 delete TestModule;
230
Misha Brukman50733362003-07-24 18:17:43 +0000231 // Run the code generator on the `Test' code, loading the shared library.
232 // The function returns whether or not the new output differs from reference.
Chris Lattner6730c812003-08-04 00:56:27 +0000233 int Result = BD.diffProgram(TestModuleBC, SharedObject, false);
234
235 if (Result)
Misha Brukman11c65922003-08-07 21:05:13 +0000236 std::cerr << ": still failing!\n";
Chris Lattner6730c812003-08-04 00:56:27 +0000237 else
238 std::cerr << ": didn't fail.\n";
239
Misha Brukman91eabc12003-07-28 19:16:14 +0000240 if (KeepFiles) {
Chris Lattner6730c812003-08-04 00:56:27 +0000241 std::cout << "You can reproduce the problem with the command line: \n";
242 if (BD.isExecutingJIT()) {
243 std::cout << " lli -load " << SharedObject << " " << TestModuleBC;
244 } else {
Chris Lattner6730c812003-08-04 00:56:27 +0000245 std::cout << " llc " << TestModuleBC << " -o " << TestModuleBC << ".s\n";
246 std::cout << " gcc " << SharedObject << " " << TestModuleBC
Chris Lattner365f09a2003-10-18 21:55:47 +0000247 << ".s -o " << TestModuleBC << ".exe -Wl,-R.\n";
Chris Lattner6730c812003-08-04 00:56:27 +0000248 std::cout << " " << TestModuleBC << ".exe";
249 }
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000250 for (unsigned i=0, e = InputArgv.size(); i != e; ++i)
251 std::cout << " " << InputArgv[i];
252 std::cout << "\n";
Misha Brukman85544ba2003-08-28 22:14:16 +0000253 std::cout << "The shared object was created with:\n llvm-dis -c "
Chris Lattnere533cef2003-08-17 22:08:25 +0000254 << SafeModuleBC << " -o temporary.c\n"
Chris Lattner57c69412003-08-17 23:38:53 +0000255 << " gcc -xc temporary.c -O2 -o " << SharedObject
256#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Chris Lattner182a21e2003-10-14 20:55:56 +0000257 << " -G" // Compile a shared library, `-G' for Sparc
Chris Lattner8deb5812003-10-14 21:01:51 +0000258#else
259 << " -shared" // `-shared' for Linux/X86, maybe others
Chris Lattner57c69412003-08-17 23:38:53 +0000260#endif
Chris Lattnerb7154972003-10-18 21:08:57 +0000261 << " -fno-strict-aliasing\n";
Misha Brukman91eabc12003-07-28 19:16:14 +0000262 } else {
263 removeFile(TestModuleBC);
264 removeFile(SafeModuleBC);
265 removeFile(SharedObject);
266 }
Misha Brukmana259c9b2003-07-24 21:59:10 +0000267 return Result;
Misha Brukman50733362003-07-24 18:17:43 +0000268}
269
270namespace {
Misha Brukmana259c9b2003-07-24 21:59:10 +0000271 struct Disambiguator {
Misha Brukman91eabc12003-07-28 19:16:14 +0000272 std::set<std::string> SymbolNames;
273 std::set<GlobalValue*> Symbols;
Misha Brukman50733362003-07-24 18:17:43 +0000274 uint64_t uniqueCounter;
275 bool externalOnly;
Misha Brukmana259c9b2003-07-24 21:59:10 +0000276 public:
Misha Brukman50733362003-07-24 18:17:43 +0000277 Disambiguator() : uniqueCounter(0), externalOnly(true) {}
278 void setExternalOnly(bool value) { externalOnly = value; }
Misha Brukmana259c9b2003-07-24 21:59:10 +0000279 void add(GlobalValue &V) {
Misha Brukman91eabc12003-07-28 19:16:14 +0000280 // If we're only processing externals and this isn't external, bail
Misha Brukman50733362003-07-24 18:17:43 +0000281 if (externalOnly && !V.isExternal()) return;
Misha Brukman91eabc12003-07-28 19:16:14 +0000282 // If we're already processed this symbol, don't add it again
283 if (Symbols.count(&V) != 0) return;
Misha Brukman3b624622003-07-30 21:45:20 +0000284 // Ignore intrinsic functions
285 if (Function *F = dyn_cast<Function>(&V))
286 if (F->getIntrinsicID() != 0)
287 return;
Misha Brukman50733362003-07-24 18:17:43 +0000288
Misha Brukman91eabc12003-07-28 19:16:14 +0000289 std::string SymName = V.getName();
290
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000291 // Use the Mangler facility to make symbol names that will be valid in
292 // shared objects.
293 SymName = Mangler::makeNameProper(SymName);
294 V.setName(SymName);
Misha Brukman91eabc12003-07-28 19:16:14 +0000295
296 if (SymbolNames.count(SymName) == 0) {
297 DEBUG(std::cerr << "Disambiguator: adding " << SymName
Misha Brukman50733362003-07-24 18:17:43 +0000298 << ", no conflicts.\n");
Misha Brukman91eabc12003-07-28 19:16:14 +0000299 SymbolNames.insert(SymName);
Misha Brukman50733362003-07-24 18:17:43 +0000300 } else {
301 // Mangle name before adding
302 std::string newName;
303 do {
Misha Brukman91eabc12003-07-28 19:16:14 +0000304 newName = SymName + "_" + utostr(uniqueCounter);
Misha Brukman50733362003-07-24 18:17:43 +0000305 if (SymbolNames.count(newName) == 0) break;
306 else ++uniqueCounter;
307 } while (1);
308 //while (SymbolNames.count(V->getName()+utostr(uniqueCounter++))==0);
Misha Brukman91eabc12003-07-28 19:16:14 +0000309 DEBUG(std::cerr << "Disambiguator: conflict: " << SymName
Misha Brukman50733362003-07-24 18:17:43 +0000310 << ", adding: " << newName << "\n");
311 V.setName(newName);
312 SymbolNames.insert(newName);
Misha Brukman50733362003-07-24 18:17:43 +0000313 }
Misha Brukman91eabc12003-07-28 19:16:14 +0000314 Symbols.insert(&V);
Misha Brukman50733362003-07-24 18:17:43 +0000315 }
316 };
317}
318
Misha Brukmande9720f2003-07-29 16:02:28 +0000319void DisambiguateGlobalSymbols(Module *M) {
Misha Brukman50733362003-07-24 18:17:43 +0000320 // First, try not to cause collisions by minimizing chances of renaming an
321 // already-external symbol, so take in external globals and functions as-is.
Misha Brukmana259c9b2003-07-24 21:59:10 +0000322 Disambiguator D;
Misha Brukman91eabc12003-07-28 19:16:14 +0000323 DEBUG(std::cerr << "Disambiguating globals (external-only)\n");
Misha Brukmana259c9b2003-07-24 21:59:10 +0000324 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) D.add(*I);
Misha Brukman91eabc12003-07-28 19:16:14 +0000325 DEBUG(std::cerr << "Disambiguating functions (external-only)\n");
Misha Brukmana259c9b2003-07-24 21:59:10 +0000326 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) D.add(*I);
Misha Brukman50733362003-07-24 18:17:43 +0000327
328 // Now just rename functions and globals as necessary, keeping what's already
329 // in the set unique.
330 D.setExternalOnly(false);
Misha Brukman91eabc12003-07-28 19:16:14 +0000331 DEBUG(std::cerr << "Disambiguating globals\n");
Misha Brukmana259c9b2003-07-24 21:59:10 +0000332 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) D.add(*I);
Misha Brukman91eabc12003-07-28 19:16:14 +0000333 DEBUG(std::cerr << "Disambiguating globals\n");
Misha Brukmana259c9b2003-07-24 21:59:10 +0000334 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) D.add(*I);
Misha Brukman50733362003-07-24 18:17:43 +0000335}
336
337
338bool BugDriver::debugCodeGenerator() {
339 // See if we can pin down which functions are being miscompiled...
340 //First, build a list of all of the non-external functions in the program.
341 std::vector<Function*> MisCodegenFunctions;
342 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
343 if (!I->isExternal())
344 MisCodegenFunctions.push_back(I);
345
Misha Brukman91eabc12003-07-28 19:16:14 +0000346 // If we are executing the JIT, we *must* keep the function `main' in the
347 // module that is passed in, and not the shared library. However, we still
348 // want to be able to debug the `main' function alone. Thus, we create a new
349 // function `main' which just calls the old one.
350 if (isExecutingJIT()) {
351 // Get the `main' function
352 Function *oldMain = Program->getNamedFunction("main");
Misha Brukmande9720f2003-07-29 16:02:28 +0000353 assert(oldMain && "`main' function not found in program!");
Misha Brukman91eabc12003-07-28 19:16:14 +0000354 // Rename it
Misha Brukman11c65922003-08-07 21:05:13 +0000355 oldMain->setName("llvm_old_main");
Misha Brukman91eabc12003-07-28 19:16:14 +0000356 // Create a NEW `main' function with same type
357 Function *newMain = new Function(oldMain->getFunctionType(),
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000358 GlobalValue::ExternalLinkage,
Misha Brukman91eabc12003-07-28 19:16:14 +0000359 "main", Program);
360 // Call the old main function and return its result
361 BasicBlock *BB = new BasicBlock("entry", newMain);
362 std::vector<Value*> args;
Chris Lattner57e5a702003-08-17 22:14:20 +0000363 for (Function::aiterator I = newMain->abegin(), E = newMain->aend(),
364 OI = oldMain->abegin(); I != E; ++I, ++OI) {
365 I->setName(OI->getName()); // Copy argument names from oldMain
Misha Brukman91eabc12003-07-28 19:16:14 +0000366 args.push_back(I);
Chris Lattner57e5a702003-08-17 22:14:20 +0000367 }
Misha Brukman91eabc12003-07-28 19:16:14 +0000368 CallInst *call = new CallInst(oldMain, args);
369 BB->getInstList().push_back(call);
370
371 // if the type of old function wasn't void, return value of call
372 ReturnInst *ret;
373 if (oldMain->getReturnType() != Type::VoidTy) {
374 ret = new ReturnInst(call);
375 } else {
376 ret = new ReturnInst();
377 }
378
379 // Add the return instruction to the BasicBlock
380 BB->getInstList().push_back(ret);
381 }
382
Misha Brukmande9720f2003-07-29 16:02:28 +0000383 DisambiguateGlobalSymbols(Program);
384
Misha Brukman50733362003-07-24 18:17:43 +0000385 // Do the reduction...
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000386 if (!ReduceMisCodegenFunctions(*this).reduceList(MisCodegenFunctions)) {
Chris Lattner6730c812003-08-04 00:56:27 +0000387 std::cerr << "*** Execution matches reference output! "
388 << "bugpoint can't help you with your problem!\n";
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000389 return false;
390 }
Misha Brukman50733362003-07-24 18:17:43 +0000391
392 std::cout << "\n*** The following functions are being miscompiled: ";
393 PrintFunctionList(MisCodegenFunctions);
394 std::cout << "\n";
395
396 // Output a bunch of bytecode files for the user...
Misha Brukman91eabc12003-07-28 19:16:14 +0000397 ReduceMisCodegenFunctions(*this).TestFuncs(MisCodegenFunctions, true);
Misha Brukman50733362003-07-24 18:17:43 +0000398
399 return false;
400}