Skip to content

Commit

Permalink
flambda-backend: Better results from meet (#406)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Chambart <pierre.chambart@ocamlpro.com>
Co-authored-by: Guillaume Bury <guillaume.bury@gmail.com>
  • Loading branch information
3 people authored Jan 11, 2024
1 parent cf21f2b commit 5cf9cdb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
20 changes: 20 additions & 0 deletions utils/misc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ module Stdlib = struct
else loop (succ i) in
loop 0

let fold_left2 f x a1 a2 =
if Array.length a1 <> Array.length a2
then invalid_arg "Misc.Stdlib.Array.fold_left2";
let r = ref x in
for i = 0 to Array.length a1 - 1 do
r := f !r (Array.unsafe_get a1 i) (Array.unsafe_get a2 i)
done;
!r

let for_alli p a =
let n = Array.length a in
let rec loop i =
Expand Down Expand Up @@ -260,6 +269,17 @@ module Stdlib = struct
false
in
loop 0

let map_sharing f a =
let same = ref true in
let f' x =
let x' = f x in
if x != x' then
same := false;
x'
in
let a' = (Array.map [@inlined hint]) f' a in
if !same then a else a'
end

module String = struct
Expand Down
12 changes: 12 additions & 0 deletions utils/misc.mli
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ module Stdlib : sig
val exists2 : ('a -> 'b -> bool) -> 'a array -> 'b array -> bool
(** Same as [Array.exists2] from the standard library. *)

val fold_left2 :
('acc -> 'a -> 'b -> 'acc) -> 'acc -> 'a array -> 'b array -> 'acc
(** [fold_left2 f init [|a1; ...; an|] [|b1; ...; bn|]] is
[f (... (f (f init a1 b1) a2 b2) ...) an bn].
@raise Invalid_argument if the two arrays are determined
to have different lengths.
*)

val for_alli : (int -> 'a -> bool) -> 'a array -> bool
(** Same as [Array.for_all] from the standard library, but the
function is applied with the index of the element as first argument,
Expand All @@ -202,6 +210,10 @@ module Stdlib : sig
val equal : ('a -> 'a -> bool) -> 'a array -> 'a array -> bool
(** Compare two arrays for equality, using the supplied predicate for
element equality *)

val map_sharing : ('a -> 'a) -> 'a array -> 'a array
(** [map_sharing f a] is [map f a]. If for all elements of the array
[f e == e] then [map_sharing f a == a] *)
end

(** {2 Extensions to the String module} *)
Expand Down

0 comments on commit 5cf9cdb

Please sign in to comment.