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

Side by Side Diff: src/code-stubs.cc

Issue 24072013: Hydrogenisation of binops (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: fix overwrite mode & better result typefeedback Created 7 years, 2 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 130
131 131
132 Handle<Code> CodeStub::GetCode(Isolate* isolate) { 132 Handle<Code> CodeStub::GetCode(Isolate* isolate) {
133 Factory* factory = isolate->factory(); 133 Factory* factory = isolate->factory();
134 Heap* heap = isolate->heap(); 134 Heap* heap = isolate->heap();
135 Code* code; 135 Code* code;
136 if (UseSpecialCache() 136 if (UseSpecialCache()
137 ? FindCodeInSpecialCache(&code, isolate) 137 ? FindCodeInSpecialCache(&code, isolate)
138 : FindCodeInCache(&code, isolate)) { 138 : FindCodeInCache(&code, isolate)) {
139 ASSERT(IsPregenerated(isolate) == code->is_pregenerated()); 139 ASSERT(IsPregenerated(isolate) == code->is_pregenerated());
140 ASSERT(GetCodeKind() == code->kind());
140 return Handle<Code>(code); 141 return Handle<Code>(code);
141 } 142 }
142 143
143 { 144 {
144 HandleScope scope(isolate); 145 HandleScope scope(isolate);
145 146
146 Handle<Code> new_object = GenerateCode(isolate); 147 Handle<Code> new_object = GenerateCode(isolate);
147 new_object->set_major_key(MajorKey()); 148 new_object->set_major_key(MajorKey());
148 FinishCode(new_object); 149 FinishCode(new_object);
149 RecordCodeGeneration(*new_object, isolate); 150 RecordCodeGeneration(*new_object, isolate);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 stream->Add("%s", MajorName(MajorKey(), false)); 197 stream->Add("%s", MajorName(MajorKey(), false));
197 } 198 }
198 199
199 200
200 void CodeStub::PrintName(StringStream* stream) { 201 void CodeStub::PrintName(StringStream* stream) {
201 PrintBaseName(stream); 202 PrintBaseName(stream);
202 PrintState(stream); 203 PrintState(stream);
203 } 204 }
204 205
205 206
206 void BinaryOpStub::Generate(MacroAssembler* masm) { 207 void BinaryOpStub::PrintBaseName(StringStream* stream) {
207 // Explicitly allow generation of nested stubs. It is safe here because 208 const char* op_name = Token::Name(op_);
208 // generation code does not use any raw pointers. 209 const char* ovr = "";
209 AllowStubCallsScope allow_stub_calls(masm, true); 210 if (mode_ == OVERWRITE_LEFT) ovr = "_ReuseLeft";
210 211 if (mode_ == OVERWRITE_RIGHT) ovr = "_ReuseRight";
211 BinaryOpIC::TypeInfo operands_type = Max(left_type_, right_type_); 212 stream->Add("BinaryOpStub_%s%s",
212 if (left_type_ == BinaryOpIC::ODDBALL && right_type_ == BinaryOpIC::ODDBALL) { 213 op_name,
Michael Starzinger 2013/09/27 08:36:11 nit: Should fit into one line.
213 // The OddballStub handles a number and an oddball, not two oddballs. 214 ovr);
214 operands_type = BinaryOpIC::GENERIC; 215 }
215 } 216
216 switch (operands_type) { 217
217 case BinaryOpIC::UNINITIALIZED: 218 void BinaryOpStub::PrintState(StringStream* stream) {
218 GenerateTypeTransition(masm); 219 stream->Add("(");
219 break; 220 stream->Add(StateToName(left_state_));
220 case BinaryOpIC::SMI: 221 if (left_bool_) {
221 GenerateSmiStub(masm); 222 stream->Add(",Boolean");
222 break; 223 }
223 case BinaryOpIC::INT32: 224 stream->Add("*");
224 GenerateInt32Stub(masm); 225 if (fixed_right_arg_.has_value) {
225 break; 226 stream->Add("%d", fixed_right_arg_.value);
226 case BinaryOpIC::NUMBER: 227 } else {
227 GenerateNumberStub(masm); 228 stream->Add(StateToName(right_state_));
228 break; 229 if (right_bool_) {
229 case BinaryOpIC::ODDBALL: 230 stream->Add(",Boolean");
230 GenerateOddballStub(masm); 231 }
231 break; 232 }
232 case BinaryOpIC::STRING: 233 stream->Add("->");
233 GenerateStringStub(masm); 234 stream->Add(StateToName(result_state_));
234 break; 235 stream->Add(")");
235 case BinaryOpIC::GENERIC: 236 }
236 GenerateGeneric(masm); 237
237 break; 238
239 Maybe<Handle<Object> > BinaryOpStub::Result(Handle<Object> left,
240 Handle<Object> right,
241 Isolate* isolate) {
242 Handle<JSBuiltinsObject> builtins(isolate->js_builtins_object());
243 Builtins::JavaScript func = BinaryOpIC::TokenToJSBuiltin(op_);
244 Object* builtin = builtins->javascript_builtin(func);
245 Handle<JSFunction> builtin_function =
246 Handle<JSFunction>(JSFunction::cast(builtin), isolate);
247 bool caught_exception;
248 Handle<Object> result = Execution::Call(isolate, builtin_function, left,
249 1, &right, &caught_exception);
250 return Maybe<Handle<Object> >(!caught_exception, result);
251 }
252
253
254 void BinaryOpStub::init() {
255 fixed_right_arg_.has_value = false;
256 left_state_ = right_state_ = result_state_ = NONE;
257 left_bool_ = right_bool_ = false;
258 }
259
260
261 void BinaryOpStub::Generate(Token::Value op,
262 State left,
263 State right,
264 State result,
265 Isolate* isolate) {
266 BinaryOpStub stub(INITIALIZED);
267 stub.op_ = op;
268 stub.left_state_ = left;
269 stub.right_state_ = right;
270 stub.result_state_ = result;
271 stub.mode_ = NO_OVERWRITE;
272 stub.GetCode(isolate);
273 stub.mode_ = OVERWRITE_LEFT;
274 stub.GetCode(isolate);
275 stub.mode_ = OVERWRITE_RIGHT;
276 stub.GetCode(isolate);
277 }
278
279
280 void BinaryOpStub::GenerateAheadOfTime(Isolate* isolate) {
281 Token::Value binop[] = {Token::SUB, Token::MOD, Token::DIV, Token::MUL,
282 Token::ADD, Token::SAR, Token::BIT_OR, Token::BIT_AND,
283 Token::BIT_XOR, Token::SHL, Token::SHR};
284 // TODO(olivf) NumberTagU is not snapshot safe yet so we have to skip SHR
285 // since that produces a unsigned int32.
286 Token::Value bitop[] = {Token::BIT_OR, Token::BIT_AND, Token::BIT_XOR,
287 Token::SAR, Token::SHL /* Token::SHR */};
288 Token::Value arithop[] = {Token::ADD, Token::SUB, Token::MOD,
289 Token::DIV, Token::MUL};
290
291 for (int i = 0; i < 10; i++) {
292 BinaryOpStub stub(UNINITIALIZED);
293 stub.op_ = binop[i];
294 stub.GetCode(isolate);
295 }
296 for (int i = 0; i < 5; i++) {
Michael Starzinger 2013/09/27 08:36:11 nit: s/5/ARRAY_SIZE(arithop)/
297 Generate(arithop[i], SMI, SMI, SMI, isolate);
298 Generate(arithop[i], SMI, SMI, INT32, isolate);
299 Generate(arithop[i], SMI, SMI, NUMBER, isolate);
300 Generate(arithop[i], SMI, INT32, INT32, isolate);
301 Generate(arithop[i], SMI, INT32, NUMBER, isolate);
302 Generate(arithop[i], SMI, NUMBER, NUMBER, isolate);
303 Generate(arithop[i], INT32, SMI, INT32, isolate);
304 Generate(arithop[i], INT32, SMI, NUMBER, isolate);
305 Generate(arithop[i], INT32, INT32, INT32, isolate);
306 Generate(arithop[i], INT32, INT32, NUMBER, isolate);
307 Generate(arithop[i], INT32, NUMBER, NUMBER, isolate);
308 Generate(arithop[i], NUMBER, SMI, NUMBER, isolate);
309 Generate(arithop[i], NUMBER, INT32, NUMBER, isolate);
310 Generate(arithop[i], NUMBER, NUMBER, NUMBER, isolate);
311 }
312 Generate(Token::SHR, SMI, SMI, SMI, isolate);
313 for (int i = 0; i < 5; i++) {
Michael Starzinger 2013/09/27 08:36:11 nit: s/5/ARRAY_SIZE(bitop)/
314 Generate(bitop[i], SMI, SMI, SMI, isolate);
315 Generate(bitop[i], SMI, SMI, INT32, isolate);
316 Generate(bitop[i], SMI, INT32, INT32, isolate);
317 Generate(bitop[i], SMI, NUMBER, INT32, isolate);
318 Generate(bitop[i], INT32, INT32, INT32, isolate);
319 Generate(bitop[i], INT32, SMI, INT32, isolate);
320 Generate(bitop[i], INT32, NUMBER, INT32, isolate);
321 Generate(bitop[i], NUMBER, SMI, INT32, isolate);
322 Generate(bitop[i], NUMBER, INT32, INT32, isolate);
323 Generate(bitop[i], NUMBER, NUMBER, INT32, isolate);
324 }
325 BinaryOpStub stub(INITIALIZED);
326 stub.op_ = Token::MOD;
327 stub.left_state_ = SMI;
328 stub.right_state_ = SMI;
329 stub.result_state_ = SMI;
330 stub.fixed_right_arg_.has_value = true;
331 stub.fixed_right_arg_.value = 4;
332 stub.mode_ = NO_OVERWRITE;
333 stub.GetCode(isolate);
334 stub.mode_ = OVERWRITE_LEFT;
335 stub.GetCode(isolate);
336 stub.fixed_right_arg_.value = 8;
337 stub.mode_ = NO_OVERWRITE;
338 stub.GetCode(isolate);
339 stub.mode_ = OVERWRITE_LEFT;
340 stub.GetCode(isolate);
341 }
342
343
344 bool BinaryOpStub::can_encode_arg_value(int32_t value) const {
345 return op_ == Token::MOD && value > 0 && IsPowerOf2(value) &&
346 FixedRightArgValueBits::is_valid(WhichPowerOf2(value));
347 }
348
349
350 int BinaryOpStub::encode_arg_value(int32_t value) const {
351 ASSERT(can_encode_arg_value(value));
352 return WhichPowerOf2(value);
353 }
354
355
356 int32_t BinaryOpStub::decode_arg_value(int value) const {
357 return 1 << value;
358 }
359
360
361 int BinaryOpStub::encode_token(Token::Value op) const {
362 ASSERT(op >= FIRST_TOKEN && op <= LAST_TOKEN);
363 return op - FIRST_TOKEN;
364 }
365
366
367 Token::Value BinaryOpStub::decode_token(int op) const {
368 int res = op + FIRST_TOKEN;
369 ASSERT(res >= FIRST_TOKEN && res <= LAST_TOKEN);
370 return static_cast<Token::Value>(res);
371 }
372
373
374 const char* BinaryOpStub::StateToName(State state) {
375 switch (state) {
238 default: 376 default:
239 UNREACHABLE(); 377 UNREACHABLE();
Michael Starzinger 2013/09/27 08:36:11 nit: Drop the default case, the switch should be e
240 } 378 case NONE:
241 } 379 return "None";
242 380 case SMI:
243 381 return "Smi";
244 #define __ ACCESS_MASM(masm) 382 case INT32:
245 383 return "Int32";
246 384 case NUMBER:
247 void BinaryOpStub::GenerateCallRuntime(MacroAssembler* masm) { 385 return "Number";
248 switch (op_) { 386 case STRING:
249 case Token::ADD: 387 return "String";
250 __ InvokeBuiltin(Builtins::ADD, CALL_FUNCTION); 388 case GENERIC:
389 return "Generic";
390 }
391 }
392
393
394 void BinaryOpStub::UpdateStatus(Handle<Object> left,
395 Handle<Object> right,
396 Maybe<Handle<Object> > result) {
397 int old_state = GetExtraICState();
398
399 UpdateStatus(left, &left_state_, &left_bool_);
400 UpdateStatus(right, &right_state_, &right_bool_);
401
402 int32_t value;
403 bool new_has_fixed_right_arg =
404 right->ToInt32(&value) && can_encode_arg_value(value) &&
405 (left_state_ == SMI || left_state_ == INT32) &&
406 (result_state_ == NONE || !fixed_right_arg_.has_value);
407
408 fixed_right_arg_ = Maybe<int32_t>(new_has_fixed_right_arg, value);
409
410 if (result.has_value) UpdateStatus(result.value, &result_state_, NULL);
411
412 State max_result = has_int_result() ? INT32 : NUMBER;
413 State max_input = Max(left_state_, right_state_);
414 State min_input = Min(left_state_, right_state_);
415
416 // Avoid unnecessary Representation changes.
417 if (left_state_ == STRING && right_state_ < STRING) {
418 right_state_ = GENERIC;
419 } else if (right_state_ == STRING && left_state_ < STRING) {
420 left_state_ = GENERIC;
421 } else if ((right_state_ == GENERIC && left_state_ != STRING) ||
422 (left_state_ == GENERIC && right_state_ != STRING)) {
423 left_state_ = right_state_ = GENERIC;
424 } else if (max_input <= NUMBER && min_input > result_state_) {
425 result_state_ = Min(max_result, min_input);
426 }
427
428 ASSERT(result_state_ <= max_result || op_ == Token::ADD);
429
430 if (old_state == GetExtraICState()) {
431 // Since the fpu is to precise, we might bail out on numbers which
432 // actually would truncate with 64 bit precision.
433 ASSERT(!CpuFeatures::IsSupported(SSE2) &&
434 result_state_ <= INT32);
435 result_state_ = NUMBER;
436 }
437 }
438
439
440 void BinaryOpStub::UpdateStatus(Handle<Object> object,
441 State* state,
442 bool* bool_state) {
443 if (object->IsBoolean() && bool_state != NULL) {
444 *bool_state = true;
445 return;
446 }
447 v8::internal::TypeInfo type = v8::internal::TypeInfo::FromValue(object);
448 if (object->IsUndefined()) {
449 // Undefined will be automatically truncated for us by HChange.
450 type = (op_ == Token::BIT_AND || op_ == Token::BIT_OR ||
451 op_ == Token::BIT_XOR || op_ == Token::SAR ||
452 op_ == Token::SHL || op_ == Token::SHR)
453 ? TypeInfo::Integer32()
454 : TypeInfo::Double();
455 }
456 State int_state = SmiValuesAre32Bits() ? NUMBER : INT32;
457 State new_state = NONE;
458 if (type.IsSmi()) {
459 new_state = SMI;
460 } else if (type.IsInteger32()) {
461 new_state = int_state;
462 } else if (type.IsNumber()) {
463 new_state = NUMBER;
464 } else if (object->IsString() && operation() == Token::ADD) {
465 new_state = STRING;
466 } else {
467 new_state = GENERIC;
468 }
469 if ((new_state <= NUMBER && *state > NUMBER) ||
470 (new_state > NUMBER && *state <= NUMBER && *state != NONE)) {
471 new_state = GENERIC;
472 }
473 *state = Max(*state, new_state);
474 }
475
476
477 Handle<Type> BinaryOpStub::StateToType(State state,
478 bool seen_bool,
479 Isolate* isolate) {
480 Handle<Type> t = handle(Type::None(), isolate);
481 switch (state) {
482 case NUMBER:
483 t = handle(Type::Union(t, handle(Type::Double(), isolate)), isolate);
Michael Starzinger 2013/09/27 08:36:11 Missing break statement.
484 case INT32:
485 t = handle(Type::Union(t, handle(Type::Signed32(), isolate)), isolate);
Michael Starzinger 2013/09/27 08:36:11 Missing break statement.
486 case SMI:
487 t = handle(Type::Union(t, handle(Type::Smi(), isolate)), isolate);
251 break; 488 break;
252 case Token::SUB: 489
253 __ InvokeBuiltin(Builtins::SUB, CALL_FUNCTION); 490 case STRING:
491 t = handle(Type::Union(t, handle(Type::String(), isolate)), isolate);
254 break; 492 break;
255 case Token::MUL: 493 case GENERIC:
256 __ InvokeBuiltin(Builtins::MUL, CALL_FUNCTION); 494 return handle(Type::Any(), isolate);
257 break; 495 break;
258 case Token::DIV: 496 case NONE:
259 __ InvokeBuiltin(Builtins::DIV, CALL_FUNCTION);
260 break; 497 break;
261 case Token::MOD: 498 }
262 __ InvokeBuiltin(Builtins::MOD, CALL_FUNCTION); 499 if (seen_bool) {
263 break; 500 t = handle(Type::Union(t, handle(Type::Boolean(), isolate)), isolate);
264 case Token::BIT_OR: 501 }
265 __ InvokeBuiltin(Builtins::BIT_OR, CALL_FUNCTION); 502 return t;
266 break; 503 }
267 case Token::BIT_AND: 504
268 __ InvokeBuiltin(Builtins::BIT_AND, CALL_FUNCTION); 505
269 break; 506 Handle<Type> BinaryOpStub::GetLeftType(Isolate* isolate) const {
270 case Token::BIT_XOR: 507 return StateToType(left_state_, left_bool_, isolate);
271 __ InvokeBuiltin(Builtins::BIT_XOR, CALL_FUNCTION); 508 }
272 break; 509
273 case Token::SAR: 510
274 __ InvokeBuiltin(Builtins::SAR, CALL_FUNCTION); 511 Handle<Type> BinaryOpStub::GetRightType(Isolate* isolate) const {
275 break; 512 return StateToType(right_state_, right_bool_, isolate);
276 case Token::SHR: 513 }
277 __ InvokeBuiltin(Builtins::SHR, CALL_FUNCTION); 514
278 break; 515
279 case Token::SHL: 516 Handle<Type> BinaryOpStub::GetResultType(Isolate* isolate) const {
280 __ InvokeBuiltin(Builtins::SHL, CALL_FUNCTION); 517 if (HasSideEffects(isolate)) return StateToType(NONE, false, isolate);
281 break; 518 if (result_state_ == GENERIC && op_ == Token::ADD) {
282 default: 519 return handle(Type::Union(handle(Type::Number(), isolate),
283 UNREACHABLE(); 520 handle(Type::String(), isolate)), isolate);
284 } 521 }
285 } 522 ASSERT(result_state_ != GENERIC);
286 523 if (result_state_ == NUMBER && op_ == Token::SHR) {
287 524 return handle(Type::Unsigned32(), isolate);
288 #undef __ 525 }
289 526 return StateToType(result_state_, false, isolate);
290 527 }
291 void BinaryOpStub::PrintName(StringStream* stream) { 528
292 const char* op_name = Token::Name(op_); 529
293 const char* overwrite_name;
294 switch (mode_) {
295 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
296 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
297 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
298 default: overwrite_name = "UnknownOverwrite"; break;
299 }
300 stream->Add("BinaryOpStub_%s_%s_%s+%s",
301 op_name,
302 overwrite_name,
303 BinaryOpIC::GetName(left_type_),
304 BinaryOpIC::GetName(right_type_));
305 }
306
307
308 void BinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
309 ASSERT(left_type_ == BinaryOpIC::STRING || right_type_ == BinaryOpIC::STRING);
310 ASSERT(op_ == Token::ADD);
311 if (left_type_ == BinaryOpIC::STRING && right_type_ == BinaryOpIC::STRING) {
312 GenerateBothStringStub(masm);
313 return;
314 }
315 // Try to add arguments as strings, otherwise, transition to the generic
316 // BinaryOpIC type.
317 GenerateAddStrings(masm);
318 GenerateTypeTransition(masm);
319 }
320
321
322 InlineCacheState ICCompareStub::GetICState() { 530 InlineCacheState ICCompareStub::GetICState() {
323 CompareIC::State state = Max(left_, right_); 531 CompareIC::State state = Max(left_, right_);
324 switch (state) { 532 switch (state) {
325 case CompareIC::UNINITIALIZED: 533 case CompareIC::UNINITIALIZED:
326 return ::v8::internal::UNINITIALIZED; 534 return ::v8::internal::UNINITIALIZED;
327 case CompareIC::SMI: 535 case CompareIC::SMI:
328 case CompareIC::NUMBER: 536 case CompareIC::NUMBER:
329 case CompareIC::INTERNALIZED_STRING: 537 case CompareIC::INTERNALIZED_STRING:
330 case CompareIC::STRING: 538 case CompareIC::STRING:
331 case CompareIC::UNIQUE_NAME: 539 case CompareIC::UNIQUE_NAME:
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after
795 InstallDescriptor(isolate, &stub3); 1003 InstallDescriptor(isolate, &stub3);
796 } 1004 }
797 1005
798 InternalArrayConstructorStub::InternalArrayConstructorStub( 1006 InternalArrayConstructorStub::InternalArrayConstructorStub(
799 Isolate* isolate) { 1007 Isolate* isolate) {
800 InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate); 1008 InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
801 } 1009 }
802 1010
803 1011
804 } } // namespace v8::internal 1012 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698