diff options
author | Cyril SIX <cyril.six@kalray.eu> | 2020-04-08 14:53:50 +0200 |
---|---|---|
committer | Cyril SIX <cyril.six@kalray.eu> | 2020-04-08 14:54:24 +0200 |
commit | e326ed9f28a2ed6869f0cb356ef9a8e189cb0a47 (patch) | |
tree | 552e75b40e6aa97397aaa65dfbccf398b482bddb /backend/Duplicateaux.ml | |
parent | ba6453483f7c742a98cd6fcefe015018df1dfea7 (diff) | |
download | compcert-kvx-e326ed9f28a2ed6869f0cb356ef9a8e189cb0a47.tar.gz compcert-kvx-e326ed9f28a2ed6869f0cb356ef9a8e189cb0a47.zip |
Some cleaning on Linearize and Duplicate
Diffstat (limited to 'backend/Duplicateaux.ml')
-rw-r--r-- | backend/Duplicateaux.ml | 124 |
1 files changed, 69 insertions, 55 deletions
diff --git a/backend/Duplicateaux.ml b/backend/Duplicateaux.ml index b137e872..89f187da 100644 --- a/backend/Duplicateaux.ml +++ b/backend/Duplicateaux.ml @@ -8,6 +8,12 @@ open RTL open Maps open Camlcoq +let debug_flag = ref false + +let debug fmt = + if !debug_flag then Printf.eprintf fmt + else Printf.ifprintf stderr fmt + let get_some = function | None -> failwith "Did not get some" | Some thing -> thing @@ -20,7 +26,7 @@ let rtl_successors = function | Ijumptable (_,ln) -> ln let bfs code entrypoint = begin - Printf.printf "bfs\n"; flush stdout; + debug "bfs\n"; let visited = ref (PTree.map (fun n i -> false) code) and bfs_list = ref [] and to_visit = Queue.create () @@ -48,7 +54,7 @@ let optbool o = match o with Some _ -> true | None -> false let ptree_get_some n ptree = get_some @@ PTree.get n ptree let get_predecessors_rtl code = begin - Printf.printf "get_predecessors_rtl\n"; flush stdout; + debug "get_predecessors_rtl\n"; let preds = ref (PTree.map (fun n i -> []) code) in let process_inst (node, i) = let succ = rtl_successors i @@ -74,19 +80,23 @@ let print_intlist l = | [] -> () | n::ln -> (Printf.printf "%d " (P.to_int n); f ln) in begin - Printf.printf "["; - f l; - Printf.printf "]" + if !debug_flag then begin + Printf.printf "["; + f l; + Printf.printf "]" + end end let print_intset s = let seq = PSet.to_seq s in begin - Printf.printf "{"; - Seq.iter (fun n -> - Printf.printf "%d " (P.to_int n) - ) seq; - Printf.printf "}" + if !debug_flag then begin + Printf.printf "{"; + Seq.iter (fun n -> + Printf.printf "%d " (P.to_int n) + ) seq; + Printf.printf "}" + end end type vstate = Unvisited | Processed | Visited @@ -99,7 +109,7 @@ type vstate = Unvisited | Processed | Visited * If we come accross an edge to a Processed node, it's a loop! *) let get_loop_headers code entrypoint = begin - Printf.printf "get_loop_headers\n"; flush stdout; + debug "get_loop_headers\n"; let visited = ref (PTree.map (fun n i -> Unvisited) code) and is_loop_header = ref (PTree.map (fun n i -> false) code) in let rec dfs_visit code = function @@ -108,7 +118,7 @@ let get_loop_headers code entrypoint = begin match (get_some @@ PTree.get node !visited) with | Visited -> () | Processed -> begin - Printf.printf "Node %d is a loop header\n" (P.to_int node); + debug "Node %d is a loop header\n" (P.to_int node); is_loop_header := PTree.set node true !is_loop_header; visited := PTree.set node Visited !visited end @@ -129,11 +139,13 @@ end let ptree_printbool pt = let elements = PTree.elements pt in begin - Printf.printf "["; - List.iter (fun (n, b) -> - if b then Printf.printf "%d, " (P.to_int n) else () - ) elements; - Printf.printf "]" + if !debug_flag then begin + Printf.printf "["; + List.iter (fun (n, b) -> + if b then Printf.printf "%d, " (P.to_int n) else () + ) elements; + Printf.printf "]" + end end (* Looks ahead (until a branch) to see if a node further down verifies @@ -150,7 +162,7 @@ let rec look_ahead code node is_loop_header predicate = let do_call_heuristic code cond ifso ifnot is_loop_header = begin - Printf.printf "\tCall heuristic..\n"; + debug "\tCall heuristic..\n"; let predicate n = (function | Icall _ -> true | _ -> false) @@ get_some @@ PTree.get n code @@ -164,13 +176,13 @@ let do_call_heuristic code cond ifso ifnot is_loop_header = let do_opcode_heuristic code cond ifso ifnot is_loop_header = begin - Printf.printf "\tOpcode heuristic..\n"; + debug "\tOpcode heuristic..\n"; DuplicateOpcodeHeuristic.opcode_heuristic code cond ifso ifnot is_loop_header end let do_return_heuristic code cond ifso ifnot is_loop_header = begin - Printf.printf "\tReturn heuristic..\n"; + debug "\tReturn heuristic..\n"; let predicate n = (function | Ireturn _ -> true | _ -> false) @@ get_some @@ PTree.get n code @@ -184,7 +196,7 @@ let do_return_heuristic code cond ifso ifnot is_loop_header = let do_store_heuristic code cond ifso ifnot is_loop_header = begin - Printf.printf "\tStore heuristic..\n"; + debug "\tStore heuristic..\n"; let predicate n = (function | Istore _ -> true | _ -> false) @@ get_some @@ PTree.get n code @@ -198,7 +210,7 @@ let do_store_heuristic code cond ifso ifnot is_loop_header = let do_loop_heuristic code cond ifso ifnot is_loop_header = begin - Printf.printf "\tLoop heuristic..\n"; + debug "\tLoop heuristic..\n"; let predicate n = get_some @@ PTree.get n is_loop_header in let ifso_loop = look_ahead code ifso is_loop_header predicate in let ifnot_loop = look_ahead code ifnot is_loop_header predicate in @@ -210,7 +222,7 @@ let do_loop_heuristic code cond ifso ifnot is_loop_header = let do_loop2_heuristic loop_info n code cond ifso ifnot is_loop_header = begin - Printf.printf "\tLoop2 heuristic..\n"; + debug "\tLoop2 heuristic..\n"; match get_some @@ PTree.get n loop_info with | None -> None | Some b -> Some b @@ -244,23 +256,23 @@ let get_loop_info is_loop_header bfs_order code = | Ijumptable _ | Itailcall _ | Ireturn _ -> None end in begin - Printf.printf "Marking path from %d to %d\n" (P.to_int n) (P.to_int s); + debug "Marking path from %d to %d\n" (P.to_int n) (P.to_int s); match advance_to_cb s with - | None -> (Printf.printf "Nothing found\n") - | Some s -> ( Printf.printf "Advancing to %d\n" (P.to_int s); + | None -> (debug "Nothing found\n") + | Some s -> ( debug "Advancing to %d\n" (P.to_int s); match get_some @@ PTree.get s !loop_info with | None | Some _ -> begin match get_some @@ PTree.get s code with | Icond (_, _, n1, n2, _) -> let b1 = explore n1 n in let b2 = explore n2 n in - if (b1 && b2) then (Printf.printf "both true\n") - else if b1 then (Printf.printf "true privileged\n"; loop_info := PTree.set s (Some true) !loop_info) - else if b2 then (Printf.printf "false privileged\n"; loop_info := PTree.set s (Some false) !loop_info) - else (Printf.printf "none true\n") - | _ -> ( Printf.printf "not an icond\n" ) + if (b1 && b2) then (debug "both true\n") + else if b1 then (debug "true privileged\n"; loop_info := PTree.set s (Some true) !loop_info) + else if b2 then (debug "false privileged\n"; loop_info := PTree.set s (Some false) !loop_info) + else (debug "none true\n") + | _ -> ( debug "not an icond\n" ) end - (* | Some _ -> ( Printf.printf "already loop info there\n" ) FIXME - we don't know yet whether a branch to a loop head is a backedge or not *) + (* | Some _ -> ( debug "already loop info there\n" ) FIXME - we don't know yet whether a branch to a loop head is a backedge or not *) ) end in begin @@ -278,34 +290,34 @@ let get_loop_info is_loop_header bfs_order code = (* Remark - compared to the original paper, we don't use the store heuristic *) let get_directions code entrypoint = begin - Printf.printf "get_directions\n"; flush stdout; + debug "get_directions\n"; let bfs_order = bfs code entrypoint in let is_loop_header = get_loop_headers code entrypoint in let loop_info = get_loop_info is_loop_header bfs_order code in let directions = ref (PTree.map (fun n i -> None) code) in (* None <=> no predicted direction *) begin (* ptree_printbool is_loop_header; *) - (* Printf.printf "\n"; *) + (* debug "\n"; *) List.iter (fun n -> match (get_some @@ PTree.get n code) with | Icond (cond, lr, ifso, ifnot, _) -> - (* Printf.printf "Analyzing %d.." (P.to_int n); *) + (* debug "Analyzing %d.." (P.to_int n); *) let heuristics = [ do_opcode_heuristic; do_return_heuristic; do_loop2_heuristic loop_info n; do_loop_heuristic; do_call_heuristic; (* do_store_heuristic *) ] in let preferred = ref None in begin - Printf.printf "Deciding condition for RTL node %d\n" (P.to_int n); + debug "Deciding condition for RTL node %d\n" (P.to_int n); List.iter (fun do_heur -> match !preferred with | None -> preferred := do_heur code cond ifso ifnot is_loop_header | Some _ -> () ) heuristics; directions := PTree.set n !preferred !directions; - (match !preferred with | Some false -> Printf.printf "\tFALLTHROUGH\n" - | Some true -> Printf.printf "\tBRANCH\n" - | None -> Printf.printf "\tUNSURE\n"); - Printf.printf "---------------------------------------\n" + (match !preferred with | Some false -> debug "\tFALLTHROUGH\n" + | Some true -> debug "\tBRANCH\n" + | None -> debug "\tUNSURE\n"); + debug "---------------------------------------\n" end | _ -> () ) bfs_order; @@ -325,12 +337,12 @@ let rec update_direction_rec directions = function (* Uses branch prediction to write prediction annotations in Icond *) let update_directions code entrypoint = begin - Printf.printf "Update_directions\n"; flush stdout; + debug "Update_directions\n"; let directions = get_directions code entrypoint in begin - (* Printf.printf "Ifso directions: "; + (* debug "Ifso directions: "; ptree_printbool directions; - Printf.printf "\n"; *) + debug "\n"; *) update_direction_rec directions (PTree.elements code) end end @@ -345,7 +357,7 @@ let exists_false boolmap = exists_false_rec (PTree.elements boolmap) (* DFS using prediction info to guide the exploration *) let dfs code entrypoint = begin - Printf.printf "dfs\n"; flush stdout; + debug "dfs\n"; let visited = ref (PTree.map (fun n i -> false) code) in let rec dfs_list code = function | [] -> [] @@ -409,9 +421,11 @@ let print_traces traces = | [] -> () | t::lt -> Printf.printf "\n\t"; print_trace t; Printf.printf ",\n"; f lt in begin - Printf.printf "Traces: {"; - f traces; - Printf.printf "}\n"; + if !debug_flag then begin + Printf.printf "Traces: {"; + f traces; + Printf.printf "}\n"; + end end (* Dumb (but linear) trace selection *) @@ -447,12 +461,12 @@ let select_traces_linear code entrypoint = (* Algorithm mostly inspired from Chang and Hwu 1988 * "Trace Selection for Compiling Large C Application Programs to Microcode" *) let select_traces_chang code entrypoint = begin - Printf.printf "select_traces\n"; flush stdout; + debug "select_traces\n"; let order = dfs code entrypoint in let predecessors = get_predecessors_rtl code in let traces = ref [] in let is_visited = ref (PTree.map (fun n i -> false) code) in begin (* mark all nodes visited *) - Printf.printf "Length: %d\n" (List.length order); flush stdout; + debug "Length: %d\n" (List.length order); while exists_false !is_visited do (* while (there are unvisited nodes) *) let seed = select_unvisited_node !is_visited order in let trace = ref [seed] in @@ -485,8 +499,8 @@ let select_traces_chang code entrypoint = begin end end done; - (* Printf.printf "DFS: \t"; print_intlist order; Printf.printf "\n"; *) - Printf.printf "Traces: "; print_traces !traces; + (* debug "DFS: \t"; print_intlist order; debug "\n"; *) + debug "Traces: "; print_traces !traces; !traces end end @@ -528,7 +542,7 @@ let rec change_pointers code n n' = function * n': the integer which should contain the duplicate of n * returns: new code, new ptree *) let duplicate code ptree parent n preds n' = - Printf.printf "Duplicating node %d into %d..\n" (P.to_int n) (P.to_int n'); + debug "Duplicating node %d into %d..\n" (P.to_int n) (P.to_int n'); match PTree.get n' code with | Some _ -> failwith "The PTree already has a node n'" | None -> @@ -593,8 +607,8 @@ let superblockify_traces code preds traces = | trace :: traces -> let new_code, new_ptree, nb_duplicated = tail_duplicate code preds ptree trace in if (nb_duplicated < max_nb_duplicated) - then (Printf.printf "End duplication\n"; f new_code new_ptree traces) - else (Printf.printf "Too many duplicated nodes, aborting tail duplication\n"; (code, ptree, 0)) + then (debug "End duplication\n"; f new_code new_ptree traces) + else (debug "Too many duplicated nodes, aborting tail duplication\n"; (code, ptree, 0)) in let new_code, new_ptree, _ = f code ptree traces in (new_code, new_ptree) @@ -604,7 +618,7 @@ let rec invert_iconds_trace code = function let code' = match ptree_get_some n code with | Icond (c, lr, ifso, ifnot, info) -> (match info with | Some true -> begin - (* Printf.printf "Reversing ifso/ifnot for node %d\n" (P.to_int n); *) + (* debug "Reversing ifso/ifnot for node %d\n" (P.to_int n); *) PTree.set n (Icond (Op.negate_condition c, lr, ifnot, ifso, Some false)) code end | _ -> code) |