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/raw-machine-assembler.h"
      6 
      7 #include "src/compiler/node-properties.h"
      8 #include "src/compiler/pipeline.h"
      9 #include "src/compiler/scheduler.h"
     10 #include "src/objects-inl.h"
     11 
     12 namespace v8 {
     13 namespace internal {
     14 namespace compiler {
     15 
     16 RawMachineAssembler::RawMachineAssembler(
     17     Isolate* isolate, Graph* graph, CallDescriptor* call_descriptor,
     18     MachineRepresentation word, MachineOperatorBuilder::Flags flags,
     19     MachineOperatorBuilder::AlignmentRequirements alignment_requirements)
     20     : isolate_(isolate),
     21       graph_(graph),
     22       schedule_(new (zone()) Schedule(zone())),
     23       machine_(zone(), word, flags, alignment_requirements),
     24       common_(zone()),
     25       call_descriptor_(call_descriptor),
     26       parameters_(parameter_count(), zone()),
     27       current_block_(schedule()->start()) {
     28   int param_count = static_cast<int>(parameter_count());
     29   // Add an extra input for the JSFunction parameter to the start node.
     30   graph->SetStart(graph->NewNode(common_.Start(param_count + 1)));
     31   for (size_t i = 0; i < parameter_count(); ++i) {
     32     parameters_[i] =
     33         AddNode(common()->Parameter(static_cast<int>(i)), graph->start());
     34   }
     35   graph->SetEnd(graph->NewNode(common_.End(0)));
     36 }
     37 
     38 Node* RawMachineAssembler::RelocatableIntPtrConstant(intptr_t value,
     39                                                      RelocInfo::Mode rmode) {
     40   return kPointerSize == 8
     41              ? RelocatableInt64Constant(value, rmode)
     42              : RelocatableInt32Constant(static_cast<int>(value), rmode);
     43 }
     44 
     45 Schedule* RawMachineAssembler::Export() {
     46   // Compute the correct codegen order.
     47   DCHECK(schedule_->rpo_order()->empty());
     48   OFStream os(stdout);
     49   if (FLAG_trace_turbo_scheduler) {
     50     PrintF("--- RAW SCHEDULE -------------------------------------------\n");
     51     os << *schedule_;
     52   }
     53   schedule_->EnsureCFGWellFormedness();
     54   Scheduler::ComputeSpecialRPO(zone(), schedule_);
     55   schedule_->PropagateDeferredMark();
     56   if (FLAG_trace_turbo_scheduler) {
     57     PrintF("--- EDGE SPLIT AND PROPAGATED DEFERRED SCHEDULE ------------\n");
     58     os << *schedule_;
     59   }
     60   // Invalidate RawMachineAssembler.
     61   Schedule* schedule = schedule_;
     62   schedule_ = nullptr;
     63   return schedule;
     64 }
     65 
     66 
     67 Node* RawMachineAssembler::Parameter(size_t index) {
     68   DCHECK(index < parameter_count());
     69   return parameters_[index];
     70 }
     71 
     72 
     73 void RawMachineAssembler::Goto(RawMachineLabel* label) {
     74   DCHECK(current_block_ != schedule()->end());
     75   schedule()->AddGoto(CurrentBlock(), Use(label));
     76   current_block_ = nullptr;
     77 }
     78 
     79 
     80 void RawMachineAssembler::Branch(Node* condition, RawMachineLabel* true_val,
     81                                  RawMachineLabel* false_val) {
     82   DCHECK(current_block_ != schedule()->end());
     83   Node* branch = MakeNode(common()->Branch(), 1, &condition);
     84   schedule()->AddBranch(CurrentBlock(), branch, Use(true_val), Use(false_val));
     85   current_block_ = nullptr;
     86 }
     87 
     88 void RawMachineAssembler::Continuations(Node* call, RawMachineLabel* if_success,
     89                                         RawMachineLabel* if_exception) {
     90   DCHECK_NOT_NULL(schedule_);
     91   DCHECK_NOT_NULL(current_block_);
     92   schedule()->AddCall(CurrentBlock(), call, Use(if_success), Use(if_exception));
     93   current_block_ = nullptr;
     94 }
     95 
     96 void RawMachineAssembler::Switch(Node* index, RawMachineLabel* default_label,
     97                                  const int32_t* case_values,
     98                                  RawMachineLabel** case_labels,
     99                                  size_t case_count) {
    100   DCHECK_NE(schedule()->end(), current_block_);
    101   size_t succ_count = case_count + 1;
    102   Node* switch_node = AddNode(common()->Switch(succ_count), index);
    103   BasicBlock** succ_blocks = zone()->NewArray<BasicBlock*>(succ_count);
    104   for (size_t index = 0; index < case_count; ++index) {
    105     int32_t case_value = case_values[index];
    106     BasicBlock* case_block = schedule()->NewBasicBlock();
    107     Node* case_node =
    108         graph()->NewNode(common()->IfValue(case_value), switch_node);
    109     schedule()->AddNode(case_block, case_node);
    110     schedule()->AddGoto(case_block, Use(case_labels[index]));
    111     succ_blocks[index] = case_block;
    112   }
    113   BasicBlock* default_block = schedule()->NewBasicBlock();
    114   Node* default_node = graph()->NewNode(common()->IfDefault(), switch_node);
    115   schedule()->AddNode(default_block, default_node);
    116   schedule()->AddGoto(default_block, Use(default_label));
    117   succ_blocks[case_count] = default_block;
    118   schedule()->AddSwitch(CurrentBlock(), switch_node, succ_blocks, succ_count);
    119   current_block_ = nullptr;
    120 }
    121 
    122 void RawMachineAssembler::Return(Node* value) {
    123   Node* values[] = {Int32Constant(0), value};
    124   Node* ret = MakeNode(common()->Return(1), 2, values);
    125   schedule()->AddReturn(CurrentBlock(), ret);
    126   current_block_ = nullptr;
    127 }
    128 
    129 
    130 void RawMachineAssembler::Return(Node* v1, Node* v2) {
    131   Node* values[] = {Int32Constant(0), v1, v2};
    132   Node* ret = MakeNode(common()->Return(2), 3, values);
    133   schedule()->AddReturn(CurrentBlock(), ret);
    134   current_block_ = nullptr;
    135 }
    136 
    137 
    138 void RawMachineAssembler::Return(Node* v1, Node* v2, Node* v3) {
    139   Node* values[] = {Int32Constant(0), v1, v2, v3};
    140   Node* ret = MakeNode(common()->Return(3), 4, values);
    141   schedule()->AddReturn(CurrentBlock(), ret);
    142   current_block_ = nullptr;
    143 }
    144 
    145 void RawMachineAssembler::PopAndReturn(Node* pop, Node* value) {
    146   Node* values[] = {pop, value};
    147   Node* ret = MakeNode(common()->Return(1), 2, values);
    148   schedule()->AddReturn(CurrentBlock(), ret);
    149   current_block_ = nullptr;
    150 }
    151 
    152 void RawMachineAssembler::PopAndReturn(Node* pop, Node* v1, Node* v2) {
    153   Node* values[] = {pop, v1, v2};
    154   Node* ret = MakeNode(common()->Return(2), 3, values);
    155   schedule()->AddReturn(CurrentBlock(), ret);
    156   current_block_ = nullptr;
    157 }
    158 
    159 void RawMachineAssembler::PopAndReturn(Node* pop, Node* v1, Node* v2,
    160                                        Node* v3) {
    161   Node* values[] = {pop, v1, v2, v3};
    162   Node* ret = MakeNode(common()->Return(3), 4, values);
    163   schedule()->AddReturn(CurrentBlock(), ret);
    164   current_block_ = nullptr;
    165 }
    166 
    167 void RawMachineAssembler::DebugBreak() { AddNode(machine()->DebugBreak()); }
    168 
    169 void RawMachineAssembler::Unreachable() {
    170   Node* values[] = {UndefinedConstant()};  // Unused.
    171   Node* ret = MakeNode(common()->Throw(), 1, values);
    172   schedule()->AddThrow(CurrentBlock(), ret);
    173   current_block_ = nullptr;
    174 }
    175 
    176 void RawMachineAssembler::Comment(const char* msg) {
    177   AddNode(machine()->Comment(msg));
    178 }
    179 
    180 Node* RawMachineAssembler::CallN(CallDescriptor* desc, int input_count,
    181                                  Node* const* inputs) {
    182   DCHECK(!desc->NeedsFrameState());
    183   // +1 is for target.
    184   DCHECK_EQ(input_count, desc->ParameterCount() + 1);
    185   return AddNode(common()->Call(desc), input_count, inputs);
    186 }
    187 
    188 Node* RawMachineAssembler::CallNWithFrameState(CallDescriptor* desc,
    189                                                int input_count,
    190                                                Node* const* inputs) {
    191   DCHECK(desc->NeedsFrameState());
    192   // +2 is for target and frame state.
    193   DCHECK_EQ(input_count, desc->ParameterCount() + 2);
    194   return AddNode(common()->Call(desc), input_count, inputs);
    195 }
    196 
    197 Node* RawMachineAssembler::TailCallN(CallDescriptor* desc, int input_count,
    198                                      Node* const* inputs) {
    199   // +1 is for target.
    200   DCHECK_EQ(input_count, desc->ParameterCount() + 1);
    201   Node* tail_call = MakeNode(common()->TailCall(desc), input_count, inputs);
    202   schedule()->AddTailCall(CurrentBlock(), tail_call);
    203   current_block_ = nullptr;
    204   return tail_call;
    205 }
    206 
    207 Node* RawMachineAssembler::CallCFunction0(MachineType return_type,
    208                                           Node* function) {
    209   MachineSignature::Builder builder(zone(), 1, 0);
    210   builder.AddReturn(return_type);
    211   const CallDescriptor* descriptor =
    212       Linkage::GetSimplifiedCDescriptor(zone(), builder.Build());
    213 
    214   return AddNode(common()->Call(descriptor), function);
    215 }
    216 
    217 
    218 Node* RawMachineAssembler::CallCFunction1(MachineType return_type,
    219                                           MachineType arg0_type, Node* function,
    220                                           Node* arg0) {
    221   MachineSignature::Builder builder(zone(), 1, 1);
    222   builder.AddReturn(return_type);
    223   builder.AddParam(arg0_type);
    224   const CallDescriptor* descriptor =
    225       Linkage::GetSimplifiedCDescriptor(zone(), builder.Build());
    226 
    227   return AddNode(common()->Call(descriptor), function, arg0);
    228 }
    229 
    230 
    231 Node* RawMachineAssembler::CallCFunction2(MachineType return_type,
    232                                           MachineType arg0_type,
    233                                           MachineType arg1_type, Node* function,
    234                                           Node* arg0, Node* arg1) {
    235   MachineSignature::Builder builder(zone(), 1, 2);
    236   builder.AddReturn(return_type);
    237   builder.AddParam(arg0_type);
    238   builder.AddParam(arg1_type);
    239   const CallDescriptor* descriptor =
    240       Linkage::GetSimplifiedCDescriptor(zone(), builder.Build());
    241 
    242   return AddNode(common()->Call(descriptor), function, arg0, arg1);
    243 }
    244 
    245 Node* RawMachineAssembler::CallCFunction3(MachineType return_type,
    246                                           MachineType arg0_type,
    247                                           MachineType arg1_type,
    248                                           MachineType arg2_type, Node* function,
    249                                           Node* arg0, Node* arg1, Node* arg2) {
    250   MachineSignature::Builder builder(zone(), 1, 3);
    251   builder.AddReturn(return_type);
    252   builder.AddParam(arg0_type);
    253   builder.AddParam(arg1_type);
    254   builder.AddParam(arg2_type);
    255   const CallDescriptor* descriptor =
    256       Linkage::GetSimplifiedCDescriptor(zone(), builder.Build());
    257 
    258   return AddNode(common()->Call(descriptor), function, arg0, arg1, arg2);
    259 }
    260 
    261 Node* RawMachineAssembler::CallCFunction8(
    262     MachineType return_type, MachineType arg0_type, MachineType arg1_type,
    263     MachineType arg2_type, MachineType arg3_type, MachineType arg4_type,
    264     MachineType arg5_type, MachineType arg6_type, MachineType arg7_type,
    265     Node* function, Node* arg0, Node* arg1, Node* arg2, Node* arg3, Node* arg4,
    266     Node* arg5, Node* arg6, Node* arg7) {
    267   MachineSignature::Builder builder(zone(), 1, 8);
    268   builder.AddReturn(return_type);
    269   builder.AddParam(arg0_type);
    270   builder.AddParam(arg1_type);
    271   builder.AddParam(arg2_type);
    272   builder.AddParam(arg3_type);
    273   builder.AddParam(arg4_type);
    274   builder.AddParam(arg5_type);
    275   builder.AddParam(arg6_type);
    276   builder.AddParam(arg7_type);
    277   Node* args[] = {function, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7};
    278   const CallDescriptor* descriptor =
    279       Linkage::GetSimplifiedCDescriptor(zone(), builder.Build());
    280   return AddNode(common()->Call(descriptor), arraysize(args), args);
    281 }
    282 
    283 
    284 void RawMachineAssembler::Bind(RawMachineLabel* label) {
    285   DCHECK(current_block_ == nullptr);
    286   DCHECK(!label->bound_);
    287   label->bound_ = true;
    288   current_block_ = EnsureBlock(label);
    289   current_block_->set_deferred(label->deferred_);
    290 }
    291 
    292 
    293 BasicBlock* RawMachineAssembler::Use(RawMachineLabel* label) {
    294   label->used_ = true;
    295   return EnsureBlock(label);
    296 }
    297 
    298 
    299 BasicBlock* RawMachineAssembler::EnsureBlock(RawMachineLabel* label) {
    300   if (label->block_ == nullptr) label->block_ = schedule()->NewBasicBlock();
    301   return label->block_;
    302 }
    303 
    304 
    305 BasicBlock* RawMachineAssembler::CurrentBlock() {
    306   DCHECK(current_block_);
    307   return current_block_;
    308 }
    309 
    310 Node* RawMachineAssembler::Phi(MachineRepresentation rep, int input_count,
    311                                Node* const* inputs) {
    312   Node** buffer = new (zone()->New(sizeof(Node*) * (input_count + 1)))
    313       Node*[input_count + 1];
    314   std::copy(inputs, inputs + input_count, buffer);
    315   buffer[input_count] = graph()->start();
    316   return AddNode(common()->Phi(rep, input_count), input_count + 1, buffer);
    317 }
    318 
    319 void RawMachineAssembler::AppendPhiInput(Node* phi, Node* new_input) {
    320   const Operator* op = phi->op();
    321   const Operator* new_op = common()->ResizeMergeOrPhi(op, phi->InputCount());
    322   phi->InsertInput(zone(), phi->InputCount() - 1, new_input);
    323   NodeProperties::ChangeOp(phi, new_op);
    324 }
    325 
    326 Node* RawMachineAssembler::AddNode(const Operator* op, int input_count,
    327                                    Node* const* inputs) {
    328   DCHECK_NOT_NULL(schedule_);
    329   DCHECK_NOT_NULL(current_block_);
    330   Node* node = MakeNode(op, input_count, inputs);
    331   schedule()->AddNode(CurrentBlock(), node);
    332   return node;
    333 }
    334 
    335 Node* RawMachineAssembler::MakeNode(const Operator* op, int input_count,
    336                                     Node* const* inputs) {
    337   // The raw machine assembler nodes do not have effect and control inputs,
    338   // so we disable checking input counts here.
    339   return graph()->NewNodeUnchecked(op, input_count, inputs);
    340 }
    341 
    342 RawMachineLabel::~RawMachineLabel() {
    343   // If this DCHECK fails, it means that the label has been bound but it's not
    344   // used, or the opposite. This would cause the register allocator to crash.
    345   DCHECK_EQ(bound_, used_);
    346 }
    347 
    348 }  // namespace compiler
    349 }  // namespace internal
    350 }  // namespace v8
    351