Home | History | Annotate | Download | only in compiler
      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.
      4 
      5 #include "src/compiler/basic-block-instrumentor.h"
      6 
      7 #include <sstream>
      8 
      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"
     16 
     17 namespace v8 {
     18 namespace internal {
     19 namespace compiler {
     20 
     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:
     30       case IrOpcode::kPhi:
     31       case IrOpcode::kEffectPhi:
     32         continue;
     33     }
     34     break;
     35   }
     36   return i;
     37 }
     38 
     39 
     40 // TODO(dcarney): need to mark code as non-serializable.
     41 static const Operator* PointerConstant(CommonOperatorBuilder* common,
     42                                        void* ptr) {
     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)));
     47 }
     48 
     49 
     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->has_shared_info() && info->shared_info()->name()->IsString()) {
     59     std::ostringstream os;
     60     String::cast(info->shared_info()->name())->PrintUC16(os);
     61     data->SetFunctionName(&os);
     62   }
     63   // Capture the schedule string before instrumentation.
     64   {
     65     std::ostringstream os;
     66     os << *schedule;
     67     data->SetSchedule(&os);
     68   }
     69   // Add the increment instructions to the start of every block.
     70   CommonOperatorBuilder common(graph->zone());
     71   Node* zero = graph->NewNode(common.Int32Constant(0));
     72   Node* one = graph->NewNode(common.Int32Constant(1));
     73   MachineOperatorBuilder machine(graph->zone());
     74   BasicBlockVector* blocks = schedule->rpo_order();
     75   size_t block_number = 0;
     76   for (BasicBlockVector::iterator it = blocks->begin(); block_number < n_blocks;
     77        ++it, ++block_number) {
     78     BasicBlock* block = (*it);
     79     data->SetBlockId(block_number, block->id().ToSize());
     80     // TODO(dcarney): wire effect and control deps for load and store.
     81     // Construct increment operation.
     82     Node* base = graph->NewNode(
     83         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         graph->NewNode(machine.Store(StoreRepresentation(
     89                            MachineRepresentation::kWord32, kNoWriteBarrier)),
     90                        base, zero, inc, graph->start(), graph->start());
     91     // Insert the new nodes.
     92     static const int kArraySize = 6;
     93     Node* to_insert[kArraySize] = {zero, one, base, load, inc, store};
     94     int insertion_start = block_number == 0 ? 0 : 2;
     95     NodeVector::iterator insertion_point = FindInsertionPoint(block);
     96     block->InsertNodes(insertion_point, &to_insert[insertion_start],
     97                        &to_insert[kArraySize]);
     98     // Tell the scheduler about the new nodes.
     99     for (int i = insertion_start; i < kArraySize; ++i) {
    100       schedule->SetBlockForNode(block, to_insert[i]);
    101     }
    102   }
    103   return data;
    104 }
    105 
    106 }  // namespace compiler
    107 }  // namespace internal
    108 }  // namespace v8
    109