Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Diff of /branches/vis15/src/compiler/simplify/simplify.sml
ViewVC logotype

Diff of /branches/vis15/src/compiler/simplify/simplify.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 4083, Tue Jun 28 20:55:46 2016 UTC revision 4113, Thu Jun 30 13:52:19 2016 UTC
# Line 85  Line 85 
85    (* make a block out of a list of statements that are in reverse order *)    (* make a block out of a list of statements that are in reverse order *)
86      fun mkBlock stms = S.Block{props = PropList.newHolder(), code = List.rev stms}      fun mkBlock stms = S.Block{props = PropList.newHolder(), code = List.rev stms}
87    
     datatype 'a ctl_flow_info  
       = EXIT                    (* stm sequence always exits; no pruning so far *)  
       | PRUNE of 'a             (* stm sequence always exits at last stm in argument, which  
                                  * is either a block or stm list *)  
       | CONT                    (* stm sequence falls through *)  
       | EDIT of 'a              (* pruned code that has non-exiting paths *)  
   
     fun pruneUnreachableCode blk = let  
           fun isExit S.S_Die = true  
             | isExit S.S_Stabilize = true  
             | isExit (S.S_Return _) = true  
             | isExit _ = false  
           fun pruneStms [] = CONT  
             | pruneStms [S.S_IfThenElse(x, blk1, blk2)] = (  
                 case pruneIf(x, blk1, blk2)  
                  of EXIT => EXIT  
                   | PRUNE stm => PRUNE[stm]  
                   | CONT => CONT  
                   | EDIT stm => EDIT[stm]  
                 (* end case *))  
             | pruneStms [stm] = if isExit stm then EXIT else CONT  
             | pruneStms ((stm as S.S_IfThenElse(x, blk1, blk2))::stms) = (  
                 case pruneIf(x, blk1, blk2)  
                  of EXIT => PRUNE[stm]  
                   | PRUNE stm => PRUNE[stm]  
                   | CONT => (case pruneStms stms  
                        of PRUNE stms => PRUNE(stm::stms)  
                         | EDIT stms => EDIT(stm::stms)  
                         | EXIT => EXIT (* different instances of ctl_flow_info *)  
                         | CONT => CONT  
                       (* end case *))  
                   | EDIT stm => (case pruneStms stms  
                        of PRUNE stms => PRUNE(stm::stms)  
                         | EDIT stms => EDIT(stm::stms)  
                         | _ => EDIT(stm::stms)  
                       (* end case *))  
                 (* end case *))  
             | pruneStms (stm::stms) = if isExit stm  
                 then PRUNE[stm]  
                 else (case pruneStms stms  
                    of PRUNE stms => PRUNE(stm::stms)  
                     | EDIT stms => EDIT(stm::stms)  
                     | info => info  
                   (* end case *))  
           and pruneIf (x, blk1, blk2) = (case (pruneBlk blk1, pruneBlk blk2)  
                  of (EXIT,       EXIT      ) => EXIT  
                   | (CONT,       CONT      ) => CONT  
                   | (CONT,       EXIT      ) => CONT  
                   | (EXIT,       CONT      ) => CONT  
                   | (CONT,       EDIT blk2 ) => EDIT(S.S_IfThenElse(x, blk1, blk2))  
                   | (EDIT blk1,  CONT      ) => EDIT(S.S_IfThenElse(x, blk1, blk2))  
                   | (CONT,       PRUNE blk2) => EDIT(S.S_IfThenElse(x, blk1, blk2))  
                   | (PRUNE blk1, CONT      ) => EDIT(S.S_IfThenElse(x, blk1, blk2))  
                   | (EXIT,       EDIT blk2 ) => EDIT(S.S_IfThenElse(x, blk1, blk2))  
                   | (EDIT blk1,  EXIT      ) => EDIT(S.S_IfThenElse(x, blk1, blk2))  
                   | (EDIT blk1,  EDIT blk2 ) => EDIT(S.S_IfThenElse(x, blk1, blk2))  
                   | (EDIT blk1,  PRUNE blk2) => EDIT(S.S_IfThenElse(x, blk1, blk2))  
                   | (PRUNE blk1, EDIT blk2 ) => EDIT(S.S_IfThenElse(x, blk1, blk2))  
                   | (EXIT,       PRUNE blk2) => PRUNE(S.S_IfThenElse(x, blk1, blk2))  
                   | (PRUNE blk1, EXIT      ) => PRUNE(S.S_IfThenElse(x, blk1, blk2))  
                   | (PRUNE blk1, PRUNE blk2) => PRUNE(S.S_IfThenElse(x, blk1, blk2))  
                 (* end case *))  
           and pruneBlk (S.Block{props, code}) = (case pruneStms code  
                  of PRUNE stms => PRUNE(S.Block{props=props, code=stms})  
                   | EDIT stms => EDIT(S.Block{props=props, code=stms})  
                   | EXIT => EXIT (* different instances of ctl_flow_info *)  
                   | CONT => CONT  
                 (* end case *))  
           in  
             case pruneBlk blk  
              of PRUNE blk => blk  
               | EDIT blk => blk  
               | _=> blk  
             (* end case *)  
           end  
   
