Add mfasr and mtasr
[oota-llvm.git] / unittests / ExecutionEngine / JIT / JITMemoryManagerTest.cpp
index d56a52a43081c636b9c36079d5f8d810b841766b..296838de61b353db582695bc4ab4f4d1b9b23bcf 100644 (file)
@@ -7,20 +7,22 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "gtest/gtest.h"
-#include "llvm/ADT/OwningPtr.h"
 #include "llvm/ExecutionEngine/JITMemoryManager.h"
-#include "llvm/DerivedTypes.h"
-#include "llvm/Function.h"
-#include "llvm/GlobalValue.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/GlobalValue.h"
+#include "llvm/IR/LLVMContext.h"
+#include "gtest/gtest.h"
 
 using namespace llvm;
 
 namespace {
 
 Function *makeFakeFunction() {
-  std::vector<const Type*> params;
-  const FunctionType *FTy = FunctionType::get(Type::VoidTy, params, false);
+  std::vector<Type*> params;
+  FunctionType *FTy =
+      FunctionType::get(Type::getVoidTy(getGlobalContext()), params, false);
   return Function::Create(FTy, GlobalValue::ExternalLinkage);
 }
 
@@ -28,50 +30,48 @@ Function *makeFakeFunction() {
 // the code in the case that we don't have to allocate more memory to store the
 // function bodies.
 TEST(JITMemoryManagerTest, NoAllocations) {
-  OwningPtr<JITMemoryManager> MemMgr(
+  std::unique_ptr<JITMemoryManager> MemMgr(
       JITMemoryManager::CreateDefaultMemManager());
   uintptr_t size;
-  uint8_t *start;
   std::string Error;
 
   // Allocate the functions.
-  OwningPtr<Function> F1(makeFakeFunction());
+  std::unique_ptr<Function> F1(makeFakeFunction());
   size = 1024;
-  start = MemMgr->startFunctionBody(F1.get(), size);
-  memset(start, 0xFF, 1024);
-  MemMgr->endFunctionBody(F1.get(), start, start + 1024);
+  uint8_t *FunctionBody1 = MemMgr->startFunctionBody(F1.get(), size);
+  memset(FunctionBody1, 0xFF, 1024);
+  MemMgr->endFunctionBody(F1.get(), FunctionBody1, FunctionBody1 + 1024);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
 
-  OwningPtr<Function> F2(makeFakeFunction());
+  std::unique_ptr<Function> F2(makeFakeFunction());
   size = 1024;
-  start = MemMgr->startFunctionBody(F2.get(), size);
-  memset(start, 0xFF, 1024);
-  MemMgr->endFunctionBody(F2.get(), start, start + 1024);
+  uint8_t *FunctionBody2 = MemMgr->startFunctionBody(F2.get(), size);
+  memset(FunctionBody2, 0xFF, 1024);
+  MemMgr->endFunctionBody(F2.get(), FunctionBody2, FunctionBody2 + 1024);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
 
-  OwningPtr<Function> F3(makeFakeFunction());
+  std::unique_ptr<Function> F3(makeFakeFunction());
   size = 1024;
-  start = MemMgr->startFunctionBody(F3.get(), size);
-  memset(start, 0xFF, 1024);
-  MemMgr->endFunctionBody(F3.get(), start, start + 1024);
+  uint8_t *FunctionBody3 = MemMgr->startFunctionBody(F3.get(), size);
+  memset(FunctionBody3, 0xFF, 1024);
+  MemMgr->endFunctionBody(F3.get(), FunctionBody3, FunctionBody3 + 1024);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
 
   // Deallocate them out of order, in case that matters.
-  MemMgr->deallocateMemForFunction(F2.get());
+  MemMgr->deallocateFunctionBody(FunctionBody2);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
-  MemMgr->deallocateMemForFunction(F1.get());
+  MemMgr->deallocateFunctionBody(FunctionBody1);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
-  MemMgr->deallocateMemForFunction(F3.get());
+  MemMgr->deallocateFunctionBody(FunctionBody3);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
 }
 
 // Make three large functions that take up most of the space in the slab.  Then
 // try allocating three smaller functions that don't require additional slabs.
 TEST(JITMemoryManagerTest, TestCodeAllocation) {
-  OwningPtr<JITMemoryManager> MemMgr(
+  std::unique_ptr<JITMemoryManager> MemMgr(
       JITMemoryManager::CreateDefaultMemManager());
   uintptr_t size;
-  uint8_t *start;
   std::string Error;
 
   // Big functions are a little less than the largest block size.
@@ -80,80 +80,83 @@ TEST(JITMemoryManagerTest, TestCodeAllocation) {
                                  smallFuncSize * 2);
 
   // Allocate big functions
-  OwningPtr<Function> F1(makeFakeFunction());
+  std::unique_ptr<Function> F1(makeFakeFunction());
   size = bigFuncSize;
-  start = MemMgr->startFunctionBody(F1.get(), size);
+  uint8_t *FunctionBody1 = MemMgr->startFunctionBody(F1.get(), size);
   ASSERT_LE(bigFuncSize, size);
-  memset(start, 0xFF, bigFuncSize);
-  MemMgr->endFunctionBody(F1.get(), start, start + bigFuncSize);
+  memset(FunctionBody1, 0xFF, bigFuncSize);
+  MemMgr->endFunctionBody(F1.get(), FunctionBody1, FunctionBody1 + bigFuncSize);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
 
-  OwningPtr<Function> F2(makeFakeFunction());
+  std::unique_ptr<Function> F2(makeFakeFunction());
   size = bigFuncSize;
-  start = MemMgr->startFunctionBody(F2.get(), size);
+  uint8_t *FunctionBody2 = MemMgr->startFunctionBody(F2.get(), size);
   ASSERT_LE(bigFuncSize, size);
-  memset(start, 0xFF, bigFuncSize);
-  MemMgr->endFunctionBody(F2.get(), start, start + bigFuncSize);
+  memset(FunctionBody2, 0xFF, bigFuncSize);
+  MemMgr->endFunctionBody(F2.get(), FunctionBody2, FunctionBody2 + bigFuncSize);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
 
-  OwningPtr<Function> F3(makeFakeFunction());
+  std::unique_ptr<Function> F3(makeFakeFunction());
   size = bigFuncSize;
-  start = MemMgr->startFunctionBody(F3.get(), size);
+  uint8_t *FunctionBody3 = MemMgr->startFunctionBody(F3.get(), size);
   ASSERT_LE(bigFuncSize, size);
-  memset(start, 0xFF, bigFuncSize);
-  MemMgr->endFunctionBody(F3.get(), start, start + bigFuncSize);
+  memset(FunctionBody3, 0xFF, bigFuncSize);
+  MemMgr->endFunctionBody(F3.get(), FunctionBody3, FunctionBody3 + bigFuncSize);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
 
   // Check that each large function took it's own slab.
   EXPECT_EQ(3U, MemMgr->GetNumCodeSlabs());
 
   // Allocate small functions
-  OwningPtr<Function> F4(makeFakeFunction());
+  std::unique_ptr<Function> F4(makeFakeFunction());
   size = smallFuncSize;
-  start = MemMgr->startFunctionBody(F4.get(), size);
+  uint8_t *FunctionBody4 = MemMgr->startFunctionBody(F4.get(), size);
   ASSERT_LE(smallFuncSize, size);
-  memset(start, 0xFF, smallFuncSize);
-  MemMgr->endFunctionBody(F4.get(), start, start + smallFuncSize);
+  memset(FunctionBody4, 0xFF, smallFuncSize);
+  MemMgr->endFunctionBody(F4.get(), FunctionBody4,
+                          FunctionBody4 + smallFuncSize);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
 
-  OwningPtr<Function> F5(makeFakeFunction());
+  std::unique_ptr<Function> F5(makeFakeFunction());
   size = smallFuncSize;
-  start = MemMgr->startFunctionBody(F5.get(), size);
+  uint8_t *FunctionBody5 = MemMgr->startFunctionBody(F5.get(), size);
   ASSERT_LE(smallFuncSize, size);
-  memset(start, 0xFF, smallFuncSize);
-  MemMgr->endFunctionBody(F5.get(), start, start + smallFuncSize);
+  memset(FunctionBody5, 0xFF, smallFuncSize);
+  MemMgr->endFunctionBody(F5.get(), FunctionBody5,
+                          FunctionBody5 + smallFuncSize);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
 
-  OwningPtr<Function> F6(makeFakeFunction());
+  std::unique_ptr<Function> F6(makeFakeFunction());
   size = smallFuncSize;
-  start = MemMgr->startFunctionBody(F6.get(), size);
+  uint8_t *FunctionBody6 = MemMgr->startFunctionBody(F6.get(), size);
   ASSERT_LE(smallFuncSize, size);
-  memset(start, 0xFF, smallFuncSize);
-  MemMgr->endFunctionBody(F6.get(), start, start + smallFuncSize);
+  memset(FunctionBody6, 0xFF, smallFuncSize);
+  MemMgr->endFunctionBody(F6.get(), FunctionBody6,
+                          FunctionBody6 + smallFuncSize);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
 
   // Check that the small functions didn't allocate any new slabs.
   EXPECT_EQ(3U, MemMgr->GetNumCodeSlabs());
 
   // Deallocate them out of order, in case that matters.
-  MemMgr->deallocateMemForFunction(F2.get());
+  MemMgr->deallocateFunctionBody(FunctionBody2);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
-  MemMgr->deallocateMemForFunction(F1.get());
+  MemMgr->deallocateFunctionBody(FunctionBody1);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
-  MemMgr->deallocateMemForFunction(F4.get());
+  MemMgr->deallocateFunctionBody(FunctionBody4);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
-  MemMgr->deallocateMemForFunction(F3.get());
+  MemMgr->deallocateFunctionBody(FunctionBody3);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
-  MemMgr->deallocateMemForFunction(F5.get());
+  MemMgr->deallocateFunctionBody(FunctionBody5);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
-  MemMgr->deallocateMemForFunction(F6.get());
+  MemMgr->deallocateFunctionBody(FunctionBody6);
   EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
 }
 
 // Allocate five global ints of varying widths and alignment, and check their
 // alignment and overlap.
 TEST(JITMemoryManagerTest, TestSmallGlobalInts) {
-  OwningPtr<JITMemoryManager> MemMgr(
+  std::unique_ptr<JITMemoryManager> MemMgr(
       JITMemoryManager::CreateDefaultMemManager());
   uint8_t  *a = (uint8_t *)MemMgr->allocateGlobal(8,  0);
   uint16_t *b = (uint16_t*)MemMgr->allocateGlobal(16, 2);
@@ -200,7 +203,7 @@ TEST(JITMemoryManagerTest, TestSmallGlobalInts) {
 // Allocate a small global, a big global, and a third global, and make sure we
 // only use two slabs for that.
 TEST(JITMemoryManagerTest, TestLargeGlobalArray) {
-  OwningPtr<JITMemoryManager> MemMgr(
+  std::unique_ptr<JITMemoryManager> MemMgr(
       JITMemoryManager::CreateDefaultMemManager());
   size_t Size = 4 * MemMgr->GetDefaultDataSlabSize();
   uint64_t *a = (uint64_t*)MemMgr->allocateGlobal(64, 8);
@@ -230,14 +233,14 @@ TEST(JITMemoryManagerTest, TestLargeGlobalArray) {
 // Allocate lots of medium globals so that we can test moving the bump allocator
 // to a new slab.
 TEST(JITMemoryManagerTest, TestManyGlobals) {
-  OwningPtr<JITMemoryManager> MemMgr(
+  std::unique_ptr<JITMemoryManager> MemMgr(
       JITMemoryManager::CreateDefaultMemManager());
   size_t SlabSize = MemMgr->GetDefaultDataSlabSize();
   size_t Size = 128;
   int Iters = (SlabSize / Size) + 1;
 
-  // We should start with one slab.
-  EXPECT_EQ(1U, MemMgr->GetNumDataSlabs());
+  // We should start with no slabs.
+  EXPECT_EQ(0U, MemMgr->GetNumDataSlabs());
 
   // After allocating a bunch of globals, we should have two.
   for (int I = 0; I < Iters; ++I)
@@ -253,24 +256,47 @@ TEST(JITMemoryManagerTest, TestManyGlobals) {
 // Allocate lots of function stubs so that we can test moving the stub bump
 // allocator to a new slab.
 TEST(JITMemoryManagerTest, TestManyStubs) {
-  OwningPtr<JITMemoryManager> MemMgr(
+  std::unique_ptr<JITMemoryManager> MemMgr(
       JITMemoryManager::CreateDefaultMemManager());
   size_t SlabSize = MemMgr->GetDefaultStubSlabSize();
   size_t Size = 128;
   int Iters = (SlabSize / Size) + 1;
 
-  // We should start with one slab.
-  EXPECT_EQ(1U, MemMgr->GetNumStubSlabs());
+  // We should start with no slabs.
+  EXPECT_EQ(0U, MemMgr->GetNumDataSlabs());
 
   // After allocating a bunch of stubs, we should have two.
   for (int I = 0; I < Iters; ++I)
-    MemMgr->allocateStub(NULL, Size, 8);
+    MemMgr->allocateStub(nullptr, Size, 8);
   EXPECT_EQ(2U, MemMgr->GetNumStubSlabs());
 
   // And after much more, we should have three.
   for (int I = 0; I < Iters; ++I)
-    MemMgr->allocateStub(NULL, Size, 8);
+    MemMgr->allocateStub(nullptr, Size, 8);
   EXPECT_EQ(3U, MemMgr->GetNumStubSlabs());
 }
 
+// Check section allocation and alignment
+TEST(JITMemoryManagerTest, AllocateSection) {
+  std::unique_ptr<JITMemoryManager> MemMgr(
+      JITMemoryManager::CreateDefaultMemManager());
+  uint8_t *code1 = MemMgr->allocateCodeSection(256, 0, 1, StringRef());
+  uint8_t *data1 = MemMgr->allocateDataSection(256, 16, 2, StringRef(), true);
+  uint8_t *code2 = MemMgr->allocateCodeSection(257, 32, 3, StringRef());
+  uint8_t *data2 = MemMgr->allocateDataSection(256, 64, 4, StringRef(), false);
+  uint8_t *code3 = MemMgr->allocateCodeSection(258, 64, 5, StringRef());
+
+  EXPECT_NE((uint8_t*)nullptr, code1);
+  EXPECT_NE((uint8_t*)nullptr, code2);
+  EXPECT_NE((uint8_t*)nullptr, data1);
+  EXPECT_NE((uint8_t*)nullptr, data2);
+
+  // Check alignment
+  EXPECT_EQ((uint64_t)code1 & 0xf, 0u);
+  EXPECT_EQ((uint64_t)code2 & 0x1f, 0u);
+  EXPECT_EQ((uint64_t)code3 & 0x3f, 0u);
+  EXPECT_EQ((uint64_t)data1 & 0xf, 0u);
+  EXPECT_EQ((uint64_t)data2 & 0x3f, 0u);
+}
+
 }