Debian Codegraph @

lib/mlrisc-lib/MLRISC/block-placement/jump-chain-elim-fn.sml

raw
1 (* jump-chain-elim-fn.sml
2 *
3 * COPYRIGHT (c) 2002 Bell Labs, Lucent Technologies
4 *
5 * Collapse jumps to jumps.
6 *
7 * TODO:
8 * check for jumps to the next block.
9 * jump tables (SWITCH edges).
10 *)
11
12 functor JumpChainElimFn (
13
14 structure CFG : CONTROL_FLOW_GRAPH
15 structure InsnProps : INSN_PROPERTIES (* where I = CFG.I *)
16 where type I.addressing_mode = CFG.I.addressing_mode
17 and type I.ea = CFG.I.ea
18 and type I.instr = CFG.I.instr
19 and type I.instruction = CFG.I.instruction
20 and type I.operand = CFG.I.operand
21
22 (* Control flag that when set true allows jumps to labels outside
23 * of the CFG to be chained. Set this false when there are many
24 * short jumps to a long jump that exits the CFG.
25 *)
26 val chainEscapes : bool ref
27
28 (* Control flag that when set true allows the direction (forward or
29 * backward) of conditional jumps to be changed. Set this false
30 * when the direction of conditional branches is used to predict
31 * the branch.
32 *)
33 val reverseDirection : bool ref
34
35 ) : sig
36
37 structure CFG : CONTROL_FLOW_GRAPH
38
39 val run : (CFG.cfg * CFG.node list) -> (CFG.cfg * CFG.node list)
40
41 end = struct
42
43 structure CFG = CFG
44 structure IP = InsnProps
45 structure G = Graph
46
47 (* flags *)
48 val disable = MLRiscControl.mkFlag (
49 "disable-jump-chain-elim",
50 "whether jump chain elimination is disabled")
51 val dumpCFG = MLRiscControl.mkFlag (
52 "dump-cfg-after-jump-chain-elim",
53 "whether flow graph is shown after jump chain elimination")
54 val dumpStrm = MLRiscControl.debug_stream
55
56 fun error msg = MLRiscErrorMsg.error("JumpChainElimFn", msg)
57
58 fun run (cfg, blocks) = let
59 val G.GRAPH{
60 node_info, out_edges, set_out_edges, in_edges,
61 forall_nodes, remove_node, ...
62 } = cfg
63 val chainEscapes = !chainEscapes
64 val reverseDirection = !reverseDirection
65 (* this flag is set to note that we need to filter out unreachable
66 * blocks after jump chaining.
67 *)
68 val needFilter = ref false
69 (* the exit block *)
70 val exit = CFG.exitId cfg
71 (* map a block ID to a label *)
72 fun labelOf blkId = (case node_info blkId
73 of CFG.BLOCK{labels=ref(lab::_), ...} => lab
74 | CFG.BLOCK{labels, ...} => let
75 val lab = Label.anon()
76 in
77 labels := [lab];
78 lab
79 end
80 (* end case *))
81 fun jumpLabelOf instr = (
82 case IP.branchTargets instr
83 of [IP.LABELLED lab] => lab
84 | _ => error ("jumpLabelOf")
85 (* end case *))
86 (* given a destination block ID, check to see if it is a block that consists
87 * a single jump instruction. If so, return the block ID and label of the
88 * block at the end of the jump chain; otherwise return NONE.
89 *)
90 fun followChain blkId = (case node_info blkId
91 of CFG.BLOCK{insns as ref[i], kind=CFG.NORMAL, ...} => (
92 (* a normal block with one instruction *)
93 case out_edges blkId
94 of [e as (_, dst, CFG.EDGE{k=CFG.JUMP, w, a})] =>
95 if ((dst <> exit) orelse chainEscapes)
96 then (
97 (* the instruction must be a jump so transitively follow it
98 * to get the target; but be careful to avoid infinite loops.
99 *)
100 set_out_edges (blkId, []);
101 case followChain dst
102 of NONE => (
103 set_out_edges (blkId, [e]);
104 SOME(dst, jumpLabelOf i))
105 | (someLab as SOME(dst', lab)) => (
106 insns := [IP.jump lab];
107 set_out_edges (blkId,
108 [(blkId, dst', CFG.EDGE{k=CFG.JUMP, w=w, a=a})]);
109 someLab)
110 (* end case *))
111 else NONE
112 | _ => NONE
113 (* end case *))
114 | _ => NONE
115 (* end case *))
116 (* For each normal block, check the outgoing edges to see if they
117 * can be redirected.
118 *)
119 fun doBlock (blkId, CFG.BLOCK{insns, kind=CFG.NORMAL, ...}) = let
120 fun setTargets labs = let
121 val (jmp, r) =
122 case !insns of
123 jmp :: r => (jmp, r)
124 | [] => error "setTargets: empty insns"
125 val newJmp =
126 (case labs
127 of [lab] => IP.setJumpTarget(jmp, lab)
128 | [lab1,lab2] => IP.setBranchTargets{i=jmp, f=lab1, t=lab2}
129 | _ => error "setTargets"
130 (*esac*))
131 in
132 needFilter := true;
133 insns := newJmp :: r
134 end
135 in
136 case out_edges blkId
137 of [(_, dst, info as CFG.EDGE{k=CFG.JUMP, ...})] => (
138 case followChain dst
139 of SOME(dst', lab) => (
140 setTargets [lab];
141 set_out_edges (blkId, [(blkId, dst', info)]))
142 | NONE => ()
143 (* end case *))
144 | [(_, dst1, info as CFG.EDGE{k=CFG.BRANCH true, ...}), e2] => (
145 case followChain dst1
146 of SOME(dst', lab) => (
147 setTargets [labelOf(#2 e2), lab];
148 set_out_edges (blkId, [(blkId, dst', info), e2]))
149 | NONE => ()
150 (* end case *))
151 | [e1, (_, dst2, info as CFG.EDGE{k=CFG.BRANCH true, ...})] => (
152 case followChain dst2
153 of SOME(dst', lab) => (
154 setTargets [labelOf(#2 e1), lab];
155 set_out_edges (blkId, [e1, (blkId, dst', info)]))
156 | NONE => ()
157 (* end case *))
158 | _ => ()
159 (* end case *)
160 end
161 | doBlock _ = ()
162 val entry = CFG.entryId cfg
163 fun keepBlock (blkId, _) =
164 if null(in_edges blkId) andalso (blkId <> entry)
165 then (remove_node blkId; false)
166 else true
167 val blocks = if !disable
168 then blocks
169 else (
170 forall_nodes doBlock;
171 if !needFilter then List.filter keepBlock blocks else blocks)
172 in
173 if !dumpCFG
174 then let
175 val prBlock = CFG.dumpBlock (!dumpStrm, cfg)
176 in
177 TextIO.output(!dumpStrm, "[ after jump-chain elimination ]\n");
178 List.app prBlock blocks
179 end
180 else ();
181 (cfg, blocks)
182 end
183
184 end