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/compilation-info.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 #include "src/objects-inl.h" 17 18 namespace v8 { 19 namespace internal { 20 namespace compiler { 21 22 // Find the first place to insert new nodes in a block that's already been 23 // scheduled that won't upset the register allocator. 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: 31 case IrOpcode::kPhi: 32 case IrOpcode::kEffectPhi: 33 continue; 34 } 35 break; 36 } 37 return i; 38 } 39 40 41 // TODO(dcarney): need to mark code as non-serializable. 42 static const Operator* PointerConstant(CommonOperatorBuilder* common, 43 void* ptr) { 44 return kPointerSize == 8 45 ? common->Int64Constant(reinterpret_cast<intptr_t>(ptr)) 46 : common->Int32Constant( 47 static_cast<int32_t>(reinterpret_cast<intptr_t>(ptr))); 48 } 49 50 51 BasicBlockProfiler::Data* BasicBlockInstrumentor::Instrument( 52 CompilationInfo* info, Graph* graph, Schedule* schedule) { 53 // Skip the exit block in profiles, since the register allocator can't handle 54 // it and entry into it means falling off the end of the function anyway. 55 size_t n_blocks = static_cast<size_t>(schedule->RpoBlockCount()) - 1; 56 BasicBlockProfiler::Data* data = 57 info->isolate()->GetOrCreateBasicBlockProfiler()->NewData(n_blocks); 58 // Set the function name. 59 if (info->has_shared_info() && info->shared_info()->name()->IsString()) { 60 std::ostringstream os; 61 String::cast(info->shared_info()->name())->PrintUC16(os); 62 data->SetFunctionName(&os); 63 } 64 // Capture the schedule string before instrumentation. 65 { 66 std::ostringstream os; 67 os << *schedule; 68 data->SetSchedule(&os); 69 } 70 // Add the increment instructions to the start of every block. 71 CommonOperatorBuilder common(graph->zone()); 72 Node* zero = graph->NewNode(common.Int32Constant(0)); 73 Node* one = graph->NewNode(common.Int32Constant(1)); 74 MachineOperatorBuilder machine(graph->zone()); 75 BasicBlockVector* blocks = schedule->rpo_order(); 76 size_t block_number = 0; 77 for (BasicBlockVector::iterator it = blocks->begin(); block_number < n_blocks; 78 ++it, ++block_number) { 79 BasicBlock* block = (*it); 80 data->SetBlockId(block_number, block->id().ToSize()); 81 // TODO(dcarney): wire effect and control deps for load and store. 82 // Construct increment operation. 83 Node* base = graph->NewNode( 84 PointerConstant(&common, data->GetCounterAddress(block_number))); 85 Node* load = graph->NewNode(machine.Load(MachineType::Uint32()), base, zero, 86 graph->start(), graph->start()); 87 Node* inc = graph->NewNode(machine.Int32Add(), load, one); 88 Node* store = 89 graph->NewNode(machine.Store(StoreRepresentation( 90 MachineRepresentation::kWord32, kNoWriteBarrier)), 91 base, zero, inc, graph->start(), graph->start()); 92 // Insert the new nodes. 93 static const int kArraySize = 6; 94 Node* to_insert[kArraySize] = {zero, one, base, load, inc, store}; 95 int insertion_start = block_number == 0 ? 0 : 2; 96 NodeVector::iterator insertion_point = FindInsertionPoint(block); 97 block->InsertNodes(insertion_point, &to_insert[insertion_start], 98 &to_insert[kArraySize]); 99 // Tell the scheduler about the new nodes. 100 for (int i = insertion_start; i < kArraySize; ++i) { 101 schedule->SetBlockForNode(block, to_insert[i]); 102 } 103 } 104 return data; 105 } 106 107 } // namespace compiler 108 } // namespace internal 109 } // namespace v8 110