Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New byte code for branch on not null #1979

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/bytecodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,11 @@ enum BytecodeFormat {
BYTECODE(BRANCH, 3, OP_SF, "branch") \
BYTECODE(BRANCH_IF_TRUE, 3, OP_SF, "branch if true") \
BYTECODE(BRANCH_IF_FALSE, 3, OP_SF, "branch if false") \
BYTECODE(BRANCH_IF_NOT_NULL, 3, OP_SF, "branch if not null") \
BYTECODE(BRANCH_BACK, 5, OP_SB_SB, "branch back") \
BYTECODE(BRANCH_BACK_IF_TRUE, 5, OP_SB_SB, "branch back if true") \
BYTECODE(BRANCH_BACK_IF_FALSE, 5, OP_SB_SB, "branch back if false") \
BYTECODE(BRANCH_BACK_IF_NOT_NULL, 5, OP_SB_SB, "branch back if not null") \
BYTECODE(PRIMITIVE, 4, OP_BU_SU, "invoke primitive") \
BYTECODE(THROW, 2, OP_BU, "throw") \
BYTECODE(RETURN, 3, OP_BS_BU, "return") \
Expand Down
14 changes: 14 additions & 0 deletions src/compiler/byte_gen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,20 @@ void ByteGen::visit_for_control(Expression* expression,
return;
}

if (yes == fallthrough && expression->is_CallBuiltin()) {
auto builtin = expression->as_CallBuiltin();
if (builtin->target()->kind() == Builtin::IDENTICAL) {
bool left_is_null = builtin->arguments()[0]->is_LiteralNull();
bool right_is_null = builtin->arguments()[1]->is_LiteralNull();
if (left_is_null || right_is_null) {
Expression* other = builtin->arguments()[left_is_null ? 1 : 0];
visit_for_value(other);
__ branch(Emitter::IF_NOT_NULL, no);
return;
}
}
}

visit_for_value(expression);
if (yes == fallthrough) {
__ branch(Emitter::IF_FALSE, no);
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/emitter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,9 @@ void Emitter::branch(Condition condition, Label* label) {
} else if (condition == IF_TRUE) {
op = label->is_bound() ? BRANCH_BACK_IF_TRUE : BRANCH_IF_TRUE;
stack_.pop();
} else if (condition == IF_NOT_NULL) {
op = label->is_bound() ? BRANCH_BACK_IF_NOT_NULL : BRANCH_IF_NOT_NULL;
stack_.pop();
} else {
ASSERT(condition == IF_FALSE);
op = label->is_bound() ? BRANCH_BACK_IF_FALSE: BRANCH_IF_FALSE;
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class Emitter {
enum Condition {
UNCONDITIONAL,
IF_TRUE,
IF_FALSE
IF_FALSE,
IF_NOT_NULL,
};

List<uint8> bytecodes();
Expand Down
27 changes: 27 additions & 0 deletions src/compiler/propagation/type_propagator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,20 @@ static TypeScope* process(TypeScope* scope, uint8* bcp, std::vector<Worklist*>&
}
OPCODE_END();

OPCODE_BEGIN(BRANCH_IF_NOT_NULL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should also be treated as an unconditional branch if the variable cannot be null?

TypeSet top = stack->local(0);
bool can_be_null = top.contains_null(program);
stack->pop();
uint8* target = bcp + Utils::read_unaligned_uint16(bcp + 1);
if (!can_be_null) {
// Unconditionally continue at the branch target.
return worklists.back()->add(target, scope, false);
} else if (top.size(propagator->words_per_type()) > 1) {
scope = worklists.back()->add(target, scope, true);
stack = scope->top();
}
OPCODE_END();

OPCODE_BEGIN(BRANCH_BACK);
uint8* target = bcp - Utils::read_unaligned_uint16(bcp + 1);
return worklists.back()->add(target, scope, false);
Expand Down Expand Up @@ -1212,6 +1226,19 @@ static TypeScope* process(TypeScope* scope, uint8* bcp, std::vector<Worklist*>&
}
OPCODE_END();

OPCODE_BEGIN(BRANCH_BACK_IF_NOT_NULL);
TypeSet top = stack->local(0);
bool can_be_null = top.contains_null(program);
stack->pop();
uint8* target = bcp - Utils::read_unaligned_uint16(bcp + 1);
if (!can_be_null) {
// Unconditionally continue at the branch target.
return worklists.back()->add(target, scope, false);
}
scope = worklists.back()->add(target, scope, true);
stack = scope->top();
OPCODE_END();

OPCODE_BEGIN(INVOKE_LAMBDA_TAIL);
propagator->ensure_code_failure();
scope->throw_maybe();
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/propagation/type_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ bool TypeSet::can_be_truthy(Program* program) const {
Iterator it(*this, TypeSet::words_per_type(program));
while (it.has_next()) {
unsigned id = it.next();
if (id != null_id || id != false_id) return true;
if (id != null_id && id != false_id) return true;
}
return false;
}
Expand Down
16 changes: 16 additions & 0 deletions src/interpreter_run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,13 @@ Interpreter::Result Interpreter::run() {
}
OPCODE_END();

OPCODE_BEGIN(BRANCH_IF_NOT_NULL);
if (POP() != program->null_object()) {
bcp += Utils::read_unaligned_uint16(bcp + 1);
DISPATCH(0);
}
OPCODE_END();

OPCODE_BEGIN(BRANCH_BACK);
uint8* entry = bcp - Utils::read_unaligned_uint16(bcp + 3);
bcp -= Utils::read_unaligned_uint16(bcp + 1);
Expand All @@ -986,6 +993,15 @@ Interpreter::Result Interpreter::run() {
}
OPCODE_END();

OPCODE_BEGIN(BRANCH_BACK_IF_NOT_NULL);
if (POP() != program->null_object()) {
uint8* entry = bcp - Utils::read_unaligned_uint16(bcp + 3);
bcp -= Utils::read_unaligned_uint16(bcp + 1);
CHECK_PREEMPT(entry);
DISPATCH(0);
}
OPCODE_END();

OPCODE_BEGIN(INVOKE_LAMBDA_TAIL);
B_ARG1(bci_offset)
Instance* receiver = Instance::cast(STACK_AT(bci_offset + FRAME_SIZE));
Expand Down
2 changes: 1 addition & 1 deletion tests/strip/throw-test.toit
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ main args:
compiler := args[1]
runner := args[2]

with-tmp-directory: | tmp-dir|
with-tmp-directory: | tmp-dir |
variants := compile-variants --compiler=compiler --tmp-dir=tmp-dir input

non-stripped-snapshot := variants[0]
Expand Down
13 changes: 13 additions & 0 deletions tests/type_propagation/default-test.toit
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
main:
foo false
foo2 false

bar: return true

foo x=bar:
if x:
print x

foo2 x=true:
if x:
print x
6 changes: 3 additions & 3 deletions tests/type_propagation/gold/array-do-test.gold
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ main tests/type_propagation/array-do-test.toit
12[015] - load local 1
13[038] - load block 1
15[058] - invoke virtual do // [{List_}, [block]] -> {Null_}
19[089] - return null S3 0
19[091] - return null S3 0

[block] in main tests/type_propagation/array-do-test.toit
- argument 0: [block]
- argument 1: {*}
0[016] - load local 2
1[053] - invoke static id tests/type_propagation/array-do-test.toit // [{*}] -> {*}
4[088] - return S1 2
4[090] - return S1 2

id tests/type_propagation/array-do-test.toit
- argument 0: {*}
0[016] - load local 2
1[088] - return S1 1
1[090] - return S1 1
6 changes: 3 additions & 3 deletions tests/type_propagation/gold/array-do-test.gold-O2
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ main tests/type_propagation/array-do-test.toit
12[015] - load local 1
13[038] - load block 1
15[058] - invoke virtual do // [{List_}, [block]] -> {Null_}
19[089] - return null S3 0
19[091] - return null S3 0

[block] in main tests/type_propagation/array-do-test.toit
- argument 0: [block]
- argument 1: {*}
0[016] - load local 2
1[053] - invoke static id tests/type_propagation/array-do-test.toit // [{*}] -> {*}
4[088] - return S1 2
4[090] - return S1 2

id tests/type_propagation/array-do-test.toit
- argument 0: {*}
0[016] - load local 2
1[088] - return S1 1
1[090] - return S1 1
Loading
Loading