Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(355)

Side by Side Diff: runtime/vm/intrinsifier.cc

Issue 1808503003: Add graph intrinsics for math library functions (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/vm/intermediate_language.cc ('k') | runtime/vm/method_recognizer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 // Class for intrinsifying functions. 4 // Class for intrinsifying functions.
5 5
6 #include "vm/assembler.h" 6 #include "vm/assembler.h"
7 #include "vm/compiler.h" 7 #include "vm/compiler.h"
8 #include "vm/flags.h" 8 #include "vm/flags.h"
9 #include "vm/flow_graph.h" 9 #include "vm/flow_graph.h"
10 #include "vm/flow_graph_compiler.h" 10 #include "vm/flow_graph_compiler.h"
11 #include "vm/flow_graph_allocator.h" 11 #include "vm/flow_graph_allocator.h"
12 #include "vm/flow_graph_builder.h" 12 #include "vm/flow_graph_builder.h"
13 #include "vm/il_printer.h" 13 #include "vm/il_printer.h"
14 #include "vm/intermediate_language.h" 14 #include "vm/intermediate_language.h"
15 #include "vm/intrinsifier.h" 15 #include "vm/intrinsifier.h"
16 #include "vm/object.h" 16 #include "vm/object.h"
17 #include "vm/parser.h" 17 #include "vm/parser.h"
18 #include "vm/symbols.h" 18 #include "vm/symbols.h"
19 19
20 20
21 namespace dart { 21 namespace dart {
22 22
23 DEFINE_FLAG(bool, intrinsify, true, "Instrinsify when possible"); 23 DEFINE_FLAG(bool, intrinsify, true, "Instrinsify when possible");
24 DEFINE_FLAG(bool, trace_intrinsifier, false, "Trace intrinsifier");
24 DECLARE_FLAG(bool, code_comments); 25 DECLARE_FLAG(bool, code_comments);
25 DECLARE_FLAG(bool, print_flow_graph); 26 DECLARE_FLAG(bool, print_flow_graph);
26 DECLARE_FLAG(bool, print_flow_graph_optimized); 27 DECLARE_FLAG(bool, print_flow_graph_optimized);
27 28
28 bool Intrinsifier::CanIntrinsify(const Function& function) { 29 bool Intrinsifier::CanIntrinsify(const Function& function) {
30 if (FLAG_trace_intrinsifier) {
31 OS::Print("CanIntrinsify %s ->", function.ToQualifiedCString());
32 }
29 if (!FLAG_intrinsify) return false; 33 if (!FLAG_intrinsify) return false;
30 if (function.IsClosureFunction()) return false; 34 if (function.IsClosureFunction()) {
35 if (FLAG_trace_intrinsifier) {
36 OS::Print("No, closure function.\n");
37 }
38 return false;
39 }
31 // Can occur because of compile-all flag. 40 // Can occur because of compile-all flag.
32 if (function.is_external()) return false; 41 if (function.is_external()) {
33 return function.is_intrinsic(); 42 if (FLAG_trace_intrinsifier) {
43 OS::Print("No, external function.\n");
44 }
45 return false;
46 }
47 if (!function.is_intrinsic()) {
48 if (FLAG_trace_intrinsifier) {
49 OS::Print("No, not intrinsic function.\n");
50 }
51 return false;
52 }
53 if (FLAG_trace_intrinsifier) {
54 OS::Print("Yes\n");
55 }
56 return true;
34 } 57 }
35 58
36 59
37 #if defined(DART_NO_SNAPSHOT) 60 #if defined(DART_NO_SNAPSHOT)
38 void Intrinsifier::InitializeState() { 61 void Intrinsifier::InitializeState() {
39 Thread* thread = Thread::Current(); 62 Thread* thread = Thread::Current();
40 Zone* zone = thread->zone(); 63 Zone* zone = thread->zone();
41 Library& lib = Library::Handle(zone); 64 Library& lib = Library::Handle(zone);
42 Class& cls = Class::Handle(zone); 65 Class& cls = Class::Handle(zone);
43 Function& func = Function::Handle(zone); 66 Function& func = Function::Handle(zone);
(...skipping 27 matching lines...) Expand all
71 lib = Library::CoreLibrary(); 94 lib = Library::CoreLibrary();
72 ASSERT(!lib.IsNull()); 95 ASSERT(!lib.IsNull());
73 CORE_LIB_INTRINSIC_LIST(SETUP_FUNCTION); 96 CORE_LIB_INTRINSIC_LIST(SETUP_FUNCTION);
74 CORE_INTEGER_LIB_INTRINSIC_LIST(SETUP_FUNCTION); 97 CORE_INTEGER_LIB_INTRINSIC_LIST(SETUP_FUNCTION);
75 GRAPH_CORE_INTRINSICS_LIST(SETUP_FUNCTION); 98 GRAPH_CORE_INTRINSICS_LIST(SETUP_FUNCTION);
76 99
77 // Set up all math lib functions that can be intrinsified. 100 // Set up all math lib functions that can be intrinsified.
78 lib = Library::MathLibrary(); 101 lib = Library::MathLibrary();
79 ASSERT(!lib.IsNull()); 102 ASSERT(!lib.IsNull());
80 MATH_LIB_INTRINSIC_LIST(SETUP_FUNCTION); 103 MATH_LIB_INTRINSIC_LIST(SETUP_FUNCTION);
104 GRAPH_MATH_LIB_INTRINSIC_LIST(SETUP_FUNCTION);
81 105
82 // Set up all dart:typed_data lib functions that can be intrinsified. 106 // Set up all dart:typed_data lib functions that can be intrinsified.
83 lib = Library::TypedDataLibrary(); 107 lib = Library::TypedDataLibrary();
84 ASSERT(!lib.IsNull()); 108 ASSERT(!lib.IsNull());
85 TYPED_DATA_LIB_INTRINSIC_LIST(SETUP_FUNCTION); 109 TYPED_DATA_LIB_INTRINSIC_LIST(SETUP_FUNCTION);
86 GRAPH_TYPED_DATA_INTRINSICS_LIST(SETUP_FUNCTION); 110 GRAPH_TYPED_DATA_INTRINSICS_LIST(SETUP_FUNCTION);
87 111
88 // Setup all dart:developer lib functions that can be intrinsified. 112 // Setup all dart:developer lib functions that can be intrinsified.
89 lib = Library::DeveloperLibrary(); 113 lib = Library::DeveloperLibrary();
90 ASSERT(!lib.IsNull()); 114 ASSERT(!lib.IsNull());
91 DEVELOPER_LIB_INTRINSIC_LIST(SETUP_FUNCTION); 115 DEVELOPER_LIB_INTRINSIC_LIST(SETUP_FUNCTION);
92 116
93 #undef SETUP_FUNCTION 117 #undef SETUP_FUNCTION
94 } 118 }
95 #endif // defined(DART_NO_SNAPSHOT). 119 #endif // defined(DART_NO_SNAPSHOT).
96 120
97 121
98 static void EmitCodeFor(FlowGraphCompiler* compiler, 122 static void EmitCodeFor(FlowGraphCompiler* compiler,
99 FlowGraph* graph) { 123 FlowGraph* graph) {
100 // The FlowGraph here is constructed by the intrinsics builder methods, and 124 // The FlowGraph here is constructed by the intrinsics builder methods, and
101 // is different from compiler->flow_graph(), the original method's flow graph. 125 // is different from compiler->flow_graph(), the original method's flow graph.
102 compiler->assembler()->Comment("Graph intrinsic"); 126 compiler->assembler()->Comment("Graph intrinsic begin");
103 for (intptr_t i = 0; i < graph->reverse_postorder().length(); i++) { 127 for (intptr_t i = 0; i < graph->reverse_postorder().length(); i++) {
104 BlockEntryInstr* block = graph->reverse_postorder()[i]; 128 BlockEntryInstr* block = graph->reverse_postorder()[i];
105 if (block->IsGraphEntry()) continue; // No code for graph entry needed. 129 if (block->IsGraphEntry()) continue; // No code for graph entry needed.
106 130
107 if (block->HasParallelMove()) { 131 if (block->HasParallelMove()) {
108 compiler->parallel_move_resolver()->EmitNativeCode( 132 compiler->parallel_move_resolver()->EmitNativeCode(
109 block->parallel_move()); 133 block->parallel_move());
110 } 134 }
111 135
112 for (ForwardInstructionIterator it(block); !it.Done(); it.Advance()) { 136 for (ForwardInstructionIterator it(block); !it.Done(); it.Advance()) {
113 Instruction* instr = it.Current(); 137 Instruction* instr = it.Current();
114 if (FLAG_code_comments) compiler->EmitComment(instr); 138 if (FLAG_code_comments) compiler->EmitComment(instr);
115 if (instr->IsParallelMove()) { 139 if (instr->IsParallelMove()) {
116 compiler->parallel_move_resolver()->EmitNativeCode( 140 compiler->parallel_move_resolver()->EmitNativeCode(
117 instr->AsParallelMove()); 141 instr->AsParallelMove());
142 } else if (instr->IsInvokeMathCFunction()) {
143 ASSERT(instr->locs() != NULL);
144 // InvokeMathCFunction always calls, but it uses registers that
145 // are free for the intrinsic to use.
146 instr->EmitNativeCode(compiler);
118 } else { 147 } else {
119 ASSERT(instr->locs() != NULL); 148 ASSERT(instr->locs() != NULL);
120 // Calls are not supported in intrinsics code. 149 // Calls are not supported in intrinsics code.
121 ASSERT(!instr->locs()->always_calls()); 150 ASSERT(!instr->locs()->always_calls());
122 instr->EmitNativeCode(compiler); 151 instr->EmitNativeCode(compiler);
123 } 152 }
124 } 153 }
125 } 154 }
155 compiler->assembler()->Comment("Graph intrinsic end");
126 } 156 }
127 157
128 158
129 bool Intrinsifier::GraphIntrinsify(const ParsedFunction& parsed_function, 159 bool Intrinsifier::GraphIntrinsify(const ParsedFunction& parsed_function,
130 FlowGraphCompiler* compiler) { 160 FlowGraphCompiler* compiler) {
131 ZoneGrowableArray<const ICData*>* ic_data_array = 161 ZoneGrowableArray<const ICData*>* ic_data_array =
132 new ZoneGrowableArray<const ICData*>(); 162 new ZoneGrowableArray<const ICData*>();
133 FlowGraphBuilder builder(parsed_function, 163 FlowGraphBuilder builder(parsed_function,
134 *ic_data_array, 164 *ic_data_array,
135 NULL, // NULL = not inlining. 165 NULL, // NULL = not inlining.
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 return kFloat32x4Cid; 247 return kFloat32x4Cid;
218 case kUnboxedUint32: 248 case kUnboxedUint32:
219 return kDynamicCid; // smi or mint. 249 return kDynamicCid; // smi or mint.
220 default: 250 default:
221 UNREACHABLE(); 251 UNREACHABLE();
222 return kIllegalCid; 252 return kIllegalCid;
223 } 253 }
224 } 254 }
225 255
226 256
257 // Notes about the graph intrinsics:
258 //
259 // IR instructions which would jump to a deoptimization sequence on failure
260 // instead branch to the intrinsic slow path.
261
227 class BlockBuilder : public ValueObject { 262 class BlockBuilder : public ValueObject {
228 public: 263 public:
229 BlockBuilder(FlowGraph* flow_graph, TargetEntryInstr* entry) 264 BlockBuilder(FlowGraph* flow_graph, TargetEntryInstr* entry)
230 : flow_graph_(flow_graph), entry_(entry), current_(entry) { } 265 : flow_graph_(flow_graph), entry_(entry), current_(entry) { }
231 266
232 Definition* AddToInitialDefinitions(Definition* def) { 267 Definition* AddToInitialDefinitions(Definition* def) {
233 def->set_ssa_temp_index(flow_graph_->alloc_ssa_temp_index()); 268 def->set_ssa_temp_index(flow_graph_->alloc_ssa_temp_index());
234 flow_graph_->AddToInitialDefinitions(def); 269 flow_graph_->AddToInitialDefinitions(def);
235 return def; 270 return def;
236 } 271 }
(...skipping 16 matching lines...) Expand all
253 } 288 }
254 289
255 Definition* AddParameter(intptr_t index) { 290 Definition* AddParameter(intptr_t index) {
256 intptr_t adjustment = Intrinsifier::ParameterSlotFromSp(); 291 intptr_t adjustment = Intrinsifier::ParameterSlotFromSp();
257 return AddToInitialDefinitions( 292 return AddToInitialDefinitions(
258 new ParameterInstr(adjustment + index, 293 new ParameterInstr(adjustment + index,
259 flow_graph_->graph_entry(), 294 flow_graph_->graph_entry(),
260 SPREG)); 295 SPREG));
261 } 296 }
262 297
298 Definition* AddCheckedUnbox(Representation rep, Definition* boxed) {
299 const ICData& value_check = ICData::ZoneHandle(ICData::New(
300 flow_graph_->function(),
301 String::Handle(flow_graph_->function().name()),
302 Object::empty_array(), // Dummy args. descr.
303 Thread::kNoDeoptId,
304 1));
305 value_check.AddReceiverCheck(CidForRepresentation(rep),
306 flow_graph_->function());
307 AddInstruction(new CheckClassInstr(new Value(boxed),
Florian Schneider 2016/03/16 15:51:49 Since these math functions call toDouble on their
Cutch 2016/03/16 19:35:55 We are recognizing 'sin' and not '_sin', so the ar
Florian Schneider 2016/03/19 09:40:56 Yes, this is why I think just UnboxInstr would jus
Cutch 2016/03/21 17:03:47 Done.
308 Thread::kNoDeoptId,
309 value_check,
310 TokenPos()));
311 return AddUnboxInstr(rep, new Value(boxed));
312 }
313
263 TokenPosition TokenPos() { 314 TokenPosition TokenPos() {
264 return flow_graph_->function().token_pos(); 315 return flow_graph_->function().token_pos();
265 } 316 }
266 317
267 Definition* AddNullDefinition() { 318 Definition* AddNullDefinition() {
268 return AddDefinition( 319 return AddDefinition(
269 new ConstantInstr(Object::ZoneHandle(Object::null()))); 320 new ConstantInstr(Object::ZoneHandle(Object::null())));
270 } 321 }
271 322
272 Definition* AddUnboxInstr(Representation rep, Value* value) { 323 Definition* AddUnboxInstr(Representation rep, Value* value) {
273 Definition* unboxed_value = AddDefinition( 324 Definition* unboxed_value = AddDefinition(
274 UnboxInstr::Create(rep, value, Thread::kNoDeoptId)); 325 UnboxInstr::Create(rep, value, Thread::kNoDeoptId));
275 // Manually adjust reaching type because there is no type propagation 326 // Manually adjust reaching type because there is no type propagation
276 // when building intrinsics. 327 // when building intrinsics.
277 unboxed_value->AsUnbox()->value()->SetReachingType(ZoneCompileType::Wrap( 328 unboxed_value->AsUnbox()->value()->SetReachingType(ZoneCompileType::Wrap(
278 CompileType::FromCid(CidForRepresentation(rep)))); 329 CompileType::FromCid(CidForRepresentation(rep))));
279 return unboxed_value; 330 return unboxed_value;
280 } 331 }
281 332
333 Definition* InvokeMathCFunction(MethodRecognizer::Kind recognized_kind,
334 Definition* input0,
335 Definition* input1) {
336 ZoneGrowableArray<Value*>* call_values =
337 new ZoneGrowableArray<Value*>(2);
338
339 call_values->Add(new Value(input0));
340 call_values->Add(new Value(input1));
341
342 return InvokeMathCFunctionHelper(recognized_kind, call_values);
343 }
344
345 Definition* InvokeMathCFunction(MethodRecognizer::Kind recognized_kind,
346 Definition* input) {
347 ZoneGrowableArray<Value*>* call_values =
348 new ZoneGrowableArray<Value*>(1);
349
350 call_values->Add(new Value(input));
351
352 return InvokeMathCFunctionHelper(recognized_kind, call_values);
353 }
354
282 private: 355 private:
356 Definition* InvokeMathCFunctionHelper(MethodRecognizer::Kind recognized_kind,
357 ZoneGrowableArray<Value*>* args) {
358 InvokeMathCFunctionInstr* invoke_math_c_function =
359 new InvokeMathCFunctionInstr(args,
360 Thread::kNoDeoptId,
361 recognized_kind,
362 TokenPos());
363 AddDefinition(invoke_math_c_function);
364 return invoke_math_c_function;
365 }
366
367
283 FlowGraph* flow_graph_; 368 FlowGraph* flow_graph_;
284 BlockEntryInstr* entry_; 369 BlockEntryInstr* entry_;
285 Instruction* current_; 370 Instruction* current_;
286 }; 371 };
287 372
288 373
289 static void PrepareIndexedOp(BlockBuilder* builder, 374 static void PrepareIndexedOp(BlockBuilder* builder,
290 Definition* array, 375 Definition* array,
291 Definition* index, 376 Definition* index,
292 intptr_t length_offset) { 377 intptr_t length_offset) {
(...skipping 583 matching lines...) Expand 10 before | Expand all | Expand 10 after
876 Definition* unboxed_result = builder.AddDefinition( 961 Definition* unboxed_result = builder.AddDefinition(
877 new UnaryDoubleOpInstr(Token::kNEGATE, 962 new UnaryDoubleOpInstr(Token::kNEGATE,
878 new Value(unboxed_value), 963 new Value(unboxed_value),
879 Thread::kNoDeoptId)); 964 Thread::kNoDeoptId));
880 Definition* result = builder.AddDefinition( 965 Definition* result = builder.AddDefinition(
881 BoxInstr::Create(kUnboxedDouble, new Value(unboxed_result))); 966 BoxInstr::Create(kUnboxedDouble, new Value(unboxed_result)));
882 builder.AddIntrinsicReturn(new Value(result)); 967 builder.AddIntrinsicReturn(new Value(result));
883 return true; 968 return true;
884 } 969 }
885 970
971
972 bool Intrinsifier::Build_MathSin(FlowGraph* flow_graph) {
973 if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
974
975 GraphEntryInstr* graph_entry = flow_graph->graph_entry();
976 TargetEntryInstr* normal_entry = graph_entry->normal_entry();
977 BlockBuilder builder(flow_graph, normal_entry);
978
979 Definition* value = builder.AddParameter(1);
980
981 Definition* unboxed_value = builder.AddCheckedUnbox(kUnboxedDouble, value);
982
983 Definition* unboxed_result =
984 builder.InvokeMathCFunction(MethodRecognizer::kMathSin,
985 unboxed_value);
986
987 Definition* result = builder.AddDefinition(
988 BoxInstr::Create(kUnboxedDouble, new Value(unboxed_result)));
989 builder.AddIntrinsicReturn(new Value(result));
990 return true;
991 }
992
993 bool Intrinsifier::Build_MathCos(FlowGraph* flow_graph) {
994 if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
995
996 return true;
997 }
998
999
1000 bool Intrinsifier::Build_MathTan(FlowGraph* flow_graph) {
1001 if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
1002
1003 return true;
1004 }
1005
1006
1007 bool Intrinsifier::Build_MathAsin(FlowGraph* flow_graph) {
1008 if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
1009
1010 return true;
1011 }
1012
1013
1014 bool Intrinsifier::Build_MathAcos(FlowGraph* flow_graph) {
1015 if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
1016
1017 return true;
1018 }
1019
1020
1021 bool Intrinsifier::Build_MathAtan(FlowGraph* flow_graph) {
1022 if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
1023
1024 return true;
1025 }
1026
1027
1028 bool Intrinsifier::Build_MathAtan2(FlowGraph* flow_graph) {
1029 if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
1030
1031 GraphEntryInstr* graph_entry = flow_graph->graph_entry();
1032 TargetEntryInstr* normal_entry = graph_entry->normal_entry();
1033 BlockBuilder builder(flow_graph, normal_entry);
1034
1035 Definition* value_a = builder.AddParameter(1);
1036 Definition* value_b = builder.AddParameter(2);
1037
1038 Definition* unboxed_a = builder.AddCheckedUnbox(kUnboxedDouble, value_a);
1039 Definition* unboxed_b = builder.AddCheckedUnbox(kUnboxedDouble, value_b);
1040
1041 Definition* unboxed_result =
1042 builder.InvokeMathCFunction(MethodRecognizer::kMathAtan2,
1043 unboxed_b,
1044 unboxed_a);
1045
1046 Definition* result = builder.AddDefinition(
1047 BoxInstr::Create(kUnboxedDouble, new Value(unboxed_result)));
1048 builder.AddIntrinsicReturn(new Value(result));
1049
1050 return true;
1051 }
1052
886 } // namespace dart 1053 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/intermediate_language.cc ('k') | runtime/vm/method_recognizer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698