Chris@42: (* Chris@42: * Copyright (c) 1997-1999 Massachusetts Institute of Technology Chris@42: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@42: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology Chris@42: * Chris@42: * This program is free software; you can redistribute it and/or modify Chris@42: * it under the terms of the GNU General Public License as published by Chris@42: * the Free Software Foundation; either version 2 of the License, or Chris@42: * (at your option) any later version. Chris@42: * Chris@42: * This program is distributed in the hope that it will be useful, Chris@42: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@42: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@42: * GNU General Public License for more details. Chris@42: * Chris@42: * You should have received a copy of the GNU General Public License Chris@42: * along with this program; if not, write to the Free Software Chris@42: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@42: * Chris@42: *) Chris@42: Chris@42: (* Here, we take a schedule (produced by schedule.ml) ordering a Chris@42: sequence of instructions, and produce an annotated schedule. The Chris@42: annotated schedule has the same ordering as the original schedule, Chris@42: but is additionally partitioned into nested blocks of temporary Chris@42: variables. The partitioning is computed via a heuristic algorithm. Chris@42: Chris@42: The blocking allows the C code that we generate to consist of Chris@42: nested blocks that help communicate variable lifetimes to the Chris@42: compiler. *) Chris@42: Chris@42: open Schedule Chris@42: open Expr Chris@42: open Variable Chris@42: Chris@42: type annotated_schedule = Chris@42: Annotate of variable list * variable list * variable list * int * aschedule Chris@42: and aschedule = Chris@42: ADone Chris@42: | AInstr of assignment Chris@42: | ASeq of (annotated_schedule * annotated_schedule) Chris@42: Chris@42: let addelem a set = if not (List.memq a set) then a :: set else set Chris@42: let union l = Chris@42: let f x = addelem x (* let is source of polymorphism *) Chris@42: in List.fold_right f l Chris@42: Chris@42: (* set difference a - b *) Chris@42: let diff a b = List.filter (fun x -> not (List.memq x b)) a Chris@42: Chris@42: let rec minimize f = function Chris@42: [] -> failwith "minimize" Chris@42: | [n] -> n Chris@42: | n :: rest -> Chris@42: let x = minimize f rest in Chris@42: if (f x) >= (f n) then n else x Chris@42: Chris@42: (* find all variables used inside a scheduling unit *) Chris@42: let rec find_block_vars = function Chris@42: Done -> [] Chris@42: | (Instr (Assign (v, x))) -> v :: (find_vars x) Chris@42: | Par a -> List.flatten (List.map find_block_vars a) Chris@42: | Seq (a, b) -> (find_block_vars a) @ (find_block_vars b) Chris@42: Chris@42: let uniq l = Chris@42: List.fold_right (fun a b -> if List.memq a b then b else a :: b) l [] Chris@42: Chris@42: let has_related x = List.exists (Variable.same_class x) Chris@42: Chris@42: let rec overlap a b = Util.count (fun y -> has_related y b) a Chris@42: Chris@42: (* reorder a list of schedules so as to maximize overlap of variables *) Chris@42: let reorder l = Chris@42: let rec loop = function Chris@42: [] -> [] Chris@42: | (a, va) :: b -> Chris@42: let c = Chris@42: List.map Chris@42: (fun (a, x) -> ((a, x), (overlap va x, List.length x))) b in Chris@42: let c' = Chris@42: Sort.list Chris@42: (fun (_, (a, la)) (_, (b, lb)) -> Chris@42: la < lb || a > b) Chris@42: c in Chris@42: let b' = List.map (fun (a, _) -> a) c' in Chris@42: a :: (loop b') in Chris@42: let l' = List.map (fun x -> x, uniq (find_block_vars x)) l in Chris@42: (* start with smallest block --- does this matter ? *) Chris@42: match l' with Chris@42: [] -> [] Chris@42: | _ -> Chris@42: let m = minimize (fun (_, x) -> (List.length x)) l' in Chris@42: let l'' = Util.remove m l' in Chris@42: loop (m :: l'') Chris@42: Chris@42: (* remove Par blocks *) Chris@42: let rec linearize = function Chris@42: | Seq (a, Done) -> linearize a Chris@42: | Seq (Done, a) -> linearize a Chris@42: | Seq (a, b) -> Seq (linearize a, linearize b) Chris@42: Chris@42: (* try to balance nested Par blocks *) Chris@42: | Par [a] -> linearize a Chris@42: | Par l -> Chris@42: let n2 = (List.length l) / 2 in Chris@42: let rec loop n a b = Chris@42: if n = 0 then Chris@42: (List.rev b, a) Chris@42: else Chris@42: match a with Chris@42: [] -> failwith "loop" Chris@42: | x :: y -> loop (n - 1) y (x :: b) Chris@42: in let (a, b) = loop n2 (reorder l) [] Chris@42: in linearize (Seq (Par a, Par b)) Chris@42: Chris@42: | x -> x Chris@42: Chris@42: let subset a b = Chris@42: List.for_all (fun x -> List.exists (fun y -> x == y) b) a Chris@42: Chris@42: let use_same_vars (Assign (av, ax)) (Assign (bv, bx)) = Chris@42: is_temporary av && Chris@42: is_temporary bv && Chris@42: (let va = Expr.find_vars ax and vb = Expr.find_vars bx in Chris@42: subset va vb && subset vb va) Chris@42: Chris@42: let store_to_same_class (Assign (av, ax)) (Assign (bv, bx)) = Chris@42: is_locative av && Chris@42: is_locative bv && Chris@42: Variable.same_class av bv Chris@42: Chris@42: let loads_from_same_class (Assign (av, ax)) (Assign (bv, bx)) = Chris@42: match (ax, bx) with Chris@42: | (Load a), (Load b) when Chris@42: Variable.is_locative a && Variable.is_locative b Chris@42: -> Variable.same_class a b Chris@42: | _ -> false Chris@42: Chris@42: (* extract instructions from schedule *) Chris@42: let rec sched_to_ilist = function Chris@42: | Done -> [] Chris@42: | Instr a -> [a] Chris@42: | Seq (a, b) -> (sched_to_ilist a) @ (sched_to_ilist b) Chris@42: | _ -> failwith "sched_to_ilist" (* Par blocks removed by linearize *) Chris@42: Chris@42: let rec find_friends friendp insn friends foes = function Chris@42: | [] -> (friends, foes) Chris@42: | a :: b -> Chris@42: if (a == insn) || (friendp a insn) then Chris@42: find_friends friendp insn (a :: friends) foes b Chris@42: else Chris@42: find_friends friendp insn friends (a :: foes) b Chris@42: Chris@42: (* schedule all instructions in the equivalence class determined Chris@42: by friendp at the point where the last one Chris@42: is executed *) Chris@42: let rec delay_friends friendp sched = Chris@42: let rec recur insns = function Chris@42: | Done -> (Done, insns) Chris@42: | Instr a -> Chris@42: let (friends, foes) = find_friends friendp a [] [] insns in Chris@42: (Schedule.sequentially friends), foes Chris@42: | Seq (a, b) -> Chris@42: let (b', insnsb) = recur insns b in Chris@42: let (a', insnsa) = recur insnsb a in Chris@42: (Seq (a', b')), insnsa Chris@42: | _ -> failwith "delay_friends" Chris@42: in match recur (sched_to_ilist sched) sched with Chris@42: | (s, []) -> s (* assert that all insns have been used *) Chris@42: | _ -> failwith "delay_friends" Chris@42: Chris@42: (* schedule all instructions in the equivalence class determined Chris@42: by friendp at the point where the first one Chris@42: is executed *) Chris@42: let rec anticipate_friends friendp sched = Chris@42: let rec recur insns = function Chris@42: | Done -> (Done, insns) Chris@42: | Instr a -> Chris@42: let (friends, foes) = find_friends friendp a [] [] insns in Chris@42: (Schedule.sequentially friends), foes Chris@42: | Seq (a, b) -> Chris@42: let (a', insnsa) = recur insns a in Chris@42: let (b', insnsb) = recur insnsa b in Chris@42: (Seq (a', b')), insnsb Chris@42: | _ -> failwith "anticipate_friends" Chris@42: in match recur (sched_to_ilist sched) sched with Chris@42: | (s, []) -> s (* assert that all insns have been used *) Chris@42: | _ -> failwith "anticipate_friends" Chris@42: Chris@42: let collect_buddy_stores buddy_list sched = Chris@42: let rec recur sched delayed_stores = match sched with Chris@42: | Done -> (sched, delayed_stores) Chris@42: | Instr (Assign (v, x)) -> Chris@42: begin Chris@42: try Chris@42: let buddies = List.find (List.memq v) buddy_list in Chris@42: let tmp = Variable.make_temporary () in Chris@42: let i = Seq(Instr (Assign (tmp, x)), Chris@42: Instr (Assign (v, Times (NaN MULTI_A, Load tmp)))) Chris@42: and delayed_stores = (v, Load tmp) :: delayed_stores in Chris@42: try Chris@42: (Seq (i, Chris@42: Instr (Assign Chris@42: (List.hd buddies, Chris@42: Times (NaN MULTI_B, Chris@42: Plus (List.map Chris@42: (fun buddy -> Chris@42: List.assq buddy Chris@42: delayed_stores) Chris@42: buddies))) ))) Chris@42: , delayed_stores Chris@42: with Not_found -> (i, delayed_stores) Chris@42: with Not_found -> (sched, delayed_stores) Chris@42: end Chris@42: | Seq (a, b) -> Chris@42: let (newa, delayed_stores) = recur a delayed_stores in Chris@42: let (newb, delayed_stores) = recur b delayed_stores in Chris@42: (Seq (newa, newb), delayed_stores) Chris@42: | _ -> failwith "collect_buddy_stores" Chris@42: in let (sched, _) = recur sched [] in Chris@42: sched Chris@42: Chris@42: let schedule_for_pipeline sched = Chris@42: let update_readytimes t (Assign (v, _)) ready_times = Chris@42: (v, (t + !Magic.pipeline_latency)) :: ready_times Chris@42: and readyp t ready_times (Assign (_, x)) = Chris@42: List.for_all Chris@42: (fun var -> Chris@42: try Chris@42: (List.assq var ready_times) <= t Chris@42: with Not_found -> false) Chris@42: (List.filter Variable.is_temporary (Expr.find_vars x)) Chris@42: in Chris@42: let rec recur sched t ready_times delayed_instructions = Chris@42: let (ready, not_ready) = Chris@42: List.partition (readyp t ready_times) delayed_instructions Chris@42: in match ready with Chris@42: | a :: b -> Chris@42: let (sched, t, ready_times, delayed_instructions) = Chris@42: recur sched (t+1) (update_readytimes t a ready_times) Chris@42: (b @ not_ready) Chris@42: in Chris@42: (Seq (Instr a, sched)), t, ready_times, delayed_instructions Chris@42: | _ -> (match sched with Chris@42: | Done -> (sched, t, ready_times, delayed_instructions) Chris@42: | Instr a -> Chris@42: if (readyp t ready_times a) then Chris@42: (sched, (t+1), (update_readytimes t a ready_times), Chris@42: delayed_instructions) Chris@42: else Chris@42: (Done, t, ready_times, (a :: delayed_instructions)) Chris@42: | Seq (a, b) -> Chris@42: let (a, t, ready_times, delayed_instructions) = Chris@42: recur a t ready_times delayed_instructions Chris@42: in Chris@42: let (b, t, ready_times, delayed_instructions) = Chris@42: recur b t ready_times delayed_instructions Chris@42: in (Seq (a, b)), t, ready_times, delayed_instructions Chris@42: | _ -> failwith "schedule_for_pipeline") Chris@42: in let rec recur_until_done sched t ready_times delayed_instructions = Chris@42: let (sched, t, ready_times, delayed_instructions) = Chris@42: recur sched t ready_times delayed_instructions Chris@42: in match delayed_instructions with Chris@42: | [] -> sched Chris@42: | _ -> Chris@42: (Seq (sched, Chris@42: (recur_until_done Done (t+1) ready_times Chris@42: delayed_instructions))) Chris@42: in recur_until_done sched 0 [] [] Chris@42: Chris@42: let rec rewrite_declarations force_declarations Chris@42: (Annotate (_, _, declared, _, what)) = Chris@42: let m = !Magic.number_of_variables in Chris@42: Chris@42: let declare_it declared = Chris@42: if (force_declarations || List.length declared >= m) then Chris@42: ([], declared) Chris@42: else Chris@42: (declared, []) Chris@42: Chris@42: in match what with Chris@42: ADone -> Annotate ([], [], [], 0, what) Chris@42: | AInstr i -> Chris@42: let (u, d) = declare_it declared Chris@42: in Annotate ([], u, d, 0, what) Chris@42: | ASeq (a, b) -> Chris@42: let ma = rewrite_declarations false a Chris@42: and mb = rewrite_declarations false b Chris@42: in let Annotate (_, ua, _, _, _) = ma Chris@42: and Annotate (_, ub, _, _, _) = mb Chris@42: in let (u, d) = declare_it (declared @ ua @ ub) Chris@42: in Annotate ([], u, d, 0, ASeq (ma, mb)) Chris@42: Chris@42: let annotate list_of_buddy_stores schedule = Chris@42: let rec analyze live_at_end = function Chris@42: Done -> Annotate (live_at_end, [], [], 0, ADone) Chris@42: | Instr i -> (match i with Chris@42: Assign (v, x) -> Chris@42: let vars = (find_vars x) in Chris@42: Annotate (Util.remove v (union live_at_end vars), [v], [], Chris@42: 0, AInstr i)) Chris@42: | Seq (a, b) -> Chris@42: let ab = analyze live_at_end b in Chris@42: let Annotate (live_at_begin_b, defined_b, _, depth_a, _) = ab in Chris@42: let aa = analyze live_at_begin_b a in Chris@42: let Annotate (live_at_begin_a, defined_a, _, depth_b, _) = aa in Chris@42: let defined = List.filter is_temporary (defined_a @ defined_b) in Chris@42: let declarable = diff defined live_at_end in Chris@42: let undeclarable = diff defined declarable Chris@42: and maxdepth = max depth_a depth_b in Chris@42: Annotate (live_at_begin_a, undeclarable, declarable, Chris@42: List.length declarable + maxdepth, Chris@42: ASeq (aa, ab)) Chris@42: | _ -> failwith "really_analyze" Chris@42: Chris@42: in Chris@42: let () = Util.info "begin annotate" in Chris@42: let x = linearize schedule in Chris@42: Chris@42: let x = Chris@42: if (!Magic.schedule_for_pipeline && !Magic.pipeline_latency > 0) then Chris@42: schedule_for_pipeline x Chris@42: else Chris@42: x Chris@42: in Chris@42: Chris@42: let x = Chris@42: if !Magic.reorder_insns then Chris@42: linearize(anticipate_friends use_same_vars x) Chris@42: else Chris@42: x Chris@42: in Chris@42: Chris@42: (* delay stores to the real and imaginary parts of the same number *) Chris@42: let x = Chris@42: if !Magic.reorder_stores then Chris@42: linearize(delay_friends store_to_same_class x) Chris@42: else Chris@42: x Chris@42: in Chris@42: Chris@42: (* move loads of the real and imaginary parts of the same number *) Chris@42: let x = Chris@42: if !Magic.reorder_loads then Chris@42: linearize(anticipate_friends loads_from_same_class x) Chris@42: else Chris@42: x Chris@42: in Chris@42: Chris@42: let x = collect_buddy_stores list_of_buddy_stores x in Chris@42: let x = analyze [] x in Chris@42: let res = rewrite_declarations true x in Chris@42: let () = Util.info "end annotate" in Chris@42: res Chris@42: Chris@42: let rec dump print (Annotate (_, _, _, _, code)) = Chris@42: dump_code print code Chris@42: and dump_code print = function Chris@42: | ADone -> () Chris@42: | AInstr x -> print ((assignment_to_string x) ^ "\n") Chris@42: | ASeq (a, b) -> dump print a; dump print b