1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "hydrogen.h"
29 #include "hydrogen-osr.h"
30
31 namespace v8 {
32 namespace internal {
33
34 // True iff. we are compiling for OSR and the statement is the entry.
HasOsrEntryAt(IterationStatement * statement)35 bool HOsrBuilder::HasOsrEntryAt(IterationStatement* statement) {
36 return statement->OsrEntryId() == builder_->current_info()->osr_ast_id();
37 }
38
39
BuildOsrLoopEntry(IterationStatement * statement)40 HBasicBlock* HOsrBuilder::BuildOsrLoopEntry(IterationStatement* statement) {
41 ASSERT(HasOsrEntryAt(statement));
42
43 Zone* zone = builder_->zone();
44 HGraph* graph = builder_->graph();
45
46 // only one OSR point per compile is allowed.
47 ASSERT(graph->osr() == NULL);
48
49 // remember this builder as the one OSR builder in the graph.
50 graph->set_osr(this);
51
52 HBasicBlock* non_osr_entry = graph->CreateBasicBlock();
53 osr_entry_ = graph->CreateBasicBlock();
54 HValue* true_value = graph->GetConstantTrue();
55 HBranch* test = builder_->New<HBranch>(true_value, ToBooleanStub::Types(),
56 non_osr_entry, osr_entry_);
57 builder_->FinishCurrentBlock(test);
58
59 HBasicBlock* loop_predecessor = graph->CreateBasicBlock();
60 builder_->Goto(non_osr_entry, loop_predecessor);
61
62 builder_->set_current_block(osr_entry_);
63 osr_entry_->set_osr_entry();
64 BailoutId osr_entry_id = statement->OsrEntryId();
65
66 HEnvironment *environment = builder_->environment();
67 int first_expression_index = environment->first_expression_index();
68 int length = environment->length();
69 osr_values_ = new(zone) ZoneList<HUnknownOSRValue*>(length, zone);
70
71 for (int i = 0; i < first_expression_index; ++i) {
72 HUnknownOSRValue* osr_value
73 = builder_->Add<HUnknownOSRValue>(environment, i);
74 environment->Bind(i, osr_value);
75 osr_values_->Add(osr_value, zone);
76 }
77
78 if (first_expression_index != length) {
79 environment->Drop(length - first_expression_index);
80 for (int i = first_expression_index; i < length; ++i) {
81 HUnknownOSRValue* osr_value
82 = builder_->Add<HUnknownOSRValue>(environment, i);
83 environment->Push(osr_value);
84 osr_values_->Add(osr_value, zone);
85 }
86 }
87
88 unoptimized_frame_slots_ =
89 environment->local_count() + environment->push_count();
90
91 // Keep a copy of the old environment, since the OSR values need it
92 // to figure out where exactly they are located in the unoptimized frame.
93 environment = environment->Copy();
94 builder_->current_block()->UpdateEnvironment(environment);
95
96 builder_->Add<HSimulate>(osr_entry_id);
97 builder_->Add<HOsrEntry>(osr_entry_id);
98 HContext* context = builder_->Add<HContext>();
99 environment->BindContext(context);
100 builder_->Goto(loop_predecessor);
101 loop_predecessor->SetJoinId(statement->EntryId());
102 builder_->set_current_block(loop_predecessor);
103
104 // Create the final loop entry
105 osr_loop_entry_ = builder_->BuildLoopEntry();
106 return osr_loop_entry_;
107 }
108
109
FinishGraph()110 void HOsrBuilder::FinishGraph() {
111 // do nothing for now.
112 }
113
114
FinishOsrValues()115 void HOsrBuilder::FinishOsrValues() {
116 const ZoneList<HPhi*>* phis = osr_loop_entry_->phis();
117 for (int j = 0; j < phis->length(); j++) {
118 HPhi* phi = phis->at(j);
119 if (phi->HasMergedIndex()) {
120 osr_values_->at(phi->merged_index())->set_incoming_value(phi);
121 }
122 }
123 }
124
125 } } // namespace v8::internal
126