11import type { ControlFlowInformation } from './control-flow-graph' ;
2- import { visitCfgInOrder } from './simple-visitor' ;
3- import type { NodeId } from '../r-bridge/lang-4.x/ast/model/processing/node-id' ;
42import { convertCfgToBasicBlocks } from './cfg-to-basic-blocks' ;
3+ import type { NormalizedAst } from '../r-bridge/lang-4.x/ast/model/processing/decorate' ;
4+ import type { DataflowGraph } from '../dataflow/graph/graph' ;
5+ import type { NodeId } from '../r-bridge/lang-4.x/ast/model/processing/node-id' ;
6+ import { visitCfgInOrder } from './simple-visitor' ;
7+ import { cfgAnalyzeDeadCode } from './cfg-dead-code' ;
58
6- export type CfgSimplificationPass = ( cfg : ControlFlowInformation ) => ControlFlowInformation ;
9+ export interface CfgPassInfo {
10+ ast ?: NormalizedAst ,
11+ dfg ?: DataflowGraph
12+ }
13+ export type CfgSimplificationPass = ( cfg : ControlFlowInformation , info : CfgPassInfo ) => ControlFlowInformation ;
714
815const CfgSimplificationPasses = {
9- 'unique-cf-sets' : uniqueControlFlowSets ,
10- 'remove-dead-code' : cfgRemoveDeadCode ,
11- 'to-basic-blocks' : toBasicBlocks
16+ 'unique-cf-sets' : uniqueControlFlowSets ,
17+ 'analyze-dead-code' : cfgAnalyzeDeadCode ,
18+ 'remove-dead-code' : cfgRemoveDeadCode ,
19+ 'to-basic-blocks' : toBasicBlocks
1220} as const satisfies Record < string , CfgSimplificationPass > ;
1321
1422export type CfgSimplificationPassName = keyof typeof CfgSimplificationPasses ;
@@ -25,28 +33,20 @@ export const DefaultCfgSimplificationOrder = [
2533 */
2634export function simplifyControlFlowInformation (
2735 cfg : ControlFlowInformation ,
36+ info : CfgPassInfo ,
2837 passes : readonly CfgSimplificationPassName [ ] = DefaultCfgSimplificationOrder
2938) : ControlFlowInformation {
3039 for ( const pass of passes ) {
3140 const passFn = CfgSimplificationPasses [ pass ] ;
32- cfg = passFn ( cfg ) ;
41+ cfg = passFn ( cfg , info ) ;
3342 }
3443 return cfg ;
3544}
3645
37- function uniqueControlFlowSets ( cfg : ControlFlowInformation ) : ControlFlowInformation {
38- return {
39- returns : [ ...new Set ( cfg . returns ) ] ,
40- entryPoints : [ ...new Set ( cfg . entryPoints ) ] ,
41- exitPoints : [ ...new Set ( cfg . exitPoints ) ] ,
42- breaks : [ ...new Set ( cfg . breaks ) ] ,
43- nexts : [ ...new Set ( cfg . nexts ) ] ,
44- graph : cfg . graph
45- } ;
46- }
47-
48- /* currently this does not do work on function definitions */
49- function cfgRemoveDeadCode ( cfg : ControlFlowInformation ) : ControlFlowInformation {
46+ /**
47+ * removes dead vertices and edges from the control flow graph.
48+ */
49+ function cfgRemoveDeadCode ( cfg : ControlFlowInformation , _info ?: CfgPassInfo ) : ControlFlowInformation {
5050 // remove every root level node and accompanying vertices that can not be reached from the entry points
5151 const reachable = new Set < NodeId > ( ) ;
5252 visitCfgInOrder ( cfg . graph , cfg . entryPoints , node => {
@@ -60,6 +60,17 @@ function cfgRemoveDeadCode(cfg: ControlFlowInformation): ControlFlowInformation
6060 return cfg ;
6161}
6262
63- function toBasicBlocks ( cfg : ControlFlowInformation ) : ControlFlowInformation {
63+ function uniqueControlFlowSets ( cfg : ControlFlowInformation , _info ?: CfgPassInfo ) : ControlFlowInformation {
64+ return {
65+ returns : [ ...new Set ( cfg . returns ) ] ,
66+ entryPoints : [ ...new Set ( cfg . entryPoints ) ] ,
67+ exitPoints : [ ...new Set ( cfg . exitPoints ) ] ,
68+ breaks : [ ...new Set ( cfg . breaks ) ] ,
69+ nexts : [ ...new Set ( cfg . nexts ) ] ,
70+ graph : cfg . graph
71+ } ;
72+ }
73+
74+ function toBasicBlocks ( cfg : ControlFlowInformation , _info ?: CfgPassInfo ) : ControlFlowInformation {
6475 return convertCfgToBasicBlocks ( cfg ) ;
6576}
0 commit comments