blob: 93ee24ea081c6bb5c19622e0f034d02e2f95e7df [file] [log] [blame]
Misha Brukman50733362003-07-24 18:17:43 +00001//===- CodeGeneratorBug.cpp - Debug code generation bugs ------------------===//
2//
3// This file implements program code generation debugging support.
4//
5//===----------------------------------------------------------------------===//
6
7#include "BugDriver.h"
8#include "SystemUtils.h"
9#include "ListReducer.h"
10#include "llvm/Pass.h"
11#include "llvm/Module.h"
12#include "llvm/Transforms/Utils/Cloning.h"
13#include "llvm/Transforms/Utils/Linker.h"
14#include "Support/CommandLine.h"
15#include "Support/Statistic.h"
16#include "Support/StringExtras.h"
17#include <algorithm>
18#include <set>
19
Misha Brukman50733362003-07-24 18:17:43 +000020class ReduceMisCodegenFunctions : public ListReducer<Function*> {
21 BugDriver &BD;
22public:
23 ReduceMisCodegenFunctions(BugDriver &bd) : BD(bd) {}
24
25 virtual TestResult doTest(std::vector<Function*> &Prefix,
26 std::vector<Function*> &Suffix) {
27 if (!Prefix.empty() && TestFuncs(Prefix))
28 return KeepPrefix;
29 if (!Suffix.empty() && TestFuncs(Suffix))
30 return KeepSuffix;
31 return NoFailure;
32 }
33
34 bool TestFuncs(const std::vector<Function*> &CodegenTest);
35
36 void DisambiguateGlobalSymbols(Module *M);
37};
38
39
40bool ReduceMisCodegenFunctions::TestFuncs(const std::vector<Function*> &Funcs)
41{
42 // Clone the module for the two halves of the program we want.
43 Module *SafeModule = CloneModule(BD.Program);
44
45 // Make sure functions & globals are all external so that linkage
46 // between the two modules will work.
47 for (Module::iterator I = SafeModule->begin(), E = SafeModule->end();I!=E;++I)
48 I->setLinkage(GlobalValue::ExternalLinkage);
49 for (Module::giterator I=SafeModule->gbegin(),E = SafeModule->gend();I!=E;++I)
50 I->setLinkage(GlobalValue::ExternalLinkage);
51
52 DisambiguateGlobalSymbols(SafeModule);
53 Module *TestModule = CloneModule(SafeModule);
54
55 // Make sure global initializers exist only in the safe module (CBE->.so)
56 for (Module::giterator I=TestModule->gbegin(),E = TestModule->gend();I!=E;++I)
57 I->setInitializer(0); // Delete the initializer to make it external
58
Misha Brukmana259c9b2003-07-24 21:59:10 +000059 // Remove the Test functions from the Safe module
Misha Brukman50733362003-07-24 18:17:43 +000060 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
61 Function *TNOF = SafeModule->getFunction(Funcs[i]->getName(),
62 Funcs[i]->getFunctionType());
63 assert(TNOF && "Function doesn't exist in module!");
64 DeleteFunctionBody(TNOF); // Function is now external in this module!
65 }
66
67 // Write out the bytecode to be sent to CBE
68 std::string SafeModuleBC = "bugpoint.safe.bc";
69 if (BD.writeProgramToFile(SafeModuleBC, SafeModule)) {
70 std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
71 exit(1);
72 }
73
74 // Make a shared library
75 std::string SharedObject;
76 BD.compileSharedObject(SafeModuleBC, SharedObject);
77
78 // Remove all functions from the Test module EXCEPT for the ones specified in
79 // Funcs. We know which ones these are because they are non-external in
80 // ToOptimize, but external in ToNotOptimize.
81 //
82 for (Module::iterator I = TestModule->begin(), E = TestModule->end();I!=E;++I)
83 if (!I->isExternal()) {
84 Function *TNOF = SafeModule->getFunction(I->getName(),
85 I->getFunctionType());
86 assert(TNOF && "Function doesn't exist in ToNotOptimize module??");
87 if (!TNOF->isExternal())
88 DeleteFunctionBody(I);
89 }
90
91 std::string TestModuleBC = "bugpoint.test.bc";
92 if (BD.writeProgramToFile(TestModuleBC, TestModule)) {
93 std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
94 exit(1);
95 }
96
97 // Run the code generator on the `Test' code, loading the shared library.
98 // The function returns whether or not the new output differs from reference.
Misha Brukmana259c9b2003-07-24 21:59:10 +000099 int Result = BD.diffProgram(TestModuleBC, SharedObject, false);
100 removeFile(SharedObject);
101 return Result;
Misha Brukman50733362003-07-24 18:17:43 +0000102}
103
104namespace {
Misha Brukmana259c9b2003-07-24 21:59:10 +0000105 struct Disambiguator {
Misha Brukman50733362003-07-24 18:17:43 +0000106 std::set<std::string> SymbolNames;
Misha Brukman50733362003-07-24 18:17:43 +0000107 uint64_t uniqueCounter;
108 bool externalOnly;
Misha Brukmana259c9b2003-07-24 21:59:10 +0000109 public:
Misha Brukman50733362003-07-24 18:17:43 +0000110 Disambiguator() : uniqueCounter(0), externalOnly(true) {}
111 void setExternalOnly(bool value) { externalOnly = value; }
Misha Brukmana259c9b2003-07-24 21:59:10 +0000112 void add(GlobalValue &V) {
Misha Brukman50733362003-07-24 18:17:43 +0000113 if (externalOnly && !V.isExternal()) return;
114
115 if (SymbolNames.count(V.getName()) == 0) {
116 DEBUG(std::cerr << "Disambiguator: adding " << V.getName()
117 << ", no conflicts.\n");
Misha Brukman50733362003-07-24 18:17:43 +0000118 SymbolNames.insert(V.getName());
119 } else {
120 // Mangle name before adding
121 std::string newName;
122 do {
123 newName = V.getName() + "_" + utostr(uniqueCounter);
124 if (SymbolNames.count(newName) == 0) break;
125 else ++uniqueCounter;
126 } while (1);
127 //while (SymbolNames.count(V->getName()+utostr(uniqueCounter++))==0);
128 DEBUG(std::cerr << "Disambiguator: conflict: " << V.getName()
129 << ", adding: " << newName << "\n");
130 V.setName(newName);
131 SymbolNames.insert(newName);
Misha Brukman50733362003-07-24 18:17:43 +0000132 }
133 }
134 };
135}
136
137void ReduceMisCodegenFunctions::DisambiguateGlobalSymbols(Module *M) {
138 // First, try not to cause collisions by minimizing chances of renaming an
139 // already-external symbol, so take in external globals and functions as-is.
Misha Brukmana259c9b2003-07-24 21:59:10 +0000140 Disambiguator D;
141 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) D.add(*I);
142 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) D.add(*I);
Misha Brukman50733362003-07-24 18:17:43 +0000143
144 // Now just rename functions and globals as necessary, keeping what's already
145 // in the set unique.
146 D.setExternalOnly(false);
Misha Brukmana259c9b2003-07-24 21:59:10 +0000147 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) D.add(*I);
148 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) D.add(*I);
Misha Brukman50733362003-07-24 18:17:43 +0000149}
150
151
152bool BugDriver::debugCodeGenerator() {
153 // See if we can pin down which functions are being miscompiled...
154 //First, build a list of all of the non-external functions in the program.
155 std::vector<Function*> MisCodegenFunctions;
156 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
157 if (!I->isExternal())
158 MisCodegenFunctions.push_back(I);
159
160 // Do the reduction...
161 ReduceMisCodegenFunctions(*this).reduceList(MisCodegenFunctions);
162
163 std::cout << "\n*** The following functions are being miscompiled: ";
164 PrintFunctionList(MisCodegenFunctions);
165 std::cout << "\n";
166
167 // Output a bunch of bytecode files for the user...
Misha Brukmana259c9b2003-07-24 21:59:10 +0000168 // ReduceMisCodegenFunctions(*this).TestFuncs(MisCodegenFunctions);
Misha Brukman50733362003-07-24 18:17:43 +0000169
170 return false;
171}
172