1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Construction of the interference graph.
|
|
|
|
Annotate pseudoregs with interference lists and preference lists. *)
|
|
|
|
|
1999-10-14 08:23:53 -07:00
|
|
|
open Misc
|
1995-06-15 01:17:29 -07:00
|
|
|
open Reg
|
|
|
|
open Mach
|
|
|
|
|
|
|
|
let build_graph fundecl =
|
|
|
|
|
|
|
|
(* The interference graph is represented in two ways:
|
|
|
|
- by adjacency lists for each register
|
|
|
|
- by a triangular bit matrix *)
|
|
|
|
|
|
|
|
let num_regs = Reg.num_registers() in
|
1999-10-14 06:34:37 -07:00
|
|
|
let mat_len = (((num_regs * (num_regs + 1)) lsr 1 + 7) lsr 3) in
|
|
|
|
if mat_len > Sys.max_string_length then
|
|
|
|
fatal_error("Interf.build_graph: too many pseudo-registers in function " ^
|
|
|
|
fundecl.fun_name);
|
|
|
|
let mat = String.make mat_len '\000' in
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
(* Record an interference between two registers *)
|
|
|
|
let add_interf ri rj =
|
|
|
|
let i = ri.stamp and j = rj.stamp in
|
|
|
|
if i = j then () else begin
|
|
|
|
let n = if i < j then ((j * (j + 1)) lsr 1) + i
|
|
|
|
else ((i * (i + 1)) lsr 1) + j in
|
|
|
|
let b = Char.code(mat.[n lsr 3]) in
|
|
|
|
let msk = 1 lsl (n land 7) in
|
|
|
|
if b land msk = 0 then begin
|
|
|
|
mat.[n lsr 3] <- Char.unsafe_chr(b lor msk);
|
|
|
|
begin match ri.loc with
|
|
|
|
Unknown -> ri.interf <- rj :: ri.interf | _ -> ()
|
|
|
|
end;
|
|
|
|
begin match rj.loc with
|
|
|
|
Unknown -> rj.interf <- ri :: rj.interf | _ -> ()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end in
|
|
|
|
|
|
|
|
(* Record interferences between a register array and a set of registers *)
|
|
|
|
let add_interf_set v s =
|
|
|
|
for i = 0 to Array.length v - 1 do
|
|
|
|
let r1 = v.(i) in
|
1995-07-02 09:41:48 -07:00
|
|
|
Reg.Set.iter (add_interf r1) s
|
1995-06-15 01:17:29 -07:00
|
|
|
done in
|
|
|
|
|
|
|
|
(* Record interferences between elements of an array *)
|
|
|
|
let add_interf_self v =
|
|
|
|
for i = 0 to Array.length v - 2 do
|
1995-07-02 09:41:48 -07:00
|
|
|
let ri = v.(i) in
|
1995-06-15 01:17:29 -07:00
|
|
|
for j = i+1 to Array.length v - 1 do
|
1995-07-02 09:41:48 -07:00
|
|
|
add_interf ri v.(j)
|
1995-06-15 01:17:29 -07:00
|
|
|
done
|
|
|
|
done in
|
|
|
|
|
|
|
|
(* Record interferences between the destination of a move and a set
|
|
|
|
of live registers. Since the destination is equal to the source,
|
|
|
|
do not add an interference between them if the source is still live
|
|
|
|
afterwards. *)
|
|
|
|
let add_interf_move src dst s =
|
|
|
|
Reg.Set.iter (fun r -> if r.stamp <> src.stamp then add_interf dst r) s in
|
|
|
|
|
1995-07-13 10:16:20 -07:00
|
|
|
(* Compute interferences *)
|
|
|
|
|
|
|
|
let rec interf i =
|
|
|
|
let destroyed = Proc.destroyed_at_oper i.desc in
|
|
|
|
if Array.length destroyed > 0 then add_interf_set destroyed i.live;
|
|
|
|
match i.desc with
|
|
|
|
Iend -> ()
|
|
|
|
| Ireturn -> ()
|
|
|
|
| Iop(Imove | Ispill | Ireload) ->
|
|
|
|
add_interf_move i.arg.(0) i.res.(0) i.live;
|
|
|
|
interf i.next
|
|
|
|
| Iop(Itailcall_ind) -> ()
|
|
|
|
| Iop(Itailcall_imm lbl) -> ()
|
|
|
|
| Iop op ->
|
|
|
|
add_interf_set i.res i.live;
|
|
|
|
add_interf_self i.res;
|
|
|
|
interf i.next
|
|
|
|
| Iifthenelse(tst, ifso, ifnot) ->
|
|
|
|
interf ifso;
|
|
|
|
interf ifnot;
|
|
|
|
interf i.next
|
|
|
|
| Iswitch(index, cases) ->
|
|
|
|
for i = 0 to Array.length cases - 1 do
|
|
|
|
interf cases.(i)
|
|
|
|
done;
|
|
|
|
interf i.next
|
|
|
|
| Iloop body ->
|
|
|
|
interf body; interf i.next
|
2000-08-11 12:50:59 -07:00
|
|
|
| Icatch(_, body, handler) ->
|
1995-07-13 10:16:20 -07:00
|
|
|
interf body; interf handler; interf i.next
|
2000-08-11 12:50:59 -07:00
|
|
|
| Iexit _ ->
|
1995-07-13 10:16:20 -07:00
|
|
|
()
|
|
|
|
| Itrywith(body, handler) ->
|
|
|
|
add_interf_set Proc.destroyed_at_raise handler.live;
|
|
|
|
interf body; interf handler; interf i.next
|
|
|
|
| Iraise -> () in
|
|
|
|
|
|
|
|
(* Add a preference from one reg to another.
|
|
|
|
Do not add anything if the two registers conflict,
|
|
|
|
or if the source register already has a location. *)
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
let add_pref weight r1 r2 =
|
1995-11-27 06:12:14 -08:00
|
|
|
if weight > 0 then begin
|
|
|
|
let i = r1.stamp and j = r2.stamp in
|
|
|
|
if i = j then () else begin
|
|
|
|
match r1.loc with
|
|
|
|
Unknown ->
|
|
|
|
let n = if i < j then ((j * (j + 1)) lsr 1) + i
|
|
|
|
else ((i * (i + 1)) lsr 1) + j in
|
|
|
|
let b = Char.code(mat.[n lsr 3]) in
|
|
|
|
let msk = 1 lsl (n land 7) in
|
|
|
|
if b land msk = 0 then r1.prefer <- (r2, weight) :: r1.prefer
|
|
|
|
| _ -> ()
|
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
end in
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Add a mutual preference between two regs *)
|
|
|
|
let add_mutual_pref weight r1 r2 =
|
|
|
|
add_pref weight r1 r2; add_pref weight r2 r1 in
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Update the spill cost of the registers involved in an operation *)
|
|
|
|
|
|
|
|
let add_spill_cost cost arg =
|
|
|
|
for i = 0 to Array.length arg - 1 do
|
|
|
|
let r = arg.(i) in r.spill_cost <- r.spill_cost + cost
|
|
|
|
done in
|
|
|
|
|
1995-07-13 10:16:20 -07:00
|
|
|
(* Compute preferences and spill costs *)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
1995-07-13 10:16:20 -07:00
|
|
|
let rec prefer weight i =
|
1995-06-15 01:17:29 -07:00
|
|
|
add_spill_cost weight i.arg;
|
|
|
|
add_spill_cost weight i.res;
|
|
|
|
match i.desc with
|
|
|
|
Iend -> ()
|
|
|
|
| Ireturn -> ()
|
|
|
|
| Iop(Imove) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
add_mutual_pref weight i.arg.(0) i.res.(0);
|
1995-07-13 10:16:20 -07:00
|
|
|
prefer weight i.next
|
1995-07-02 09:41:48 -07:00
|
|
|
| Iop(Ispill) ->
|
|
|
|
add_pref (weight / 4) i.arg.(0) i.res.(0);
|
1995-07-13 10:16:20 -07:00
|
|
|
prefer weight i.next
|
1995-07-02 09:41:48 -07:00
|
|
|
| Iop(Ireload) ->
|
|
|
|
add_pref (weight / 4) i.res.(0) i.arg.(0);
|
1995-07-13 10:16:20 -07:00
|
|
|
prefer weight i.next
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iop(Itailcall_ind) -> ()
|
|
|
|
| Iop(Itailcall_imm lbl) -> ()
|
|
|
|
| Iop op ->
|
1995-07-13 10:16:20 -07:00
|
|
|
prefer weight i.next
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iifthenelse(tst, ifso, ifnot) ->
|
1995-07-13 10:16:20 -07:00
|
|
|
prefer (weight / 2) ifso;
|
|
|
|
prefer (weight / 2) ifnot;
|
|
|
|
prefer weight i.next
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iswitch(index, cases) ->
|
|
|
|
for i = 0 to Array.length cases - 1 do
|
1995-07-13 10:16:20 -07:00
|
|
|
prefer (weight / 2) cases.(i)
|
1995-06-15 01:17:29 -07:00
|
|
|
done;
|
1995-07-13 10:16:20 -07:00
|
|
|
prefer weight i.next
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iloop body ->
|
1996-09-18 07:16:16 -07:00
|
|
|
(* Avoid overflow of weight and spill_cost *)
|
|
|
|
prefer (if weight < 1000 then 8 * weight else weight) body;
|
|
|
|
prefer weight i.next
|
2000-08-11 12:50:59 -07:00
|
|
|
| Icatch(_, body, handler) ->
|
1995-07-13 10:16:20 -07:00
|
|
|
prefer weight body; prefer weight handler; prefer weight i.next
|
2000-08-11 12:50:59 -07:00
|
|
|
| Iexit _ ->
|
1995-06-15 01:17:29 -07:00
|
|
|
()
|
|
|
|
| Itrywith(body, handler) ->
|
1995-07-13 10:16:20 -07:00
|
|
|
prefer weight body; prefer weight handler; prefer weight i.next
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iraise -> ()
|
|
|
|
in
|
1995-07-13 10:16:20 -07:00
|
|
|
|
|
|
|
interf fundecl.fun_body; prefer 8 fundecl.fun_body
|
1995-06-15 01:17:29 -07:00
|
|
|
|