basic-block-instrumentor.cc 4.07 KB
Newer Older
1 2 3 4 5
// 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"
6 7 8

#include <sstream>

9
#include "src/compiler.h"
10 11 12
#include "src/compiler/common-operator.h"
#include "src/compiler/graph.h"
#include "src/compiler/machine-operator.h"
13
#include "src/compiler/node.h"
14
#include "src/compiler/operator-properties.h"
15 16 17 18 19 20 21 22
#include "src/compiler/schedule.h"

namespace v8 {
namespace internal {
namespace compiler {

// Find the first place to insert new nodes in a block that's already been
// scheduled that won't upset the register allocator.
23 24 25
static NodeVector::iterator FindInsertionPoint(BasicBlock* block) {
  NodeVector::iterator i = block->begin();
  for (; i != block->end(); ++i) {
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    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;
}


// TODO(dcarney): need to mark code as non-serializable.
static const Operator* PointerConstant(CommonOperatorBuilder* common,
                                       void* ptr) {
  return kPointerSize == 8
             ? common->Int64Constant(reinterpret_cast<intptr_t>(ptr))
             : common->Int32Constant(
                   static_cast<int32_t>(reinterpret_cast<intptr_t>(ptr)));
}


BasicBlockProfiler::Data* BasicBlockInstrumentor::Instrument(
    CompilationInfo* info, Graph* graph, Schedule* schedule) {
  // Skip the exit block in profiles, since the register allocator can't handle
  // it and entry into it means falling off the end of the function anyway.
  size_t n_blocks = static_cast<size_t>(schedule->RpoBlockCount()) - 1;
  BasicBlockProfiler::Data* data =
      info->isolate()->GetOrCreateBasicBlockProfiler()->NewData(n_blocks);
  // Set the function name.
58
  if (info->has_shared_info() && info->shared_info()->name()->IsString()) {
59
    std::ostringstream os;
60 61 62 63 64
    String::cast(info->shared_info()->name())->PrintUC16(os);
    data->SetFunctionName(&os);
  }
  // Capture the schedule string before instrumentation.
  {
65
    std::ostringstream os;
66 67 68 69 70 71 72
    os << *schedule;
    data->SetSchedule(&os);
  }
  // Add the increment instructions to the start of every block.
  CommonOperatorBuilder common(graph->zone());
  Node* zero = graph->NewNode(common.Int32Constant(0));
  Node* one = graph->NewNode(common.Int32Constant(1));
73
  MachineOperatorBuilder machine(graph->zone());
74 75 76 77 78
  BasicBlockVector* blocks = schedule->rpo_order();
  size_t block_number = 0;
  for (BasicBlockVector::iterator it = blocks->begin(); block_number < n_blocks;
       ++it, ++block_number) {
    BasicBlock* block = (*it);
79
    data->SetBlockId(block_number, block->id().ToSize());
80 81 82 83
    // TODO(dcarney): wire effect and control deps for load and store.
    // Construct increment operation.
    Node* base = graph->NewNode(
        PointerConstant(&common, data->GetCounterAddress(block_number)));
84
    Node* load = graph->NewNode(machine.Load(MachineType::Uint32()), base, zero,
85
                                graph->start(), graph->start());
86
    Node* inc = graph->NewNode(machine.Int32Add(), load, one);
87
    Node* store =
88 89
        graph->NewNode(machine.Store(StoreRepresentation(
                           MachineRepresentation::kWord32, kNoWriteBarrier)),
90
                       base, zero, inc, graph->start(), graph->start());
91 92 93 94
    // Insert the new nodes.
    static const int kArraySize = 6;
    Node* to_insert[kArraySize] = {zero, one, base, load, inc, store};
    int insertion_start = block_number == 0 ? 0 : 2;
95 96 97
    NodeVector::iterator insertion_point = FindInsertionPoint(block);
    block->InsertNodes(insertion_point, &to_insert[insertion_start],
                       &to_insert[kArraySize]);
98 99 100 101 102 103 104 105 106 107 108
    // Tell the scheduler about the new nodes.
    for (int i = insertion_start; i < kArraySize; ++i) {
      schedule->SetBlockForNode(block, to_insert[i]);
    }
  }
  return data;
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8