| 1 |
(* buildFlowgraph.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2001 Bell Labs, Lucent Technologies |
| 4 |
*) |
| 5 |
|
| 6 |
signature CONTROL_FLOWGRAPH_GEN = |
| 7 |
sig |
| 8 |
|
| 9 |
structure S : INSTRUCTION_STREAM |
| 10 |
structure I : INSTRUCTIONS |
| 11 |
structure P : PSEUDO_OPS |
| 12 |
structure CFG : CONTROL_FLOW_GRAPH (* where I = I and P = P *) |
| 13 |
where type I.addressing_mode = I.addressing_mode |
| 14 |
and type I.ea = I.ea |
| 15 |
and type I.instr = I.instr |
| 16 |
and type I.instruction = I.instruction |
| 17 |
and type I.operand = I.operand |
| 18 |
where type P.Client.pseudo_op = P.Client.pseudo_op |
| 19 |
and type P.T.Basis.cond = P.T.Basis.cond |
| 20 |
and type P.T.Basis.div_rounding_mode = P.T.Basis.div_rounding_mode |
| 21 |
and type P.T.Basis.ext = P.T.Basis.ext |
| 22 |
and type P.T.Basis.fcond = P.T.Basis.fcond |
| 23 |
and type P.T.Basis.rounding_mode = P.T.Basis.rounding_mode |
| 24 |
and type P.T.Constant.const = P.T.Constant.const |
| 25 |
and type ('s,'r,'f,'c) P.T.Extension.ccx = ('s,'r,'f,'c) P.T.Extension.ccx |
| 26 |
and type ('s,'r,'f,'c) P.T.Extension.fx = ('s,'r,'f,'c) P.T.Extension.fx |
| 27 |
and type ('s,'r,'f,'c) P.T.Extension.rx = ('s,'r,'f,'c) P.T.Extension.rx |
| 28 |
and type ('s,'r,'f,'c) P.T.Extension.sx = ('s,'r,'f,'c) P.T.Extension.sx |
| 29 |
and type P.T.I.div_rounding_mode = P.T.I.div_rounding_mode |
| 30 |
and type P.T.Region.region = P.T.Region.region |
| 31 |
and type P.T.ccexp = P.T.ccexp |
| 32 |
and type P.T.fexp = P.T.fexp |
| 33 |
(* and type P.T.labexp = P.T.labexp *) |
| 34 |
and type P.T.mlrisc = P.T.mlrisc |
| 35 |
and type P.T.oper = P.T.oper |
| 36 |
and type P.T.rep = P.T.rep |
| 37 |
and type P.T.rexp = P.T.rexp |
| 38 |
and type P.T.stm = P.T.stm |
| 39 |
|
| 40 |
(* |
| 41 |
* This creates an emitter which can be used to build a CFG incrementally |
| 42 |
*) |
| 43 |
type instrStream = |
| 44 |
(I.instruction, Annotations.annotations, I.C.cellset, CFG.cfg) S.stream |
| 45 |
|
| 46 |
val build : unit -> instrStream |
| 47 |
|
| 48 |
end |
| 49 |
|
| 50 |
|
| 51 |
functor BuildFlowgraph |
| 52 |
(structure Props : INSN_PROPERTIES |
| 53 |
structure Stream : INSTRUCTION_STREAM |
| 54 |
structure CFG : CONTROL_FLOW_GRAPH (* where I = Props.I and P = Stream.P *) |
| 55 |
where type I.addressing_mode = Props.I.addressing_mode |
| 56 |
and type I.ea = Props.I.ea |
| 57 |
and type I.instr = Props.I.instr |
| 58 |
and type I.instruction = Props.I.instruction |
| 59 |
and type I.operand = Props.I.operand |
| 60 |
where type P.Client.pseudo_op = Stream.P.Client.pseudo_op |
| 61 |
and type P.T.Basis.cond = Stream.P.T.Basis.cond |
| 62 |
and type P.T.Basis.div_rounding_mode = Stream.P.T.Basis.div_rounding_mode |
| 63 |
and type P.T.Basis.ext = Stream.P.T.Basis.ext |
| 64 |
and type P.T.Basis.fcond = Stream.P.T.Basis.fcond |
| 65 |
and type P.T.Basis.rounding_mode = Stream.P.T.Basis.rounding_mode |
| 66 |
and type P.T.Constant.const = Stream.P.T.Constant.const |
| 67 |
and type ('s,'r,'f,'c) P.T.Extension.ccx = ('s,'r,'f,'c) Stream.P.T.Extension.ccx |
| 68 |
and type ('s,'r,'f,'c) P.T.Extension.fx = ('s,'r,'f,'c) Stream.P.T.Extension.fx |
| 69 |
and type ('s,'r,'f,'c) P.T.Extension.rx = ('s,'r,'f,'c) Stream.P.T.Extension.rx |
| 70 |
and type ('s,'r,'f,'c) P.T.Extension.sx = ('s,'r,'f,'c) Stream.P.T.Extension.sx |
| 71 |
and type P.T.I.div_rounding_mode = Stream.P.T.I.div_rounding_mode |
| 72 |
and type P.T.Region.region = Stream.P.T.Region.region |
| 73 |
and type P.T.ccexp = Stream.P.T.ccexp |
| 74 |
and type P.T.fexp = Stream.P.T.fexp |
| 75 |
(* and type P.T.labexp = Stream.P.T.labexp *) |
| 76 |
and type P.T.mlrisc = Stream.P.T.mlrisc |
| 77 |
and type P.T.oper = Stream.P.T.oper |
| 78 |
and type P.T.rep = Stream.P.T.rep |
| 79 |
and type P.T.rexp = Stream.P.T.rexp |
| 80 |
and type P.T.stm = Stream.P.T.stm |
| 81 |
) : CONTROL_FLOWGRAPH_GEN = |
| 82 |
struct |
| 83 |
structure CFG = CFG |
| 84 |
structure P = CFG.P |
| 85 |
structure I = Props.I |
| 86 |
structure G = Graph |
| 87 |
structure S = Stream |
| 88 |
structure Fmt = Format |
| 89 |
structure PB = PseudoOpsBasisTyp |
| 90 |
|
| 91 |
exception LabelNotFound |
| 92 |
|
| 93 |
type instrStream = |
| 94 |
(I.instruction, Annotations.annotations, CFG.I.C.cellset, CFG.cfg) S.stream |
| 95 |
|
| 96 |
val dumpCFG = |
| 97 |
MLRiscControl.mkFlag |
| 98 |
("dump-initial-cfg", |
| 99 |
"Dump CFG after instruction selection") |
| 100 |
|
| 101 |
|
| 102 |
fun error msg = MLRiscErrorMsg.error ("BuildFlowGraph", msg) |
| 103 |
|
| 104 |
val hashLabel = Word.toInt o Label.hash |
| 105 |
|
| 106 |
fun build () = let |
| 107 |
val cfg as ref(G.GRAPH graph) = ref(CFG.new()) |
| 108 |
|
| 109 |
(* list of blocks generated so far *) |
| 110 |
val blockList = ref ([] : CFG.block list) |
| 111 |
|
| 112 |
(* list of entry labels to patch successors of ENTRY *) |
| 113 |
val entryLabels = ref ([] : Label.label list) |
| 114 |
|
| 115 |
(* block id associated with a label*) |
| 116 |
val labelMap = IntHashTable.mkTable(32, LabelNotFound) |
| 117 |
val findLabel = IntHashTable.find labelMap |
| 118 |
val addLabel = IntHashTable.insert labelMap |
| 119 |
|
| 120 |
(* Data in text segment is read-only *) |
| 121 |
datatype segment_t = TEXT | DATA | RO_DATA | BSS | DECLS |
| 122 |
val segmentF = ref DECLS |
| 123 |
|
| 124 |
(* the block names *) |
| 125 |
val blockNames = ref [] : Annotations.annotations ref |
| 126 |
|
| 127 |
(* can instructions be reordered *) |
| 128 |
val reorder = ref [] : Annotations.annotations ref |
| 129 |
|
| 130 |
(* noblock or invalid block has id of ~1 *) |
| 131 |
val noBlock = CFG.newBlock(~1, ref 0.0) |
| 132 |
|
| 133 |
(* current block being built up *) |
| 134 |
val currentBlock = ref noBlock |
| 135 |
|
| 136 |
|
| 137 |
(* add a new block and make it the current block being built up *) |
| 138 |
fun newBlock (freq) = let |
| 139 |
val G.GRAPH graph = !cfg |
| 140 |
val id = #new_id graph () |
| 141 |
val blk as CFG.BLOCK{annotations, ...} = CFG.newBlock(id, ref freq) |
| 142 |
in |
| 143 |
currentBlock := blk; |
| 144 |
annotations := !blockNames @ !reorder; |
| 145 |
blockList := blk :: !blockList; |
| 146 |
#add_node graph (id, blk); |
| 147 |
blk |
| 148 |
end |
| 149 |
|
| 150 |
|
| 151 |
(* get current basic block *) |
| 152 |
fun getBlock () = |
| 153 |
(case !currentBlock of CFG.BLOCK{id= ~1, ...} => newBlock(1.0) | blk => blk) |
| 154 |
|
| 155 |
|
| 156 |
(* ------------------------cluster---------------------------*) |
| 157 |
(* start a new cluster *) |
| 158 |
fun beginCluster _ = |
| 159 |
(blockList := []; |
| 160 |
entryLabels := []; |
| 161 |
IntHashTable.clear labelMap; |
| 162 |
blockNames := []; |
| 163 |
currentBlock := noBlock) |
| 164 |
|
| 165 |
(* emit an instruction *) |
| 166 |
fun emit i = let |
| 167 |
val CFG.BLOCK{insns, ...} = getBlock() |
| 168 |
fun terminate() = currentBlock := noBlock; |
| 169 |
in |
| 170 |
insns := i:: !insns; |
| 171 |
case Props.instrKind(i) |
| 172 |
of Props.IK_JUMP => terminate() |
| 173 |
| Props.IK_CALL_WITH_CUTS => terminate() |
| 174 |
| _ => () |
| 175 |
(*esac*) |
| 176 |
end |
| 177 |
|
| 178 |
(* make current block an exit block *) |
| 179 |
fun exitBlock liveout = let |
| 180 |
fun setLiveOut(CFG.BLOCK{annotations, ...}) = |
| 181 |
annotations := #create CFG.LIVEOUT liveout :: !annotations |
| 182 |
in |
| 183 |
case !currentBlock |
| 184 |
of CFG.BLOCK{id= ~1, ...} => |
| 185 |
(case !blockList |
| 186 |
of [] => error "exitBlocks" |
| 187 |
| blk::_ => setLiveOut blk |
| 188 |
(*esac*)) |
| 189 |
| blk => setLiveOut blk |
| 190 |
end (* exitBlock *) |
| 191 |
|
| 192 |
|
| 193 |
(* end cluster --- all done *) |
| 194 |
fun endCluster (annotations) = let |
| 195 |
val cfg as G.GRAPH graph = (!cfg before cfg := CFG.new()) |
| 196 |
val _ = CFG.init(cfg) (* create unique ENTRY/EXIT nodes *) |
| 197 |
|
| 198 |
val ENTRY = hd(#entries graph ()) |
| 199 |
val EXIT = hd(#exits graph ()) |
| 200 |
|
| 201 |
fun addEdge(from, to, kind) = |
| 202 |
#add_edge graph (from, to, CFG.EDGE{k=kind, w=ref 0.0, a=ref[]}) |
| 203 |
|
| 204 |
fun addEdgeAn(from, to, kind, an) = |
| 205 |
#add_edge graph (from, to, CFG.EDGE{k=kind, w=ref 0.0, a=ref an}) |
| 206 |
|
| 207 |
fun target lab = |
| 208 |
(case (IntHashTable.find labelMap (hashLabel lab)) |
| 209 |
of SOME bId => bId |
| 210 |
| NONE => EXIT) |
| 211 |
|
| 212 |
val {get=getProb, ...} = MLRiscAnnotations.BRANCH_PROB |
| 213 |
|
| 214 |
fun jump(from, instr, blocks) = let |
| 215 |
fun branch(targetLab) = let |
| 216 |
val (_, an) = Props.getAnnotations instr |
| 217 |
val an = List.filter |
| 218 |
(fn (MLRiscAnnotations.BRANCHPROB _) => true | _ => false) |
| 219 |
an |
| 220 |
fun next(CFG.BLOCK{id, ...}::_) = id |
| 221 |
| next [] = error "jump.next" |
| 222 |
in |
| 223 |
addEdgeAn(from, target targetLab, CFG.BRANCH true, an); |
| 224 |
addEdge(from, next blocks, CFG.BRANCH false) |
| 225 |
end |
| 226 |
in |
| 227 |
case Props.branchTargets instr |
| 228 |
of [Props.ESCAPES] => addEdge(from, EXIT, CFG.EXIT) |
| 229 |
| [Props.LABELLED lab] => addEdge(from, target lab, CFG.JUMP) |
| 230 |
| [Props.LABELLED lab, Props.FALLTHROUGH] => branch(lab) |
| 231 |
| [Props.FALLTHROUGH, Props.LABELLED lab] => branch(lab) |
| 232 |
| targets => let |
| 233 |
fun switch(Props.LABELLED lab, n) = |
| 234 |
(addEdge(from, target lab, CFG.SWITCH(n)); n+1) |
| 235 |
| switch _ = error "jump.switch" |
| 236 |
in List.foldl switch 0 targets; () |
| 237 |
end |
| 238 |
end |
| 239 |
|
| 240 |
and fallsThru(id, blks) = |
| 241 |
case blks |
| 242 |
of [] => addEdge(id, EXIT, CFG.EXIT) |
| 243 |
| CFG.BLOCK{id=next, ...}::_ => addEdge(id, next, CFG.FALLSTHRU) |
| 244 |
(*esac*) |
| 245 |
|
| 246 |
and addEdges [] = () |
| 247 |
| addEdges(CFG.BLOCK{id, insns=ref[], ...}::blocks) = fallsThru(id, blocks) |
| 248 |
| addEdges(CFG.BLOCK{id, insns=ref(instr::_), ...}::blocks) = let |
| 249 |
fun doJmp () = jump(id, instr, blocks) |
| 250 |
in |
| 251 |
case Props.instrKind instr |
| 252 |
of Props.IK_JUMP => doJmp() |
| 253 |
| Props.IK_CALL_WITH_CUTS => doJmp() |
| 254 |
| _ => fallsThru(id, blocks) |
| 255 |
(*esac*); |
| 256 |
addEdges(blocks) |
| 257 |
end |
| 258 |
in |
| 259 |
addEdges (rev(!blockList)); |
| 260 |
app (fn lab => addEdge(ENTRY, target lab, CFG.ENTRY)) (!entryLabels); |
| 261 |
let val an = CFG.annotations cfg in an := annotations @ (!an) end; |
| 262 |
if !dumpCFG |
| 263 |
then CFG.dump ( |
| 264 |
!MLRiscControl.debug_stream, |
| 265 |
"after instruction selection", cfg) |
| 266 |
else (); |
| 267 |
cfg |
| 268 |
end (* endCluster *) |
| 269 |
|
| 270 |
|
| 271 |
(* ------------------------annotations-----------------------*) |
| 272 |
(* XXX: Bug: EMPTYBLOCK does not really generate an empty block |
| 273 |
* but merely terminates the current block. Contradicts the comment |
| 274 |
* in instructions/mlriscAnnotations.sig. |
| 275 |
* It should be (newBlock(1.0); newBlock(1.0); ()) |
| 276 |
*) |
| 277 |
|
| 278 |
(* Add a new annotation *) |
| 279 |
fun addAnnotation a = |
| 280 |
(case a |
| 281 |
of MLRiscAnnotations.BLOCKNAMES names => |
| 282 |
(blockNames := names; newBlock(1.0); ()) |
| 283 |
| MLRiscAnnotations.EMPTYBLOCK => (newBlock(1.0); ()) |
| 284 |
| MLRiscAnnotations.EXECUTIONFREQ f => |
| 285 |
(case !currentBlock |
| 286 |
of CFG.BLOCK{id= ~1, ...} => (newBlock(real f); ()) |
| 287 |
| CFG.BLOCK{freq, ...} => freq := real f |
| 288 |
(*esac*)) |
| 289 |
| a => let |
| 290 |
val CFG.BLOCK{annotations,...} = getBlock() |
| 291 |
in annotations := a :: !annotations |
| 292 |
end |
| 293 |
(*esac*)) |
| 294 |
|
| 295 |
(* get annotation associated with flow graph *) |
| 296 |
fun getAnnotations () = CFG.annotations(!cfg) |
| 297 |
|
| 298 |
(* add a comment annotation to the current block *) |
| 299 |
fun comment msg = |
| 300 |
case !segmentF |
| 301 |
of TEXT => addAnnotation (#create MLRiscAnnotations.COMMENT msg) |
| 302 |
| _ => let |
| 303 |
val Graph.GRAPH graph = !cfg |
| 304 |
val CFG.INFO{data, ...} = #graph_info graph |
| 305 |
in data := PB.COMMENT msg :: !data |
| 306 |
end |
| 307 |
|
| 308 |
|
| 309 |
(* -------------------------labels---------------------------*) |
| 310 |
(* BUG: Does not respect any ordering between labels and pseudoOps. |
| 311 |
* This could be a problem with jump tables. |
| 312 |
*) |
| 313 |
fun addPseudoOp p = let |
| 314 |
val Graph.GRAPH graph = !cfg |
| 315 |
val CFG.INFO{data, decls, ...} = #graph_info graph |
| 316 |
|
| 317 |
fun addAlignment () = |
| 318 |
(case !segmentF |
| 319 |
of DECLS => error "addAlignment: DECLS" |
| 320 |
| TEXT => let |
| 321 |
val CFG.BLOCK{align, ...} = newBlock 1.0 |
| 322 |
in align := SOME p |
| 323 |
end |
| 324 |
| _ => data := p :: !data |
| 325 |
(*esac*)) |
| 326 |
|
| 327 |
fun startSegment(seg) = (data := p :: !data; segmentF := seg) |
| 328 |
|
| 329 |
fun addData () = data := p :: !data |
| 330 |
|
| 331 |
fun chkAddData(seg) = let |
| 332 |
fun errmsg curr = |
| 333 |
Fmt.format "addPseudoOp: %s in %s segment" [Fmt.STR seg, Fmt.STR curr] |
| 334 |
in |
| 335 |
case !segmentF |
| 336 |
of DECLS => error(errmsg "DECLS") |
| 337 |
| TEXT => error(errmsg "TEXT") |
| 338 |
| _ => data := p :: !data |
| 339 |
(*esac*) |
| 340 |
end |
| 341 |
|
| 342 |
fun addDecl() = |
| 343 |
(case !segmentF |
| 344 |
of DECLS => decls := p :: !decls |
| 345 |
| _ => data := p :: !data |
| 346 |
(*esac*)) |
| 347 |
in |
| 348 |
case p |
| 349 |
of PB.ALIGN_SZ _ => addAlignment() |
| 350 |
| PB.ALIGN_ENTRY => addAlignment() |
| 351 |
| PB.ALIGN_LABEL => addAlignment() |
| 352 |
| PB.DATA_LABEL _ => |
| 353 |
(case !segmentF |
| 354 |
of TEXT => error "addPseudoOp: DATA_LABEL in TEXT segment" |
| 355 |
| _ => (data := p:: !data) |
| 356 |
(*esac*)) |
| 357 |
|
| 358 |
| PB.DATA_READ_ONLY => startSegment(RO_DATA) |
| 359 |
| PB.DATA => startSegment(DATA) |
| 360 |
| PB.TEXT => segmentF := TEXT |
| 361 |
| PB.BSS => startSegment(BSS) |
| 362 |
| PB.SECTION _ => |
| 363 |
(case !segmentF |
| 364 |
of TEXT => error "addPseudoOp: SECTION in TEXT segment" |
| 365 |
| _ => data := p :: !data |
| 366 |
(*esac*)) |
| 367 |
| PB.REORDER => (reorder := []; newBlock 1.0; ()) |
| 368 |
| PB.NOREORDER => |
| 369 |
(reorder := [#create MLRiscAnnotations.NOREORDER ()]; newBlock 1.0; ()) |
| 370 |
|
| 371 |
| PB.INT _ => chkAddData("INT") |
| 372 |
| PB.FLOAT _ => chkAddData("FLOAT") |
| 373 |
| PB.ASCII _ => chkAddData("ASCII") |
| 374 |
| PB.ASCIIZ _ => chkAddData("ASCIIZ") |
| 375 |
| PB.SPACE _ => chkAddData("SPACE") |
| 376 |
| PB.COMMENT _ => addDecl() |
| 377 |
| PB.IMPORT _ => addDecl() |
| 378 |
| PB.EXPORT _ => addDecl() |
| 379 |
| PB.EXT _ => |
| 380 |
(case !segmentF |
| 381 |
of TEXT => error "EXT in TEXT segment" |
| 382 |
| _ => addDecl() |
| 383 |
(*esac*)) |
| 384 |
end |
| 385 |
|
| 386 |
fun defineLabel lab = |
| 387 |
(case !segmentF |
| 388 |
of TEXT => |
| 389 |
(case findLabel (hashLabel lab) |
| 390 |
of NONE => let |
| 391 |
fun newBlk () = |
| 392 |
(case !currentBlock |
| 393 |
of CFG.BLOCK{id= ~1, ...} => newBlock 1.0 |
| 394 |
| CFG.BLOCK{insns=ref[], ...} => !currentBlock (* probably aligned block *) |
| 395 |
| _ => newBlock 1.0 |
| 396 |
(*esac*)) |
| 397 |
val CFG.BLOCK{id, labels, ...} = newBlk() |
| 398 |
in |
| 399 |
labels := lab :: !labels; |
| 400 |
addLabel(hashLabel lab, id) |
| 401 |
end |
| 402 |
| SOME _ => |
| 403 |
error (concat |
| 404 |
["multiple definitions of label \"", Label.toString lab, "\""]) |
| 405 |
(*esac*)) |
| 406 |
| _ => let |
| 407 |
(* non-text segment *) |
| 408 |
val Graph.GRAPH graph = !cfg |
| 409 |
val CFG.INFO{data, ...} = #graph_info graph |
| 410 |
in |
| 411 |
data := PB.DATA_LABEL lab :: !data |
| 412 |
end |
| 413 |
(*esac*)) |
| 414 |
|
| 415 |
fun entryLabel lab = (defineLabel lab; entryLabels := lab :: !entryLabels) |
| 416 |
in |
| 417 |
S.STREAM |
| 418 |
{ |
| 419 |
comment = comment, |
| 420 |
getAnnotations= getAnnotations, |
| 421 |
annotation = addAnnotation, |
| 422 |
defineLabel = defineLabel, |
| 423 |
entryLabel = entryLabel, |
| 424 |
pseudoOp = addPseudoOp, |
| 425 |
beginCluster = beginCluster, |
| 426 |
emit = emit, |
| 427 |
exitBlock = exitBlock, |
| 428 |
endCluster = endCluster |
| 429 |
} |
| 430 |
end (* build *) |
| 431 |
end |