blob: d7d3ade1b2b938aea250169b28ff23e28481f27f [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// 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.h"
10#include "src/compiler/common-operator.h"
11#include "src/compiler/graph.h"
12#include "src/compiler/machine-operator.h"
13#include "src/compiler/operator-properties.h"
14#include "src/compiler/schedule.h"
15
16namespace v8 {
17namespace internal {
18namespace compiler {
19
20// Find the first place to insert new nodes in a block that's already been
21// scheduled that won't upset the register allocator.
22static NodeVector::iterator FindInsertionPoint(BasicBlock* block) {
23 NodeVector::iterator i = block->begin();
24 for (; i != block->end(); ++i) {
25 const Operator* op = (*i)->op();
26 if (OperatorProperties::IsBasicBlockBegin(op)) continue;
27 switch (op->opcode()) {
28 case IrOpcode::kParameter:
29 case IrOpcode::kPhi:
30 case IrOpcode::kEffectPhi:
31 continue;
32 }
33 break;
34 }
35 return i;
36}
37
38
39// TODO(dcarney): need to mark code as non-serializable.
40static const Operator* PointerConstant(CommonOperatorBuilder* common,
41 void* ptr) {
42 return kPointerSize == 8
43 ? common->Int64Constant(reinterpret_cast<intptr_t>(ptr))
44 : common->Int32Constant(
45 static_cast<int32_t>(reinterpret_cast<intptr_t>(ptr)));
46}
47
48
49BasicBlockProfiler::Data* BasicBlockInstrumentor::Instrument(
50 CompilationInfo* info, Graph* graph, Schedule* schedule) {
51 // Skip the exit block in profiles, since the register allocator can't handle
52 // it and entry into it means falling off the end of the function anyway.
53 size_t n_blocks = static_cast<size_t>(schedule->RpoBlockCount()) - 1;
54 BasicBlockProfiler::Data* data =
55 info->isolate()->GetOrCreateBasicBlockProfiler()->NewData(n_blocks);
56 // Set the function name.
57 if (!info->shared_info().is_null() &&
58 info->shared_info()->name()->IsString()) {
59 std::ostringstream os;
60 String::cast(info->shared_info()->name())->PrintUC16(os);
61 data->SetFunctionName(&os);
62 }
63 // Capture the schedule string before instrumentation.
64 {
65 std::ostringstream os;
66 os << *schedule;
67 data->SetSchedule(&os);
68 }
69 // Add the increment instructions to the start of every block.
70 CommonOperatorBuilder common(graph->zone());
71 Node* zero = graph->NewNode(common.Int32Constant(0));
72 Node* one = graph->NewNode(common.Int32Constant(1));
73 MachineOperatorBuilder machine(graph->zone());
74 BasicBlockVector* blocks = schedule->rpo_order();
75 size_t block_number = 0;
76 for (BasicBlockVector::iterator it = blocks->begin(); block_number < n_blocks;
77 ++it, ++block_number) {
78 BasicBlock* block = (*it);
79 data->SetBlockId(block_number, block->id().ToSize());
80 // TODO(dcarney): wire effect and control deps for load and store.
81 // Construct increment operation.
82 Node* base = graph->NewNode(
83 PointerConstant(&common, data->GetCounterAddress(block_number)));
84 Node* load = graph->NewNode(machine.Load(kMachUint32), base, zero);
85 Node* inc = graph->NewNode(machine.Int32Add(), load, one);
86 Node* store = graph->NewNode(
87 machine.Store(StoreRepresentation(kMachUint32, kNoWriteBarrier)), base,
88 zero, inc);
89 // Insert the new nodes.
90 static const int kArraySize = 6;
91 Node* to_insert[kArraySize] = {zero, one, base, load, inc, store};
92 int insertion_start = block_number == 0 ? 0 : 2;
93 NodeVector::iterator insertion_point = FindInsertionPoint(block);
94 block->InsertNodes(insertion_point, &to_insert[insertion_start],
95 &to_insert[kArraySize]);
96 // Tell the scheduler about the new nodes.
97 for (int i = insertion_start; i < kArraySize; ++i) {
98 schedule->SetBlockForNode(block, to_insert[i]);
99 }
100 }
101 return data;
102}
103
104} // namespace compiler
105} // namespace internal
106} // namespace v8