Home My Page Projects Code Snippets Project Openings 3D graphics for Standard ML
Summary Activity SCM

SCM Repository

[sml3d] Diff of /trunk/sml3d/src/particles/compiler/optimizer.sml
ViewVC logotype

Diff of /trunk/sml3d/src/particles/compiler/optimizer.sml

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

revision 863, Tue Apr 27 23:09:18 2010 UTC revision 864, Wed Apr 28 17:21:18 2010 UTC
# Line 534  Line 534 
534         fun hasUseless(uves, []) = false         fun hasUseless(uves, []) = false
535           | hasUseless(uves, v :: vl) = IRSet.member(uves, v) orelse hasUseless(uves, vl)           | hasUseless(uves, v :: vl) = IRSet.member(uves, v) orelse hasUseless(uves, vl)
536    
537         fun checkStmt(IR.PRIM(v, p, vl, s)) = let         fun checkStmt(IR.PRIM(v as IR.V{name, id=vid, ...}, p, vl, s)) = let
538             val uvs = checkStmt(s)             val uvs = checkStmt(s)
539            in            in
540             if hasUseless(uvs, vl) then             if hasUseless(uvs, vl) then
# Line 548  Line 548 
548             val uv1s = checkStmt(s1)             val uv1s = checkStmt(s1)
549             val uv2s = checkStmt(s2)             val uv2s = checkStmt(s2)
550            in            in
551             IRSet.union(uv1s, uv2s)             IRSet.intersection(uv1s, uv2s)
552            end            end
553    
554           | checkStmt(IR.GOTO(IR.BLK{params=args, ...}, vlist)) = let           | checkStmt(IR.GOTO(IR.BLK{params=args, ...}, vlist)) = let
555              fun update(uvs, (v as IR.V{id=id1, ...}) :: vl, (p as IR.V{id=id2, ...}) :: ps) =              fun update(uvs, (v as IR.V{id=id1, ...}) :: vl, (p as IR.V{id=id2, ...}) :: ps) =
556                  if IRSet.member(uvs, v) andalso (not (IRSet.member(uvs, p))) then                  if IRSet.member(uvs, v) andalso (not (IRSet.member(uvs, p))) then
557                   (gProgramChanged := true;                   (gProgramChanged := true;
558                   printErr (String.concat ["Found useless var: ", Int.toString id1, " mapping to useful param: ", Int.toString id2]);                   (* printErr (String.concat ["Found useless var: ", Int.toString id1, " mapping to useful param: ", Int.toString id2]);*)
559                   update(IRSet.delete(uvs, v), vl, ps))                   update(IRSet.delete(uvs, v), vl, ps))
560                  else if IRSet.member(uvs, p) andalso (not (IRSet.member(uvs, v))) then                  else if IRSet.member(uvs, p) andalso (not (IRSet.member(uvs, v))) then
561                   (gProgramChanged := true;                   (gProgramChanged := true;
562                   printErr (String.concat ["Found useless param:", Int.toString id2, " mapping from useful var: ", Int.toString id1]);                   (* printErr (String.concat ["Found useless param:", Int.toString id2, " mapping from useful var: ", Int.toString id1]); *)
563                   update(IRSet.delete(uvs, p), vl, ps))                   update(IRSet.delete(uvs, p), vl, ps))
564                  else                  else
565                   update(uvs, vl, ps)                   update(uvs, vl, ps)
# Line 569  Line 569 
569              update(uselessVars, vlist, args)              update(uselessVars, vlist, args)
570             end             end
571    
          | checkStmt(IR.RETURN(vl)) = let  
             val (sames, diffs) = findDifferentVars vl (IRSet.listItems uselessVars)  
            in  
             sames  
            end  
   
572           | checkStmt(_) = uselessVars           | checkStmt(_) = uselessVars
573    
574         val result = checkStmt(body)         val result = checkStmt(body)
575        in        in
576         printErr (String.concat ["Block ", Int.toString id, ", Size of list: ", Int.toString (List.length (IRSet.listItems result))]);         (* printErr (String.concat ["Block ", Int.toString id, ", Size of list: ", Int.toString (List.length (IRSet.listItems result))]); *)
577         result         result
578        end (* propagateUselessInBlock *)        end (* propagateUselessInBlock *)
579    
580       val result = (gProgramChanged := false; List.foldl propagateUselessInBlock initUVs blocks)       val result = (gProgramChanged := false; List.foldr propagateUselessInBlock initUVs blocks)
      val resultSanity = if List.length (IRSet.listItems result) = 0 then raise Fail("WTF") else ()  
581    
582      in      in
583       if (!gProgramChanged) then       if (!gProgramChanged) then
# Line 594  Line 587 
587      end      end
588    
589     fun prVarIds([]) = ()     fun prVarIds([]) = ()
590       | prVarIds(v as IR.V{id, ...} :: vl) = (printErr (Int.toString id); prVarIds(vl))       | prVarIds(IR.V{id, ...} :: vl) = (printErr (Int.toString id); prVarIds(vl))
591    
592     (* Returns true if the variable is useless in the given block. It     (* Returns true if the variable is useless in the given block. It
593      * checks all of the subsequent blocks to make sure that the variable      * checks all of the subsequent blocks to make sure that the variable
# Line 631  Line 624 
624        uselessInNextBlock(body)        uselessInNextBlock(body)
625       else       else
626        false        false
627      end      end (* isVarUselessInBlock *)
   
   (* TODO: We have the ability to test whether or not a given variable,  
    * necessarily a parameter to a block, is useless from that block  
    * forward. Now, what we need to do is generate a map between that  
    * variable and the rest of the blocks to see where the blocks use  
    * it as a variable and where they pass it as an argument (this may  
    * not be uniform across all blocks, so we need to be safe). Finally,  
    * using this map we can make one final pass along all of the blocks  
    * and prune it from our program.  
    *)  
628    
629     in     in
630      printErr "Initial useless variables:";      printErr ("Initial useless variables (" ^ (Int.toString (List.length (IRSet.listItems (findUseless(blocks))))) ^"):");
631      prVarIds(IRSet.listItems (findUseless(blocks)));      prVarIds(IRSet.listItems (findUseless(blocks)));
632      printErr "\nFinal useless variables:";      printErr "\nFinal useless variables:";
633      prVarIds(IRSet.listItems (propagateUseless(blocks, findUseless(blocks))))      prVarIds(IRSet.listItems (propagateUseless(blocks, findUseless(blocks))))
# Line 654  Line 637 
637     let     let
638      val foldedBlks = foldConstants(blocks)      val foldedBlks = foldConstants(blocks)
639      val optBlks = removeUnused(foldedBlks)      val optBlks = removeUnused(foldedBlks)
640      val optBlks2 = removeUseless(optBlks)      (* val optBlks2 = removeUseless(optBlks) *)
641     in     in
642      (*List.app prRefCtForBlk optBlks; *)      (*List.app prRefCtForBlk optBlks; *)
643      optBlks      optBlks

Legend:
Removed from v.863  
changed lines
  Added in v.864

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