From d11b8aa3ef68c8c247e3154c957d94626395a60c Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Fri, 16 Dec 2016 13:24:49 +0100 Subject: [PATCH] Optimize += 1, -= 1 to ++, --. Purely cosmetic, but easier to read. --- reference/shaders/comp/cfg.comp | 2 +- reference/shaders/comp/dowhile.comp | 2 +- reference/shaders/comp/loop.comp | 18 +++++++++--------- reference/shaders/comp/return.comp | 2 +- reference/shaders/comp/torture-loop.comp | 8 ++++---- reference/shaders/frag/for-loop-init.frag | 12 ++++++------ shaders/frag/for-loop-init.frag | 2 +- spirv_glsl.cpp | 12 +++++++++++- 8 files changed, 34 insertions(+), 24 deletions(-) diff --git a/reference/shaders/comp/cfg.comp b/reference/shaders/comp/cfg.comp index ed4a45c8..707968e7 100644 --- a/reference/shaders/comp/cfg.comp +++ b/reference/shaders/comp/cfg.comp @@ -64,7 +64,7 @@ void test() } } float h; - for (int i = 0; i < 20; i += 1, h += 10.0) + for (int i = 0; i < 20; i++, h += 10.0) { } _11.data = h; diff --git a/reference/shaders/comp/dowhile.comp b/reference/shaders/comp/dowhile.comp index dabf08f5..16ba4001 100644 --- a/reference/shaders/comp/dowhile.comp +++ b/reference/shaders/comp/dowhile.comp @@ -22,7 +22,7 @@ void main() do { idat = _28.mvp * idat; - i += 1; + i++; } while (i < 16); _52.out_data[ident] = idat; } diff --git a/reference/shaders/comp/loop.comp b/reference/shaders/comp/loop.comp index fbc7d4a0..9853acaa 100644 --- a/reference/shaders/comp/loop.comp +++ b/reference/shaders/comp/loop.comp @@ -23,7 +23,7 @@ void main() do { k *= 2; - i += uint(1); + i++; } while (i < ident); } switch (k) @@ -32,7 +32,7 @@ void main() { for (;;) { - i += uint(1); + i++; if (i > 10u) { break; @@ -58,11 +58,11 @@ void main() while (k < 10) { idat *= 2.0; - k += 1; + k++; } - for (uint i_1 = 0u; i_1 < 16u; i_1 += uint(1), k += 1) + for (uint i_1 = 0u; i_1 < 16u; i_1++, k++) { - for (uint j = 0u; j < 30u; j += uint(1)) + for (uint j = 0u; j < 30u; j++) { idat = _24.mvp * idat; } @@ -70,7 +70,7 @@ void main() k = 0; for (;;) { - k += 1; + k++; if (k > 10) { k += 2; @@ -86,18 +86,18 @@ void main() k = 0; do { - k += 1; + k++; } while (k > 10); int l = 0; for (;;) { if (l == 5) { - l += 1; + l++; continue; } idat += vec4(1.0); - l += 1; + l++; continue; } _177.out_data[ident] = idat; diff --git a/reference/shaders/comp/return.comp b/reference/shaders/comp/return.comp index b3e51cc0..20d61d25 100644 --- a/reference/shaders/comp/return.comp +++ b/reference/shaders/comp/return.comp @@ -21,7 +21,7 @@ void main() return; } } - for (int i = 0; i < 20; i += 1) + for (int i = 0; i < 20; i++) { if (i == 10) { diff --git a/reference/shaders/comp/torture-loop.comp b/reference/shaders/comp/torture-loop.comp index 083a3b5a..ae183190 100644 --- a/reference/shaders/comp/torture-loop.comp +++ b/reference/shaders/comp/torture-loop.comp @@ -25,7 +25,7 @@ void main() if (_40 < 10) { idat *= 2.0; - k += 1; + k++; continue; } else @@ -33,16 +33,16 @@ void main() break; } } - for (uint i = 0u; i < 16u; i += uint(1), k += 1) + for (uint i = 0u; i < 16u; i++, k++) { - for (uint j = 0u; j < 30u; j += uint(1)) + for (uint j = 0u; j < 30u; j++) { idat = _24.mvp * idat; } } do { - k += 1; + k++; } while (k > 10); _89.out_data[ident] = idat; } diff --git a/reference/shaders/frag/for-loop-init.frag b/reference/shaders/frag/for-loop-init.frag index 80fb3ac1..95483d9f 100644 --- a/reference/shaders/frag/for-loop-init.frag +++ b/reference/shaders/frag/for-loop-init.frag @@ -7,16 +7,16 @@ layout(location = 0) out mediump int FragColor; void main() { FragColor = 15; - for (mediump int i = 0; i < 25; i += 1) + for (mediump int i = 0; i < 25; i++) { FragColor += 10; } - for (mediump int j = 4, i_1 = 1; i_1 < 30; i_1 += 1, j += 4) + for (mediump int j = 4, i_1 = 1; i_1 < 30; i_1++, j += 4) { FragColor += 11; } mediump int k = 0; - for (; k < 20; k += 1) + for (; k < 20; k++) { FragColor += 12; } @@ -26,7 +26,7 @@ void main() if (k == 40) { l = 0; - for (; l < 40; l += 1) + for (; l < 40; l++) { FragColor += 13; } @@ -38,12 +38,12 @@ void main() FragColor += l; } mediump ivec2 i_2 = ivec2(0); - for (; i_2.x < 10; i_2.x += 1) + for (; i_2.x < 10; i_2.x += 4) { FragColor += i_2.y; } mediump int o = k; - for (mediump int m = k; m < 40; m += 1) + for (mediump int m = k; m < 40; m++) { FragColor += m; } diff --git a/shaders/frag/for-loop-init.frag b/shaders/frag/for-loop-init.frag index 78b8903e..af2af7e2 100644 --- a/shaders/frag/for-loop-init.frag +++ b/shaders/frag/for-loop-init.frag @@ -37,7 +37,7 @@ void main() } // Vectors cannot be loop variables - for (ivec2 i = ivec2(0); i.x < 10; i.x++) + for (ivec2 i = ivec2(0); i.x < 10; i.x += 4) { FragColor += i.y; } diff --git a/spirv_glsl.cpp b/spirv_glsl.cpp index 78fbc3ab..360c4152 100644 --- a/spirv_glsl.cpp +++ b/spirv_glsl.cpp @@ -3430,6 +3430,9 @@ bool CompilerGLSL::optimize_read_modify_write(const string &lhs, const string &r { // Do this with strings because we have a very clear pattern we can check for and it avoids // adding lots of special cases to the code emission. + if (rhs.size() < lhs.size() + 3) + return false; + auto index = rhs.find(lhs); if (index != 0) return false; @@ -3439,7 +3442,14 @@ bool CompilerGLSL::optimize_read_modify_write(const string &lhs, const string &r if (op != lhs.size() + 1) return false; - statement(lhs, " ", rhs[op], "=", rhs.substr(lhs.size() + 2), ";"); + char bop = rhs[op]; + auto expr = rhs.substr(lhs.size() + 3); + // Try to find increments and decrements. Makes it look neater as += 1, -= 1 is fairly rare to see in real code. + // Find some common patterns which are equivalent. + if ((bop == '+' || bop == '-') && (expr == "1" || expr == "uint(1)" || expr == "1u" || expr == "int(1u)")) + statement(lhs, bop, bop, ";"); + else + statement(lhs, " ", bop, "= ", expr, ";"); return true; }