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