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

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 THR_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 THR_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 THR_Print("No, external function.\n");
44 }
45 return false;
46 }
47 if (!function.is_intrinsic()) {
48 if (FLAG_trace_intrinsifier) {
49 THR_Print("No, not intrinsic function.\n");
50 }
51 return false;
52 }
53 if (FLAG_trace_intrinsifier) {
54 THR_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),
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* AddUnboxInstr(Representation rep, Definition* boxed) {
334 return AddUnboxInstr(rep, new Value(boxed));
335 }
336
337 Definition* InvokeMathCFunction(MethodRecognizer::Kind recognized_kind,
338 ZoneGrowableArray<Value*>* args) {
339 return InvokeMathCFunctionHelper(recognized_kind, args);
340 }
341
282 private: 342 private:
343 Definition* InvokeMathCFunctionHelper(MethodRecognizer::Kind recognized_kind,
344 ZoneGrowableArray<Value*>* args) {
345 InvokeMathCFunctionInstr* invoke_math_c_function =
346 new InvokeMathCFunctionInstr(args,
347 Thread::kNoDeoptId,
348 recognized_kind,
349 TokenPos());
350 AddDefinition(invoke_math_c_function);
351 return invoke_math_c_function;
352 }
353
354
283 FlowGraph* flow_graph_; 355 FlowGraph* flow_graph_;
284 BlockEntryInstr* entry_; 356 BlockEntryInstr* entry_;
285 Instruction* current_; 357 Instruction* current_;
286 }; 358 };
287 359
288 360
289 static void PrepareIndexedOp(BlockBuilder* builder, 361 static void PrepareIndexedOp(BlockBuilder* builder,
290 Definition* array, 362 Definition* array,
291 Definition* index, 363 Definition* index,
292 intptr_t length_offset) { 364 intptr_t length_offset) {
(...skipping 583 matching lines...) Expand 10 before | Expand all | Expand 10 after
876 Definition* unboxed_result = builder.AddDefinition( 948 Definition* unboxed_result = builder.AddDefinition(
877 new UnaryDoubleOpInstr(Token::kNEGATE, 949 new UnaryDoubleOpInstr(Token::kNEGATE,
878 new Value(unboxed_value), 950 new Value(unboxed_value),
879 Thread::kNoDeoptId)); 951 Thread::kNoDeoptId));
880 Definition* result = builder.AddDefinition( 952 Definition* result = builder.AddDefinition(
881 BoxInstr::Create(kUnboxedDouble, new Value(unboxed_result))); 953 BoxInstr::Create(kUnboxedDouble, new Value(unboxed_result)));
882 builder.AddIntrinsicReturn(new Value(result)); 954 builder.AddIntrinsicReturn(new Value(result));
883 return true; 955 return true;
884 } 956 }
885 957
958
959 static bool BuildInvokeMathCFunction(BlockBuilder* builder,
960 MethodRecognizer::Kind kind) {
961 ZoneGrowableArray<Value*>* args =
962 new ZoneGrowableArray<Value*>(1);
963
964 Definition* value = builder->AddParameter(1);
965 Definition* unboxed_value = builder->AddUnboxInstr(kUnboxedDouble, value);
966 args->Add(new Value(unboxed_value));
967
968 Definition* unboxed_result =
969 builder->InvokeMathCFunction(kind, args);
970
971 Definition* result = builder->AddDefinition(
972 BoxInstr::Create(kUnboxedDouble, new Value(unboxed_result)));
973
974 builder->AddIntrinsicReturn(new Value(result));
975
976 return true;
977 }
978
979
980 bool Intrinsifier::Build_MathSin(FlowGraph* flow_graph) {
981 if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
982
983 GraphEntryInstr* graph_entry = flow_graph->graph_entry();
984 TargetEntryInstr* normal_entry = graph_entry->normal_entry();
985 BlockBuilder builder(flow_graph, normal_entry);
986
987 return BuildInvokeMathCFunction(&builder,
988 MethodRecognizer::kMathSin);
989 }
990
991
992 bool Intrinsifier::Build_MathCos(FlowGraph* flow_graph) {
993 if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
994
995 GraphEntryInstr* graph_entry = flow_graph->graph_entry();
996 TargetEntryInstr* normal_entry = graph_entry->normal_entry();
997 BlockBuilder builder(flow_graph, normal_entry);
998
999 return BuildInvokeMathCFunction(&builder,
1000 MethodRecognizer::kMathCos);
1001 }
1002
1003
1004 bool Intrinsifier::Build_MathTan(FlowGraph* flow_graph) {
1005 if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
1006
1007 GraphEntryInstr* graph_entry = flow_graph->graph_entry();
1008 TargetEntryInstr* normal_entry = graph_entry->normal_entry();
1009 BlockBuilder builder(flow_graph, normal_entry);
1010
1011 return BuildInvokeMathCFunction(&builder,
1012 MethodRecognizer::kMathTan);
1013 }
1014
1015
1016 bool Intrinsifier::Build_MathAsin(FlowGraph* flow_graph) {
1017 if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
1018
1019 GraphEntryInstr* graph_entry = flow_graph->graph_entry();
1020 TargetEntryInstr* normal_entry = graph_entry->normal_entry();
1021 BlockBuilder builder(flow_graph, normal_entry);
1022
1023 return BuildInvokeMathCFunction(&builder,
1024 MethodRecognizer::kMathAsin);
1025 }
1026
1027
1028 bool Intrinsifier::Build_MathAcos(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 return BuildInvokeMathCFunction(&builder,
1036 MethodRecognizer::kMathAcos);
1037 }
1038
1039
1040 bool Intrinsifier::Build_MathAtan(FlowGraph* flow_graph) {
1041 if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
1042
1043 GraphEntryInstr* graph_entry = flow_graph->graph_entry();
1044 TargetEntryInstr* normal_entry = graph_entry->normal_entry();
1045 BlockBuilder builder(flow_graph, normal_entry);
1046
1047 return BuildInvokeMathCFunction(&builder,
1048 MethodRecognizer::kMathAtan);
1049 }
1050
1051
1052 bool Intrinsifier::Build_MathAtan2(FlowGraph* flow_graph) {
1053 if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
1054
1055 GraphEntryInstr* graph_entry = flow_graph->graph_entry();
1056 TargetEntryInstr* normal_entry = graph_entry->normal_entry();
1057 BlockBuilder builder(flow_graph, normal_entry);
1058
1059 Definition* value_a = builder.AddParameter(1);
1060 Definition* value_b = builder.AddParameter(2);
1061
1062 Definition* unboxed_a = builder.AddUnboxInstr(kUnboxedDouble, value_a);
1063 Definition* unboxed_b = builder.AddUnboxInstr(kUnboxedDouble, value_b);
1064
1065 // C's atan2 expects the arguments in the opposite order.
1066 ZoneGrowableArray<Value*>* args =
1067 new ZoneGrowableArray<Value*>(2);
1068 args->Add(new Value(unboxed_b));
1069 args->Add(new Value(unboxed_a));
1070
1071 Definition* unboxed_result =
1072 builder.InvokeMathCFunction(MethodRecognizer::kMathAtan2,
1073 args);
1074
1075 Definition* result = builder.AddDefinition(
1076 BoxInstr::Create(kUnboxedDouble, new Value(unboxed_result)));
1077
1078 builder.AddIntrinsicReturn(new Value(result));
1079
1080 return true;
1081 }
1082
886 } // namespace dart 1083 } // 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