LoopMergeReductionOpportunities extends ScopeTrackingVisitor { static List<LoopMergeReductionOpportunity> findOpportunities( ShaderJob shaderJob, ReducerContext context) { return shaderJob.getShaders() .stream() .map(item -> findOpportunitiesForShader(item)) .reduce(Arrays.asList(), ListConcat::concatenate); } private  LoopMergeReductionOpportunities(); @Override void visitBlockStmt(BlockStmt block);  }
@Test public void findOpportunities() throws Exception { final int id = 72; final String loopVariable = Constants.SPLIT_LOOP_COUNTER_PREFIX + id + "c"; final String loopBody = " x = x + " + loopVariable + ";\n" + " x = " + loopVariable + " + 2;\n"; final String firstLoop = " for(int " + loopVariable + " = 3; " + loopVariable + " < 10; " + "++" + loopVariable + ") {\n" + loopBody + " }"; final String secondLoop = " for(int " + loopVariable + " = 10; " + loopVariable + " < 100; " + "++" + loopVariable + ") {\n" + loopBody + " }\n"; final String program = "void main() {\n" + " int x = 4;\n" + firstLoop + secondLoop + "}\n"; final TranslationUnit tu = ParseHelper.parse(program); List<LoopMergeReductionOpportunity> opportunities = LoopMergeReductionOpportunities.findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(1, opportunities.size()); opportunities.get(0).applyReduction(); final String expectedProgram = "void main()\n" + "{\n" + " int x = 4;\n" + " for(\n" + " int c = 3;\n" + " c < 100;\n" + " ++ c\n" + " )\n" + " {\n" + " x = x + c;\n" + " x = c + 2;\n" + " }\n" + "}\n"; assertEquals(PrettyPrinterVisitor.prettyPrintAsString(ParseHelper.parse(expectedProgram)), PrettyPrinterVisitor.prettyPrintAsString(tu)); }