1 //===- CodeGeneratorBug.cpp - Debug code generation bugs ------------------===//
3 // This file implements program code generation debugging support.
5 //===----------------------------------------------------------------------===//
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"
20 class ReduceMisCodegenFunctions : public ListReducer<Function*> {
23 ReduceMisCodegenFunctions(BugDriver &bd) : BD(bd) {}
25 virtual TestResult doTest(std::vector<Function*> &Prefix,
26 std::vector<Function*> &Suffix) {
27 if (!Prefix.empty() && TestFuncs(Prefix))
29 if (!Suffix.empty() && TestFuncs(Suffix))
34 bool TestFuncs(const std::vector<Function*> &CodegenTest);
36 void DisambiguateGlobalSymbols(Module *M);
40 bool ReduceMisCodegenFunctions::TestFuncs(const std::vector<Function*> &Funcs)
42 // Clone the module for the two halves of the program we want.
43 Module *SafeModule = CloneModule(BD.Program);
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);
52 DisambiguateGlobalSymbols(SafeModule);
53 Module *TestModule = CloneModule(SafeModule);
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
59 // Remove the Test functions from the Safe module
60 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!
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.";
74 // Make a shared library
75 std::string SharedObject;
76 BD.compileSharedObject(SafeModuleBC, SharedObject);
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.
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);
91 std::string TestModuleBC = "bugpoint.test.bc";
92 if (BD.writeProgramToFile(TestModuleBC, TestModule)) {
93 std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
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.
99 int Result = BD.diffProgram(TestModuleBC, SharedObject, false);
100 removeFile(SharedObject);
105 struct Disambiguator {
106 std::set<std::string> SymbolNames;
107 uint64_t uniqueCounter;
110 Disambiguator() : uniqueCounter(0), externalOnly(true) {}
111 void setExternalOnly(bool value) { externalOnly = value; }
112 void add(GlobalValue &V) {
113 if (externalOnly && !V.isExternal()) return;
115 if (SymbolNames.count(V.getName()) == 0) {
116 DEBUG(std::cerr << "Disambiguator: adding " << V.getName()
117 << ", no conflicts.\n");
118 SymbolNames.insert(V.getName());
120 // Mangle name before adding
123 newName = V.getName() + "_" + utostr(uniqueCounter);
124 if (SymbolNames.count(newName) == 0) break;
125 else ++uniqueCounter;
127 //while (SymbolNames.count(V->getName()+utostr(uniqueCounter++))==0);
128 DEBUG(std::cerr << "Disambiguator: conflict: " << V.getName()
129 << ", adding: " << newName << "\n");
131 SymbolNames.insert(newName);
137 void 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.
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);
144 // Now just rename functions and globals as necessary, keeping what's already
145 // in the set unique.
146 D.setExternalOnly(false);
147 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);
152 bool 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);
160 // Do the reduction...
161 ReduceMisCodegenFunctions(*this).reduceList(MisCodegenFunctions);
163 std::cout << "\n*** The following functions are being miscompiled: ";
164 PrintFunctionList(MisCodegenFunctions);
167 // Output a bunch of bytecode files for the user...
168 // ReduceMisCodegenFunctions(*this).TestFuncs(MisCodegenFunctions);