Revert r165777, "Mark function as 'used' so that LTO doesn't try to get rid of it."
[oota-llvm.git] / unittests / ExecutionEngine / JIT / JITTest.cpp
index 8f0582d3e8de2772a77e19ce86c74cee46bd9808..5e2af030f20f5b157712b3479f9518aba1f4d28e 100644 (file)
@@ -7,29 +7,29 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "gtest/gtest.h"
-#include "llvm/ADT/OwningPtr.h"
-#include "llvm/ADT/SmallPtrSet.h"
-#include "llvm/Assembly/Parser.h"
 #include "llvm/BasicBlock.h"
-#include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/Constant.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
-#include "llvm/ExecutionEngine/JIT.h"
-#include "llvm/ExecutionEngine/JITMemoryManager.h"
 #include "llvm/Function.h"
 #include "llvm/GlobalValue.h"
 #include "llvm/GlobalVariable.h"
+#include "llvm/IRBuilder.h"
 #include "llvm/LLVMContext.h"
 #include "llvm/Module.h"
-#include "llvm/Support/IRBuilder.h"
+#include "llvm/Type.h"
+#include "llvm/TypeBuilder.h"
+#include "llvm/ADT/OwningPtr.h"
+#include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/Assembly/Parser.h"
+#include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/ExecutionEngine/JIT.h"
+#include "llvm/ExecutionEngine/JITMemoryManager.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
-#include "llvm/Support/TypeBuilder.h"
-#include "llvm/Target/TargetSelect.h"
-#include "llvm/Type.h"
+#include "llvm/Support/TargetSelect.h"
 
+#include "gtest/gtest.h"
 #include <vector>
 
 using namespace llvm;
@@ -37,14 +37,14 @@ using namespace llvm;
 namespace {
 
 Function *makeReturnGlobal(std::string Name, GlobalVariable *G, Module *M) {
-  std::vector<const Type*> params;
-  const FunctionType *FTy = FunctionType::get(G->getType()->getElementType(),
+  std::vector<Type*> params;
+  FunctionType *FTy = FunctionType::get(G->getType()->getElementType(),
                                               params, false);
   Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M);
   BasicBlock *Entry = BasicBlock::Create(M->getContext(), "entry", F);
   IRBuilder<> builder(Entry);
   Value *Load = builder.CreateLoad(G);
-  const Type *GTy = G->getType()->getElementType();
+  Type *GTy = G->getType()->getElementType();
   Value *Add = builder.CreateAdd(Load, ConstantInt::get(GTy, 1LL));
   builder.CreateStore(Add, G);
   builder.CreateRet(Add);
@@ -64,8 +64,10 @@ public:
     : Base(JITMemoryManager::CreateDefaultMemManager()) {
     stubsAllocated = 0;
   }
-
-  void setSizeRequired(bool Required) { SizeRequired = Required; }
+  virtual void *getPointerToNamedFunction(const std::string &Name,
+                                          bool AbortOnFailure = true) {
+    return Base->getPointerToNamedFunction(Name, AbortOnFailure);
+  }
 
   virtual void setMemoryWritable() { Base->setMemoryWritable(); }
   virtual void setMemoryExecutable() { Base->setMemoryExecutable(); }
@@ -115,6 +117,14 @@ public:
       EndFunctionBodyCall(F, FunctionStart, FunctionEnd));
     Base->endFunctionBody(F, FunctionStart, FunctionEnd);
   }
+  virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
+                                       unsigned SectionID) {
+    return Base->allocateDataSection(Size, Alignment, SectionID);
+  }
+  virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
+                                       unsigned SectionID) {
+    return Base->allocateCodeSection(Size, Alignment, SectionID);
+  }
   virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) {
     return Base->allocateSpace(Size, Alignment);
   }
@@ -186,7 +196,7 @@ bool LoadAssemblyInto(Module *M, const char *assembly) {
     NULL != ParseAssemblyString(assembly, M, Error, M->getContext());
   std::string errMsg;
   raw_string_ostream os(errMsg);
-  Error.Print("", os);
+  Error.print("", os);
   EXPECT_TRUE(success) << os.str();
   return success;
 }
