Debian Codegraph @

lib/mlrisc-lib/MLRISC/IR/mlrisc-cfg.sig

raw
1 (*
2 * Control flow graph data structure used by the MLRISC IR.
3 * All basic optimizations are based on this representation.
4 *
5 * -- Allen
6 *)
7
8 signature CONTROL_FLOW_GRAPH =
9 sig
10
11 structure I : INSTRUCTIONS
12 structure P : PSEUDO_OPS
13 structure C : CELLS
14 structure W : FREQ
15 sharing I.C = C
16
17 type weight = W.freq
18
19 datatype block_kind =
20 START (* entry node *)
21 | STOP (* exit node *)
22 | NORMAL (* normal node *)
23 | HYPERBLOCK (* hyperblock *)
24
25 and data = LABEL of Label.label
26 | PSEUDO of P.pseudo_op
27
28 (*
29 * NOTE: the instructions are listed in reverse order.
30 * This choice is for a few reasons:
31 * i) Clusters represent instructions in reverse order, so keeping this
32 * the same avoid having to do conversions.
33 * ii) This makes it easier to add instructions at the end of the block,
34 * which is more common than adding instructions to the front.
35 * iii) This also makes it easier to manipulate the branch/jump instruction
36 * at the end of the block.
37 *)
38
39 and block =
40 BLOCK of
41 { id : int, (* block id *)
42 kind : block_kind, (* block kind *)
43 freq : weight ref, (* execution frequency *)
44 data : data list ref, (* data preceeding block *)
45 labels : Label.label list ref, (* labels on blocks *)
46 insns : I.instruction list ref, (* in rev order *)
47 annotations : Annotations.annotations ref (* annotations *)
48 }
49
50
51 and edge_kind = ENTRY (* entry edge *)
52 | EXIT (* exit edge *)
53 | JUMP (* unconditional jump *)
54 | FALLSTHRU (* falls through to next block *)
55 | BRANCH of bool (* branch *)
56 | SWITCH of int (* computed goto *)
57 | SIDEEXIT of int (* the ith side exit in a hyperblock *)
58
59 and edge_info = EDGE of { k : edge_kind, (* edge kind *)
60 w : weight ref, (* edge freq *)
61 a : Annotations.annotations ref (* annotations *)
62 }
63
64 type edge = edge_info Graph.edge
65 type node = block Graph.node
66
67 datatype info =
68 INFO of { annotations : Annotations.annotations ref,
69 firstBlock : int ref, (* id of first block *)
70 reorder : bool ref (* has the CFG been reordered? *)
71 }
72
73 type cfg = (block,edge_info,info) Graph.graph
74
75 (*========================================================================
76 *
77 * Various kinds of annotations on basic blocks
78 *
79 *========================================================================*)
80 val LIVEOUT : C.cellset Annotations.property
81 (* escaping live out information *)
82 val CHANGED : (string * (unit -> unit)) Annotations.property
83
84 (*========================================================================
85 *
86 * Methods for manipulating basic blocks
87 *
88 *========================================================================*)
89 val newBlock : int * W.freq ref -> block (* empty *)
90 val newStart : int * W.freq ref -> block (* start node *)
91 val newStop : int * W.freq ref -> block (* stop node *)
92 val copyBlock : int * block -> block (* copy a block *)
93 val defineLabel : block -> Label.label (* define a label *)
94 val insns : block -> I.instruction list ref
95 val freq : block -> W.freq ref
96 val branchOf : edge_info -> bool option
97
98 (* emit assembly *)
99 val emit : Annotations.annotations -> block -> unit
100 val show_block : Annotations.annotations -> block -> string
101
102 (*========================================================================
103 *
104 * Methods for manipulating CFG
105 *
106 *========================================================================*)
107 val cfg : info -> cfg (* create a new cfg *)
108 val new : unit -> cfg (* create a new cfg *)
109 val subgraph : cfg -> cfg (* mark as subgraph *)
110 val init : cfg -> unit (* add start/stop nodes *)
111 val changed : cfg -> unit (* mark cfg as changed *)
112
113 val annotations : cfg -> Annotations.annotations ref
114 val liveOut : block -> C.cellset
115 val fallsThruFrom : cfg * Graph.node_id -> Graph.node_id option
116 val fallsThruTo : cfg * Graph.node_id -> Graph.node_id option
117 val removeEdge : cfg -> edge -> unit
118 val setBranch : cfg * Graph.node_id * bool -> I.instruction
119 val edgeDir : edge_info Graph.edge -> bool option
120
121 (*========================================================================
122 *
123 * For viewing
124 *
125 *========================================================================*)
126 val viewStyle : cfg -> (block,edge_info,info) GraphLayout.style
127 val viewLayout : cfg -> GraphLayout.layout
128 val headerText : block -> string
129 val footerText : block -> string
130 val subgraphLayout : { cfg : cfg, subgraph : cfg } -> GraphLayout.layout
131
132 (*========================================================================
133 *
134 * Miscellaneous stuff
135 *
136 *========================================================================*)
137 val cdgEdge : edge_info -> bool (* for building a CDG *)
138
139 end
140