V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
basic-block-instrumentor.cc
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/compiler/basic-block-instrumentor.h"
6 
7 #include <sstream>
8 
9 #include "src/compiler/common-operator.h"
10 #include "src/compiler/graph.h"
11 #include "src/compiler/machine-operator.h"
12 #include "src/compiler/node.h"
13 #include "src/compiler/operator-properties.h"
14 #include "src/compiler/schedule.h"
15 #include "src/objects-inl.h"
16 #include "src/optimized-compilation-info.h"
17 
18 namespace v8 {
19 namespace internal {
20 namespace compiler {
21 
22 // Find the first place to insert new nodes in a block that's already been
23 // scheduled that won't upset the register allocator.
24 static NodeVector::iterator FindInsertionPoint(BasicBlock* block) {
25  NodeVector::iterator i = block->begin();
26  for (; i != block->end(); ++i) {
27  const Operator* op = (*i)->op();
28  if (OperatorProperties::IsBasicBlockBegin(op)) continue;
29  switch (op->opcode()) {
30  case IrOpcode::kParameter:
31  case IrOpcode::kPhi:
32  case IrOpcode::kEffectPhi:
33  continue;
34  }
35  break;
36  }
37  return i;
38 }
39 
40 
41 // TODO(dcarney): need to mark code as non-serializable.
42 static const Operator* PointerConstant(CommonOperatorBuilder* common,
43  intptr_t ptr) {
44  return kPointerSize == 8 ? common->Int64Constant(ptr)
45  : common->Int32Constant(static_cast<int32_t>(ptr));
46 }
47 
48 BasicBlockProfiler::Data* BasicBlockInstrumentor::Instrument(
49  OptimizedCompilationInfo* info, Graph* graph, Schedule* schedule,
50  Isolate* isolate) {
51  // Basic block profiling disables concurrent compilation, so handle deref is
52  // fine.
53  AllowHandleDereference allow_handle_dereference;
54  // Skip the exit block in profiles, since the register allocator can't handle
55  // it and entry into it means falling off the end of the function anyway.
56  size_t n_blocks = static_cast<size_t>(schedule->RpoBlockCount()) - 1;
57  BasicBlockProfiler::Data* data = BasicBlockProfiler::Get()->NewData(n_blocks);
58  // Set the function name.
59  data->SetFunctionName(info->GetDebugName());
60  // Capture the schedule string before instrumentation.
61  {
62  std::ostringstream os;
63  os << *schedule;
64  data->SetSchedule(&os);
65  }
66  // Add the increment instructions to the start of every block.
67  CommonOperatorBuilder common(graph->zone());
68  Node* zero = graph->NewNode(common.Int32Constant(0));
69  Node* one = graph->NewNode(common.Int32Constant(1));
70  MachineOperatorBuilder machine(graph->zone());
71  BasicBlockVector* blocks = schedule->rpo_order();
72  size_t block_number = 0;
73  for (BasicBlockVector::iterator it = blocks->begin(); block_number < n_blocks;
74  ++it, ++block_number) {
75  BasicBlock* block = (*it);
76  data->SetBlockRpoNumber(block_number, block->rpo_number());
77  // TODO(dcarney): wire effect and control deps for load and store.
78  // Construct increment operation.
79  Node* base = graph->NewNode(
80  PointerConstant(&common, data->GetCounterAddress(block_number)));
81  Node* load = graph->NewNode(machine.Load(MachineType::Uint32()), base, zero,
82  graph->start(), graph->start());
83  Node* inc = graph->NewNode(machine.Int32Add(), load, one);
84  Node* store =
85  graph->NewNode(machine.Store(StoreRepresentation(
86  MachineRepresentation::kWord32, kNoWriteBarrier)),
87  base, zero, inc, graph->start(), graph->start());
88  // Insert the new nodes.
89  static const int kArraySize = 6;
90  Node* to_insert[kArraySize] = {zero, one, base, load, inc, store};
91  int insertion_start = block_number == 0 ? 0 : 2;
92  NodeVector::iterator insertion_point = FindInsertionPoint(block);
93  block->InsertNodes(insertion_point, &to_insert[insertion_start],
94  &to_insert[kArraySize]);
95  // Tell the scheduler about the new nodes.
96  for (int i = insertion_start; i < kArraySize; ++i) {
97  schedule->SetBlockForNode(block, to_insert[i]);
98  }
99  }
100  return data;
101 }
102 
103 } // namespace compiler
104 } // namespace internal
105 } // namespace v8
Definition: libplatform.h:13