Skip to content

Commit

Permalink
Improve code-generation for inlined comparisons (ocaml#10228)
Browse files Browse the repository at this point in the history
Compile `if (let x = E in COND) then IFSO else IFNOT` like `let x = E in if COND then IFSO else IFNOT`.
  • Loading branch information
stedolan authored and smuenzel committed Mar 30, 2021
1 parent 8d3522e commit f1b4a71
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Changes
Expand Up @@ -70,6 +70,9 @@ Working version
- #9937: improvements in ARM64 code generation (constants, sign extensions)
(Xavier Leroy, review by Stephen Dolan)

- #10228: Better code-generation for inlined comparisons
(Stephen Dolan, review by Alain Frisch and Xavier Leroy)

### Standard library:

- #9533: Added String.starts_with and String.ends_with.
Expand Down
15 changes: 9 additions & 6 deletions asmcomp/cmmgen.ml
Expand Up @@ -424,7 +424,7 @@ let rec transl env e =
let args = List.map (transl env) args in
send kind met obj args dbg
| Ulet(str, kind, id, exp, body) ->
transl_let env str kind id exp body
transl_let env str kind id exp (fun env -> transl env body)
| Uphantom_let (var, defining_expr, body) ->
let defining_expr =
match defining_expr with
Expand Down Expand Up @@ -1117,7 +1117,7 @@ and transl_unbox_sized size dbg env exp =
| Thirty_two -> transl_unbox_int dbg env Pint32 exp
| Sixty_four -> transl_unbox_int dbg env Pint64 exp

and transl_let env str kind id exp body =
and transl_let env str kind id exp transl_body =
let dbg = Debuginfo.none in
let cexp = transl env exp in
let unboxing =
Expand Down Expand Up @@ -1151,16 +1151,16 @@ and transl_let env str kind id exp body =
(* N.B. [body] must still be traversed even if [exp] will never return:
there may be constant closures inside that need lifting out. *)
begin match str, kind with
| Immutable, _ -> Clet(id, cexp, transl env body)
| Mutable, Pintval -> Clet_mut(id, typ_int, cexp, transl env body)
| Mutable, _ -> Clet_mut(id, typ_val, cexp, transl env body)
| Immutable, _ -> Clet(id, cexp, transl_body env)
| Mutable, Pintval -> Clet_mut(id, typ_int, cexp, transl_body env)
| Mutable, _ -> Clet_mut(id, typ_val, cexp, transl_body env)
end
| Boxed (boxed_number, false) ->
let unboxed_id = V.create_local (VP.name id) in
let v = VP.create unboxed_id in
let cexp = unbox_number dbg boxed_number cexp in
let body =
transl (add_unboxed_id (VP.var id) unboxed_id boxed_number env) body in
transl_body (add_unboxed_id (VP.var id) unboxed_id boxed_number env) in
begin match str, boxed_number with
| Immutable, _ -> Clet (v, cexp, body)
| Mutable, bn -> Clet_mut (v, typ_of_boxed_number bn, cexp, body)
Expand Down Expand Up @@ -1202,6 +1202,9 @@ and transl_if env (approx : then_else)
ifso_dbg arg2
then_dbg then_
else_dbg else_
| Ulet(str, kind, id, exp, cond) ->
transl_let env str kind id exp (fun env ->
transl_if env approx dbg cond then_dbg then_ else_dbg else_)
| Uprim (Psequand, [arg1; arg2], inner_dbg) ->
transl_sequand env approx
inner_dbg arg1
Expand Down

0 comments on commit f1b4a71

Please sign in to comment.