diff options
| author | gingerBill <bill@gingerbill.org> | 2023-06-14 12:49:33 +0100 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2023-06-14 12:49:33 +0100 |
| commit | 3a761395bee4f14c81682d4c21a5aa1e9565959a (patch) | |
| tree | 8f3b177217176b059c8651a23ef12054f36a534e /src/llvm_backend_expr.cpp | |
| parent | e036155bdb391c097b91415f730754784ce6f38c (diff) | |
Add basic optimization for comparisons against the empty string `""`
Diffstat (limited to 'src/llvm_backend_expr.cpp')
| -rw-r--r-- | src/llvm_backend_expr.cpp | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index 619b40fda..68ad9e6e0 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -1325,6 +1325,15 @@ handle_op:; return {}; } +gb_internal bool lb_is_empty_string_constant(Ast *expr) { + if (expr->tav.value.kind == ExactValue_String && + is_type_string(expr->tav.type)) { + String s = expr->tav.value.value_string; + return s.len == 0; + } + return false; +} + gb_internal lbValue lb_build_binary_expr(lbProcedure *p, Ast *expr) { ast_node(be, BinaryExpr, expr); @@ -1373,15 +1382,29 @@ gb_internal lbValue lb_build_binary_expr(lbProcedure *p, Ast *expr) { case Token_CmpEq: case Token_NotEq: if (is_type_untyped_nil(be->right->tav.type)) { + // `x == nil` or `x != nil` lbValue left = lb_build_expr(p, be->left); lbValue cmp = lb_emit_comp_against_nil(p, be->op.kind, left); Type *type = default_type(tv.type); return lb_emit_conv(p, cmp, type); } else if (is_type_untyped_nil(be->left->tav.type)) { + // `nil == x` or `nil != x` lbValue right = lb_build_expr(p, be->right); lbValue cmp = lb_emit_comp_against_nil(p, be->op.kind, right); Type *type = default_type(tv.type); return lb_emit_conv(p, cmp, type); + } else if (lb_is_empty_string_constant(be->right)) { + // `x == ""` or `x != ""` + lbValue len = lb_string_len(p, lb_build_expr(p, be->left)); + lbValue cmp = lb_emit_comp(p, be->op.kind, len, lb_const_int(p->module, t_int, 0)); + Type *type = default_type(tv.type); + return lb_emit_conv(p, cmp, type); + } else if (lb_is_empty_string_constant(be->left)) { + // `"" == x` or `"" != x` + lbValue len = lb_string_len(p, lb_build_expr(p, be->right)); + lbValue cmp = lb_emit_comp(p, be->op.kind, len, lb_const_int(p->module, t_int, 0)); + Type *type = default_type(tv.type); + return lb_emit_conv(p, cmp, type); } /*fallthrough*/ case Token_Lt: @@ -2246,7 +2269,6 @@ gb_internal lbValue lb_compare_records(lbProcedure *p, TokenKind op_kind, lbValu - gb_internal lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue right) { Type *a = core_type(left.type); Type *b = core_type(right.type); |