blob: 18650c039696d704652866cde176718cc1c1fcfd [file] [log] [blame]
Misha Brukman50733362003-07-24 18:17:43 +00001//===- CodeGeneratorBug.cpp - Debug code generation bugs ------------------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Misha Brukman50733362003-07-24 18:17:43 +00009//
10// This file implements program code generation debugging support.
11//
12//===----------------------------------------------------------------------===//
13
14#include "BugDriver.h"
Misha Brukman50733362003-07-24 18:17:43 +000015#include "ListReducer.h"
Misha Brukman91eabc12003-07-28 19:16:14 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/GlobalValue.h"
19#include "llvm/iMemory.h"
20#include "llvm/iTerminators.h"
21#include "llvm/iOther.h"
Misha Brukman50733362003-07-24 18:17:43 +000022#include "llvm/Module.h"
Misha Brukman91eabc12003-07-28 19:16:14 +000023#include "llvm/Pass.h"
24#include "llvm/Analysis/Verifier.h"
Misha Brukmanc1e39ee2003-07-28 21:07:39 +000025#include "llvm/Support/Mangler.h"
Misha Brukman91eabc12003-07-28 19:16:14 +000026#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Misha Brukman50733362003-07-24 18:17:43 +000027#include "llvm/Transforms/Utils/Cloning.h"
28#include "llvm/Transforms/Utils/Linker.h"
Misha Brukmanbe6bf562003-07-30 20:15:56 +000029#include "Support/CommandLine.h"
Chris Lattnerc648dab2003-08-01 22:13:59 +000030#include "Support/Debug.h"
Misha Brukman50733362003-07-24 18:17:43 +000031#include "Support/StringExtras.h"
Misha Brukman3d9cafa2003-08-07 21:42:28 +000032#include "Support/FileUtilities.h"
Misha Brukman50733362003-07-24 18:17:43 +000033#include <algorithm>
34#include <set>
Chris Lattnerfa761832004-01-14 03:38:37 +000035using namespace llvm;
Misha Brukman50733362003-07-24 18:17:43 +000036
Brian Gaeked0fde302003-11-11 22:41:34 +000037namespace llvm {
Chris Lattnerfa761832004-01-14 03:38:37 +000038 extern cl::list<std::string> InputArgv;
Brian Gaeked0fde302003-11-11 22:41:34 +000039
Chris Lattnerfa761832004-01-14 03:38:37 +000040 class ReduceMisCodegenFunctions : public ListReducer<Function*> {
41 BugDriver &BD;
42 public:
43 ReduceMisCodegenFunctions(BugDriver &bd) : BD(bd) {}
44
45 virtual TestResult doTest(std::vector<Function*> &Prefix,
46 std::vector<Function*> &Suffix) {
47 if (!Prefix.empty() && TestFuncs(Prefix))
48 return KeepPrefix;
49 if (!Suffix.empty() && TestFuncs(Suffix))
50 return KeepSuffix;
51 return NoFailure;
52 }
53
54 bool TestFuncs(const std::vector<Function*> &CodegenTest,
55 bool KeepFiles = false);
56 };
57}
Misha Brukman50733362003-07-24 18:17:43 +000058
Misha Brukman91eabc12003-07-28 19:16:14 +000059bool ReduceMisCodegenFunctions::TestFuncs(const std::vector<Function*> &Funcs,
Chris Lattner769f1fe2003-10-14 21:59:36 +000060 bool KeepFiles) {
Misha Brukmanbe6bf562003-07-30 20:15:56 +000061 std::cout << "Testing functions: ";
62 BD.PrintFunctionList(Funcs);
63 std::cout << "\t";
Misha Brukman91eabc12003-07-28 19:16:14 +000064
Misha Brukman50733362003-07-24 18:17:43 +000065 // Clone the module for the two halves of the program we want.
66 Module *SafeModule = CloneModule(BD.Program);
67
68 // Make sure functions & globals are all external so that linkage
69 // between the two modules will work.
70 for (Module::iterator I = SafeModule->begin(), E = SafeModule->end();I!=E;++I)
71 I->setLinkage(GlobalValue::ExternalLinkage);
72 for (Module::giterator I=SafeModule->gbegin(),E = SafeModule->gend();I!=E;++I)
73 I->setLinkage(GlobalValue::ExternalLinkage);
74
Misha Brukman50733362003-07-24 18:17:43 +000075 Module *TestModule = CloneModule(SafeModule);
76
77 // Make sure global initializers exist only in the safe module (CBE->.so)
78 for (Module::giterator I=TestModule->gbegin(),E = TestModule->gend();I!=E;++I)
79 I->setInitializer(0); // Delete the initializer to make it external
80
Misha Brukmana259c9b2003-07-24 21:59:10 +000081 // Remove the Test functions from the Safe module
Misha Brukman50733362003-07-24 18:17:43 +000082 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
83 Function *TNOF = SafeModule->getFunction(Funcs[i]->getName(),
84 Funcs[i]->getFunctionType());
Misha Brukmande9720f2003-07-29 16:02:28 +000085 DEBUG(std::cerr << "Removing function " << Funcs[i]->getName() << "\n");
Misha Brukman50733362003-07-24 18:17:43 +000086 assert(TNOF && "Function doesn't exist in module!");
87 DeleteFunctionBody(TNOF); // Function is now external in this module!
88 }
89
Misha Brukman91eabc12003-07-28 19:16:14 +000090 // Remove the Safe functions from the Test module
91 for (Module::iterator I=TestModule->begin(),E=TestModule->end(); I!=E; ++I) {
92 bool funcFound = false;
93 for (std::vector<Function*>::const_iterator F=Funcs.begin(),Fe=Funcs.end();
94 F != Fe; ++F)
95 if (I->getName() == (*F)->getName()) funcFound = true;
96
97 if (!funcFound && !(BD.isExecutingJIT() && I->getName() == "main"))
98 DeleteFunctionBody(I);
99 }
100
101 // This is only applicable if we are debugging the JIT:
102 // Find all external functions in the Safe modules that are actually used
103 // (called or taken address of), and make them call the JIT wrapper instead
104 if (BD.isExecutingJIT()) {
105 // Must delete `main' from Safe module if it has it
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000106 Function *safeMain = SafeModule->getNamedFunction("main");
Misha Brukmande9720f2003-07-29 16:02:28 +0000107 assert(safeMain && "`main' function not found in safe module!");
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000108 DeleteFunctionBody(safeMain);
Misha Brukman91eabc12003-07-28 19:16:14 +0000109
110 // Add an external function "getPointerToNamedFunction" that JIT provides
111 // Prototype: void *getPointerToNamedFunction(const char* Name)
112 std::vector<const Type*> Params;
113 Params.push_back(PointerType::get(Type::SByteTy)); // std::string&
114 FunctionType *resolverTy = FunctionType::get(PointerType::get(Type::VoidTy),
115 Params, false /* isVarArg */);
Misha Brukman91eabc12003-07-28 19:16:14 +0000116 Function *resolverFunc = new Function(resolverTy,
117 GlobalValue::ExternalLinkage,
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000118 "getPointerToNamedFunction",
Misha Brukman91eabc12003-07-28 19:16:14 +0000119 SafeModule);
120
121 // Use the function we just added to get addresses of functions we need
122 // Iterate over the global declarations in the Safe module
123 for (Module::iterator F=SafeModule->begin(),E=SafeModule->end(); F!=E; ++F){
Chris Lattner9fc2adc2003-10-19 23:32:50 +0000124 if (F->isExternal() && !F->use_empty() && &*F != resolverFunc &&
125 F->getIntrinsicID() == 0 /* ignore intrinsics */ &&
126 // Don't forward functions which are external in the test module too.
127 !TestModule->getNamedFunction(F->getName())->isExternal()) {
Misha Brukman91eabc12003-07-28 19:16:14 +0000128 // If it has a non-zero use list,
129 // 1. Add a string constant with its name to the global file
130 // The correct type is `const [ NUM x sbyte ]' where NUM is length of
131 // function name + 1
132 const std::string &Name = F->getName();
133 GlobalVariable *funcName =
134 new GlobalVariable(ArrayType::get(Type::SByteTy, Name.length()+1),
135 true /* isConstant */,
136 GlobalValue::InternalLinkage,
137 ConstantArray::get(Name),
138 Name + "_name",
139 SafeModule);
140
141 // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
142 // sbyte* so it matches the signature of the resolver function.
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000143 std::vector<Constant*> GEPargs(2, Constant::getNullValue(Type::LongTy));
Misha Brukman91eabc12003-07-28 19:16:14 +0000144
145 // 3. Replace all uses of `func' with calls to resolver by:
146 // (a) Iterating through the list of uses of this function
147 // (b) Insert a cast instruction in front of each use
148 // (c) Replace use of old call with new call
149
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000150 // GetElementPtr *funcName, ulong 0, ulong 0
151 Value *GEP =
152 ConstantExpr::getGetElementPtr(ConstantPointerRef::get(funcName),
153 GEPargs);
154 std::vector<Value*> ResolverArgs;
155 ResolverArgs.push_back(GEP);
Misha Brukman91eabc12003-07-28 19:16:14 +0000156
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000157 // Insert code at the beginning of the function
Chris Lattnerb656c202003-10-19 21:48:27 +0000158 while (!F->use_empty())
159 if (Instruction *Inst = dyn_cast<Instruction>(F->use_back())) {
Misha Brukman91eabc12003-07-28 19:16:14 +0000160 // call resolver(GetElementPtr...)
161 CallInst *resolve = new CallInst(resolverFunc, ResolverArgs,
162 "resolver", Inst);
163 // cast the result from the resolver to correctly-typed function
164 CastInst *castResolver =
165 new CastInst(resolve, PointerType::get(F->getFunctionType()),
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000166 "resolverCast", Inst);
Misha Brukman91eabc12003-07-28 19:16:14 +0000167 // actually use the resolved function
168 Inst->replaceUsesOfWith(F, castResolver);
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000169 } else {
170 // FIXME: need to take care of cases where a function is used that
171 // is not an instruction, e.g. global variable initializer...
Misha Brukman06ea1512003-10-20 19:43:47 +0000172 std::cerr <<
173 "UNSUPPORTED: External function used as global initializer!\n";
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000174 abort();
Misha Brukman91eabc12003-07-28 19:16:14 +0000175 }
Misha Brukman91eabc12003-07-28 19:16:14 +0000176 }
177 }
178 }
179
Chris Lattner10b9fa82003-08-01 16:14:33 +0000180 if (verifyModule(*SafeModule) || verifyModule(*TestModule)) {
181 std::cerr << "Bugpoint has a bug, an corrupted a module!!\n";
182 abort();
183 }
184
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000185 DEBUG(std::cerr << "Safe module:\n";
186 typedef Module::iterator MI;
187 typedef Module::giterator MGI;
Misha Brukman91eabc12003-07-28 19:16:14 +0000188
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000189 for (MI I = SafeModule->begin(), E = SafeModule->end(); I != E; ++I)
190 if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n";
191 for (MGI I = SafeModule->gbegin(), E = SafeModule->gend(); I!=E; ++I)
192 if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n";
193
194 std::cerr << "Test module:\n";
195 for (MI I = TestModule->begin(), E = TestModule->end(); I != E; ++I)
196 if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n";
197 for (MGI I=TestModule->gbegin(),E = TestModule->gend(); I!= E; ++I)
198 if (!I->isExternal()) std::cerr << "\t" << I->getName() << "\n";
199 );
Misha Brukman91eabc12003-07-28 19:16:14 +0000200
Misha Brukman50733362003-07-24 18:17:43 +0000201 // Write out the bytecode to be sent to CBE
Misha Brukman91eabc12003-07-28 19:16:14 +0000202 std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
Chris Lattner10b9fa82003-08-01 16:14:33 +0000203
Misha Brukman50733362003-07-24 18:17:43 +0000204 if (BD.writeProgramToFile(SafeModuleBC, SafeModule)) {
205 std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
206 exit(1);
207 }
208
Misha Brukman50733362003-07-24 18:17:43 +0000209 // Remove all functions from the Test module EXCEPT for the ones specified in
210 // Funcs. We know which ones these are because they are non-external in
211 // ToOptimize, but external in ToNotOptimize.
212 //
213 for (Module::iterator I = TestModule->begin(), E = TestModule->end();I!=E;++I)
214 if (!I->isExternal()) {
215 Function *TNOF = SafeModule->getFunction(I->getName(),
216 I->getFunctionType());
217 assert(TNOF && "Function doesn't exist in ToNotOptimize module??");
218 if (!TNOF->isExternal())
219 DeleteFunctionBody(I);
220 }
221
Misha Brukman91eabc12003-07-28 19:16:14 +0000222 std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
223 if (verifyModule(*TestModule)) {
224 std::cerr << "Bytecode file corrupted!\n";
225 exit(1);
226 }
Chris Lattner15d11272003-08-03 22:29:43 +0000227
228 // Clean up the modules, removing extra cruft that we don't need anymore...
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000229 SafeModule = BD.performFinalCleanups(SafeModule);
230 TestModule = BD.performFinalCleanups(TestModule);
Chris Lattner15d11272003-08-03 22:29:43 +0000231
Misha Brukman50733362003-07-24 18:17:43 +0000232 if (BD.writeProgramToFile(TestModuleBC, TestModule)) {
233 std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
234 exit(1);
235 }
236
Chris Lattner15d11272003-08-03 22:29:43 +0000237 // Make a shared library
Chris Lattner769f1fe2003-10-14 21:59:36 +0000238 std::string SharedObject = BD.compileSharedObject(SafeModuleBC);
Chris Lattner15d11272003-08-03 22:29:43 +0000239
Misha Brukman91eabc12003-07-28 19:16:14 +0000240 delete SafeModule;
241 delete TestModule;
242
Misha Brukman50733362003-07-24 18:17:43 +0000243 // Run the code generator on the `Test' code, loading the shared library.
244 // The function returns whether or not the new output differs from reference.
Chris Lattner6730c812003-08-04 00:56:27 +0000245 int Result = BD.diffProgram(TestModuleBC, SharedObject, false);
246
247 if (Result)
Misha Brukman11c65922003-08-07 21:05:13 +0000248 std::cerr << ": still failing!\n";
Chris Lattner6730c812003-08-04 00:56:27 +0000249 else
250 std::cerr << ": didn't fail.\n";
251
Misha Brukman91eabc12003-07-28 19:16:14 +0000252 if (KeepFiles) {
Chris Lattner6730c812003-08-04 00:56:27 +0000253 std::cout << "You can reproduce the problem with the command line: \n";
254 if (BD.isExecutingJIT()) {
255 std::cout << " lli -load " << SharedObject << " " << TestModuleBC;
256 } else {
Chris Lattner6730c812003-08-04 00:56:27 +0000257 std::cout << " llc " << TestModuleBC << " -o " << TestModuleBC << ".s\n";
258 std::cout << " gcc " << SharedObject << " " << TestModuleBC
Chris Lattner365f09a2003-10-18 21:55:47 +0000259 << ".s -o " << TestModuleBC << ".exe -Wl,-R.\n";
Chris Lattner6730c812003-08-04 00:56:27 +0000260 std::cout << " " << TestModuleBC << ".exe";
261 }
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000262 for (unsigned i=0, e = InputArgv.size(); i != e; ++i)
263 std::cout << " " << InputArgv[i];
264 std::cout << "\n";
Misha Brukman85544ba2003-08-28 22:14:16 +0000265 std::cout << "The shared object was created with:\n llvm-dis -c "
Chris Lattnere533cef2003-08-17 22:08:25 +0000266 << SafeModuleBC << " -o temporary.c\n"
Chris Lattner57c69412003-08-17 23:38:53 +0000267 << " gcc -xc temporary.c -O2 -o " << SharedObject
268#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Chris Lattner182a21e2003-10-14 20:55:56 +0000269 << " -G" // Compile a shared library, `-G' for Sparc
Chris Lattner8deb5812003-10-14 21:01:51 +0000270#else
271 << " -shared" // `-shared' for Linux/X86, maybe others
Chris Lattner57c69412003-08-17 23:38:53 +0000272#endif
Chris Lattnerb7154972003-10-18 21:08:57 +0000273 << " -fno-strict-aliasing\n";
Misha Brukman91eabc12003-07-28 19:16:14 +0000274 } else {
275 removeFile(TestModuleBC);
276 removeFile(SafeModuleBC);
277 removeFile(SharedObject);
278 }
Misha Brukmana259c9b2003-07-24 21:59:10 +0000279 return Result;
Misha Brukman50733362003-07-24 18:17:43 +0000280}
281
282namespace {
Misha Brukmana259c9b2003-07-24 21:59:10 +0000283 struct Disambiguator {
Misha Brukman91eabc12003-07-28 19:16:14 +0000284 std::set<std::string> SymbolNames;
285 std::set<GlobalValue*> Symbols;
Misha Brukman50733362003-07-24 18:17:43 +0000286 uint64_t uniqueCounter;
287 bool externalOnly;
Misha Brukmana259c9b2003-07-24 21:59:10 +0000288 public:
Misha Brukman50733362003-07-24 18:17:43 +0000289 Disambiguator() : uniqueCounter(0), externalOnly(true) {}
290 void setExternalOnly(bool value) { externalOnly = value; }
Misha Brukmana259c9b2003-07-24 21:59:10 +0000291 void add(GlobalValue &V) {
Misha Brukman91eabc12003-07-28 19:16:14 +0000292 // If we're only processing externals and this isn't external, bail
Misha Brukman50733362003-07-24 18:17:43 +0000293 if (externalOnly && !V.isExternal()) return;
Misha Brukman91eabc12003-07-28 19:16:14 +0000294 // If we're already processed this symbol, don't add it again
295 if (Symbols.count(&V) != 0) return;
Misha Brukman3b624622003-07-30 21:45:20 +0000296 // Ignore intrinsic functions
297 if (Function *F = dyn_cast<Function>(&V))
298 if (F->getIntrinsicID() != 0)
299 return;
Misha Brukman50733362003-07-24 18:17:43 +0000300
Misha Brukman91eabc12003-07-28 19:16:14 +0000301 std::string SymName = V.getName();
302
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000303 // Use the Mangler facility to make symbol names that will be valid in
304 // shared objects.
305 SymName = Mangler::makeNameProper(SymName);
306 V.setName(SymName);
Misha Brukman91eabc12003-07-28 19:16:14 +0000307
308 if (SymbolNames.count(SymName) == 0) {
309 DEBUG(std::cerr << "Disambiguator: adding " << SymName
Misha Brukman50733362003-07-24 18:17:43 +0000310 << ", no conflicts.\n");
Misha Brukman91eabc12003-07-28 19:16:14 +0000311 SymbolNames.insert(SymName);
Misha Brukman50733362003-07-24 18:17:43 +0000312 } else {
313 // Mangle name before adding
314 std::string newName;
315 do {
Misha Brukman91eabc12003-07-28 19:16:14 +0000316 newName = SymName + "_" + utostr(uniqueCounter);
Misha Brukman50733362003-07-24 18:17:43 +0000317 if (SymbolNames.count(newName) == 0) break;
318 else ++uniqueCounter;
319 } while (1);
320 //while (SymbolNames.count(V->getName()+utostr(uniqueCounter++))==0);
Misha Brukman91eabc12003-07-28 19:16:14 +0000321 DEBUG(std::cerr << "Disambiguator: conflict: " << SymName
Misha Brukman50733362003-07-24 18:17:43 +0000322 << ", adding: " << newName << "\n");
323 V.setName(newName);
324 SymbolNames.insert(newName);
Misha Brukman50733362003-07-24 18:17:43 +0000325 }
Misha Brukman91eabc12003-07-28 19:16:14 +0000326 Symbols.insert(&V);
Misha Brukman50733362003-07-24 18:17:43 +0000327 }
328 };
329}
330
Chris Lattnerfa761832004-01-14 03:38:37 +0000331static void DisambiguateGlobalSymbols(Module *M) {
Misha Brukman50733362003-07-24 18:17:43 +0000332 // First, try not to cause collisions by minimizing chances of renaming an
333 // already-external symbol, so take in external globals and functions as-is.
Misha Brukmana259c9b2003-07-24 21:59:10 +0000334 Disambiguator D;
Misha Brukman91eabc12003-07-28 19:16:14 +0000335 DEBUG(std::cerr << "Disambiguating globals (external-only)\n");
Misha Brukmana259c9b2003-07-24 21:59:10 +0000336 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) D.add(*I);
Misha Brukman91eabc12003-07-28 19:16:14 +0000337 DEBUG(std::cerr << "Disambiguating functions (external-only)\n");
Misha Brukmana259c9b2003-07-24 21:59:10 +0000338 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) D.add(*I);
Misha Brukman50733362003-07-24 18:17:43 +0000339
340 // Now just rename functions and globals as necessary, keeping what's already
341 // in the set unique.
342 D.setExternalOnly(false);
Misha Brukman91eabc12003-07-28 19:16:14 +0000343 DEBUG(std::cerr << "Disambiguating globals\n");
Misha Brukmana259c9b2003-07-24 21:59:10 +0000344 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) D.add(*I);
Misha Brukman91eabc12003-07-28 19:16:14 +0000345 DEBUG(std::cerr << "Disambiguating globals\n");
Misha Brukmana259c9b2003-07-24 21:59:10 +0000346 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) D.add(*I);
Misha Brukman50733362003-07-24 18:17:43 +0000347}
348
349
350bool BugDriver::debugCodeGenerator() {
351 // See if we can pin down which functions are being miscompiled...
352 //First, build a list of all of the non-external functions in the program.
353 std::vector<Function*> MisCodegenFunctions;
354 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
355 if (!I->isExternal())
356 MisCodegenFunctions.push_back(I);
357
Misha Brukman91eabc12003-07-28 19:16:14 +0000358 // If we are executing the JIT, we *must* keep the function `main' in the
359 // module that is passed in, and not the shared library. However, we still
360 // want to be able to debug the `main' function alone. Thus, we create a new
361 // function `main' which just calls the old one.
362 if (isExecutingJIT()) {
363 // Get the `main' function
364 Function *oldMain = Program->getNamedFunction("main");
Misha Brukmande9720f2003-07-29 16:02:28 +0000365 assert(oldMain && "`main' function not found in program!");
Misha Brukman91eabc12003-07-28 19:16:14 +0000366 // Rename it
Misha Brukman11c65922003-08-07 21:05:13 +0000367 oldMain->setName("llvm_old_main");
Misha Brukman91eabc12003-07-28 19:16:14 +0000368 // Create a NEW `main' function with same type
369 Function *newMain = new Function(oldMain->getFunctionType(),
Misha Brukmanc1e39ee2003-07-28 21:07:39 +0000370 GlobalValue::ExternalLinkage,
Misha Brukman91eabc12003-07-28 19:16:14 +0000371 "main", Program);
372 // Call the old main function and return its result
373 BasicBlock *BB = new BasicBlock("entry", newMain);
374 std::vector<Value*> args;
Chris Lattner57e5a702003-08-17 22:14:20 +0000375 for (Function::aiterator I = newMain->abegin(), E = newMain->aend(),
376 OI = oldMain->abegin(); I != E; ++I, ++OI) {
377 I->setName(OI->getName()); // Copy argument names from oldMain
Misha Brukman91eabc12003-07-28 19:16:14 +0000378 args.push_back(I);
Chris Lattner57e5a702003-08-17 22:14:20 +0000379 }
Misha Brukman91eabc12003-07-28 19:16:14 +0000380 CallInst *call = new CallInst(oldMain, args);
381 BB->getInstList().push_back(call);
382
383 // if the type of old function wasn't void, return value of call
Misha Brukman91eabc12003-07-28 19:16:14 +0000384 if (oldMain->getReturnType() != Type::VoidTy) {
Chris Lattner89eca902003-11-22 02:10:26 +0000385 new ReturnInst(call, BB);
Misha Brukman91eabc12003-07-28 19:16:14 +0000386 } else {
Chris Lattner89eca902003-11-22 02:10:26 +0000387 new ReturnInst(0, BB);
Misha Brukman91eabc12003-07-28 19:16:14 +0000388 }
Misha Brukman91eabc12003-07-28 19:16:14 +0000389 }
390
Misha Brukmande9720f2003-07-29 16:02:28 +0000391 DisambiguateGlobalSymbols(Program);
392
Misha Brukman50733362003-07-24 18:17:43 +0000393 // Do the reduction...
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000394 if (!ReduceMisCodegenFunctions(*this).reduceList(MisCodegenFunctions)) {
Chris Lattner6730c812003-08-04 00:56:27 +0000395 std::cerr << "*** Execution matches reference output! "
396 << "bugpoint can't help you with your problem!\n";
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000397 return false;
398 }
Misha Brukman50733362003-07-24 18:17:43 +0000399
400 std::cout << "\n*** The following functions are being miscompiled: ";
401 PrintFunctionList(MisCodegenFunctions);
402 std::cout << "\n";
403
404 // Output a bunch of bytecode files for the user...
Misha Brukman91eabc12003-07-28 19:16:14 +0000405 ReduceMisCodegenFunctions(*this).TestFuncs(MisCodegenFunctions, true);
Misha Brukman50733362003-07-24 18:17:43 +0000406
407 return false;
408}