SwitchToLoopReductionOpportunities extends
    ReductionOpportunitiesBase<SwitchToLoopReductionOpportunity> { static List<SwitchToLoopReductionOpportunity> findOpportunities( ShaderJob shaderJob, ReducerContext context) { return shaderJob.getShaders() .stream() .map(item -> findOpportunitiesForShader(item, context)) .reduce(Collections.emptyList(), ListConcat::concatenate); } private  SwitchToLoopReductionOpportunities(TranslationUnit tu, ReducerContext context); @Override void visitSwitchStmt(SwitchStmt switchStmt);  }
@Test public void testDoNotReduceIfPreservingSemantics() throws Exception { final String original = "void main() {\n" + " if (" + Constants.GLF_DEAD + "(false)) {\n" + " switch (0) {\n" + " case 0:\n" + " return;\n" + " }\n" + " }\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(original); final List<VariableDeclToExprReductionOpportunity> ops = VariableDeclToExprReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_320, new RandomWrapper(0), new IdGenerator())); assertTrue(ops.isEmpty()); }
@Test public void testDoReduceWhenPreservingSemanticsIfInDeadCode() throws Exception { final String original = "void main() {\n" + " if (" + Constants.GLF_DEAD + "(false)) {\n" + " switch (0) {\n" + " case 0:\n" + " return;\n" + " }\n" + " }\n" + "}\n"; final String expected = "void main() {\n" + " if (" + Constants.GLF_DEAD + "(false)) {\n" + " do {\n" + " 0;" + " return;\n" + " } while (false);\n" + " }\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(original); final List<SwitchToLoopReductionOpportunity> ops = SwitchToLoopReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_320, new RandomWrapper(0), new IdGenerator())); assertEquals(1, ops.size()); ops.get(0).applyReduction(); CompareAsts.assertEqualAsts(expected, tu); }
@Test public void testNoBreaks() throws Exception { final String original = "void main() {\n" + " int a;\n" + " int x = 3;\n" + " switch (x) {\n" + " case 0:\n" + " {\n" + " x = 1;\n" + " }\n" + " case 1:\n" + " a = 4;\n" + " x = 2;\n" + " case 3:\n" + " default:\n" + " x = 1;\n" + " }\n" + "}\n"; final String expected = "void main() {\n" + " int a;\n" + " int x = 3;\n" + " do {\n" + " x;\n" + " {\n" + " x = 1;\n" + " }\n" + " a = 4;\n" + " x = 2;\n" + " x = 1;\n" + " } while (false);\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(original); final List<SwitchToLoopReductionOpportunity> ops = SwitchToLoopReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(true, ShadingLanguageVersion.ESSL_320, new RandomWrapper(0), new IdGenerator())); assertEquals(1, ops.size()); ops.get(0).applyReduction(); CompareAsts.assertEqualAsts(expected, tu); }
@Test public void testWithBreaks() throws Exception { final String original = "void main() {\n" + " int a;\n" + " int x = 3;\n" + " switch (x) {\n" + " case 0:\n" + " {\n" + " x = 1;\n" + " }\n" + " break;\n" + " case 1:\n" + " a = 4;\n" + " x = 2;\n" + " break;\n" + " case 3:\n" + " default:\n" + " x = 1;\n" + " break;\n" + " }\n" + "}\n"; final String expected = "void main() {\n" + " int a;\n" + " int x = 3;\n" + " do {\n" + " x;\n" + " {\n" + " x = 1;\n" + " }\n" + " break;\n" + " a = 4;\n" + " x = 2;\n" + " break;\n" + " x = 1;\n" + " break;\n" + " } while (false);\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(original); final List<SwitchToLoopReductionOpportunity> ops = SwitchToLoopReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(true, ShadingLanguageVersion.ESSL_320, new RandomWrapper(0), new IdGenerator())); assertEquals(1, ops.size()); ops.get(0).applyReduction(); CompareAsts.assertEqualAsts(expected, tu); }
@Test public void testTwoSwitches() throws Exception { final String original = "void main() {\n" + " int x;\n" + " switch (0) {\n" + " case 0:\n" + " x = 1;\n" + " }\n" + " switch (1) {\n" + " case 1:\n" + " x = 2;\n" + " }\n" + "}\n"; final String expected = "void main() {\n" + " int x;\n" + " do {\n" + " 0;\n" + " x = 1;\n" + " } while (false);\n" + " do {\n" + " 1;\n" + " x = 2;\n" + " } while (false);\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(original); final List<SwitchToLoopReductionOpportunity> ops = SwitchToLoopReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(true, ShadingLanguageVersion.ESSL_320, new RandomWrapper(0), new IdGenerator())); assertEquals(2, ops.size()); ops.get(0).applyReduction(); ops.get(1).applyReduction(); CompareAsts.assertEqualAsts(expected, tu); }