Optimize += 1, -= 1 to ++, --.

Purely cosmetic, but easier to read.
This commit is contained in:
Hans-Kristian Arntzen 2016-12-16 13:24:49 +01:00
parent 62613df5a5
commit d11b8aa3ef
8 changed files with 34 additions and 24 deletions

View File

@ -64,7 +64,7 @@ void test()
} }
} }
float h; 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; _11.data = h;

View File

@ -22,7 +22,7 @@ void main()
do do
{ {
idat = _28.mvp * idat; idat = _28.mvp * idat;
i += 1; i++;
} while (i < 16); } while (i < 16);
_52.out_data[ident] = idat; _52.out_data[ident] = idat;
} }

View File

@ -23,7 +23,7 @@ void main()
do do
{ {
k *= 2; k *= 2;
i += uint(1); i++;
} while (i < ident); } while (i < ident);
} }
switch (k) switch (k)
@ -32,7 +32,7 @@ void main()
{ {
for (;;) for (;;)
{ {
i += uint(1); i++;
if (i > 10u) if (i > 10u)
{ {
break; break;
@ -58,11 +58,11 @@ void main()
while (k < 10) while (k < 10)
{ {
idat *= 2.0; 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; idat = _24.mvp * idat;
} }
@ -70,7 +70,7 @@ void main()
k = 0; k = 0;
for (;;) for (;;)
{ {
k += 1; k++;
if (k > 10) if (k > 10)
{ {
k += 2; k += 2;
@ -86,18 +86,18 @@ void main()
k = 0; k = 0;
do do
{ {
k += 1; k++;
} while (k > 10); } while (k > 10);
int l = 0; int l = 0;
for (;;) for (;;)
{ {
if (l == 5) if (l == 5)
{ {
l += 1; l++;
continue; continue;
} }
idat += vec4(1.0); idat += vec4(1.0);
l += 1; l++;
continue; continue;
} }
_177.out_data[ident] = idat; _177.out_data[ident] = idat;

View File

@ -21,7 +21,7 @@ void main()
return; return;
} }
} }
for (int i = 0; i < 20; i += 1) for (int i = 0; i < 20; i++)
{ {
if (i == 10) if (i == 10)
{ {

View File

@ -25,7 +25,7 @@ void main()
if (_40 < 10) if (_40 < 10)
{ {
idat *= 2.0; idat *= 2.0;
k += 1; k++;
continue; continue;
} }
else else
@ -33,16 +33,16 @@ void main()
break; 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; idat = _24.mvp * idat;
} }
} }
do do
{ {
k += 1; k++;
} while (k > 10); } while (k > 10);
_89.out_data[ident] = idat; _89.out_data[ident] = idat;
} }

View File

@ -7,16 +7,16 @@ layout(location = 0) out mediump int FragColor;
void main() void main()
{ {
FragColor = 15; FragColor = 15;
for (mediump int i = 0; i < 25; i += 1) for (mediump int i = 0; i < 25; i++)
{ {
FragColor += 10; 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; FragColor += 11;
} }
mediump int k = 0; mediump int k = 0;
for (; k < 20; k += 1) for (; k < 20; k++)
{ {
FragColor += 12; FragColor += 12;
} }
@ -26,7 +26,7 @@ void main()
if (k == 40) if (k == 40)
{ {
l = 0; l = 0;
for (; l < 40; l += 1) for (; l < 40; l++)
{ {
FragColor += 13; FragColor += 13;
} }
@ -38,12 +38,12 @@ void main()
FragColor += l; FragColor += l;
} }
mediump ivec2 i_2 = ivec2(0); 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; FragColor += i_2.y;
} }
mediump int o = k; mediump int o = k;
for (mediump int m = k; m < 40; m += 1) for (mediump int m = k; m < 40; m++)
{ {
FragColor += m; FragColor += m;
} }

View File

@ -37,7 +37,7 @@ void main()
} }
// Vectors cannot be loop variables // 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; FragColor += i.y;
} }

View File

@ -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 // 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. // adding lots of special cases to the code emission.
if (rhs.size() < lhs.size() + 3)
return false;
auto index = rhs.find(lhs); auto index = rhs.find(lhs);
if (index != 0) if (index != 0)
return false; return false;
@ -3439,7 +3442,14 @@ bool CompilerGLSL::optimize_read_modify_write(const string &lhs, const string &r
if (op != lhs.size() + 1) if (op != lhs.size() + 1)
return false; 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; return true;
} }