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