AVX-512: changes in intel_ocl_bi calling conventions
[oota-llvm.git] / unittests / ProfileData / InstrProfTest.cpp
1 //===- unittest/ProfileData/InstrProfTest.cpp -------------------------------=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/ProfileData/InstrProfReader.h"
11 #include "llvm/ProfileData/InstrProfWriter.h"
12 #include "gtest/gtest.h"
13
14 #include <cstdarg>
15
16 using namespace llvm;
17
18 namespace {
19
20 struct InstrProfTest : ::testing::Test {
21   InstrProfWriter Writer;
22   std::unique_ptr<IndexedInstrProfReader> Reader;
23
24   void addCounts(StringRef Name, uint64_t Hash, int NumCounts, ...) {
25     SmallVector<uint64_t, 8> Counts;
26     va_list Args;
27     va_start(Args, NumCounts);
28     for (int I = 0; I < NumCounts; ++I)
29       Counts.push_back(va_arg(Args, uint64_t));
30     va_end(Args);
31     Writer.addFunctionCounts(Name, Hash, Counts);
32   }
33
34   std::string writeProfile() { return Writer.writeString(); }
35   void readProfile(std::string Profile) {
36     auto ReaderOrErr =
37         IndexedInstrProfReader::create(MemoryBuffer::getMemBuffer(Profile));
38     ASSERT_EQ(std::error_code(), ReaderOrErr.getError());
39     Reader = std::move(ReaderOrErr.get());
40   }
41 };
42
43 TEST_F(InstrProfTest, write_and_read_empty_profile) {
44   std::string Profile = writeProfile();
45   readProfile(Profile);
46   ASSERT_TRUE(Reader->begin() == Reader->end());
47 }
48
49 TEST_F(InstrProfTest, write_and_read_one_function) {
50   addCounts("foo", 0x1234, 4, 1ULL, 2ULL, 3ULL, 4ULL);
51   std::string Profile = writeProfile();
52   readProfile(Profile);
53
54   auto I = Reader->begin(), E = Reader->end();
55   ASSERT_TRUE(I != E);
56   ASSERT_EQ(StringRef("foo"), I->Name);
57   ASSERT_EQ(0x1234U, I->Hash);
58   ASSERT_EQ(4U, I->Counts.size());
59   ASSERT_EQ(1U, I->Counts[0]);
60   ASSERT_EQ(2U, I->Counts[1]);
61   ASSERT_EQ(3U, I->Counts[2]);
62   ASSERT_EQ(4U, I->Counts[3]);
63   ASSERT_TRUE(++I == E);
64 }
65
66 TEST_F(InstrProfTest, get_function_counts) {
67   addCounts("foo", 0x1234, 2, 1ULL, 2ULL);
68   std::string Profile = writeProfile();
69   readProfile(Profile);
70
71   std::vector<uint64_t> Counts;
72   std::error_code EC;
73
74   EC = Reader->getFunctionCounts("foo", 0x1234, Counts);
75   ASSERT_EQ(instrprof_error::success, EC);
76   ASSERT_EQ(2U, Counts.size());
77   ASSERT_EQ(1U, Counts[0]);
78   ASSERT_EQ(2U, Counts[1]);
79
80   EC = Reader->getFunctionCounts("foo", 0x5678, Counts);
81   ASSERT_EQ(instrprof_error::hash_mismatch, EC);
82
83   EC = Reader->getFunctionCounts("bar", 0x1234, Counts);
84   ASSERT_EQ(instrprof_error::unknown_function, EC);
85 }
86
87 TEST_F(InstrProfTest, get_max_function_count) {
88   addCounts("foo", 0x1234, 2, 1ULL << 31, 2ULL);
89   addCounts("bar", 0, 1, 1ULL << 63);
90   addCounts("baz", 0x5678, 4, 0ULL, 0ULL, 0ULL, 0ULL);
91   std::string Profile = writeProfile();
92   readProfile(Profile);
93
94   ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
95 }
96
97 } // end anonymous namespace