1995-08-13 02:31:50 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-13 02:31:50 -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 *)
|
1995-08-13 02:31:50 -07:00
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
(* Instruction scheduling *)
|
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Reg
|
|
|
|
open Mach
|
|
|
|
open Linearize
|
|
|
|
|
|
|
|
(* Representation of the code DAG. *)
|
|
|
|
|
|
|
|
type code_dag_node =
|
|
|
|
{ instr: instruction; (* The instruction *)
|
1997-07-24 04:49:12 -07:00
|
|
|
delay: int; (* How many cycles before result is available *)
|
1995-08-13 02:31:50 -07:00
|
|
|
mutable sons: (code_dag_node * int) list;
|
|
|
|
(* Instructions that depend on it *)
|
|
|
|
mutable date: int; (* Start date *)
|
|
|
|
mutable length: int; (* Length of longest path to result *)
|
|
|
|
mutable ancestors: int; (* Number of ancestors *)
|
|
|
|
mutable emitted_ancestors: int } (* Number of emitted ancestors *)
|
|
|
|
|
|
|
|
let dummy_node =
|
|
|
|
{ instr = end_instr; delay = 0; sons = []; date = 0;
|
|
|
|
length = -1; ancestors = 0; emitted_ancestors = 0 }
|
|
|
|
|
|
|
|
(* The code dag itself is represented by two tables from registers to nodes:
|
|
|
|
- "results" maps registers to the instructions that produced them;
|
1998-04-27 02:56:13 -07:00
|
|
|
- "uses" maps registers to the instructions that use them.
|
|
|
|
In addition, code_stores contains the latest store nodes emitted so far
|
|
|
|
and code_loads contains all load nodes emitted since the last store. *)
|
1995-08-13 02:31:50 -07:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let code_results = (Hashtbl.create 31 : (location, code_dag_node) Hashtbl.t)
|
|
|
|
let code_uses = (Hashtbl.create 31 : (location, code_dag_node) Hashtbl.t)
|
1998-04-27 02:56:13 -07:00
|
|
|
let code_stores = ref ([] : code_dag_node list)
|
|
|
|
let code_loads = ref ([] : code_dag_node list)
|
1995-08-13 02:31:50 -07:00
|
|
|
|
|
|
|
let clear_code_dag () =
|
|
|
|
Hashtbl.clear code_results;
|
1998-04-27 02:56:13 -07:00
|
|
|
Hashtbl.clear code_uses;
|
|
|
|
code_stores := [];
|
|
|
|
code_loads := []
|
1995-08-13 02:31:50 -07:00
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
(* Add an edge to the code DAG *)
|
1995-08-13 02:31:50 -07:00
|
|
|
|
|
|
|
let add_edge ancestor son delay =
|
|
|
|
ancestor.sons <- (son, delay) :: ancestor.sons;
|
|
|
|
son.ancestors <- son.ancestors + 1
|
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
(* Compute length of longest path to a result.
|
|
|
|
For leafs of the DAG, see whether their result is used in the instruction
|
|
|
|
immediately following the basic block (a "critical" output). *)
|
|
|
|
|
|
|
|
let is_critical critical_outputs results =
|
|
|
|
try
|
|
|
|
for i = 0 to Array.length results - 1 do
|
|
|
|
let r = results.(i).loc in
|
|
|
|
for j = 0 to Array.length critical_outputs - 1 do
|
|
|
|
if critical_outputs.(j).loc = r then raise Exit
|
|
|
|
done
|
|
|
|
done;
|
|
|
|
false
|
|
|
|
with Exit ->
|
|
|
|
true
|
|
|
|
|
|
|
|
let rec longest_path critical_outputs node =
|
|
|
|
if node.length < 0 then begin
|
|
|
|
match node.sons with
|
|
|
|
[] ->
|
|
|
|
node.length <-
|
|
|
|
if is_critical critical_outputs node.instr.res
|
|
|
|
or node.instr.desc = Lreloadretaddr (* alway critical *)
|
|
|
|
then node.delay
|
|
|
|
else 0
|
|
|
|
| sons ->
|
|
|
|
node.length <-
|
|
|
|
List.fold_left
|
|
|
|
(fun len (son, delay) ->
|
|
|
|
max len (longest_path critical_outputs son + delay))
|
|
|
|
0 sons
|
|
|
|
end;
|
|
|
|
node.length
|
|
|
|
|
|
|
|
(* Remove an instruction from the ready queue *)
|
|
|
|
|
|
|
|
let rec remove_instr node = function
|
|
|
|
[] -> []
|
|
|
|
| instr :: rem ->
|
|
|
|
if instr == node then rem else instr :: remove_instr node rem
|
|
|
|
|
|
|
|
(* We treat Lreloadretaddr as a word-sized load *)
|
|
|
|
|
|
|
|
let some_load = (Iload(Cmm.Word, Arch.identity_addressing))
|
|
|
|
|
|
|
|
(* The generic scheduler *)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
class virtual scheduler_generic = object (self)
|
1997-07-24 04:49:12 -07:00
|
|
|
|
|
|
|
(* Determine whether an operation ends a basic block or not.
|
|
|
|
Can be overriden for some processors to signal specific instructions
|
1998-04-27 02:56:13 -07:00
|
|
|
that terminate a basic block. *)
|
1997-07-24 04:49:12 -07:00
|
|
|
|
|
|
|
method oper_in_basic_block = function
|
|
|
|
Icall_ind -> false
|
|
|
|
| Icall_imm _ -> false
|
|
|
|
| Itailcall_ind -> false
|
|
|
|
| Itailcall_imm _ -> false
|
|
|
|
| Iextcall(_, _) -> false
|
|
|
|
| Istackoffset _ -> false
|
|
|
|
| Ialloc _ -> false
|
|
|
|
| _ -> true
|
|
|
|
|
|
|
|
(* Determine whether an instruction ends a basic block or not *)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private instr_in_basic_block instr =
|
1997-07-24 04:49:12 -07:00
|
|
|
match instr.desc with
|
|
|
|
Lop op -> self#oper_in_basic_block op
|
|
|
|
| Lreloadretaddr -> true
|
|
|
|
| _ -> false
|
|
|
|
|
1998-04-27 02:56:13 -07:00
|
|
|
(* Determine whether an operation is a memory store or a memory load.
|
|
|
|
Can be overriden for some processors to signal specific
|
|
|
|
load or store instructions (e.g. on the I386). *)
|
|
|
|
|
|
|
|
method is_store = function
|
|
|
|
Istore(_, _) -> true
|
|
|
|
| _ -> false
|
|
|
|
|
|
|
|
method is_load = function
|
|
|
|
Iload(_, _) -> true
|
|
|
|
| _ -> false
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private instr_is_store instr =
|
1998-04-27 02:56:13 -07:00
|
|
|
match instr.desc with
|
|
|
|
Lop op -> self#is_store op
|
|
|
|
| _ -> false
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private instr_is_load instr =
|
1998-04-27 02:56:13 -07:00
|
|
|
match instr.desc with
|
|
|
|
Lop op -> self#is_load op
|
|
|
|
| _ -> false
|
|
|
|
|
|
|
|
(* Estimate the latency of an operation. *)
|
1997-07-24 04:49:12 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method virtual oper_latency : Mach.operation -> int
|
1997-07-24 04:49:12 -07:00
|
|
|
|
1998-04-27 02:56:13 -07:00
|
|
|
(* Estimate the latency of a Lreloadretaddr operation. *)
|
|
|
|
|
|
|
|
method reload_retaddr_latency = self#oper_latency some_load
|
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
(* Estimate the delay needed to evaluate an instruction *)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private instr_latency instr =
|
1997-07-24 04:49:12 -07:00
|
|
|
match instr.desc with
|
1998-04-27 02:56:13 -07:00
|
|
|
Lop op -> self#oper_latency op
|
|
|
|
| Lreloadretaddr -> self#reload_retaddr_latency
|
|
|
|
| _ -> assert false
|
1997-07-24 04:49:12 -07:00
|
|
|
|
|
|
|
(* Estimate the number of cycles consumed by emitting an operation. *)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method virtual oper_issue_cycles : Mach.operation -> int
|
1997-07-24 04:49:12 -07:00
|
|
|
|
1998-04-27 02:56:13 -07:00
|
|
|
(* Estimate the number of cycles consumed by emitting a Lreloadretaddr. *)
|
|
|
|
|
|
|
|
method reload_retaddr_issue_cycles = self#oper_issue_cycles some_load
|
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
(* Estimate the number of cycles consumed by emitting an instruction. *)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private instr_issue_cycles instr =
|
1997-07-24 04:49:12 -07:00
|
|
|
match instr.desc with
|
1998-04-27 02:56:13 -07:00
|
|
|
Lop op -> self#oper_issue_cycles op
|
|
|
|
| Lreloadretaddr -> self#reload_retaddr_issue_cycles
|
|
|
|
| _ -> assert false
|
1997-07-24 04:49:12 -07:00
|
|
|
|
|
|
|
(* Add an instruction to the code dag *)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private add_instruction ready_queue instr =
|
1997-07-24 04:49:12 -07:00
|
|
|
let delay = self#instr_latency instr in
|
1995-08-13 02:31:50 -07:00
|
|
|
let node =
|
|
|
|
{ instr = instr;
|
|
|
|
delay = delay;
|
|
|
|
sons = [];
|
|
|
|
date = 0;
|
|
|
|
length = -1;
|
|
|
|
ancestors = 0;
|
|
|
|
emitted_ancestors = 0 } in
|
1997-07-24 04:49:12 -07:00
|
|
|
(* Add edges from all instructions that define one of the registers used
|
|
|
|
(RAW dependencies) *)
|
1995-08-13 02:31:50 -07:00
|
|
|
for i = 0 to Array.length instr.arg - 1 do
|
|
|
|
try
|
|
|
|
let ancestor = Hashtbl.find code_results instr.arg.(i).loc in
|
|
|
|
add_edge ancestor node ancestor.delay
|
|
|
|
with Not_found ->
|
|
|
|
()
|
|
|
|
done;
|
1997-07-24 04:49:12 -07:00
|
|
|
(* Also add edges from all instructions that use one of the result regs
|
|
|
|
of this instruction (WAR dependencies). *)
|
1995-08-13 02:31:50 -07:00
|
|
|
for i = 0 to Array.length instr.res - 1 do
|
|
|
|
let ancestors = Hashtbl.find_all code_uses instr.res.(i).loc in
|
|
|
|
List.iter (fun ancestor -> add_edge ancestor node 0) ancestors
|
|
|
|
done;
|
|
|
|
(* Also add edges from all instructions that have already defined one
|
1997-07-24 04:49:12 -07:00
|
|
|
of the results of this instruction (WAW dependencies). *)
|
1995-08-13 02:31:50 -07:00
|
|
|
for i = 0 to Array.length instr.res - 1 do
|
|
|
|
try
|
|
|
|
let ancestor = Hashtbl.find code_results instr.res.(i).loc in
|
|
|
|
add_edge ancestor node 0
|
|
|
|
with Not_found ->
|
|
|
|
()
|
|
|
|
done;
|
1998-04-27 02:56:13 -07:00
|
|
|
(* If this is a load, add edges from the most recent store viewed so
|
|
|
|
far (if any) and remember the load *)
|
|
|
|
if self#instr_is_load instr then begin
|
|
|
|
List.iter (fun store -> add_edge store node 0) !code_stores;
|
|
|
|
code_loads := node :: !code_loads
|
|
|
|
end
|
|
|
|
(* If this is a store, add edges from the most recent store,
|
|
|
|
as well as all loads viewed since then. Remember the store,
|
|
|
|
discarding the previous store and loads. *)
|
|
|
|
else if self#instr_is_store instr then begin
|
|
|
|
List.iter (fun store -> add_edge store node 0) !code_stores;
|
|
|
|
List.iter (fun load -> add_edge load node 0) !code_loads;
|
|
|
|
code_stores := [node];
|
|
|
|
code_loads := []
|
|
|
|
end;
|
|
|
|
|
1995-08-13 02:31:50 -07:00
|
|
|
(* Remember the registers used and produced by this instruction *)
|
|
|
|
for i = 0 to Array.length instr.res - 1 do
|
|
|
|
Hashtbl.add code_results instr.res.(i).loc node
|
|
|
|
done;
|
|
|
|
for i = 0 to Array.length instr.arg - 1 do
|
|
|
|
Hashtbl.add code_uses instr.arg.(i).loc node
|
|
|
|
done;
|
|
|
|
(* If this is a root instruction (all arguments already computed),
|
|
|
|
add it to the ready queue *)
|
|
|
|
if node.ancestors = 0 then node :: ready_queue else ready_queue
|
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
(* Given a list of instructions and a date, choose one or several
|
|
|
|
that are ready to be computed (start date <= current date)
|
|
|
|
and that we can emit in one cycle. Favor instructions with
|
|
|
|
maximal distance to result. If we can't find any, return None.
|
|
|
|
This does not take multiple issues into account, though. *)
|
1995-08-13 02:31:50 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private ready_instruction date queue =
|
1995-08-13 02:31:50 -07:00
|
|
|
let rec extract best = function
|
|
|
|
[] ->
|
|
|
|
if best == dummy_node then None else Some best
|
|
|
|
| instr :: rem ->
|
|
|
|
let new_best =
|
1997-07-24 04:49:12 -07:00
|
|
|
if instr.date <= date && instr.length > best.length
|
1995-08-13 02:31:50 -07:00
|
|
|
then instr else best in
|
|
|
|
extract new_best rem in
|
|
|
|
extract dummy_node queue
|
1997-07-24 04:49:12 -07:00
|
|
|
|
1995-08-13 02:31:50 -07:00
|
|
|
(* Schedule a basic block, adding its instructions in front of the given
|
|
|
|
instruction sequence *)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private reschedule ready_queue date cont =
|
1997-07-24 04:49:12 -07:00
|
|
|
if ready_queue = [] then cont else begin
|
|
|
|
match self#ready_instruction date ready_queue with
|
|
|
|
None ->
|
|
|
|
self#reschedule ready_queue (date + 1) cont
|
|
|
|
| Some node ->
|
|
|
|
(* Remove node from queue *)
|
|
|
|
let new_queue = ref (remove_instr node ready_queue) in
|
|
|
|
(* Update the start date and number of ancestors emitted of
|
|
|
|
all descendents of this node. Enter those that become ready
|
|
|
|
in the queue. *)
|
1997-07-29 18:12:19 -07:00
|
|
|
let issue_cycles = self#instr_issue_cycles node.instr in
|
1997-07-24 04:49:12 -07:00
|
|
|
List.iter
|
|
|
|
(fun (son, delay) ->
|
1997-07-29 18:12:19 -07:00
|
|
|
let completion_date = date + issue_cycles + delay - 1 in
|
1997-07-24 04:49:12 -07:00
|
|
|
if son.date < completion_date then son.date <- completion_date;
|
|
|
|
son.emitted_ancestors <- son.emitted_ancestors + 1;
|
|
|
|
if son.emitted_ancestors = son.ancestors then
|
|
|
|
new_queue := son :: !new_queue)
|
|
|
|
node.sons;
|
|
|
|
instr_cons node.instr.desc node.instr.arg node.instr.res
|
|
|
|
(self#reschedule !new_queue (date + issue_cycles) cont)
|
1995-08-25 01:46:03 -07:00
|
|
|
end
|
1995-08-13 02:31:50 -07:00
|
|
|
|
|
|
|
(* Entry point *)
|
|
|
|
(* Don't bother to schedule for initialization code and the like. *)
|
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
method schedule_fundecl f =
|
|
|
|
|
|
|
|
let rec schedule i =
|
|
|
|
match i.desc with
|
|
|
|
Lend -> i
|
|
|
|
| _ ->
|
|
|
|
if self#instr_in_basic_block i then begin
|
|
|
|
clear_code_dag();
|
|
|
|
schedule_block [] i
|
|
|
|
end else
|
|
|
|
{ desc = i.desc; arg = i.arg; res = i.res; live = i.live;
|
|
|
|
next = schedule i.next }
|
|
|
|
|
|
|
|
and schedule_block ready_queue i =
|
|
|
|
if self#instr_in_basic_block i then
|
|
|
|
schedule_block (self#add_instruction ready_queue i) i.next
|
|
|
|
else begin
|
|
|
|
let critical_outputs =
|
|
|
|
match i.desc with
|
|
|
|
Lop(Icall_ind | Itailcall_ind) -> [| i.arg.(0) |]
|
|
|
|
| Lop(Icall_imm _ | Itailcall_imm _ | Iextcall(_, _)) -> [||]
|
|
|
|
| Lreturn -> [||]
|
|
|
|
| _ -> i.arg in
|
1997-10-31 04:56:28 -08:00
|
|
|
List.iter (fun x -> longest_path critical_outputs x; ()) ready_queue;
|
1997-07-24 04:49:12 -07:00
|
|
|
self#reschedule ready_queue 0 (schedule i)
|
|
|
|
end in
|
|
|
|
|
|
|
|
if f.fun_fast then begin
|
1995-08-13 02:31:50 -07:00
|
|
|
let new_body = schedule f.fun_body in
|
|
|
|
clear_code_dag();
|
|
|
|
{ fun_name = f.fun_name;
|
|
|
|
fun_body = new_body;
|
|
|
|
fun_fast = f.fun_fast }
|
|
|
|
end else
|
|
|
|
f
|
1997-07-24 04:49:12 -07:00
|
|
|
|
|
|
|
end
|