-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliveness.sml
447 lines (375 loc) · 14.4 KB
/
liveness.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
structure Liveness:
sig
datatype igraph =
IGRAPH of {
graph: IGraph.graph,
tnode: Temp.temp -> IGraph.node,
gtemp: IGraph.node -> Temp.temp,
moves: (Graph.node * Graph.node) list
}
type liveSet
type liveMap
val interferenceGraph: Flow.flowgraph -> igraph * ((*Flow.*)Graph.node -> Temp.temp list)
val show: TextIO.outstream * igraph -> unit
val show': igraph -> unit
end
=
struct
structure Frame : FRAME = MipsFrame
datatype igraph =
IGRAPH of {
graph: IGraph.graph,
tnode: Temp.temp -> IGraph.node,
gtemp: IGraph.node -> Temp.temp,
moves: (IGraph.node * IGraph.node) list
}
(*The table is useful for efficient membership tests, the list is useful for enumerating all the live variables in the set*)
type liveSet = unit Temp.Table.table * Temp.temp list
type liveMap = liveSet (*Flow.*)Graph.Table.table
val allowPrint = false
fun log info = if allowPrint then print("***Liveness*** "^info^"\n") else ()
fun log' info = if allowPrint then print(info) else ()
(*Reference Only*)
(*
* in[n] = use[n] U (out[n] - def[n])
* out[n] = U_(s of succ[n]) in[s]
*)
(*datatype flowgraph = FGRAPH of {
control: Graph.graph,
def: Temp.temp list Graph.Table.table,
use: Temp.temp list Graph.Table.table,
ismove: bool Graph.Table.table
}*)
(*def,use : node -> temp list*)
(*Helper functions*)
fun printNodeList(nodelist,nodeToTemp) = (
List.app (fn item => log'(IGraph.nodename(item)^"("^Temp.makestring(nodeToTemp(item))^") ")) nodelist;
log'("\n")
)
fun interferenceGraph(Flow.FGRAPH{control,def,use,ismove}) =
let
(*Helper functions*)
fun printTempList templist = (
List.app (fn item => log'(Temp.makestring(item)^" ")) templist;
log'("\n")
)
(*Make sure register lik $fp will never be assign*)
fun filterTempWithSpecialArgs(templist : Temp.temp list) : Temp.temp list =
let
val specialArgs : Temp.temp list = [email protected]
fun filterSpecialArgs temp =
let
val found = ref false
val () = List.app (fn specialArg =>
if temp = specialArg then (found := true) else ()
) specialArgs
in
(*!found*) true
end
in
(*List.filter filterSpecialArgs templist*)
templist
end
fun getTempListFromMap(map,node) : Temp.temp list =
case Graph.Table.look(map,node) of
SOME(templist) => templist
| NONE => []
fun getExistingLiveSet(map,node) : Temp.temp list=
case Graph.Table.look(map,node) of
SOME((table,templist)) => templist
| NONE => []
(*combine two temp lists and get rid of duplicate items*)
fun combineTempList(temps1,temps2) : Temp.temp list =
let
val set = ref IntBinarySet.empty
val () = List.app (fn temp => (set := IntBinarySet.add(!set,temp))) temps1
val () = List.app (fn temp => (set := IntBinarySet.add(!set,temp))) temps2
val tempsResult = IntBinarySet.listItems(!set)
(*val () = log("Combine Liveout temps1:"^Int.toString(List.length(temps1))^" temps2:"^Int.toString(List.length(temps2))^" tempsResult:"^Int.toString(List.length(tempsResult)))*)
in
tempsResult
end
(*diff templist1 with templist2, return [] if no diff*)
fun diffTempList(temps1,temps2) : Temp.temp list =
let
val () = log("DiffTempList temp1:"^Int.toString(List.length(temps1))^" temp2:"^Int.toString(List.length(temps2)))
val set1 = ref IntBinarySet.empty
val set2 = ref IntBinarySet.empty
val () = List.app (fn temp => (set1 := IntBinarySet.add(!set1,temp))) temps1
val () = List.app (fn temp => (set2 := IntBinarySet.add(!set2,temp))) temps2
val () = log("set1:"^Int.toString(IntBinarySet.numItems(!set1))^" set2:"^Int.toString(IntBinarySet.numItems(!set2)))
val diffSet = IntBinarySet.difference(!set1,!set2)
val tempsResult = IntBinarySet.listItems(diffSet)
in
tempsResult
end
(*Initialization*)
val controlNodes = Graph.nodes(control)
val liveInMapInstance : liveMap ref = ref Graph.Table.empty
val liveOutMapInstance : liveMap ref = ref Graph.Table.empty
(*Warning! using IntBinaryMap*)
val tempToNodeMap : IGraph.node IntBinaryMap.map ref = ref IntBinaryMap.empty
val nodeToTempMap : Temp.temp Graph.Table.table ref = ref Graph.Table.empty
(*iGraph*)
val interferenceGraphResult = IGraph.newGraph()
(*Method for result*)
fun tempToNode(temp:Temp.temp) : IGraph.node = (*IGraph.newNode(IGraph.newGraph())*)
case IntBinaryMap.find(!tempToNodeMap,temp) of
SOME(node) => node
| NONE =>
let
val newNode = IGraph.newNode(interferenceGraphResult)
(*update tempToNodeMap & nodeToTempMap*)
val () = (tempToNodeMap := IntBinaryMap.insert(!tempToNodeMap,temp,newNode))
val () = (nodeToTempMap := Graph.Table.enter(!nodeToTempMap,newNode,temp))
in
log("Warning,couldn't find the temp. Creating newNode."^IGraph.nodename(newNode));
newNode
end
fun nodeToTemp node : Temp.temp =
case Graph.Table.look(!nodeToTempMap,node) of
SOME(temp) => temp
| NONE =>
let
val newTemp = Temp.newtemp()
(*update tempToNodeMap & nodeToTempMap*)
val () = (tempToNodeMap := IntBinaryMap.insert(!tempToNodeMap,newTemp,node))
val () = (nodeToTempMap := Graph.Table.enter(!nodeToTempMap,node,newTemp))
in
log("Warning,couldn't find the node. Creating newTemp.");
newTemp
end
(*TO-DO*)
val movesList = []
(*A table mapping each flow-graph node to the set of temps that are live-out at that node*)
fun getLiveOutTemps(node:IGraph.node) : Temp.temp list =
case Graph.Table.look(!liveOutMapInstance,node) of
SOME(table,templist) => templist
| NONE => (log("Error!Should have found."); [])
(*
* Traverse the flowGraph(nodes) reversely
* Update live-in & live-out for each node until no more chagnes
*)
val liveInfoModified = ref false
fun updateLiveInfo node =
let
(*Init*)
val () = log("=====traversing:====="^Graph.nodename(node))
val succs = Graph.succ(node)
val currDef = getTempListFromMap(def,node)
val currUse = getTempListFromMap(use,node)
val () = List.app (fn succ => log(Graph.nodename(node)^"'s succ:"^Graph.nodename(succ))) succs
(*Previous live-out & live-in*)
val prevIn = getExistingLiveSet(!liveInMapInstance,node)
val prevOut = getExistingLiveSet(!liveOutMapInstance,node)
(*Updated live-out & live-in*)
(*traverse succs*)
fun traverseSucc(succ::succs,prevList) : Temp.temp list =
let
(*out[n] = U_(s of succ[n]) in[s]*)
val succList = getExistingLiveSet(!liveInMapInstance,succ)
val () = log(" in Succ List Lei Li ***** "^Graph.nodename(succ)^" **** in ")
val currList = combineTempList(prevList,succList)
in
traverseSucc(succs,currList)
end
|traverseSucc([],prevList) = (prevList)
val currOut = traverseSucc(succs,prevOut)
val currIn = combineTempList(currUse,diffTempList(currOut,currDef)) (*in[n] = use[n] U (out[n] - def[n])*)
(*val () = log("CurrIn:"^Int.toString(List.length(currIn))^" CurrOut:"^Int.toString(List.length(currOut)))*)
val () = log("Def:")
val () = printTempList currDef
val () = log("Use:")
val () = printTempList currUse
val () = log("CurrLiveOut:")
val () = printTempList currOut
val () = log("CurrLiveIn:")
val () = printTempList currIn
val () = log("PrevLiveOut:")
val () = printTempList prevOut
val () = log("PrevLiveIn:")
val () = printTempList prevIn
(*Least fix point check, diffTempList = [] means no change*)
fun checkNoChange(l1,l2) =
if diffTempList(l2,l1) = []
then (log("No change.")(*shouldn't set false, should only set true when modified*))
else (log("There's change."); liveInfoModified := true)
val () = checkNoChange(prevIn,currIn)
val () = checkNoChange(prevOut,currOut)
(*Result*)
val liveInSetOfCurrentNode : liveSet = (Temp.Table.empty,currIn)
val liveOutSetOfCurrentNode : liveSet = (Temp.Table.empty,currOut)
in
(*Update map*)
liveInMapInstance := Graph.Table.enter(!liveInMapInstance,node,liveInSetOfCurrentNode);
liveOutMapInstance := Graph.Table.enter(!liveOutMapInstance,node,liveOutSetOfCurrentNode)
end
fun traverseNodeReversely () = (
log("in in in in in in in in in ");
log("val unit in bool traverseNodeReversely start!");
List.app updateLiveInfo (List.rev(controlNodes));
(*Continue until no more chagnes in live-in & live-out*)
if !liveInfoModified
then (
liveInfoModified := false;
log("traverseNodeReversely next!");
traverseNodeReversely()
)
else (
log("in traverseNodeReversely finish!")
)
)
val () = traverseNodeReversely()
(*
* Build the interference graph according to the liveness info
* Add an edge to the graph for each pair of temporaies in the set
*
* The node passed in is the control-flow graph node
*)
fun createNodeAndConnectEdge controlNode =
let
(*TO-DO*)
(*
* Check current node ismove?
* If yes, store the info and find a match
* and store to
* moves: (IGraph.node * IGraph.node) list
*)
val () = log("=====createNodeAndConnectEdge:====="^Graph.nodename(controlNode))
val () = log("Temp:"^Temp.makestring(nodeToTemp(controlNode)))
(*Assum definitely can be found*)
val currDefTempList = getTempListFromMap(def,controlNode)
val liveOutTemplist = getLiveOutTemps(controlNode)
(*TO-DO may not be right*)
(*val tempList = combineTempList(currDefTempList,liveOutTemplist)*)
(*Traverse live-out temp list, and find/creat new igraph.node in the meanwhile*)
(*See tempToNode & nodeToTemp*)
(*fun traverseLiveOutTempList(temp::temps,igraphNodes) = *)
(*let*)
(*val igraphNode = tempToNode(temp)*)
(*val () = log("igraphNode:"^IGraph.nodename(igraphNode))*)
(*val igraphNodesResult = igraphNode::traverseLiveOutTempList(temps,igraphNodes)*)
(*val () = log("igraphNode number:"^Int.toString(List.length(igraphNodesResult)))*)
(*in*)
(*igraphNodesResult*)
(*igraphNode::igraphNodes*)
(*end*)
(*| traverseLiveOutTempList([],igraphNodes) = igraphNodes*)
(*val igraphNodes = traverseLiveOutTempList(liveOutTemplist,[])*)
val currDefIgraphNodes = map tempToNode currDefTempList
val liveOutIgraphNodes = map tempToNode liveOutTemplist
(*val igraphNodes = map tempToNode tempList*)
val () = log("!!!!!!igraphNodes!!!!!")
(*val () = printNodeList(igraphNodes)*)
val () = log'("Def:")
val () = printNodeList(currDefIgraphNodes,nodeToTemp)
val () = log'("Live out:")
val () = printNodeList(liveOutIgraphNodes,nodeToTemp)
(*------------------------------------------------------------------*)
(* take a look at following *)
(* || *)
(* \/ *)
(*------------------------------------------------------------------*)
(* 这里难道不应该是把 node 和 igraphNodes 连起来么? *)
(* node是我们在处理的那个node,igraphNodes 是所有跟他interfere的节点 *)
(* 按照老师上课讲的,应该是把node和所有liveoutnode连接起来,
也就是你这里的igraphnodes, *)
(* 而不是igraphnodes 和igraphNodes 连 *)
(* am i right? *)
(*------------------------------------------------------------------*)
(*Connect the igraph node with each other*)
(*TO-DO bug with duplicated igraph node*)
(*fun connectIGraphNode(node1::nodes1,node2::nodes2) =
if IGraph.eq(node1,node2)
then (
connectIGraphNode(node1::nil,nodes2);
connectIGraphNode(nodes1,nodes2)
)
else (
log("Connecting edge:"^IGraph.nodename(node1)^"and"^IGraph.nodename(node2));
IGraph.mk_edge{from=node1,to=node2};
Connect node1 with all nodes2
connectIGraphNode(node1::nil,nodes2);
Connect nodes1 with all node2 and nodes2
connectIGraphNode(nodes1,node2::nodes2)
)
| connectIGraphNode([],_) = ()
| connectIGraphNode(_,[]) = ()*)
(*val () = connectIGraphNode(currDefIgraphNodes,liveOutIgraphNodes)*)
(*don't connect if already connected*)
fun alreadyAdjacent(node1,node2) : bool =
let
val adjNodes1 = IGraph.adj(node1)
in
List.exists (fn adjNode => IGraph.eq(adjNode,node2)) adjNodes1
end
fun connectIGraphNode(node1,node2) =
if IGraph.eq(node1,node2) orelse alreadyAdjacent(node1,node2)
then ()
else IGraph.mk_edge{from=node1,to=node2}
val () = app (fn defNode =>
app (fn liveOutNode =>
connectIGraphNode(defNode,liveOutNode)
) liveOutIgraphNodes
) currDefIgraphNodes
in
()
end
fun travserLiveOutMap() =
(
List.app createNodeAndConnectEdge controlNodes
)
val () = travserLiveOutMap()
(*Result*)
val igraph = IGRAPH{
graph=interferenceGraphResult,
tnode=tempToNode,
gtemp=nodeToTemp,
moves=movesList
}
val () = show(TextIO.openOut("liveness_show"),igraph)
in
(igraph, getLiveOutTemps)
end
(*
* Show prints out for, debugging purposes, a list of nodes in the interference graph,
* and for each node, a list of nodes adjacent to it.
*)
(*TO-DO for outstream*)
and show (outstream,IGRAPH{graph,tnode,gtemp,moves}) =
let
val () = log("show")
val igraphNodes = IGraph.nodes(graph)
val () = List.app (
fn inode =>
let
val adjNodes = IGraph.adj(inode)
in
log'("Current node:");
printNodeList([inode],gtemp);
log'("Adjacent nodes:");
printNodeList(adjNodes,gtemp)
end
) igraphNodes
in
()
end
and show' (IGRAPH{graph,tnode,gtemp,moves}) =
let
val () = log("show")
val igraphNodes = IGraph.nodes(graph)
val () = List.app (
fn inode =>
let
val adjNodes = IGraph.adj(inode)
in
log'("Current node:");
printNodeList([inode],gtemp);
log'("Adjacent nodes:");
printNodeList(adjNodes,gtemp)
end
) igraphNodes
in
()
end
end