X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Finternal%2Fcodegen%2Foptimization%2FFoldlBuildFusion.java;h=45865a3e88261e2814db14850bf49ae25b012eac;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=6535bb6efc5b2ec945b84f0afc9200cec4c86661;hpb=cb5fc8d606d8b322563e9345c441eecfa7f01753;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/optimization/FoldlBuildFusion.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/optimization/FoldlBuildFusion.java index 6535bb6ef..45865a3e8 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/optimization/FoldlBuildFusion.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/optimization/FoldlBuildFusion.java @@ -1,99 +1,99 @@ -package org.simantics.scl.compiler.internal.codegen.optimization; - -import org.simantics.scl.compiler.common.names.Name; -import org.simantics.scl.compiler.common.names.Names; -import org.simantics.scl.compiler.constants.SCLConstant; -import org.simantics.scl.compiler.internal.codegen.analysis.StatementBrowser; -import org.simantics.scl.compiler.internal.codegen.references.BoundVar; -import org.simantics.scl.compiler.internal.codegen.references.Val; -import org.simantics.scl.compiler.internal.codegen.references.ValRef; -import org.simantics.scl.compiler.internal.codegen.ssa.SSABlock; -import org.simantics.scl.compiler.internal.codegen.ssa.SSAStatement; -import org.simantics.scl.compiler.internal.codegen.ssa.statements.LetApply; -import org.simantics.scl.compiler.internal.codegen.utils.SSASimplificationContext; - -public enum FoldlBuildFusion implements Optimization { - INSTANCE; - - private static class Analysis extends StatementBrowser { - - @Override - protected void unanalyzedBrowse() { - //System.out.println("- unanalyzedBrowse"); - stopBrowse(); - } - - @Override - protected void handleLoop(SSABlock block, - SSAStatement recursiveStatement) { - //System.out.println("- loop"); - stopBrowse(); - } - - @Override - protected void visitStatement(SSAStatement statement) { - if(statement instanceof LetApply) { - LetApply apply = (LetApply)statement; - if(apply.hasEffect()) { - //System.out.println("- effectful statement between"); - stopBrowse(); - } - } - } - - } - - @Override - public void inline(SSASimplificationContext context, LetApply foldlApplication) { - ValRef[] foldlParameters = foldlApplication.getParameters(); - if(foldlParameters.length != 3) // TODO case parameters.length > 3 - return; - Val list = foldlParameters[2].getBinding(); - if(!(list instanceof BoundVar)) - return; - if(list.hasMoreThanOneOccurences()) - return; - BoundVar listVar = (BoundVar)list; - if(!(listVar.getParent() instanceof LetApply)) - return; - LetApply buildApplication = (LetApply)listVar.getParent(); - - { // Check that buildApplication.getFunction() is Prelude.build - if(buildApplication.getParameters().length != 1) - return; - Val buildFunction = buildApplication.getFunction().getBinding(); - if(!(buildFunction instanceof SCLConstant)) - return; - if(((SCLConstant)buildFunction).getName() != Names.Prelude_build) - return; - } - - if(buildApplication.hasEffect() && foldlApplication.hasEffect()) - return; - - Analysis analysis = new Analysis(); - analysis.browseBetween(buildApplication, foldlApplication); - if(analysis.isStopped()) - return; - - /*PrintingContext cx = new PrintingContext(); - cx.append("listDef: "); - listDef.toString(cx); - cx.append("apply: "); - apply.toString(cx);*/ - - foldlApplication.setParameters(new ValRef[] {foldlParameters[1], foldlParameters[0]}); - foldlParameters[2].remove(); - ValRef foldlFunctionRef = foldlApplication.getFunction(); - foldlApplication.setFunction(buildApplication.getParameters()[0].createOccurrence( - foldlFunctionRef.getTypeParameters()[0], foldlFunctionRef.getTypeParameters()[2])); - foldlFunctionRef.remove(); - buildApplication.remove(); - - /*cx.append("transformed apply: "); - apply.toString(cx); - System.out.println(cx.toString());*/ - - context.markModified("foldl/build"); - } -} +package org.simantics.scl.compiler.internal.codegen.optimization; + +import org.simantics.scl.compiler.common.names.Name; +import org.simantics.scl.compiler.common.names.Names; +import org.simantics.scl.compiler.constants.SCLConstant; +import org.simantics.scl.compiler.internal.codegen.analysis.StatementBrowser; +import org.simantics.scl.compiler.internal.codegen.references.BoundVar; +import org.simantics.scl.compiler.internal.codegen.references.Val; +import org.simantics.scl.compiler.internal.codegen.references.ValRef; +import org.simantics.scl.compiler.internal.codegen.ssa.SSABlock; +import org.simantics.scl.compiler.internal.codegen.ssa.SSAStatement; +import org.simantics.scl.compiler.internal.codegen.ssa.statements.LetApply; +import org.simantics.scl.compiler.internal.codegen.utils.SSASimplificationContext; + +public enum FoldlBuildFusion implements Optimization { + INSTANCE; + + private static class Analysis extends StatementBrowser { + + @Override + protected void unanalyzedBrowse() { + //System.out.println("- unanalyzedBrowse"); + stopBrowse(); + } + + @Override + protected void handleLoop(SSABlock block, + SSAStatement recursiveStatement) { + //System.out.println("- loop"); + stopBrowse(); + } + + @Override + protected void visitStatement(SSAStatement statement) { + if(statement instanceof LetApply) { + LetApply apply = (LetApply)statement; + if(apply.hasEffect()) { + //System.out.println("- effectful statement between"); + stopBrowse(); + } + } + } + + } + + @Override + public void inline(SSASimplificationContext context, LetApply foldlApplication) { + ValRef[] foldlParameters = foldlApplication.getParameters(); + if(foldlParameters.length != 3) // TODO case parameters.length > 3 + return; + Val list = foldlParameters[2].getBinding(); + if(!(list instanceof BoundVar)) + return; + if(list.hasMoreThanOneOccurences()) + return; + BoundVar listVar = (BoundVar)list; + if(!(listVar.getParent() instanceof LetApply)) + return; + LetApply buildApplication = (LetApply)listVar.getParent(); + + { // Check that buildApplication.getFunction() is Prelude.build + if(buildApplication.getParameters().length != 1) + return; + Val buildFunction = buildApplication.getFunction().getBinding(); + if(!(buildFunction instanceof SCLConstant)) + return; + if(((SCLConstant)buildFunction).getName() != Names.Prelude_build) + return; + } + + if(buildApplication.hasEffect() && foldlApplication.hasEffect()) + return; + + Analysis analysis = new Analysis(); + analysis.browseBetween(buildApplication, foldlApplication); + if(analysis.isStopped()) + return; + + /*PrintingContext cx = new PrintingContext(); + cx.append("listDef: "); + listDef.toString(cx); + cx.append("apply: "); + apply.toString(cx);*/ + + foldlApplication.setParameters(new ValRef[] {foldlParameters[1], foldlParameters[0]}); + foldlParameters[2].remove(); + ValRef foldlFunctionRef = foldlApplication.getFunction(); + foldlApplication.setFunction(buildApplication.getParameters()[0].createOccurrence( + foldlFunctionRef.getTypeParameters()[0], foldlFunctionRef.getTypeParameters()[2])); + foldlFunctionRef.remove(); + buildApplication.remove(); + + /*cx.append("transformed apply: "); + apply.toString(cx); + System.out.println(cx.toString());*/ + + context.markModified("foldl/build"); + } +}