Added copyright header to all C++ source files.
[oota-llvm.git] / tools / bugpoint / CodeGeneratorBug.cpp
index da990c7f725330c8d4054fea8082c4fd43feaf37..60964bcfb3d44c2b184153b7c4d49c3a4f1c493e 100644 (file)
@@ -1,11 +1,18 @@
 //===- CodeGeneratorBug.cpp - Debug code generation bugs ------------------===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
+// 
 //
 // This file implements program code generation debugging support.
 //
 //===----------------------------------------------------------------------===//
 
 #include "BugDriver.h"
-#include "Support/SystemUtils.h"
 #include "ListReducer.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
@@ -23,6 +30,7 @@
 #include "Support/CommandLine.h"
 #include "Support/Debug.h"
 #include "Support/StringExtras.h"
+#include "Support/FileUtilities.h"
 #include <algorithm>
 #include <set>
 
@@ -48,8 +56,7 @@ public:
 
 
 bool ReduceMisCodegenFunctions::TestFuncs(const std::vector<Function*> &Funcs,
-                                          bool KeepFiles)
-{
+                                          bool KeepFiles) {
   std::cout << "Testing functions: ";
   BD.PrintFunctionList(Funcs);
   std::cout << "\t";
@@ -113,8 +120,10 @@ bool ReduceMisCodegenFunctions::TestFuncs(const std::vector<Function*> &Funcs,
     // Use the function we just added to get addresses of functions we need
     // Iterate over the global declarations in the Safe module
     for (Module::iterator F=SafeModule->begin(),E=SafeModule->end(); F!=E; ++F){
-      if (F->isExternal() && !F->use_empty() && &(*F) != resolverFunc &&
-          F->getIntrinsicID() == 0 /* ignore intrinsics */) {
+      if (F->isExternal() && !F->use_empty() && &*F != resolverFunc &&
+          F->getIntrinsicID() == 0 /* ignore intrinsics */ &&
+          // Don't forward functions which are external in the test module too.
+          !TestModule->getNamedFunction(F->getName())->isExternal()) {
         // If it has a non-zero use list,
         // 1. Add a string constant with its name to the global file
         // The correct type is `const [ NUM x sbyte ]' where NUM is length of
@@ -145,8 +154,8 @@ bool ReduceMisCodegenFunctions::TestFuncs(const std::vector<Function*> &Funcs,
         ResolverArgs.push_back(GEP);
 
         // Insert code at the beginning of the function
-        for (Value::use_iterator i=F->use_begin(), e=F->use_end(); i!=e; ++i) {
-          if (Instruction* Inst = dyn_cast<Instruction>(*i)) {
+        while (!F->use_empty())
+          if (Instruction *Inst = dyn_cast<Instruction>(F->use_back())) {
             // call resolver(GetElementPtr...)
             CallInst *resolve = new CallInst(resolverFunc, ResolverArgs, 
                                              "resolver", Inst);
@@ -162,7 +171,6 @@ bool ReduceMisCodegenFunctions::TestFuncs(const std::vector<Function*> &Funcs,
             std::cerr << "Non-instruction is using an external function!\n";
             abort();
           }
-        }
       }
     }
   }
@@ -225,8 +233,7 @@ bool ReduceMisCodegenFunctions::TestFuncs(const std::vector<Function*> &Funcs,
   }
 
   // Make a shared library
-  std::string SharedObject;
-  BD.compileSharedObject(SafeModuleBC, SharedObject);
+  std::string SharedObject = BD.compileSharedObject(SafeModuleBC);
 
   delete SafeModule;
   delete TestModule;
@@ -247,14 +254,21 @@ bool ReduceMisCodegenFunctions::TestFuncs(const std::vector<Function*> &Funcs,
     } else {
       std::cout << "  llc " << TestModuleBC << " -o " << TestModuleBC << ".s\n";
       std::cout << "  gcc " << SharedObject << " " << TestModuleBC
-                << ".s -o " << TestModuleBC << ".exe\n";
+                << ".s -o " << TestModuleBC << ".exe -Wl,-R.\n";
       std::cout << "  " << TestModuleBC << ".exe";
     }
     for (unsigned i=0, e = InputArgv.size(); i != e; ++i)
       std::cout << " " << InputArgv[i];
     std::cout << "\n";
-    std::cout << "The shared object was created with:\ndis -c " << SafeModuleBC 
-              << "-o " << SharedObject << "\n";
+    std::cout << "The shared object was created with:\n  llvm-dis -c "
+              << SafeModuleBC << " -o temporary.c\n"
+              << "  gcc -xc temporary.c -O2 -o " << SharedObject
+#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
+              << " -G"            // Compile a shared library, `-G' for Sparc
+#else
+              << " -shared"       // `-shared' for Linux/X86, maybe others
+#endif
+              << " -fno-strict-aliasing\n";
   } else {
     removeFile(TestModuleBC);
     removeFile(SafeModuleBC);
@@ -356,8 +370,11 @@ bool BugDriver::debugCodeGenerator() {
     // Call the old main function and return its result
     BasicBlock *BB = new BasicBlock("entry", newMain);
     std::vector<Value*> args;
-    for (Function::aiterator I=newMain->abegin(), E=newMain->aend(); I!=E; ++I)
+    for (Function::aiterator I = newMain->abegin(), E = newMain->aend(),
+           OI = oldMain->abegin(); I != E; ++I, ++OI) {
+      I->setName(OI->getName());    // Copy argument names from oldMain
       args.push_back(I);
+    }
     CallInst *call = new CallInst(oldMain, args);
     BB->getInstList().push_back(call);