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/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" 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:
32 case IrOpcode::kEffectPhi:
42 static const Operator* PointerConstant(CommonOperatorBuilder* common,
44 return kPointerSize == 8 ? common->Int64Constant(ptr)
45 : common->Int32Constant(static_cast<int32_t>(ptr));
48 BasicBlockProfiler::Data* BasicBlockInstrumentor::Instrument(
49 OptimizedCompilationInfo* info, Graph* graph, Schedule* schedule,
53 AllowHandleDereference allow_handle_dereference;
56 size_t n_blocks =
static_cast<size_t>(schedule->RpoBlockCount()) - 1;
57 BasicBlockProfiler::Data* data = BasicBlockProfiler::Get()->NewData(n_blocks);
59 data->SetFunctionName(info->GetDebugName());
62 std::ostringstream os;
64 data->SetSchedule(&os);
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());
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);
85 graph->NewNode(machine.Store(StoreRepresentation(
86 MachineRepresentation::kWord32, kNoWriteBarrier)),
87 base, zero, inc, graph->start(), graph->start());
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]);
96 for (
int i = insertion_start;
i < kArraySize; ++
i) {
97 schedule->SetBlockForNode(block, to_insert[
i]);