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

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 rewrite mode & finetune type feedback 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
« no previous file with comments | « src/code-stubs.h ('k') | src/code-stubs-hydrogen.cc » ('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 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", op_name, ovr);
212 if (left_type_ == BinaryOpIC::ODDBALL && right_type_ == BinaryOpIC::ODDBALL) { 213 }
213 // The OddballStub handles a number and an oddball, not two oddballs. 214
214 operands_type = BinaryOpIC::GENERIC; 215
215 } 216 void BinaryOpStub::PrintState(StringStream* stream) {
216 switch (operands_type) { 217 stream->Add("(");
217 case BinaryOpIC::UNINITIALIZED: 218 stream->Add(StateToName(left_state_));
218 GenerateTypeTransition(masm); 219 if (left_bool_) {
220 stream->Add(",Boolean");
221 }
222 stream->Add("*");
223 if (fixed_right_arg_.has_value) {
224 stream->Add("%d", fixed_right_arg_.value);
225 } else {
226 stream->Add(StateToName(right_state_));
227 if (right_bool_) {
228 stream->Add(",Boolean");
229 }
230 }
231 stream->Add("->");
232 stream->Add(StateToName(result_state_));
233 stream->Add(")");
234 }
235
236
237 Maybe<Handle<Object> > BinaryOpStub::Result(Handle<Object> left,
238 Handle<Object> right,
239 Isolate* isolate) {
240 Handle<JSBuiltinsObject> builtins(isolate->js_builtins_object());
241 Builtins::JavaScript func = BinaryOpIC::TokenToJSBuiltin(op_);
242 Object* builtin = builtins->javascript_builtin(func);
243 Handle<JSFunction> builtin_function =
244 Handle<JSFunction>(JSFunction::cast(builtin), isolate);
245 bool caught_exception;
246 Handle<Object> result = Execution::Call(isolate, builtin_function, left,
247 1, &right, &caught_exception);
248 return Maybe<Handle<Object> >(!caught_exception, result);
249 }
250
251
252 void BinaryOpStub::Initialize() {
253 fixed_right_arg_.has_value = false;
254 left_state_ = right_state_ = result_state_ = NONE;
255 left_bool_ = right_bool_ = false;
256 }
257
258
259 void BinaryOpStub::Generate(Token::Value op,
260 State left,
261 State right,
262 State result,
263 Isolate* isolate) {
264 BinaryOpStub stub(INITIALIZED);
265 stub.op_ = op;
266 stub.left_state_ = left;
267 stub.right_state_ = right;
268 stub.result_state_ = result;
269 stub.mode_ = NO_OVERWRITE;
270 stub.GetCode(isolate);
271 stub.mode_ = OVERWRITE_LEFT;
272 stub.GetCode(isolate);
273 }
274
275
276 void BinaryOpStub::GenerateAheadOfTime(Isolate* isolate) {
277 Token::Value binop[] = {Token::SUB, Token::MOD, Token::DIV, Token::MUL,
278 Token::ADD, Token::SAR, Token::BIT_OR, Token::BIT_AND,
279 Token::BIT_XOR, Token::SHL, Token::SHR};
280 // TODO(olivf) NumberTagU is not snapshot safe yet so we have to skip SHR
281 // since that produces a unsigned int32.
282 Token::Value bitop[] = {Token::BIT_OR, Token::BIT_AND, Token::BIT_XOR,
283 Token::SAR, Token::SHL /* Token::SHR */};
284 Token::Value arithop[] = {Token::ADD, Token::SUB, Token::MOD,
285 Token::DIV, Token::MUL};
286 for (unsigned i = 0; i < ARRAY_SIZE(binop); i++) {
287 BinaryOpStub stub(UNINITIALIZED);
288 stub.op_ = binop[i];
289 stub.GetCode(isolate);
290 }
291 for (unsigned i = 0; i < ARRAY_SIZE(arithop); i++) {
292 Generate(arithop[i], SMI, SMI, SMI, isolate);
293 Generate(arithop[i], SMI, SMI, INT32, isolate);
294 Generate(arithop[i], SMI, SMI, NUMBER, isolate);
295 Generate(arithop[i], INT32, INT32, INT32, isolate);
296 Generate(arithop[i], NUMBER, SMI, SMI, isolate);
297 Generate(arithop[i], NUMBER, SMI, NUMBER, isolate);
298 Generate(arithop[i], NUMBER, INT32, NUMBER, isolate);
299 Generate(arithop[i], NUMBER, NUMBER, NUMBER, isolate);
300 }
301 Generate(Token::SHR, SMI, SMI, SMI, isolate);
302 for (unsigned i = 0; i < ARRAY_SIZE(bitop); i++) {
303 Generate(bitop[i], SMI, SMI, SMI, isolate);
304 Generate(bitop[i], SMI, INT32, INT32, isolate);
305 Generate(bitop[i], INT32, INT32, INT32, isolate);
306 Generate(bitop[i], NUMBER, INT32, INT32, isolate);
307 Generate(bitop[i], NUMBER, NUMBER, INT32, isolate);
308 }
309 Generate(Token::ADD, STRING, STRING, STRING, isolate);
310
311 BinaryOpStub stub(INITIALIZED);
312 stub.op_ = Token::MOD;
313 stub.left_state_ = SMI;
314 stub.right_state_ = SMI;
315 stub.result_state_ = SMI;
316 stub.fixed_right_arg_.has_value = true;
317 stub.fixed_right_arg_.value = 4;
318 stub.mode_ = NO_OVERWRITE;
319 stub.GetCode(isolate);
320 stub.fixed_right_arg_.value = 8;
321 stub.GetCode(isolate);
322 }
323
324
325 bool BinaryOpStub::can_encode_arg_value(int32_t value) const {
326 return op_ == Token::MOD && value > 0 && IsPowerOf2(value) &&
327 FixedRightArgValueBits::is_valid(WhichPowerOf2(value));
328 }
329
330
331 int BinaryOpStub::encode_arg_value(int32_t value) const {
332 ASSERT(can_encode_arg_value(value));
333 return WhichPowerOf2(value);
334 }
335
336
337 int32_t BinaryOpStub::decode_arg_value(int value) const {
338 return 1 << value;
339 }
340
341
342 int BinaryOpStub::encode_token(Token::Value op) const {
343 ASSERT(op >= FIRST_TOKEN && op <= LAST_TOKEN);
344 return op - FIRST_TOKEN;
345 }
346
347
348 Token::Value BinaryOpStub::decode_token(int op) const {
349 int res = op + FIRST_TOKEN;
350 ASSERT(res >= FIRST_TOKEN && res <= LAST_TOKEN);
351 return static_cast<Token::Value>(res);
352 }
353
354
355 const char* BinaryOpStub::StateToName(State state) {
356 switch (state) {
357 case NONE:
358 return "None";
359 case SMI:
360 return "Smi";
361 case INT32:
362 return "Int32";
363 case NUMBER:
364 return "Number";
365 case STRING:
366 return "String";
367 case GENERIC:
368 return "Generic";
369 }
370 return "";
371 }
372
373
374 void BinaryOpStub::UpdateStatus(Handle<Object> left,
375 Handle<Object> right,
376 Maybe<Handle<Object> > result) {
377 int old_state = GetExtraICState();
378
379 UpdateStatus(left, &left_state_, &left_bool_);
380 UpdateStatus(right, &right_state_, &right_bool_);
381
382 int32_t value;
383 bool new_has_fixed_right_arg =
384 right->ToInt32(&value) && can_encode_arg_value(value) &&
385 (left_state_ == SMI || left_state_ == INT32) &&
386 (result_state_ == NONE || !fixed_right_arg_.has_value);
387
388 fixed_right_arg_ = Maybe<int32_t>(new_has_fixed_right_arg, value);
389
390 if (result.has_value) UpdateStatus(result.value, &result_state_, NULL);
391
392 State max_result = has_int_result() ? INT32 : NUMBER;
393 State max_input = Max(left_state_, right_state_);
394
395 // Avoid unnecessary Representation changes.
396 if (left_state_ == STRING && right_state_ < STRING) {
397 right_state_ = GENERIC;
398 } else if (right_state_ == STRING && left_state_ < STRING) {
399 left_state_ = GENERIC;
400 } else if ((right_state_ == GENERIC && left_state_ != STRING) ||
401 (left_state_ == GENERIC && right_state_ != STRING)) {
402 left_state_ = right_state_ = GENERIC;
403 } else if (max_input <= NUMBER && max_input > result_state_) {
404 result_state_ = Min(max_result, max_input);
405 }
406
407 ASSERT(result_state_ <= max_result || op_ == Token::ADD);
408
409 if (old_state == GetExtraICState()) {
410 // Since the fpu is to precise, we might bail out on numbers which
411 // actually would truncate with 64 bit precision.
412 ASSERT(!CpuFeatures::IsSupported(SSE2) &&
413 result_state_ <= INT32);
414 result_state_ = NUMBER;
415 }
416 }
417
418
419 void BinaryOpStub::UpdateStatus(Handle<Object> object,
420 State* state,
421 bool* bool_state) {
422 if (object->IsBoolean() && bool_state != NULL) {
423 *bool_state = true;
424 return;
425 }
426 v8::internal::TypeInfo type = v8::internal::TypeInfo::FromValue(object);
427 if (object->IsUndefined()) {
428 // Undefined will be automatically truncated for us by HChange.
429 type = (op_ == Token::BIT_AND || op_ == Token::BIT_OR ||
430 op_ == Token::BIT_XOR || op_ == Token::SAR ||
431 op_ == Token::SHL || op_ == Token::SHR)
432 ? TypeInfo::Integer32()
433 : TypeInfo::Double();
434 }
435 State int_state = SmiValuesAre32Bits() ? NUMBER : INT32;
436 State new_state = NONE;
437 if (type.IsSmi()) {
438 new_state = SMI;
439 } else if (type.IsInteger32()) {
440 new_state = int_state;
441 } else if (type.IsNumber()) {
442 new_state = NUMBER;
443 } else if (object->IsString() && operation() == Token::ADD) {
444 new_state = STRING;
445 } else {
446 new_state = GENERIC;
447 }
448 if ((new_state <= NUMBER && *state > NUMBER) ||
449 (new_state > NUMBER && *state <= NUMBER && *state != NONE)) {
450 new_state = GENERIC;
451 }
452 *state = Max(*state, new_state);
453 }
454
455
456 Handle<Type> BinaryOpStub::StateToType(State state,
457 bool seen_bool,
458 Isolate* isolate) {
459 Handle<Type> t = handle(Type::None(), isolate);
460 switch (state) {
461 case NUMBER:
462 t = handle(Type::Union(t, handle(Type::Double(), isolate)), isolate);
463 // Fall through.
464 case INT32:
465 t = handle(Type::Union(t, handle(Type::Signed32(), isolate)), isolate);
466 // Fall through.
467 case SMI:
468 t = handle(Type::Union(t, handle(Type::Smi(), isolate)), isolate);
219 break; 469 break;
220 case BinaryOpIC::SMI: 470
221 GenerateSmiStub(masm); 471 case STRING:
472 t = handle(Type::Union(t, handle(Type::String(), isolate)), isolate);
222 break; 473 break;
223 case BinaryOpIC::INT32: 474 case GENERIC:
224 GenerateInt32Stub(masm); 475 return handle(Type::Any(), isolate);
225 break; 476 break;
226 case BinaryOpIC::NUMBER: 477 case NONE:
227 GenerateNumberStub(masm);
228 break; 478 break;
229 case BinaryOpIC::ODDBALL: 479 }
230 GenerateOddballStub(masm); 480 if (seen_bool) {
231 break; 481 t = handle(Type::Union(t, handle(Type::Boolean(), isolate)), isolate);
232 case BinaryOpIC::STRING: 482 }
233 GenerateStringStub(masm); 483 return t;
234 break; 484 }
235 case BinaryOpIC::GENERIC: 485
236 GenerateGeneric(masm); 486
237 break; 487 Handle<Type> BinaryOpStub::GetLeftType(Isolate* isolate) const {
238 default: 488 return StateToType(left_state_, left_bool_, isolate);
239 UNREACHABLE(); 489 }
240 } 490
241 } 491
242 492 Handle<Type> BinaryOpStub::GetRightType(Isolate* isolate) const {
243 493 return StateToType(right_state_, right_bool_, isolate);
244 #define __ ACCESS_MASM(masm) 494 }
245 495
246 496
247 void BinaryOpStub::GenerateCallRuntime(MacroAssembler* masm) { 497 Handle<Type> BinaryOpStub::GetResultType(Isolate* isolate) const {
248 switch (op_) { 498 if (HasSideEffects(isolate)) return StateToType(NONE, false, isolate);
249 case Token::ADD: 499 if (result_state_ == GENERIC && op_ == Token::ADD) {
250 __ InvokeBuiltin(Builtins::ADD, CALL_FUNCTION); 500 return handle(Type::Union(handle(Type::Number(), isolate),
251 break; 501 handle(Type::String(), isolate)), isolate);
252 case Token::SUB: 502 }
253 __ InvokeBuiltin(Builtins::SUB, CALL_FUNCTION); 503 ASSERT(result_state_ != GENERIC);
254 break; 504 if (result_state_ == NUMBER && op_ == Token::SHR) {
255 case Token::MUL: 505 return handle(Type::Unsigned32(), isolate);
256 __ InvokeBuiltin(Builtins::MUL, CALL_FUNCTION); 506 }
257 break; 507 return StateToType(result_state_, false, isolate);
258 case Token::DIV: 508 }
259 __ InvokeBuiltin(Builtins::DIV, CALL_FUNCTION); 509
260 break; 510
261 case Token::MOD:
262 __ InvokeBuiltin(Builtins::MOD, CALL_FUNCTION);
263 break;
264 case Token::BIT_OR:
265 __ InvokeBuiltin(Builtins::BIT_OR, CALL_FUNCTION);
266 break;
267 case Token::BIT_AND:
268 __ InvokeBuiltin(Builtins::BIT_AND, CALL_FUNCTION);
269 break;
270 case Token::BIT_XOR:
271 __ InvokeBuiltin(Builtins::BIT_XOR, CALL_FUNCTION);
272 break;
273 case Token::SAR:
274 __ InvokeBuiltin(Builtins::SAR, CALL_FUNCTION);
275 break;
276 case Token::SHR:
277 __ InvokeBuiltin(Builtins::SHR, CALL_FUNCTION);
278 break;
279 case Token::SHL:
280 __ InvokeBuiltin(Builtins::SHL, CALL_FUNCTION);
281 break;
282 default:
283 UNREACHABLE();
284 }
285 }
286
287
288 #undef __
289
290
291 void BinaryOpStub::PrintName(StringStream* stream) {
292 const char* op_name = Token::Name(op_);
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() { 511 InlineCacheState ICCompareStub::GetICState() {
323 CompareIC::State state = Max(left_, right_); 512 CompareIC::State state = Max(left_, right_);
324 switch (state) { 513 switch (state) {
325 case CompareIC::UNINITIALIZED: 514 case CompareIC::UNINITIALIZED:
326 return ::v8::internal::UNINITIALIZED; 515 return ::v8::internal::UNINITIALIZED;
327 case CompareIC::SMI: 516 case CompareIC::SMI:
328 case CompareIC::NUMBER: 517 case CompareIC::NUMBER:
329 case CompareIC::INTERNALIZED_STRING: 518 case CompareIC::INTERNALIZED_STRING:
330 case CompareIC::STRING: 519 case CompareIC::STRING:
331 case CompareIC::UNIQUE_NAME: 520 case CompareIC::UNIQUE_NAME:
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after
795 InstallDescriptor(isolate, &stub3); 984 InstallDescriptor(isolate, &stub3);
796 } 985 }
797 986
798 InternalArrayConstructorStub::InternalArrayConstructorStub( 987 InternalArrayConstructorStub::InternalArrayConstructorStub(
799 Isolate* isolate) { 988 Isolate* isolate) {
800 InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate); 989 InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
801 } 990 }
802 991
803 992
804 } } // namespace v8::internal 993 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/code-stubs.h ('k') | src/code-stubs-hydrogen.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698