Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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/js-context-specialization.h" |
| 6 | #include "src/compiler/js-operator.h" |
| 7 | #include "src/compiler/node-matchers.h" |
| 8 | #include "src/compiler/node-properties-inl.h" |
| 9 | #include "src/compiler/source-position.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include "test/cctest/cctest.h" |
| 11 | #include "test/cctest/compiler/function-tester.h" |
| 12 | #include "test/cctest/compiler/graph-builder-tester.h" |
| 13 | |
| 14 | using namespace v8::internal; |
| 15 | using namespace v8::internal::compiler; |
| 16 | |
| 17 | class ContextSpecializationTester : public HandleAndZoneScope, |
| 18 | public DirectGraphBuilder { |
| 19 | public: |
| 20 | ContextSpecializationTester() |
| 21 | : DirectGraphBuilder(new (main_zone()) Graph(main_zone())), |
| 22 | common_(main_zone()), |
| 23 | javascript_(main_zone()), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 24 | machine_(main_zone()), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 25 | simplified_(main_zone()), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 26 | jsgraph_(graph(), common(), &javascript_, &machine_), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 27 | info_(main_isolate(), main_zone()) {} |
| 28 | |
| 29 | Factory* factory() { return main_isolate()->factory(); } |
| 30 | CommonOperatorBuilder* common() { return &common_; } |
| 31 | JSOperatorBuilder* javascript() { return &javascript_; } |
| 32 | SimplifiedOperatorBuilder* simplified() { return &simplified_; } |
| 33 | JSGraph* jsgraph() { return &jsgraph_; } |
| 34 | CompilationInfo* info() { return &info_; } |
| 35 | |
| 36 | private: |
| 37 | CommonOperatorBuilder common_; |
| 38 | JSOperatorBuilder javascript_; |
| 39 | MachineOperatorBuilder machine_; |
| 40 | SimplifiedOperatorBuilder simplified_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 41 | JSGraph jsgraph_; |
| 42 | CompilationInfo info_; |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | TEST(ReduceJSLoadContext) { |
| 47 | ContextSpecializationTester t; |
| 48 | |
| 49 | Node* start = t.NewNode(t.common()->Start(0)); |
| 50 | t.graph()->SetStart(start); |
| 51 | |
| 52 | // Make a context and initialize it a bit for this test. |
| 53 | Handle<Context> native = t.factory()->NewNativeContext(); |
| 54 | Handle<Context> subcontext1 = t.factory()->NewNativeContext(); |
| 55 | Handle<Context> subcontext2 = t.factory()->NewNativeContext(); |
| 56 | subcontext2->set_previous(*subcontext1); |
| 57 | subcontext1->set_previous(*native); |
| 58 | Handle<Object> expected = t.factory()->InternalizeUtf8String("gboy!"); |
| 59 | const int slot = Context::GLOBAL_OBJECT_INDEX; |
| 60 | native->set(slot, *expected); |
| 61 | |
| 62 | Node* const_context = t.jsgraph()->Constant(native); |
| 63 | Node* deep_const_context = t.jsgraph()->Constant(subcontext2); |
| 64 | Node* param_context = t.NewNode(t.common()->Parameter(0), start); |
| 65 | JSContextSpecializer spec(t.info(), t.jsgraph(), const_context); |
| 66 | |
| 67 | { |
| 68 | // Mutable slot, constant context, depth = 0 => do nothing. |
| 69 | Node* load = t.NewNode(t.javascript()->LoadContext(0, 0, false), |
| 70 | const_context, const_context, start); |
| 71 | Reduction r = spec.ReduceJSLoadContext(load); |
| 72 | CHECK(!r.Changed()); |
| 73 | } |
| 74 | |
| 75 | { |
| 76 | // Mutable slot, non-constant context, depth = 0 => do nothing. |
| 77 | Node* load = t.NewNode(t.javascript()->LoadContext(0, 0, false), |
| 78 | param_context, param_context, start); |
| 79 | Reduction r = spec.ReduceJSLoadContext(load); |
| 80 | CHECK(!r.Changed()); |
| 81 | } |
| 82 | |
| 83 | { |
| 84 | // Mutable slot, constant context, depth > 0 => fold-in parent context. |
| 85 | Node* load = t.NewNode( |
| 86 | t.javascript()->LoadContext(2, Context::GLOBAL_EVAL_FUN_INDEX, false), |
| 87 | deep_const_context, deep_const_context, start); |
| 88 | Reduction r = spec.ReduceJSLoadContext(load); |
| 89 | CHECK(r.Changed()); |
| 90 | Node* new_context_input = NodeProperties::GetValueInput(r.replacement(), 0); |
| 91 | CHECK_EQ(IrOpcode::kHeapConstant, new_context_input->opcode()); |
| 92 | HeapObjectMatcher<Context> match(new_context_input); |
| 93 | CHECK_EQ(*native, *match.Value().handle()); |
| 94 | ContextAccess access = OpParameter<ContextAccess>(r.replacement()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 95 | CHECK_EQ(Context::GLOBAL_EVAL_FUN_INDEX, static_cast<int>(access.index())); |
| 96 | CHECK_EQ(0, static_cast<int>(access.depth())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 97 | CHECK_EQ(false, access.immutable()); |
| 98 | } |
| 99 | |
| 100 | { |
| 101 | // Immutable slot, constant context, depth = 0 => specialize. |
| 102 | Node* load = t.NewNode(t.javascript()->LoadContext(0, slot, true), |
| 103 | const_context, const_context, start); |
| 104 | Reduction r = spec.ReduceJSLoadContext(load); |
| 105 | CHECK(r.Changed()); |
| 106 | CHECK(r.replacement() != load); |
| 107 | |
| 108 | HeapObjectMatcher<Object> match(r.replacement()); |
| 109 | CHECK(match.HasValue()); |
| 110 | CHECK_EQ(*expected, *match.Value().handle()); |
| 111 | } |
| 112 | |
| 113 | // TODO(titzer): test with other kinds of contexts, e.g. a function context. |
| 114 | // TODO(sigurds): test that loads below create context are not optimized |
| 115 | } |
| 116 | |
| 117 | |
| 118 | TEST(ReduceJSStoreContext) { |
| 119 | ContextSpecializationTester t; |
| 120 | |
| 121 | Node* start = t.NewNode(t.common()->Start(0)); |
| 122 | t.graph()->SetStart(start); |
| 123 | |
| 124 | // Make a context and initialize it a bit for this test. |
| 125 | Handle<Context> native = t.factory()->NewNativeContext(); |
| 126 | Handle<Context> subcontext1 = t.factory()->NewNativeContext(); |
| 127 | Handle<Context> subcontext2 = t.factory()->NewNativeContext(); |
| 128 | subcontext2->set_previous(*subcontext1); |
| 129 | subcontext1->set_previous(*native); |
| 130 | Handle<Object> expected = t.factory()->InternalizeUtf8String("gboy!"); |
| 131 | const int slot = Context::GLOBAL_OBJECT_INDEX; |
| 132 | native->set(slot, *expected); |
| 133 | |
| 134 | Node* const_context = t.jsgraph()->Constant(native); |
| 135 | Node* deep_const_context = t.jsgraph()->Constant(subcontext2); |
| 136 | Node* param_context = t.NewNode(t.common()->Parameter(0), start); |
| 137 | JSContextSpecializer spec(t.info(), t.jsgraph(), const_context); |
| 138 | |
| 139 | { |
| 140 | // Mutable slot, constant context, depth = 0 => do nothing. |
| 141 | Node* load = t.NewNode(t.javascript()->StoreContext(0, 0), const_context, |
| 142 | const_context, start); |
| 143 | Reduction r = spec.ReduceJSStoreContext(load); |
| 144 | CHECK(!r.Changed()); |
| 145 | } |
| 146 | |
| 147 | { |
| 148 | // Mutable slot, non-constant context, depth = 0 => do nothing. |
| 149 | Node* load = t.NewNode(t.javascript()->StoreContext(0, 0), param_context, |
| 150 | param_context, start); |
| 151 | Reduction r = spec.ReduceJSStoreContext(load); |
| 152 | CHECK(!r.Changed()); |
| 153 | } |
| 154 | |
| 155 | { |
| 156 | // Immutable slot, constant context, depth = 0 => do nothing. |
| 157 | Node* load = t.NewNode(t.javascript()->StoreContext(0, slot), const_context, |
| 158 | const_context, start); |
| 159 | Reduction r = spec.ReduceJSStoreContext(load); |
| 160 | CHECK(!r.Changed()); |
| 161 | } |
| 162 | |
| 163 | { |
| 164 | // Mutable slot, constant context, depth > 0 => fold-in parent context. |
| 165 | Node* load = t.NewNode( |
| 166 | t.javascript()->StoreContext(2, Context::GLOBAL_EVAL_FUN_INDEX), |
| 167 | deep_const_context, deep_const_context, start); |
| 168 | Reduction r = spec.ReduceJSStoreContext(load); |
| 169 | CHECK(r.Changed()); |
| 170 | Node* new_context_input = NodeProperties::GetValueInput(r.replacement(), 0); |
| 171 | CHECK_EQ(IrOpcode::kHeapConstant, new_context_input->opcode()); |
| 172 | HeapObjectMatcher<Context> match(new_context_input); |
| 173 | CHECK_EQ(*native, *match.Value().handle()); |
| 174 | ContextAccess access = OpParameter<ContextAccess>(r.replacement()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 175 | CHECK_EQ(Context::GLOBAL_EVAL_FUN_INDEX, static_cast<int>(access.index())); |
| 176 | CHECK_EQ(0, static_cast<int>(access.depth())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 177 | CHECK_EQ(false, access.immutable()); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | |
| 182 | // TODO(titzer): factor out common code with effects checking in typed lowering. |
| 183 | static void CheckEffectInput(Node* effect, Node* use) { |
| 184 | CHECK_EQ(effect, NodeProperties::GetEffectInput(use)); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | TEST(SpecializeToContext) { |
| 189 | ContextSpecializationTester t; |
| 190 | |
| 191 | Node* start = t.NewNode(t.common()->Start(0)); |
| 192 | t.graph()->SetStart(start); |
| 193 | |
| 194 | // Make a context and initialize it a bit for this test. |
| 195 | Handle<Context> native = t.factory()->NewNativeContext(); |
| 196 | Handle<Object> expected = t.factory()->InternalizeUtf8String("gboy!"); |
| 197 | const int slot = Context::GLOBAL_OBJECT_INDEX; |
| 198 | native->set(slot, *expected); |
| 199 | t.info()->SetContext(native); |
| 200 | |
| 201 | Node* const_context = t.jsgraph()->Constant(native); |
| 202 | Node* param_context = t.NewNode(t.common()->Parameter(0), start); |
| 203 | JSContextSpecializer spec(t.info(), t.jsgraph(), const_context); |
| 204 | |
| 205 | { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 206 | // Check that specialization replaces values and forwards effects |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 207 | // correctly, and folds values from constant and non-constant contexts |
| 208 | Node* effect_in = start; |
| 209 | Node* load = t.NewNode(t.javascript()->LoadContext(0, slot, true), |
| 210 | const_context, const_context, effect_in); |
| 211 | |
| 212 | |
| 213 | Node* value_use = t.NewNode(t.simplified()->ChangeTaggedToInt32(), load); |
| 214 | Node* other_load = t.NewNode(t.javascript()->LoadContext(0, slot, true), |
| 215 | param_context, param_context, load); |
| 216 | Node* effect_use = other_load; |
| 217 | Node* other_use = |
| 218 | t.NewNode(t.simplified()->ChangeTaggedToInt32(), other_load); |
| 219 | |
| 220 | Node* add = t.NewNode(t.javascript()->Add(), value_use, other_use, |
| 221 | param_context, other_load, start); |
| 222 | |
| 223 | Node* ret = t.NewNode(t.common()->Return(), add, effect_use, start); |
| 224 | Node* end = t.NewNode(t.common()->End(), ret); |
| 225 | USE(end); |
| 226 | t.graph()->SetEnd(end); |
| 227 | |
| 228 | // Double check the above graph is what we expect, or the test is broken. |
| 229 | CheckEffectInput(effect_in, load); |
| 230 | CheckEffectInput(load, effect_use); |
| 231 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 232 | // Perform the reduction on the entire graph. |
| 233 | GraphReducer graph_reducer(t.graph(), t.main_zone()); |
| 234 | graph_reducer.AddReducer(&spec); |
| 235 | graph_reducer.ReduceGraph(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 236 | |
| 237 | // Effects should have been forwarded (not replaced with a value). |
| 238 | CheckEffectInput(effect_in, effect_use); |
| 239 | |
| 240 | // Use of {other_load} should not have been replaced. |
| 241 | CHECK_EQ(other_load, other_use->InputAt(0)); |
| 242 | |
| 243 | Node* replacement = value_use->InputAt(0); |
| 244 | HeapObjectMatcher<Object> match(replacement); |
| 245 | CHECK(match.HasValue()); |
| 246 | CHECK_EQ(*expected, *match.Value().handle()); |
| 247 | } |
| 248 | // TODO(titzer): clean up above test and test more complicated effects. |
| 249 | } |
| 250 | |
| 251 | |
| 252 | TEST(SpecializeJSFunction_ToConstant1) { |
| 253 | FunctionTester T( |
| 254 | "(function() { var x = 1; function inc(a)" |
| 255 | " { return a + x; } return inc; })()"); |
| 256 | |
| 257 | T.CheckCall(1.0, 0.0, 0.0); |
| 258 | T.CheckCall(2.0, 1.0, 0.0); |
| 259 | T.CheckCall(2.1, 1.1, 0.0); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | TEST(SpecializeJSFunction_ToConstant2) { |
| 264 | FunctionTester T( |
| 265 | "(function() { var x = 1.5; var y = 2.25; var z = 3.75;" |
| 266 | " function f(a) { return a - x + y - z; } return f; })()"); |
| 267 | |
| 268 | T.CheckCall(-3.0, 0.0, 0.0); |
| 269 | T.CheckCall(-2.0, 1.0, 0.0); |
| 270 | T.CheckCall(-1.9, 1.1, 0.0); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | TEST(SpecializeJSFunction_ToConstant3) { |
| 275 | FunctionTester T( |
| 276 | "(function() { var x = -11.5; function inc()" |
| 277 | " { return (function(a) { return a + x; }); }" |
| 278 | " return inc(); })()"); |
| 279 | |
| 280 | T.CheckCall(-11.5, 0.0, 0.0); |
| 281 | T.CheckCall(-10.5, 1.0, 0.0); |
| 282 | T.CheckCall(-10.4, 1.1, 0.0); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | TEST(SpecializeJSFunction_ToConstant_uninit) { |
| 287 | { |
| 288 | FunctionTester T( |
| 289 | "(function() { if (false) { var x = 1; } function inc(a)" |
| 290 | " { return x; } return inc; })()"); // x is undefined! |
| 291 | |
| 292 | CHECK(T.Call(T.Val(0.0), T.Val(0.0)).ToHandleChecked()->IsUndefined()); |
| 293 | CHECK(T.Call(T.Val(2.0), T.Val(0.0)).ToHandleChecked()->IsUndefined()); |
| 294 | CHECK(T.Call(T.Val(-2.1), T.Val(0.0)).ToHandleChecked()->IsUndefined()); |
| 295 | } |
| 296 | |
| 297 | { |
| 298 | FunctionTester T( |
| 299 | "(function() { if (false) { var x = 1; } function inc(a)" |
| 300 | " { return a + x; } return inc; })()"); // x is undefined! |
| 301 | |
| 302 | CHECK(T.Call(T.Val(0.0), T.Val(0.0)).ToHandleChecked()->IsNaN()); |
| 303 | CHECK(T.Call(T.Val(2.0), T.Val(0.0)).ToHandleChecked()->IsNaN()); |
| 304 | CHECK(T.Call(T.Val(-2.1), T.Val(0.0)).ToHandleChecked()->IsNaN()); |
| 305 | } |
| 306 | } |