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