@@ -214,6 +224,9 @@ class JITTest : public testing::Test {
   OwningPtr<ExecutionEngine> TheJIT;
 };
 
+// Tests on ARM disabled as we're running the old jit
+#if !defined(__arm__)
+
 // Regression test for a bug.  The JIT used to allocate globals inside the same
 // memory block used for the function, and when the function code was freed,
 // the global was left in the same place.  This test allocates a function
@@ -238,7 +251,7 @@ TEST(JIT, GlobalInFunction) {
   ASSERT_EQ(Error, "");
 
   // Create a global variable.
-  const Type *GTy = Type::getInt32Ty(context);
+  Type *GTy = Type::getInt32Ty(context);
   GlobalVariable *G = new GlobalVariable(
       *M,
       GTy,
@@ -282,10 +295,14 @@ TEST(JIT, GlobalInFunction) {
   EXPECT_EQ(3, *GPtr);
 }
 
+#endif // !defined(__arm__)
+
 int PlusOne(int arg) {
   return arg + 1;
 }
 
+// ARM tests disabled pending fix for PR10783.
+#if !defined(__arm__)
 TEST_F(JITTest, FarCallToKnownFunction) {
   // x86-64 can only make direct calls to functions within 32 bits of
   // the current PC.  To call anything farther away, we have to load
@@ -322,11 +339,11 @@ TEST_F(JITTest, FarCallToKnownFunction) {
 TEST_F(JITTest, NonLazyCompilationStillNeedsStubs) {
   TheJIT->DisableLazyCompilation(true);
 
-  const FunctionType *Func1Ty =
+  FunctionType *Func1Ty =
       cast<FunctionType>(TypeBuilder<void(void), false>::get(Context));
-  std::vector<const Type*> arg_types;
+  std::vector<Type*> arg_types;
   arg_types.push_back(Type::getInt1Ty(Context));
-  const FunctionType *FuncTy = FunctionType::get(
+  FunctionType *FuncTy = FunctionType::get(
       Type::getVoidTy(Context), arg_types, false);
   Function *Func1 = Function::Create(Func1Ty, Function::ExternalLinkage,
                                      "func1", M);
@@ -379,7 +396,7 @@ TEST_F(JITTest, NonLazyLeaksNoStubs) {
   TheJIT->DisableLazyCompilation(true);
 
   // Create two functions with a single basic block each.
-  const FunctionType *FuncTy =
+  FunctionType *FuncTy =
       cast<FunctionType>(TypeBuilder<int(), false>::get(Context));
   Function *Func1 = Function::Create(FuncTy, Function::ExternalLinkage,
                                      "func1", M);
@@ -463,11 +480,13 @@ TEST_F(JITTest, ModuleDeletion) {
   EXPECT_EQ(RJMM->startExceptionTableCalls.size(),
             NumTablesDeallocated);
 }
+#endif // !defined(__arm__)
 
-// ARM and PPC still emit stubs for calls since the target may be too far away
-// to call directly.  This #if can probably be removed when
+// ARM, MIPS and PPC still emit stubs for calls since the target may be
+// too far away to call directly.  This #if can probably be removed when
 // http://llvm.org/PR5201 is fixed.
-#if !defined(__arm__) && !defined(__powerpc__) && !defined(__ppc__)
+#if !defined(__arm__) && !defined(__mips__) && \
+    !defined(__powerpc__) && !defined(__ppc__)
 typedef int (*FooPtr) ();
 
 TEST_F(JITTest, NoStubs) {
@@ -507,6 +526,9 @@ TEST_F(JITTest, NoStubs) {
 }
 #endif  // !ARM && !PPC
 
+// Tests on ARM disabled as we're running the old jit
+#if !defined(__arm__)
+
 TEST_F(JITTest, FunctionPointersOutliveTheirCreator) {
   TheJIT->DisableLazyCompilation(true);
   LoadAssembly("define i8()* @get_foo_addr() { "
@@ -541,8 +563,11 @@ TEST_F(JITTest, FunctionPointersOutliveTheirCreator) {
 #endif
 }
 
-// ARM doesn't have an implementation of replaceMachineCodeForFunction(), so
-// recompileAndRelinkFunction doesn't work.
+#endif //!defined(__arm__)
+
+// ARM does not have an implementation
+// of replaceMachineCodeForFunction(), so recompileAndRelinkFunction
+// doesn't work.
 #if !defined(__arm__)
 TEST_F(JITTest, FunctionIsRecompiledAndRelinked) {
   Function *F = Function::Create(TypeBuilder<int(void), false>::get(Context),
@@ -584,6 +609,9 @@ extern "C" int32_t JITTest_AvailableExternallyGlobal;
 int32_t JITTest_AvailableExternallyGlobal = 42;
 namespace {
 
+// Tests on ARM disabled as we're running the old jit
+#if !defined(__arm__)
+
 TEST_F(JITTest, AvailableExternallyGlobalIsntEmitted) {
   TheJIT->DisableLazyCompilation(true);
   LoadAssembly("@JITTest_AvailableExternallyGlobal = "
@@ -600,7 +628,7 @@ TEST_F(JITTest, AvailableExternallyGlobalIsntEmitted) {
   EXPECT_EQ(42, loader()) << "func should return 42 from the external global,"
                           << " not 7 from the IR version.";
 }
-
+#endif //!defined(__arm__)
 }  // anonymous namespace
 // This function is intentionally defined differently in the statically-compiled
 // program from the IR input to the JIT to assert that the JIT doesn't use its
@@ -610,6 +638,8 @@ extern "C" int32_t JITTest_AvailableExternallyFunction() {
 }
 namespace {
 
+// ARM tests disabled pending fix for PR10783.
+#if !defined(__arm__)
 TEST_F(JITTest, AvailableExternallyFunctionIsntCompiled) {
   TheJIT->DisableLazyCompilation(true);
   LoadAssembly("define available_externally i32 "
@@ -630,31 +660,6 @@ TEST_F(JITTest, AvailableExternallyFunctionIsntCompiled) {
                         << " not 7 from the IR version.";
 }
 
-TEST_F(JITTest, NeedsExactSizeWithManyGlobals) {
-  // PR5291: When the JMM needed the exact size of function bodies before
-  // starting to emit them, the JITEmitter would modify a set while iterating
-  // over it.
-  TheJIT->DisableLazyCompilation(true);
-  RJMM->setSizeRequired(true);
-
-  LoadAssembly("@A = global i32 42 "
-               "@B = global i32* @A "
-               "@C = global i32** @B "
-               "@D = global i32*** @C "
-               "@E = global i32**** @D "
-               "@F = global i32***** @E "
-               "@G = global i32****** @F "
-               "@H = global i32******* @G "
-               "@I = global i32******** @H "
-               "define i32********* @test() { "
-               "  ret i32********* @I "
-               "}");
-  Function *testIR = M->getFunction("test");
-  int32_t********* (*test)() = reinterpret_cast<int32_t*********(*)()>(
-    (intptr_t)TheJIT->getPointerToFunction(testIR));
-  EXPECT_EQ(42, *********test());
-}
-
 TEST_F(JITTest, EscapedLazyStubStillCallable) {
   TheJIT->DisableLazyCompilation(false);
   LoadAssembly("define internal i32 @stubbed() { "
@@ -790,6 +795,7 @@ TEST(LazyLoadedJITTest, EagerCompiledRecursionThroughGhost) {
     (intptr_t)TheJIT->getPointerToFunction(recur1IR));
   EXPECT_EQ(3, recur1(4));
 }
+#endif // !defined(__arm__)
 
 // This code is copied from JITEventListenerTest, but it only runs once for all
 // the tests in this directory.  Everything seems fine, but that's strange