test-basic-block-profiler.cc 2.7 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "src/diagnostics/basic-block-profiler.h"
6
#include "src/objects/objects-inl.h"
7 8 9
#include "test/cctest/cctest.h"
#include "test/cctest/compiler/codegen-tester.h"

10 11 12
namespace v8 {
namespace internal {
namespace compiler {
13 14 15

class BasicBlockProfilerTest : public RawMachineAssemblerTester<int32_t> {
 public:
16 17
  BasicBlockProfilerTest()
      : RawMachineAssemblerTester<int32_t>(MachineType::Int32()) {
18 19 20
    FLAG_turbo_profiling = true;
  }

21
  void ResetCounts() { BasicBlockProfiler::Get()->ResetCounts(); }
22 23 24

  void Expect(size_t size, uint32_t* expected) {
    const BasicBlockProfiler::DataList* l =
25
        BasicBlockProfiler::Get()->data_list();
26 27 28 29 30 31 32 33 34 35 36 37 38 39
    CHECK_NE(0, static_cast<int>(l->size()));
    const BasicBlockProfiler::Data* data = l->back();
    CHECK_EQ(static_cast<int>(size), static_cast<int>(data->n_blocks()));
    const uint32_t* counts = data->counts();
    for (size_t i = 0; i < size; ++i) {
      CHECK_EQ(static_cast<int>(expected[i]), static_cast<int>(counts[i]));
    }
  }
};


TEST(ProfileDiamond) {
  BasicBlockProfilerTest m;

40
  RawMachineLabel blocka, blockb, end;
41 42 43 44 45 46 47 48 49 50
  m.Branch(m.Parameter(0), &blocka, &blockb);
  m.Bind(&blocka);
  m.Goto(&end);
  m.Bind(&blockb);
  m.Goto(&end);
  m.Bind(&end);
  m.Return(m.Int32Constant(0));

  m.GenerateCode();
  {
51
    uint32_t expected[] = {0, 0, 0, 0, 0, 0};
52 53 54 55 56
    m.Expect(arraysize(expected), expected);
  }

  m.Call(0);
  {
57
    uint32_t expected[] = {1, 1, 1, 0, 0, 1};
58 59 60 61 62 63 64
    m.Expect(arraysize(expected), expected);
  }

  m.ResetCounts();

  m.Call(1);
  {
65
    uint32_t expected[] = {1, 0, 0, 1, 1, 1};
66 67 68 69 70
    m.Expect(arraysize(expected), expected);
  }

  m.Call(0);
  {
71
    uint32_t expected[] = {2, 1, 1, 1, 1, 2};
72 73 74 75 76 77 78 79
    m.Expect(arraysize(expected), expected);
  }
}


TEST(ProfileLoop) {
  BasicBlockProfilerTest m;

80
  RawMachineLabel header, body, end;
81 82 83 84
  Node* one = m.Int32Constant(1);
  m.Goto(&header);

  m.Bind(&header);
85
  Node* count = m.Phi(MachineRepresentation::kWord32, m.Parameter(0), one);
86 87 88 89 90 91 92 93 94 95 96
  m.Branch(count, &body, &end);

  m.Bind(&body);
  count->ReplaceInput(1, m.Int32Sub(count, one));
  m.Goto(&header);

  m.Bind(&end);
  m.Return(one);

  m.GenerateCode();
  {
97
    uint32_t expected[] = {0, 0, 0, 0, 0, 0};
98 99 100 101 102 103 104
    m.Expect(arraysize(expected), expected);
  }

  uint32_t runs[] = {0, 1, 500, 10000};
  for (size_t i = 0; i < arraysize(runs); i++) {
    m.ResetCounts();
    CHECK_EQ(1, m.Call(static_cast<int>(runs[i])));
105
    uint32_t expected[] = {1, runs[i] + 1, runs[i], runs[i], 1, 1};
106 107 108
    m.Expect(arraysize(expected), expected);
  }
}
109 110 111 112

}  // namespace compiler
}  // namespace internal
}  // namespace v8