Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "ast.h" |
| 31 | #include "func-name-inferrer.h" |
| 32 | #include "scopes.h" |
| 33 | #include "rewriter.h" |
| 34 | |
| 35 | namespace v8 { |
| 36 | namespace internal { |
| 37 | |
| 38 | |
| 39 | class AstOptimizer: public AstVisitor { |
| 40 | public: |
| 41 | explicit AstOptimizer() : has_function_literal_(false) {} |
| 42 | explicit AstOptimizer(Handle<String> enclosing_name) |
| 43 | : has_function_literal_(false) { |
| 44 | func_name_inferrer_.PushEnclosingName(enclosing_name); |
| 45 | } |
| 46 | |
| 47 | void Optimize(ZoneList<Statement*>* statements); |
| 48 | |
| 49 | private: |
| 50 | // Used for loop condition analysis. Cleared before visiting a loop |
| 51 | // condition, set when a function literal is visited. |
| 52 | bool has_function_literal_; |
| 53 | // Helper object for function name inferring. |
| 54 | FuncNameInferrer func_name_inferrer_; |
| 55 | |
| 56 | // Helpers |
| 57 | void OptimizeArguments(ZoneList<Expression*>* arguments); |
| 58 | |
| 59 | // Node visitors. |
| 60 | #define DEF_VISIT(type) \ |
| 61 | virtual void Visit##type(type* node); |
| 62 | AST_NODE_LIST(DEF_VISIT) |
| 63 | #undef DEF_VISIT |
| 64 | |
| 65 | DISALLOW_COPY_AND_ASSIGN(AstOptimizer); |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | void AstOptimizer::Optimize(ZoneList<Statement*>* statements) { |
| 70 | int len = statements->length(); |
| 71 | for (int i = 0; i < len; i++) { |
| 72 | Visit(statements->at(i)); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void AstOptimizer::OptimizeArguments(ZoneList<Expression*>* arguments) { |
| 78 | for (int i = 0; i < arguments->length(); i++) { |
| 79 | Visit(arguments->at(i)); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | void AstOptimizer::VisitBlock(Block* node) { |
| 85 | Optimize(node->statements()); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | void AstOptimizer::VisitExpressionStatement(ExpressionStatement* node) { |
| 90 | Visit(node->expression()); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | void AstOptimizer::VisitIfStatement(IfStatement* node) { |
| 95 | Visit(node->condition()); |
| 96 | Visit(node->then_statement()); |
| 97 | if (node->HasElseStatement()) { |
| 98 | Visit(node->else_statement()); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 103 | void AstOptimizer::VisitDoWhileStatement(DoWhileStatement* node) { |
| 104 | Visit(node->cond()); |
| 105 | Visit(node->body()); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | void AstOptimizer::VisitWhileStatement(WhileStatement* node) { |
| 110 | has_function_literal_ = false; |
| 111 | Visit(node->cond()); |
| 112 | node->may_have_function_literal_ = has_function_literal_; |
| 113 | Visit(node->body()); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | void AstOptimizer::VisitForStatement(ForStatement* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 118 | if (node->init() != NULL) { |
| 119 | Visit(node->init()); |
| 120 | } |
| 121 | if (node->cond() != NULL) { |
| 122 | has_function_literal_ = false; |
| 123 | Visit(node->cond()); |
| 124 | node->may_have_function_literal_ = has_function_literal_; |
| 125 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 126 | Visit(node->body()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 127 | if (node->next() != NULL) { |
| 128 | Visit(node->next()); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | |
| 133 | void AstOptimizer::VisitForInStatement(ForInStatement* node) { |
| 134 | Visit(node->each()); |
| 135 | Visit(node->enumerable()); |
| 136 | Visit(node->body()); |
| 137 | } |
| 138 | |
| 139 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 140 | void AstOptimizer::VisitTryCatchStatement(TryCatchStatement* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 141 | Visit(node->try_block()); |
| 142 | Visit(node->catch_var()); |
| 143 | Visit(node->catch_block()); |
| 144 | } |
| 145 | |
| 146 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 147 | void AstOptimizer::VisitTryFinallyStatement(TryFinallyStatement* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 148 | Visit(node->try_block()); |
| 149 | Visit(node->finally_block()); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | void AstOptimizer::VisitSwitchStatement(SwitchStatement* node) { |
| 154 | Visit(node->tag()); |
| 155 | for (int i = 0; i < node->cases()->length(); i++) { |
| 156 | CaseClause* clause = node->cases()->at(i); |
| 157 | if (!clause->is_default()) { |
| 158 | Visit(clause->label()); |
| 159 | } |
| 160 | Optimize(clause->statements()); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | |
| 165 | void AstOptimizer::VisitContinueStatement(ContinueStatement* node) { |
| 166 | USE(node); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | void AstOptimizer::VisitBreakStatement(BreakStatement* node) { |
| 171 | USE(node); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | void AstOptimizer::VisitDeclaration(Declaration* node) { |
| 176 | // Will not be reached by the current optimizations. |
| 177 | USE(node); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | void AstOptimizer::VisitEmptyStatement(EmptyStatement* node) { |
| 182 | USE(node); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | void AstOptimizer::VisitReturnStatement(ReturnStatement* node) { |
| 187 | Visit(node->expression()); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | void AstOptimizer::VisitWithEnterStatement(WithEnterStatement* node) { |
| 192 | Visit(node->expression()); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | void AstOptimizer::VisitWithExitStatement(WithExitStatement* node) { |
| 197 | USE(node); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | void AstOptimizer::VisitDebuggerStatement(DebuggerStatement* node) { |
| 202 | USE(node); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | void AstOptimizer::VisitFunctionLiteral(FunctionLiteral* node) { |
| 207 | has_function_literal_ = true; |
| 208 | |
| 209 | if (node->name()->length() == 0) { |
| 210 | // Anonymous function. |
| 211 | func_name_inferrer_.AddFunction(node); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 216 | void AstOptimizer::VisitSharedFunctionInfoLiteral( |
| 217 | SharedFunctionInfoLiteral* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 218 | USE(node); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | void AstOptimizer::VisitConditional(Conditional* node) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 223 | node->condition()->set_no_negative_zero(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 224 | Visit(node->condition()); |
| 225 | Visit(node->then_expression()); |
| 226 | Visit(node->else_expression()); |
| 227 | } |
| 228 | |
| 229 | |
| 230 | void AstOptimizer::VisitSlot(Slot* node) { |
| 231 | USE(node); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | void AstOptimizer::VisitVariableProxy(VariableProxy* node) { |
| 236 | Variable* var = node->AsVariable(); |
| 237 | if (var != NULL) { |
| 238 | if (var->type()->IsKnown()) { |
| 239 | node->type()->CopyFrom(var->type()); |
| 240 | } else if (node->type()->IsLikelySmi()) { |
| 241 | var->type()->SetAsLikelySmi(); |
| 242 | } |
| 243 | |
| 244 | if (!var->is_this() && |
| 245 | !Heap::result_symbol()->Equals(*var->name())) { |
| 246 | func_name_inferrer_.PushName(var->name()); |
| 247 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 248 | |
| 249 | if (FLAG_safe_int32_compiler) { |
| 250 | if (var->IsStackAllocated() && |
| 251 | !var->is_arguments() && |
| 252 | var->mode() != Variable::CONST) { |
| 253 | node->set_side_effect_free(true); |
| 254 | } |
| 255 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | |
| 260 | void AstOptimizer::VisitLiteral(Literal* node) { |
| 261 | Handle<Object> literal = node->handle(); |
| 262 | if (literal->IsSmi()) { |
| 263 | node->type()->SetAsLikelySmi(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 264 | node->set_side_effect_free(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 265 | } else if (literal->IsString()) { |
| 266 | Handle<String> lit_str(Handle<String>::cast(literal)); |
| 267 | if (!Heap::prototype_symbol()->Equals(*lit_str)) { |
| 268 | func_name_inferrer_.PushName(lit_str); |
| 269 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 270 | } else if (literal->IsHeapNumber()) { |
| 271 | if (node->to_int32()) { |
| 272 | // Any HeapNumber has an int32 value if it is the input to a bit op. |
| 273 | node->set_side_effect_free(true); |
| 274 | } else { |
| 275 | double double_value = HeapNumber::cast(*literal)->value(); |
| 276 | int32_t int32_value = DoubleToInt32(double_value); |
| 277 | node->set_side_effect_free(double_value == int32_value); |
| 278 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | |
| 283 | void AstOptimizer::VisitRegExpLiteral(RegExpLiteral* node) { |
| 284 | USE(node); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | void AstOptimizer::VisitArrayLiteral(ArrayLiteral* node) { |
| 289 | for (int i = 0; i < node->values()->length(); i++) { |
| 290 | Visit(node->values()->at(i)); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | void AstOptimizer::VisitObjectLiteral(ObjectLiteral* node) { |
| 295 | for (int i = 0; i < node->properties()->length(); i++) { |
| 296 | ScopedFuncNameInferrer scoped_fni(&func_name_inferrer_); |
| 297 | scoped_fni.Enter(); |
| 298 | Visit(node->properties()->at(i)->key()); |
| 299 | Visit(node->properties()->at(i)->value()); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | |
| 304 | void AstOptimizer::VisitCatchExtensionObject(CatchExtensionObject* node) { |
| 305 | Visit(node->key()); |
| 306 | Visit(node->value()); |
| 307 | } |
| 308 | |
| 309 | |
| 310 | void AstOptimizer::VisitAssignment(Assignment* node) { |
| 311 | ScopedFuncNameInferrer scoped_fni(&func_name_inferrer_); |
| 312 | switch (node->op()) { |
| 313 | case Token::INIT_VAR: |
| 314 | case Token::INIT_CONST: |
| 315 | case Token::ASSIGN: |
| 316 | // No type can be infered from the general assignment. |
| 317 | |
| 318 | // Don't infer if it is "a = function(){...}();"-like expression. |
| 319 | if (node->value()->AsCall() == NULL) { |
| 320 | scoped_fni.Enter(); |
| 321 | } |
| 322 | break; |
| 323 | case Token::ASSIGN_BIT_OR: |
| 324 | case Token::ASSIGN_BIT_XOR: |
| 325 | case Token::ASSIGN_BIT_AND: |
| 326 | case Token::ASSIGN_SHL: |
| 327 | case Token::ASSIGN_SAR: |
| 328 | case Token::ASSIGN_SHR: |
| 329 | node->type()->SetAsLikelySmiIfUnknown(); |
| 330 | node->target()->type()->SetAsLikelySmiIfUnknown(); |
| 331 | node->value()->type()->SetAsLikelySmiIfUnknown(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 332 | node->value()->set_to_int32(true); |
| 333 | node->value()->set_no_negative_zero(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 334 | break; |
| 335 | case Token::ASSIGN_ADD: |
| 336 | case Token::ASSIGN_SUB: |
| 337 | case Token::ASSIGN_MUL: |
| 338 | case Token::ASSIGN_DIV: |
| 339 | case Token::ASSIGN_MOD: |
| 340 | if (node->type()->IsLikelySmi()) { |
| 341 | node->target()->type()->SetAsLikelySmiIfUnknown(); |
| 342 | node->value()->type()->SetAsLikelySmiIfUnknown(); |
| 343 | } |
| 344 | break; |
| 345 | default: |
| 346 | UNREACHABLE(); |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | Visit(node->target()); |
| 351 | Visit(node->value()); |
| 352 | |
| 353 | switch (node->op()) { |
| 354 | case Token::INIT_VAR: |
| 355 | case Token::INIT_CONST: |
| 356 | case Token::ASSIGN: |
| 357 | // Pure assignment copies the type from the value. |
| 358 | node->type()->CopyFrom(node->value()->type()); |
| 359 | break; |
| 360 | case Token::ASSIGN_BIT_OR: |
| 361 | case Token::ASSIGN_BIT_XOR: |
| 362 | case Token::ASSIGN_BIT_AND: |
| 363 | case Token::ASSIGN_SHL: |
| 364 | case Token::ASSIGN_SAR: |
| 365 | case Token::ASSIGN_SHR: |
| 366 | // Should have been setup above already. |
| 367 | break; |
| 368 | case Token::ASSIGN_ADD: |
| 369 | case Token::ASSIGN_SUB: |
| 370 | case Token::ASSIGN_MUL: |
| 371 | case Token::ASSIGN_DIV: |
| 372 | case Token::ASSIGN_MOD: |
| 373 | if (node->type()->IsUnknown()) { |
| 374 | if (node->target()->type()->IsLikelySmi() || |
| 375 | node->value()->type()->IsLikelySmi()) { |
| 376 | node->type()->SetAsLikelySmi(); |
| 377 | } |
| 378 | } |
| 379 | break; |
| 380 | default: |
| 381 | UNREACHABLE(); |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | // Since this is an assignment. We have to propagate this node's type to the |
| 386 | // variable. |
| 387 | VariableProxy* proxy = node->target()->AsVariableProxy(); |
| 388 | if (proxy != NULL) { |
| 389 | Variable* var = proxy->AsVariable(); |
| 390 | if (var != NULL) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 391 | StaticType* var_type = var->type(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 392 | if (var_type->IsUnknown()) { |
| 393 | var_type->CopyFrom(node->type()); |
| 394 | } else if (var_type->IsLikelySmi()) { |
| 395 | // We do not reset likely types to Unknown. |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | |
| 402 | void AstOptimizer::VisitThrow(Throw* node) { |
| 403 | Visit(node->exception()); |
| 404 | } |
| 405 | |
| 406 | |
| 407 | void AstOptimizer::VisitProperty(Property* node) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 408 | node->key()->set_no_negative_zero(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 409 | Visit(node->obj()); |
| 410 | Visit(node->key()); |
| 411 | } |
| 412 | |
| 413 | |
| 414 | void AstOptimizer::VisitCall(Call* node) { |
| 415 | Visit(node->expression()); |
| 416 | OptimizeArguments(node->arguments()); |
| 417 | } |
| 418 | |
| 419 | |
| 420 | void AstOptimizer::VisitCallNew(CallNew* node) { |
| 421 | Visit(node->expression()); |
| 422 | OptimizeArguments(node->arguments()); |
| 423 | } |
| 424 | |
| 425 | |
| 426 | void AstOptimizer::VisitCallRuntime(CallRuntime* node) { |
| 427 | ScopedFuncNameInferrer scoped_fni(&func_name_inferrer_); |
| 428 | if (Factory::InitializeVarGlobal_symbol()->Equals(*node->name()) && |
| 429 | node->arguments()->length() >= 2 && |
| 430 | node->arguments()->at(1)->AsFunctionLiteral() != NULL) { |
| 431 | scoped_fni.Enter(); |
| 432 | } |
| 433 | OptimizeArguments(node->arguments()); |
| 434 | } |
| 435 | |
| 436 | |
| 437 | void AstOptimizer::VisitUnaryOperation(UnaryOperation* node) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 438 | if (node->op() == Token::ADD || node->op() == Token::SUB) { |
| 439 | node->expression()->set_no_negative_zero(node->no_negative_zero()); |
| 440 | } else { |
| 441 | node->expression()->set_no_negative_zero(true); |
| 442 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 443 | Visit(node->expression()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 444 | if (FLAG_safe_int32_compiler) { |
| 445 | switch (node->op()) { |
| 446 | case Token::BIT_NOT: |
| 447 | node->expression()->set_to_int32(true); |
| 448 | // Fall through. |
| 449 | case Token::ADD: |
| 450 | case Token::SUB: |
| 451 | node->set_side_effect_free(node->expression()->side_effect_free()); |
| 452 | break; |
| 453 | case Token::NOT: |
| 454 | case Token::DELETE: |
| 455 | case Token::TYPEOF: |
| 456 | case Token::VOID: |
| 457 | break; |
| 458 | default: |
| 459 | UNREACHABLE(); |
| 460 | break; |
| 461 | } |
| 462 | } else if (node->op() == Token::BIT_NOT) { |
| 463 | node->expression()->set_to_int32(true); |
| 464 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | |
| 468 | void AstOptimizer::VisitCountOperation(CountOperation* node) { |
| 469 | // Count operations assume that they work on Smis. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 470 | node->expression()->set_no_negative_zero(node->is_prefix() ? |
| 471 | true : |
| 472 | node->no_negative_zero()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 473 | node->type()->SetAsLikelySmiIfUnknown(); |
| 474 | node->expression()->type()->SetAsLikelySmiIfUnknown(); |
| 475 | Visit(node->expression()); |
| 476 | } |
| 477 | |
| 478 | |
| 479 | void AstOptimizer::VisitBinaryOperation(BinaryOperation* node) { |
| 480 | // Depending on the operation we can propagate this node's type down the |
| 481 | // AST nodes. |
| 482 | switch (node->op()) { |
| 483 | case Token::COMMA: |
| 484 | case Token::OR: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 485 | node->left()->set_no_negative_zero(true); |
| 486 | node->right()->set_no_negative_zero(node->no_negative_zero()); |
| 487 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 488 | case Token::AND: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 489 | node->left()->set_no_negative_zero(node->no_negative_zero()); |
| 490 | node->right()->set_no_negative_zero(node->no_negative_zero()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 491 | break; |
| 492 | case Token::BIT_OR: |
| 493 | case Token::BIT_XOR: |
| 494 | case Token::BIT_AND: |
| 495 | case Token::SHL: |
| 496 | case Token::SAR: |
| 497 | case Token::SHR: |
| 498 | node->type()->SetAsLikelySmiIfUnknown(); |
| 499 | node->left()->type()->SetAsLikelySmiIfUnknown(); |
| 500 | node->right()->type()->SetAsLikelySmiIfUnknown(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 501 | node->left()->set_to_int32(true); |
| 502 | node->right()->set_to_int32(true); |
| 503 | node->left()->set_no_negative_zero(true); |
| 504 | node->right()->set_no_negative_zero(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 505 | break; |
| 506 | case Token::ADD: |
| 507 | case Token::SUB: |
| 508 | case Token::MUL: |
| 509 | case Token::DIV: |
| 510 | case Token::MOD: |
| 511 | if (node->type()->IsLikelySmi()) { |
| 512 | node->left()->type()->SetAsLikelySmiIfUnknown(); |
| 513 | node->right()->type()->SetAsLikelySmiIfUnknown(); |
| 514 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 515 | node->left()->set_no_negative_zero(node->no_negative_zero()); |
| 516 | node->right()->set_no_negative_zero(node->no_negative_zero()); |
| 517 | if (node->op() == Token::DIV) { |
| 518 | node->right()->set_no_negative_zero(false); |
| 519 | } else if (node->op() == Token::MOD) { |
| 520 | node->right()->set_no_negative_zero(true); |
| 521 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 522 | break; |
| 523 | default: |
| 524 | UNREACHABLE(); |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | Visit(node->left()); |
| 529 | Visit(node->right()); |
| 530 | |
| 531 | // After visiting the operand nodes we have to check if this node's type |
| 532 | // can be updated. If it does, then we can push that information down |
| 533 | // towards the leafs again if the new information is an upgrade over the |
| 534 | // previous type of the operand nodes. |
| 535 | if (node->type()->IsUnknown()) { |
| 536 | if (node->left()->type()->IsLikelySmi() || |
| 537 | node->right()->type()->IsLikelySmi()) { |
| 538 | node->type()->SetAsLikelySmi(); |
| 539 | } |
| 540 | if (node->type()->IsLikelySmi()) { |
| 541 | // The type of this node changed to LIKELY_SMI. Propagate this knowledge |
| 542 | // down through the nodes. |
| 543 | if (node->left()->type()->IsUnknown()) { |
| 544 | node->left()->type()->SetAsLikelySmi(); |
| 545 | Visit(node->left()); |
| 546 | } |
| 547 | if (node->right()->type()->IsUnknown()) { |
| 548 | node->right()->type()->SetAsLikelySmi(); |
| 549 | Visit(node->right()); |
| 550 | } |
| 551 | } |
| 552 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 553 | |
| 554 | if (FLAG_safe_int32_compiler) { |
| 555 | switch (node->op()) { |
| 556 | case Token::COMMA: |
| 557 | case Token::OR: |
| 558 | case Token::AND: |
| 559 | break; |
| 560 | case Token::BIT_OR: |
| 561 | case Token::BIT_XOR: |
| 562 | case Token::BIT_AND: |
| 563 | case Token::SHL: |
| 564 | case Token::SAR: |
| 565 | case Token::SHR: |
| 566 | // Add one to the number of bit operations in this expression. |
| 567 | node->set_num_bit_ops(1); |
| 568 | // Fall through. |
| 569 | case Token::ADD: |
| 570 | case Token::SUB: |
| 571 | case Token::MUL: |
| 572 | case Token::DIV: |
| 573 | case Token::MOD: |
| 574 | node->set_side_effect_free(node->left()->side_effect_free() && |
| 575 | node->right()->side_effect_free()); |
| 576 | node->set_num_bit_ops(node->num_bit_ops() + |
| 577 | node->left()->num_bit_ops() + |
| 578 | node->right()->num_bit_ops()); |
| 579 | if (!node->no_negative_zero() && node->op() == Token::MUL) { |
| 580 | node->set_side_effect_free(false); |
| 581 | } |
| 582 | break; |
| 583 | default: |
| 584 | UNREACHABLE(); |
| 585 | break; |
| 586 | } |
| 587 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | |
| 591 | void AstOptimizer::VisitCompareOperation(CompareOperation* node) { |
| 592 | if (node->type()->IsKnown()) { |
| 593 | // Propagate useful information down towards the leafs. |
| 594 | node->left()->type()->SetAsLikelySmiIfUnknown(); |
| 595 | node->right()->type()->SetAsLikelySmiIfUnknown(); |
| 596 | } |
| 597 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 598 | node->left()->set_no_negative_zero(true); |
| 599 | // Only [[HasInstance]] has the right argument passed unchanged to it. |
| 600 | node->right()->set_no_negative_zero(true); |
| 601 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 602 | Visit(node->left()); |
| 603 | Visit(node->right()); |
| 604 | |
| 605 | // After visiting the operand nodes we have to check if this node's type |
| 606 | // can be updated. If it does, then we can push that information down |
| 607 | // towards the leafs again if the new information is an upgrade over the |
| 608 | // previous type of the operand nodes. |
| 609 | if (node->type()->IsUnknown()) { |
| 610 | if (node->left()->type()->IsLikelySmi() || |
| 611 | node->right()->type()->IsLikelySmi()) { |
| 612 | node->type()->SetAsLikelySmi(); |
| 613 | } |
| 614 | if (node->type()->IsLikelySmi()) { |
| 615 | // The type of this node changed to LIKELY_SMI. Propagate this knowledge |
| 616 | // down through the nodes. |
| 617 | if (node->left()->type()->IsUnknown()) { |
| 618 | node->left()->type()->SetAsLikelySmi(); |
| 619 | Visit(node->left()); |
| 620 | } |
| 621 | if (node->right()->type()->IsUnknown()) { |
| 622 | node->right()->type()->SetAsLikelySmi(); |
| 623 | Visit(node->right()); |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | |
| 630 | void AstOptimizer::VisitThisFunction(ThisFunction* node) { |
| 631 | USE(node); |
| 632 | } |
| 633 | |
| 634 | |
| 635 | class Processor: public AstVisitor { |
| 636 | public: |
| 637 | explicit Processor(VariableProxy* result) |
| 638 | : result_(result), |
| 639 | result_assigned_(false), |
| 640 | is_set_(false), |
| 641 | in_try_(false) { |
| 642 | } |
| 643 | |
| 644 | void Process(ZoneList<Statement*>* statements); |
| 645 | bool result_assigned() const { return result_assigned_; } |
| 646 | |
| 647 | private: |
| 648 | VariableProxy* result_; |
| 649 | |
| 650 | // We are not tracking result usage via the result_'s use |
| 651 | // counts (we leave the accurate computation to the |
| 652 | // usage analyzer). Instead we simple remember if |
| 653 | // there was ever an assignment to result_. |
| 654 | bool result_assigned_; |
| 655 | |
| 656 | // To avoid storing to .result all the time, we eliminate some of |
| 657 | // the stores by keeping track of whether or not we're sure .result |
| 658 | // will be overwritten anyway. This is a bit more tricky than what I |
| 659 | // was hoping for |
| 660 | bool is_set_; |
| 661 | bool in_try_; |
| 662 | |
| 663 | Expression* SetResult(Expression* value) { |
| 664 | result_assigned_ = true; |
| 665 | return new Assignment(Token::ASSIGN, result_, value, |
| 666 | RelocInfo::kNoPosition); |
| 667 | } |
| 668 | |
| 669 | // Node visitors. |
| 670 | #define DEF_VISIT(type) \ |
| 671 | virtual void Visit##type(type* node); |
| 672 | AST_NODE_LIST(DEF_VISIT) |
| 673 | #undef DEF_VISIT |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 674 | |
| 675 | void VisitIterationStatement(IterationStatement* stmt); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 676 | }; |
| 677 | |
| 678 | |
| 679 | void Processor::Process(ZoneList<Statement*>* statements) { |
| 680 | for (int i = statements->length() - 1; i >= 0; --i) { |
| 681 | Visit(statements->at(i)); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | |
| 686 | void Processor::VisitBlock(Block* node) { |
| 687 | // An initializer block is the rewritten form of a variable declaration |
| 688 | // with initialization expressions. The initializer block contains the |
| 689 | // list of assignments corresponding to the initialization expressions. |
| 690 | // While unclear from the spec (ECMA-262, 3rd., 12.2), the value of |
| 691 | // a variable declaration with initialization expression is 'undefined' |
| 692 | // with some JS VMs: For instance, using smjs, print(eval('var x = 7')) |
| 693 | // returns 'undefined'. To obtain the same behavior with v8, we need |
| 694 | // to prevent rewriting in that case. |
| 695 | if (!node->is_initializer_block()) Process(node->statements()); |
| 696 | } |
| 697 | |
| 698 | |
| 699 | void Processor::VisitExpressionStatement(ExpressionStatement* node) { |
| 700 | // Rewrite : <x>; -> .result = <x>; |
| 701 | if (!is_set_) { |
| 702 | node->set_expression(SetResult(node->expression())); |
| 703 | if (!in_try_) is_set_ = true; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | |
| 708 | void Processor::VisitIfStatement(IfStatement* node) { |
| 709 | // Rewrite both then and else parts (reversed). |
| 710 | bool save = is_set_; |
| 711 | Visit(node->else_statement()); |
| 712 | bool set_after_then = is_set_; |
| 713 | is_set_ = save; |
| 714 | Visit(node->then_statement()); |
| 715 | is_set_ = is_set_ && set_after_then; |
| 716 | } |
| 717 | |
| 718 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 719 | void Processor::VisitIterationStatement(IterationStatement* node) { |
| 720 | // Rewrite the body. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 721 | bool set_after_loop = is_set_; |
| 722 | Visit(node->body()); |
| 723 | is_set_ = is_set_ && set_after_loop; |
| 724 | } |
| 725 | |
| 726 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 727 | void Processor::VisitDoWhileStatement(DoWhileStatement* node) { |
| 728 | VisitIterationStatement(node); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 732 | void Processor::VisitWhileStatement(WhileStatement* node) { |
| 733 | VisitIterationStatement(node); |
| 734 | } |
| 735 | |
| 736 | |
| 737 | void Processor::VisitForStatement(ForStatement* node) { |
| 738 | VisitIterationStatement(node); |
| 739 | } |
| 740 | |
| 741 | |
| 742 | void Processor::VisitForInStatement(ForInStatement* node) { |
| 743 | VisitIterationStatement(node); |
| 744 | } |
| 745 | |
| 746 | |
| 747 | void Processor::VisitTryCatchStatement(TryCatchStatement* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 748 | // Rewrite both try and catch blocks (reversed order). |
| 749 | bool set_after_catch = is_set_; |
| 750 | Visit(node->catch_block()); |
| 751 | is_set_ = is_set_ && set_after_catch; |
| 752 | bool save = in_try_; |
| 753 | in_try_ = true; |
| 754 | Visit(node->try_block()); |
| 755 | in_try_ = save; |
| 756 | } |
| 757 | |
| 758 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 759 | void Processor::VisitTryFinallyStatement(TryFinallyStatement* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 760 | // Rewrite both try and finally block (reversed order). |
| 761 | Visit(node->finally_block()); |
| 762 | bool save = in_try_; |
| 763 | in_try_ = true; |
| 764 | Visit(node->try_block()); |
| 765 | in_try_ = save; |
| 766 | } |
| 767 | |
| 768 | |
| 769 | void Processor::VisitSwitchStatement(SwitchStatement* node) { |
| 770 | // Rewrite statements in all case clauses in reversed order. |
| 771 | ZoneList<CaseClause*>* clauses = node->cases(); |
| 772 | bool set_after_switch = is_set_; |
| 773 | for (int i = clauses->length() - 1; i >= 0; --i) { |
| 774 | CaseClause* clause = clauses->at(i); |
| 775 | Process(clause->statements()); |
| 776 | } |
| 777 | is_set_ = is_set_ && set_after_switch; |
| 778 | } |
| 779 | |
| 780 | |
| 781 | void Processor::VisitContinueStatement(ContinueStatement* node) { |
| 782 | is_set_ = false; |
| 783 | } |
| 784 | |
| 785 | |
| 786 | void Processor::VisitBreakStatement(BreakStatement* node) { |
| 787 | is_set_ = false; |
| 788 | } |
| 789 | |
| 790 | |
| 791 | // Do nothing: |
| 792 | void Processor::VisitDeclaration(Declaration* node) {} |
| 793 | void Processor::VisitEmptyStatement(EmptyStatement* node) {} |
| 794 | void Processor::VisitReturnStatement(ReturnStatement* node) {} |
| 795 | void Processor::VisitWithEnterStatement(WithEnterStatement* node) {} |
| 796 | void Processor::VisitWithExitStatement(WithExitStatement* node) {} |
| 797 | void Processor::VisitDebuggerStatement(DebuggerStatement* node) {} |
| 798 | |
| 799 | |
| 800 | // Expressions are never visited yet. |
| 801 | void Processor::VisitFunctionLiteral(FunctionLiteral* node) { |
| 802 | USE(node); |
| 803 | UNREACHABLE(); |
| 804 | } |
| 805 | |
| 806 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 807 | void Processor::VisitSharedFunctionInfoLiteral( |
| 808 | SharedFunctionInfoLiteral* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 809 | USE(node); |
| 810 | UNREACHABLE(); |
| 811 | } |
| 812 | |
| 813 | |
| 814 | void Processor::VisitConditional(Conditional* node) { |
| 815 | USE(node); |
| 816 | UNREACHABLE(); |
| 817 | } |
| 818 | |
| 819 | |
| 820 | void Processor::VisitSlot(Slot* node) { |
| 821 | USE(node); |
| 822 | UNREACHABLE(); |
| 823 | } |
| 824 | |
| 825 | |
| 826 | void Processor::VisitVariableProxy(VariableProxy* node) { |
| 827 | USE(node); |
| 828 | UNREACHABLE(); |
| 829 | } |
| 830 | |
| 831 | |
| 832 | void Processor::VisitLiteral(Literal* node) { |
| 833 | USE(node); |
| 834 | UNREACHABLE(); |
| 835 | } |
| 836 | |
| 837 | |
| 838 | void Processor::VisitRegExpLiteral(RegExpLiteral* node) { |
| 839 | USE(node); |
| 840 | UNREACHABLE(); |
| 841 | } |
| 842 | |
| 843 | |
| 844 | void Processor::VisitArrayLiteral(ArrayLiteral* node) { |
| 845 | USE(node); |
| 846 | UNREACHABLE(); |
| 847 | } |
| 848 | |
| 849 | |
| 850 | void Processor::VisitObjectLiteral(ObjectLiteral* node) { |
| 851 | USE(node); |
| 852 | UNREACHABLE(); |
| 853 | } |
| 854 | |
| 855 | |
| 856 | void Processor::VisitCatchExtensionObject(CatchExtensionObject* node) { |
| 857 | USE(node); |
| 858 | UNREACHABLE(); |
| 859 | } |
| 860 | |
| 861 | |
| 862 | void Processor::VisitAssignment(Assignment* node) { |
| 863 | USE(node); |
| 864 | UNREACHABLE(); |
| 865 | } |
| 866 | |
| 867 | |
| 868 | void Processor::VisitThrow(Throw* node) { |
| 869 | USE(node); |
| 870 | UNREACHABLE(); |
| 871 | } |
| 872 | |
| 873 | |
| 874 | void Processor::VisitProperty(Property* node) { |
| 875 | USE(node); |
| 876 | UNREACHABLE(); |
| 877 | } |
| 878 | |
| 879 | |
| 880 | void Processor::VisitCall(Call* node) { |
| 881 | USE(node); |
| 882 | UNREACHABLE(); |
| 883 | } |
| 884 | |
| 885 | |
| 886 | void Processor::VisitCallNew(CallNew* node) { |
| 887 | USE(node); |
| 888 | UNREACHABLE(); |
| 889 | } |
| 890 | |
| 891 | |
| 892 | void Processor::VisitCallRuntime(CallRuntime* node) { |
| 893 | USE(node); |
| 894 | UNREACHABLE(); |
| 895 | } |
| 896 | |
| 897 | |
| 898 | void Processor::VisitUnaryOperation(UnaryOperation* node) { |
| 899 | USE(node); |
| 900 | UNREACHABLE(); |
| 901 | } |
| 902 | |
| 903 | |
| 904 | void Processor::VisitCountOperation(CountOperation* node) { |
| 905 | USE(node); |
| 906 | UNREACHABLE(); |
| 907 | } |
| 908 | |
| 909 | |
| 910 | void Processor::VisitBinaryOperation(BinaryOperation* node) { |
| 911 | USE(node); |
| 912 | UNREACHABLE(); |
| 913 | } |
| 914 | |
| 915 | |
| 916 | void Processor::VisitCompareOperation(CompareOperation* node) { |
| 917 | USE(node); |
| 918 | UNREACHABLE(); |
| 919 | } |
| 920 | |
| 921 | |
| 922 | void Processor::VisitThisFunction(ThisFunction* node) { |
| 923 | USE(node); |
| 924 | UNREACHABLE(); |
| 925 | } |
| 926 | |
| 927 | |
| 928 | bool Rewriter::Process(FunctionLiteral* function) { |
| 929 | HistogramTimerScope timer(&Counters::rewriting); |
| 930 | Scope* scope = function->scope(); |
| 931 | if (scope->is_function_scope()) return true; |
| 932 | |
| 933 | ZoneList<Statement*>* body = function->body(); |
| 934 | if (body->is_empty()) return true; |
| 935 | |
| 936 | VariableProxy* result = scope->NewTemporary(Factory::result_symbol()); |
| 937 | Processor processor(result); |
| 938 | processor.Process(body); |
| 939 | if (processor.HasStackOverflow()) return false; |
| 940 | |
| 941 | if (processor.result_assigned()) body->Add(new ReturnStatement(result)); |
| 942 | return true; |
| 943 | } |
| 944 | |
| 945 | |
| 946 | bool Rewriter::Optimize(FunctionLiteral* function) { |
| 947 | ZoneList<Statement*>* body = function->body(); |
| 948 | |
| 949 | if (FLAG_optimize_ast && !body->is_empty()) { |
| 950 | HistogramTimerScope timer(&Counters::ast_optimization); |
| 951 | AstOptimizer optimizer(function->name()); |
| 952 | optimizer.Optimize(body); |
| 953 | if (optimizer.HasStackOverflow()) { |
| 954 | return false; |
| 955 | } |
| 956 | } |
| 957 | return true; |
| 958 | } |
| 959 | |
| 960 | |
| 961 | } } // namespace v8::internal |