Chromium Code Reviews| Index: src/compiler/basic-block-instrumentor.cc |
| diff --git a/src/compiler/basic-block-instrumentor.cc b/src/compiler/basic-block-instrumentor.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c1486a1b9ac336e9cb63bca065ba7555effd356c |
| --- /dev/null |
| +++ b/src/compiler/basic-block-instrumentor.cc |
| @@ -0,0 +1,104 @@ |
| +// Copyright 2014 the V8 project authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "src/compiler/basic-block-instrumentor.h" |
| +#include "src/compiler/common-operator.h" |
| +#include "src/compiler/graph.h" |
| +#include "src/compiler/machine-operator.h" |
| +#include "src/compiler/operator-properties-inl.h" |
| +#include "src/compiler/schedule.h" |
| + |
| +namespace v8 { |
| +namespace internal { |
| +namespace compiler { |
| + |
| +static NodeVector::iterator FindInsertionPoint(NodeVector* nodes) { |
|
titzer
2014/09/24 14:59:31
Comment me.
dcarney
2014/09/25 08:15:15
Done.
|
| + NodeVector::iterator i = nodes->begin(); |
| + for (; i != nodes->end(); ++i) { |
| + const Operator* op = (*i)->op(); |
| + if (OperatorProperties::IsBasicBlockBegin(op)) continue; |
| + switch (op->opcode()) { |
| + case IrOpcode::kParameter: |
| + case IrOpcode::kPhi: |
| + case IrOpcode::kEffectPhi: |
| + continue; |
| + } |
| + break; |
| + } |
| + return i; |
| +} |
| + |
| + |
| +BasicBlockProfiler::Data* BasicBlockInstrumentor::Instrument( |
| + CompilationInfo* info, Graph* graph, Schedule* schedule) { |
| + // Apply filter. |
| + if (!info->closure().is_null() && |
| + !info->closure()->PassesFilter(FLAG_turbo_profiling_filter)) { |
| + return NULL; |
| + } |
| + size_t n_blocks = static_cast<size_t>(schedule->RpoBlockCount()); |
| + BasicBlockProfiler::Data* data = |
| + info->isolate()->GetOrCreateBasicBlockProfiler()->NewData(n_blocks); |
| + // Set the function name. |
| + if (!info->shared_info().is_null() && |
| + info->shared_info()->name()->IsString()) { |
| + OStringStream os; |
| + String::cast(info->shared_info()->name())->PrintUC16(os); |
| + data->SetFunctionName(&os); |
| + } |
| + // Capture the schedule string before instrumentation. |
| + { |
| + OStringStream os; |
| + os << *schedule; |
| + data->SetSchedule(&os); |
| + } |
| + // Add the increment instructions to the start of every block. |
| + CommonOperatorBuilder common(graph->zone()); |
| + uint32_t* counts = data->counts(); |
| + intptr_t base_address = reinterpret_cast<intptr_t>(counts); |
| + // TODO(dcarney): need to mark code as non-serializable. |
| + const Operator* base_op = |
| + kPointerSize == 8 ? common.Int64Constant(base_address) |
| + : common.Int32Constant(static_cast<int>(base_address)); |
| + Node* base = graph->NewNode(base_op); |
| + Node* one = graph->NewNode(common.Int32Constant(1)); |
| + MachineOperatorBuilder machine; |
| + BasicBlockVector* blocks = schedule->rpo_order(); |
| + int block_number = 0; |
| + int* block_ids = data->block_ids(); |
| + for (BasicBlockVector::iterator it = blocks->begin(); it != blocks->end(); |
| + ++it, ++block_number) { |
| + BasicBlock* block = (*it); |
|
titzer
2014/09/24 14:59:31
Pull me out into a separate function
dcarney
2014/09/25 08:15:14
that would be ugly
|
| + block_ids[block_number] = block->id(); |
| + // Don't instrument exit block. Register allocator doesn't like this. |
| + if (static_cast<size_t>(block_number) == n_blocks - 1) { |
| + continue; |
| + } |
| + // Construct increment operation. |
| + int index_value = sizeof(counts[0]) * block_number; |
| + Node* index = graph->NewNode(common.Int32Constant(index_value)); |
| + // TODO(dcarney): wire effect and control deps for load and store. |
| + Node* load = graph->NewNode(machine.Load(kMachUint32), base, index); |
|
titzer
2014/09/24 14:59:31
I'd just smash in the direct pointer into the midd
dcarney
2014/09/25 08:15:14
Done.
|
| + Node* inc = graph->NewNode(machine.Int32Add(), load, one); |
| + Node* store = graph->NewNode( |
| + machine.Store(StoreRepresentation(kMachUint32, kNoWriteBarrier)), base, |
| + index, inc); |
| + // skip parameters and phis when inserting. |
| + static const int kArraySize = 6; |
| + Node* to_insert[kArraySize] = {base, one, index, load, inc, store}; |
| + int insertion_start = block_number == 0 ? 0 : 2; |
| + NodeVector* nodes = &block->nodes_; |
| + NodeVector::iterator insertion_point = FindInsertionPoint(nodes); |
| + nodes->insert(insertion_point, &to_insert[insertion_start], |
| + &to_insert[kArraySize]); |
| + for (int i = insertion_start; i < kArraySize; ++i) { |
| + schedule->SetBlockForNode(block, to_insert[i]); |
| + } |
| + } |
| + return data; |
| +} |
| + |
| +} // namespace compiler |
| +} // namespace internal |
| +} // namespace v8 |