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

Unified Diff: src/interpreter/bytecode-generator.cc

Issue 1414193006: [Interpreter] Removes unnecessary jumps and dead code from If and loops. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Addressed review comments Created 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/cctest/interpreter/test-bytecode-generator.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/interpreter/bytecode-generator.cc
diff --git a/src/interpreter/bytecode-generator.cc b/src/interpreter/bytecode-generator.cc
index a8407294a7c112cbef29fcd734722faf65331657..c662d61879ede79aa750b42ce2d76566e13a63a0 100644
--- a/src/interpreter/bytecode-generator.cc
+++ b/src/interpreter/bytecode-generator.cc
@@ -489,24 +489,29 @@ void BytecodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
void BytecodeGenerator::VisitIfStatement(IfStatement* stmt) {
- // TODO(oth): Spot easy cases where there code would not need to
- // emit the then block or the else block, e.g. condition is
- // obviously true/1/false/0.
-
BytecodeLabel else_label, end_label;
-
- VisitForAccumulatorValue(stmt->condition());
- builder()->CastAccumulatorToBoolean();
- builder()->JumpIfFalse(&else_label);
- Visit(stmt->then_statement());
- if (stmt->HasElseStatement()) {
- builder()->Jump(&end_label);
- builder()->Bind(&else_label);
- Visit(stmt->else_statement());
+ if (stmt->condition()->ToBooleanIsTrue()) {
+ // Generate only then block.
+ Visit(stmt->then_statement());
+ } else if (stmt->condition()->ToBooleanIsFalse()) {
+ // Generate only else block if it exists.
+ if (stmt->HasElseStatement()) {
+ Visit(stmt->else_statement());
+ }
} else {
- builder()->Bind(&else_label);
+ VisitForAccumulatorValue(stmt->condition());
+ builder()->CastAccumulatorToBoolean();
+ builder()->JumpIfFalse(&else_label);
+ Visit(stmt->then_statement());
+ if (stmt->HasElseStatement()) {
+ builder()->Jump(&end_label);
+ builder()->Bind(&else_label);
+ Visit(stmt->else_statement());
+ } else {
+ builder()->Bind(&else_label);
+ }
+ builder()->Bind(&end_label);
}
- builder()->Bind(&end_label);
}
@@ -549,15 +554,25 @@ void BytecodeGenerator::VisitCaseClause(CaseClause* clause) { UNIMPLEMENTED(); }
void BytecodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
LoopBuilder loop_builder(builder());
ControlScopeForIteration execution_control(this, stmt, &loop_builder);
-
BytecodeLabel body_label, condition_label, done_label;
- builder()->Bind(&body_label);
- Visit(stmt->body());
- builder()->Bind(&condition_label);
- VisitForAccumulatorValue(stmt->cond());
- builder()->JumpIfTrue(&body_label);
- builder()->Bind(&done_label);
+ if (stmt->cond()->ToBooleanIsFalse()) {
+ Visit(stmt->body());
+ // Bind condition_label and done_label for processing continue and break.
+ builder()->Bind(&condition_label);
+ builder()->Bind(&done_label);
+ } else {
+ builder()->Bind(&body_label);
+ Visit(stmt->body());
rmcilroy 2015/10/30 15:49:53 nit - add line of whitespace here between body and
mythria 2015/11/02 10:46:07 Done.
+ builder()->Bind(&condition_label);
+ if (stmt->cond()->ToBooleanIsTrue()) {
+ builder()->Jump(&body_label);
+ } else {
+ VisitForAccumulatorValue(stmt->cond());
+ builder()->JumpIfTrue(&body_label);
+ }
+ builder()->Bind(&done_label);
+ }
loop_builder.SetBreakTarget(done_label);
loop_builder.SetContinueTarget(condition_label);
}
@@ -568,12 +583,23 @@ void BytecodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
ControlScopeForIteration execution_control(this, stmt, &loop_builder);
BytecodeLabel body_label, condition_label, done_label;
- builder()->Jump(&condition_label);
+ if (stmt->cond()->ToBooleanIsFalse()) {
+ // If the condition is false there is no need to generating the loop.
+ return;
+ }
+
+ if (!stmt->cond()->ToBooleanIsTrue()) {
+ builder()->Jump(&condition_label);
+ }
builder()->Bind(&body_label);
Visit(stmt->body());
rmcilroy 2015/10/30 15:49:53 nit - add a line of whitespace here between body
mythria 2015/11/02 10:46:07 Done.
builder()->Bind(&condition_label);
- VisitForAccumulatorValue(stmt->cond());
- builder()->JumpIfTrue(&body_label);
+ if (stmt->cond()->ToBooleanIsTrue()) {
+ builder()->Jump(&body_label);
+ } else {
+ VisitForAccumulatorValue(stmt->cond());
+ builder()->JumpIfTrue(&body_label);
+ }
builder()->Bind(&done_label);
loop_builder.SetBreakTarget(done_label);
@@ -589,8 +615,14 @@ void BytecodeGenerator::VisitForStatement(ForStatement* stmt) {
Visit(stmt->init());
}
+ if (stmt->cond() && stmt->cond()->ToBooleanIsFalse()) {
+ // If the condition is known to be false there is no need to generate
+ // body, next or condition blocks. Init block should be generated.
+ return;
+ }
+
BytecodeLabel body_label, condition_label, next_label, done_label;
- if (stmt->cond() != nullptr) {
+ if (stmt->cond() && !stmt->cond()->ToBooleanIsTrue()) {
builder()->Jump(&condition_label);
}
builder()->Bind(&body_label);
@@ -599,12 +631,12 @@ void BytecodeGenerator::VisitForStatement(ForStatement* stmt) {
if (stmt->next() != nullptr) {
Visit(stmt->next());
}
- if (stmt->cond()) {
+ if ((stmt->cond() == nullptr) || stmt->cond()->ToBooleanIsTrue()) {
rmcilroy 2015/10/30 15:49:53 nit - you removed the '== nullptr' above and added
mythria 2015/11/02 10:46:07 Done.
+ builder()->Jump(&body_label);
+ } else {
builder()->Bind(&condition_label);
VisitForAccumulatorValue(stmt->cond());
builder()->JumpIfTrue(&body_label);
- } else {
- builder()->Jump(&body_label);
}
builder()->Bind(&done_label);
« no previous file with comments | « no previous file | test/cctest/interpreter/test-bytecode-generator.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698