88    (* simplify a statement into a single statement (i.e., a block if it expands    (* simplify a statement into a single statement (i.e., a block if it expands
89     * into more than one new statement).     * into more than one new statement).
90     *)     *)
91      fun simplifyBlock errStrm stm = mkBlock (simplifyStmt (errStrm, stm, []))      fun simplifyBlock (errStrm, stm) = mkBlock (simplifyStmt (errStrm, stm, []))
92    
93    (* simplify the statement stm where stms is a reverse-order list of preceeding simplified    (* simplify the statement stm where stms is a reverse-order list of preceeding simplified
94     * statements.  This function returns a reverse-order list of simplified statements.     * statements.  This function returns a reverse-order list of simplified statements.
# Line 191  Line 115 
115                  end                  end
116              | AST.S_IfThenElse(e, s1, s2) => let              | AST.S_IfThenElse(e, s1, s2) => let
117                  val (stms, x) = simplifyExpToVar (errStrm, e, stms)                  val (stms, x) = simplifyExpToVar (errStrm, e, stms)
118                  val s1 = simplifyBlock errStrm s1                  val s1 = simplifyBlock (errStrm, s1)
119                  val s2 = simplifyBlock errStrm s2                  val s2 = simplifyBlock (errStrm, s2)
120                  in                  in
121                    S.S_IfThenElse(x, s1, s2) :: stms                    S.S_IfThenElse(x, s1, s2) :: stms
122                  end                  end
123              | AST.S_Foreach((x, e), body) => let              | AST.S_Foreach((x, e), body) => let
124                  val (stms, xs') = simplifyExpToVar (errStrm, e, stms)                  val (stms, xs') = simplifyExpToVar (errStrm, e, stms)
125                  val body' = simplifyBlock errStrm body                  val body' = simplifyBlock (errStrm, body)
126                  in                  in
127                    S.S_Foreach(cvtVar x, xs', body') :: stms                    S.S_Foreach(cvtVar x, xs', body') :: stms
128                  end                  end
# Line 423  Line 347 
347              f (exps, [], stms)              f (exps, [], stms)
348            end            end
349    
350      (* simplify a block and then prune unreachable and dead code *)
351        fun simplifyAndPruneBlock errStrm blk =
352              DeadCode.eliminate (simplifyBlock (errStrm, blk))
353    
354      fun simplifyStrand (errStrm, strand) = let      fun simplifyStrand (errStrm, strand) = let
355            val AST.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand            val AST.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
356            val params' = cvtVars params            val params' = cvtVars params
# Line 446  Line 374 
374                  params = params',                  params = params',
375                  state = xs,                  state = xs,
376                  stateInit = stm,                  stateInit = stm,
377                  initM = Option.map (simplifyBlock errStrm) initM,                  initM = Option.map (simplifyAndPruneBlock errStrm) initM,
378                  updateM = simplifyBlock errStrm updateM,                  updateM = simplifyAndPruneBlock errStrm updateM,
379                  stabilizeM = Option.map (simplifyBlock errStrm) stabilizeM                  stabilizeM = Option.map (simplifyAndPruneBlock errStrm) stabilizeM
380                }                }
381            end            end
382    
# Line 552  Line 480 
480              | simplifyGlobalDcl (AST.D_Func(f, params, body)) = let              | simplifyGlobalDcl (AST.D_Func(f, params, body)) = let
481                  val f' = cvtVar f                  val f' = cvtVar f
482                  val params' = cvtVars params                  val params' = cvtVars params
483                  val body' = pruneUnreachableCode (simplifyBlock errStrm body)                  val body' = simplifyAndPruneBlock errStrm body
484                  in                  in
485                    funcs := S.Func{f=f', params=params', body=body'} :: !funcs                    funcs := S.Func{f=f', params=params', body=body'} :: !funcs
486                  end                  end
# Line 575  Line 503 
503                  globInit = globInit,                  globInit = globInit,
504                  funcs = List.rev(!funcs),                  funcs = List.rev(!funcs),
505                  strand = simplifyStrand (errStrm, strand),                  strand = simplifyStrand (errStrm, strand),
506                  create = Create.map (simplifyBlock errStrm) create,                  create = Create.map (simplifyAndPruneBlock errStrm) create,
507                  init = Option.map (simplifyBlock errStrm) init,                  init = Option.map (simplifyAndPruneBlock errStrm) init,
508                  update = Option.map (simplifyBlock errStrm) update                  update = Option.map (simplifyAndPruneBlock errStrm) update
509                }                }
510            end            end
511    

Legend:
Removed from v.4083  
changed lines
  Added in v.4113

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0