diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-12 11:38:42 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-12 11:38:42 -0300 |
commit | f6aab3ec1f111cd8d968bdcb7ca800e93b819d24 (patch) | |
tree | 4c36c418ecc9062e6d95de73457198b38b0afce9 /testes | |
parent | be8445d7e4b6122620c428877b51a27d464253d5 (diff) | |
download | lua-f6aab3ec1f111cd8d968bdcb7ca800e93b819d24.tar.gz lua-f6aab3ec1f111cd8d968bdcb7ca800e93b819d24.tar.bz2 lua-f6aab3ec1f111cd8d968bdcb7ca800e93b819d24.zip |
First implementation of constant propagation
Local constant variables initialized with compile-time constants
are optimized away from the code.
Diffstat (limited to 'testes')
-rw-r--r-- | testes/code.lua | 89 | ||||
-rw-r--r-- | testes/constructs.lua | 24 | ||||
-rw-r--r-- | testes/locals.lua | 2 | ||||
-rw-r--r-- | testes/math.lua | 16 |
4 files changed, 89 insertions, 42 deletions
diff --git a/testes/code.lua b/testes/code.lua index 128ca2cb..49d682f8 100644 --- a/testes/code.lua +++ b/testes/code.lua | |||
@@ -7,6 +7,22 @@ if T==nil then | |||
7 | end | 7 | end |
8 | print "testing code generation and optimizations" | 8 | print "testing code generation and optimizations" |
9 | 9 | ||
10 | -- to test constant propagation | ||
11 | local <const> k0 = 0 | ||
12 | local <const> k1 = 1 | ||
13 | local <const> k3 = 3 | ||
14 | local <const> k6 = k3 + (k3 << k0) | ||
15 | local <const> kFF0 = 0xFF0 | ||
16 | local <const> k3_78 = 3.78 | ||
17 | local <const> x, <const> k3_78_4 = 10, k3_78 / 4 | ||
18 | assert(x == 10) | ||
19 | |||
20 | local <const> kx = "x" | ||
21 | |||
22 | local <const> kTrue = true | ||
23 | local <const> kFalse = false | ||
24 | |||
25 | local <const> kNil = nil | ||
10 | 26 | ||
11 | -- this code gave an error for the code checker | 27 | -- this code gave an error for the code checker |
12 | do | 28 | do |
@@ -27,12 +43,12 @@ end | |||
27 | 43 | ||
28 | local function foo () | 44 | local function foo () |
29 | local a | 45 | local a |
30 | a = 3; | 46 | a = k3; |
31 | a = 0; a = 0.0; a = -7 + 7 | 47 | a = 0; a = 0.0; a = -7 + 7 |
32 | a = 3.78/4; a = 3.78/4 | 48 | a = k3_78/4; a = k3_78_4 |
33 | a = -3.78/4; a = 3.78/4; a = -3.78/4 | 49 | a = -k3_78/4; a = k3_78/4; a = -3.78/4 |
34 | a = -3.79/4; a = 0.0; a = -0; | 50 | a = -3.79/4; a = 0.0; a = -0; |
35 | a = 3; a = 3.0; a = 3; a = 3.0 | 51 | a = k3; a = 3.0; a = 3; a = 3.0 |
36 | end | 52 | end |
37 | 53 | ||
38 | checkKlist(foo, {3.78/4, -3.78/4, -3.79/4}) | 54 | checkKlist(foo, {3.78/4, -3.78/4, -3.79/4}) |
@@ -86,10 +102,11 @@ end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN') | |||
86 | 102 | ||
87 | -- sequence of LOADNILs | 103 | -- sequence of LOADNILs |
88 | check(function () | 104 | check(function () |
105 | local <const> kNil = nil | ||
89 | local a,b,c | 106 | local a,b,c |
90 | local d; local e; | 107 | local d; local e; |
91 | local f,g,h; | 108 | local f,g,h; |
92 | d = nil; d=nil; b=nil; a=nil; c=nil; | 109 | d = nil; d=nil; b=nil; a=kNil; c=nil; |
93 | end, 'LOADNIL', 'RETURN0') | 110 | end, 'LOADNIL', 'RETURN0') |
94 | 111 | ||
95 | check(function () | 112 | check(function () |
@@ -109,7 +126,7 @@ check (function (a,b,c) return a end, 'RETURN1') | |||
109 | 126 | ||
110 | 127 | ||
111 | -- infinite loops | 128 | -- infinite loops |
112 | check(function () while true do local a = -1 end end, | 129 | check(function () while kTrue do local a = -1 end end, |
113 | 'LOADI', 'JMP', 'RETURN0') | 130 | 'LOADI', 'JMP', 'RETURN0') |
114 | 131 | ||
115 | check(function () while 1 do local a = -1 end end, | 132 | check(function () while 1 do local a = -1 end end, |
@@ -125,9 +142,9 @@ check(function (a,b,c,d) return a..b..c..d end, | |||
125 | 142 | ||
126 | -- not | 143 | -- not |
127 | check(function () return not not nil end, 'LOADBOOL', 'RETURN1') | 144 | check(function () return not not nil end, 'LOADBOOL', 'RETURN1') |
128 | check(function () return not not false end, 'LOADBOOL', 'RETURN1') | 145 | check(function () return not not kFalse end, 'LOADBOOL', 'RETURN1') |
129 | check(function () return not not true end, 'LOADBOOL', 'RETURN1') | 146 | check(function () return not not true end, 'LOADBOOL', 'RETURN1') |
130 | check(function () return not not 1 end, 'LOADBOOL', 'RETURN1') | 147 | check(function () return not not k3 end, 'LOADBOOL', 'RETURN1') |
131 | 148 | ||
132 | -- direct access to locals | 149 | -- direct access to locals |
133 | check(function () | 150 | check(function () |
@@ -144,7 +161,8 @@ end, | |||
144 | -- direct access to constants | 161 | -- direct access to constants |
145 | check(function () | 162 | check(function () |
146 | local a,b | 163 | local a,b |
147 | a.x = 3.2 | 164 | local c = kNil |
165 | a[kx] = 3.2 | ||
148 | a.x = b | 166 | a.x = b |
149 | a[b] = 'x' | 167 | a[b] = 'x' |
150 | end, | 168 | end, |
@@ -152,8 +170,9 @@ end, | |||
152 | 170 | ||
153 | -- "get/set table" with numeric indices | 171 | -- "get/set table" with numeric indices |
154 | check(function (a) | 172 | check(function (a) |
173 | local <const> k255 = 255 | ||
155 | a[1] = a[100] | 174 | a[1] = a[100] |
156 | a[255] = a[256] | 175 | a[k255] = a[256] |
157 | a[256] = 5 | 176 | a[256] = 5 |
158 | end, | 177 | end, |
159 | 'GETI', 'SETI', | 178 | 'GETI', 'SETI', |
@@ -170,7 +189,7 @@ end, | |||
170 | 189 | ||
171 | check(function () | 190 | check(function () |
172 | local a,b | 191 | local a,b |
173 | a[true] = false | 192 | a[kTrue] = false |
174 | end, | 193 | end, |
175 | 'LOADNIL', 'LOADBOOL', 'SETTABLE', 'RETURN0') | 194 | 'LOADNIL', 'LOADBOOL', 'SETTABLE', 'RETURN0') |
176 | 195 | ||
@@ -238,37 +257,39 @@ local function checkF (func, val) | |||
238 | end | 257 | end |
239 | 258 | ||
240 | checkF(function () return 0.0 end, 0.0) | 259 | checkF(function () return 0.0 end, 0.0) |
241 | checkI(function () return 0 end, 0) | 260 | checkI(function () return k0 end, 0) |
242 | checkI(function () return -0//1 end, 0) | 261 | checkI(function () return -k0//1 end, 0) |
243 | checkK(function () return 3^-1 end, 1/3) | 262 | checkK(function () return 3^-1 end, 1/3) |
244 | checkK(function () return (1 + 1)^(50 + 50) end, 2^100) | 263 | checkK(function () return (1 + 1)^(50 + 50) end, 2^100) |
245 | checkK(function () return (-2)^(31 - 2) end, -0x20000000 + 0.0) | 264 | checkK(function () return (-2)^(31 - 2) end, -0x20000000 + 0.0) |
246 | checkF(function () return (-3^0 + 5) // 3.0 end, 1.0) | 265 | checkF(function () return (-k3^0 + 5) // 3.0 end, 1.0) |
247 | checkI(function () return -3 % 5 end, 2) | 266 | checkI(function () return -k3 % 5 end, 2) |
248 | checkF(function () return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3 end, -5.0) | 267 | checkF(function () return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3 end, -5.0) |
249 | checkF(function () return -((2^8 + -(-1)) % 8)//2 * 4 - 3 end, -7.0) | 268 | checkF(function () return -((2^8 + -(-1)) % 8)//2 * 4 - 3 end, -7.0) |
250 | checkI(function () return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD end, 0xF4) | 269 | checkI(function () return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD end, 0xF4) |
251 | checkI(function () return ~(~0xFF0 | 0xFF0) end, 0) | 270 | checkI(function () return ~(~kFF0 | kFF0) end, 0) |
252 | checkI(function () return ~~-1024.0 end, -1024) | 271 | checkI(function () return ~~-1024.0 end, -1024) |
253 | checkI(function () return ((100 << 6) << -4) >> 2 end, 100) | 272 | checkI(function () return ((100 << k6) << -4) >> 2 end, 100) |
254 | 273 | ||
255 | -- borders around MAXARG_sBx ((((1 << 17) - 1) >> 1) == 65535) | 274 | -- borders around MAXARG_sBx ((((1 << 17) - 1) >> 1) == 65535) |
256 | local a = 17; local sbx = ((1 << a) - 1) >> 1 -- avoid folding | 275 | local a = 17; local sbx = ((1 << a) - 1) >> 1 -- avoid folding |
257 | checkI(function () return 65535 end, sbx) | 276 | local <const> border = 65535 |
258 | checkI(function () return -65535 end, -sbx) | 277 | checkI(function () return border end, sbx) |
259 | checkI(function () return 65536 end, sbx + 1) | 278 | checkI(function () return -border end, -sbx) |
260 | checkK(function () return 65537 end, sbx + 2) | 279 | checkI(function () return border + 1 end, sbx + 1) |
261 | checkK(function () return -65536 end, -(sbx + 1)) | 280 | checkK(function () return border + 2 end, sbx + 2) |
281 | checkK(function () return -(border + 1) end, -(sbx + 1)) | ||
262 | 282 | ||
263 | checkF(function () return 65535.0 end, sbx + 0.0) | 283 | local <const> border = 65535.0 |
264 | checkF(function () return -65535.0 end, -sbx + 0.0) | 284 | checkF(function () return border end, sbx + 0.0) |
265 | checkF(function () return 65536.0 end, (sbx + 1.0)) | 285 | checkF(function () return -border end, -sbx + 0.0) |
266 | checkK(function () return 65537.0 end, (sbx + 2.0)) | 286 | checkF(function () return border + 1 end, (sbx + 1.0)) |
267 | checkK(function () return -65536.0 end, -(sbx + 1.0)) | 287 | checkK(function () return border + 2 end, (sbx + 2.0)) |
288 | checkK(function () return -(border + 1) end, -(sbx + 1.0)) | ||
268 | 289 | ||
269 | 290 | ||
270 | -- immediate operands | 291 | -- immediate operands |
271 | checkR(function (x) return x + 1 end, 10, 11, 'ADDI', 'RETURN1') | 292 | checkR(function (x) return x + k1 end, 10, 11, 'ADDI', 'RETURN1') |
272 | checkR(function (x) return 128 + x end, 0.0, 128.0, 'ADDI', 'RETURN1') | 293 | checkR(function (x) return 128 + x end, 0.0, 128.0, 'ADDI', 'RETURN1') |
273 | checkR(function (x) return x * -127 end, -1.0, 127.0, 'MULI', 'RETURN1') | 294 | checkR(function (x) return x * -127 end, -1.0, 127.0, 'MULI', 'RETURN1') |
274 | checkR(function (x) return 20 * x end, 2, 40, 'MULI', 'RETURN1') | 295 | checkR(function (x) return 20 * x end, 2, 40, 'MULI', 'RETURN1') |
@@ -276,7 +297,7 @@ checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWI', 'RETURN1') | |||
276 | checkR(function (x) return x / 40 end, 40, 1.0, 'DIVI', 'RETURN1') | 297 | checkR(function (x) return x / 40 end, 40, 1.0, 'DIVI', 'RETURN1') |
277 | checkR(function (x) return x // 1 end, 10.0, 10.0, 'IDIVI', 'RETURN1') | 298 | checkR(function (x) return x // 1 end, 10.0, 10.0, 'IDIVI', 'RETURN1') |
278 | checkR(function (x) return x % (100 - 10) end, 91, 1, 'MODI', 'RETURN1') | 299 | checkR(function (x) return x % (100 - 10) end, 91, 1, 'MODI', 'RETURN1') |
279 | checkR(function (x) return 1 << x end, 3, 8, 'SHLI', 'RETURN1') | 300 | checkR(function (x) return k1 << x end, 3, 8, 'SHLI', 'RETURN1') |
280 | checkR(function (x) return x << 2 end, 10, 40, 'SHRI', 'RETURN1') | 301 | checkR(function (x) return x << 2 end, 10, 40, 'SHRI', 'RETURN1') |
281 | checkR(function (x) return x >> 2 end, 8, 2, 'SHRI', 'RETURN1') | 302 | checkR(function (x) return x >> 2 end, 8, 2, 'SHRI', 'RETURN1') |
282 | checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'RETURN1') | 303 | checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'RETURN1') |
@@ -295,7 +316,7 @@ checkR(function (x) return x % (100.0 - 10) end, 91, 1.0, 'MODK', 'RETURN1') | |||
295 | 316 | ||
296 | -- no foldings (and immediate operands) | 317 | -- no foldings (and immediate operands) |
297 | check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1') | 318 | check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1') |
298 | check(function () return 3/0 end, 'LOADI', 'DIVI', 'RETURN1') | 319 | check(function () return k3/0 end, 'LOADI', 'DIVI', 'RETURN1') |
299 | check(function () return 0%0 end, 'LOADI', 'MODI', 'RETURN1') | 320 | check(function () return 0%0 end, 'LOADI', 'MODI', 'RETURN1') |
300 | check(function () return -4//0 end, 'LOADI', 'IDIVI', 'RETURN1') | 321 | check(function () return -4//0 end, 'LOADI', 'IDIVI', 'RETURN1') |
301 | check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'RETURN1') | 322 | check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'RETURN1') |
@@ -335,7 +356,7 @@ end, | |||
335 | 356 | ||
336 | do -- tests for table access in upvalues | 357 | do -- tests for table access in upvalues |
337 | local t | 358 | local t |
338 | check(function () t.x = t.y end, 'GETTABUP', 'SETTABUP') | 359 | check(function () t[kx] = t.y end, 'GETTABUP', 'SETTABUP') |
339 | check(function (a) t[a()] = t[a()] end, | 360 | check(function (a) t[a()] = t[a()] end, |
340 | 'MOVE', 'CALL', 'GETUPVAL', 'MOVE', 'CALL', | 361 | 'MOVE', 'CALL', 'GETUPVAL', 'MOVE', 'CALL', |
341 | 'GETUPVAL', 'GETTABLE', 'SETTABLE') | 362 | 'GETUPVAL', 'GETTABLE', 'SETTABLE') |
@@ -379,6 +400,12 @@ function (a) | |||
379 | end | 400 | end |
380 | ) | 401 | ) |
381 | 402 | ||
403 | checkequal(function () return 6 or true or nil end, | ||
404 | function () return k6 or kTrue or kNil end) | ||
405 | |||
406 | checkequal(function () return 6 and true or nil end, | ||
407 | function () return k6 and kTrue or kNil end) | ||
408 | |||
382 | 409 | ||
383 | print 'OK' | 410 | print 'OK' |
384 | 411 | ||
diff --git a/testes/constructs.lua b/testes/constructs.lua index fe4db2cb..8a549e10 100644 --- a/testes/constructs.lua +++ b/testes/constructs.lua | |||
@@ -287,7 +287,7 @@ a,b = F(nil)==nil; assert(a == true and b == nil) | |||
287 | ------------------------------------------------------------------ | 287 | ------------------------------------------------------------------ |
288 | 288 | ||
289 | -- sometimes will be 0, sometimes will not... | 289 | -- sometimes will be 0, sometimes will not... |
290 | _ENV.GLOB1 = math.floor(os.time()) % 2 | 290 | _ENV.GLOB1 = math.random(0, 1) |
291 | 291 | ||
292 | -- basic expressions with their respective values | 292 | -- basic expressions with their respective values |
293 | local basiccases = { | 293 | local basiccases = { |
@@ -298,6 +298,26 @@ local basiccases = { | |||
298 | {"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1}, | 298 | {"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1}, |
299 | } | 299 | } |
300 | 300 | ||
301 | local prog | ||
302 | |||
303 | if _ENV.GLOB1 == 0 then | ||
304 | basiccases[2][1] = "F" -- constant false | ||
305 | |||
306 | prog = [[ | ||
307 | local <const> F = false | ||
308 | if %s then IX = true end | ||
309 | return %s | ||
310 | ]] | ||
311 | else | ||
312 | basiccases[4][1] = "k10" -- constant 10 | ||
313 | |||
314 | prog = [[ | ||
315 | local <const> k10 = 10 | ||
316 | if %s then IX = true end | ||
317 | return %s | ||
318 | ]] | ||
319 | end | ||
320 | |||
301 | print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')') | 321 | print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')') |
302 | 322 | ||
303 | 323 | ||
@@ -337,8 +357,6 @@ cases[1] = basiccases | |||
337 | for i = 2, level do cases[i] = createcases(i) end | 357 | for i = 2, level do cases[i] = createcases(i) end |
338 | print("+") | 358 | print("+") |
339 | 359 | ||
340 | local prog = [[if %s then IX = true end; return %s]] | ||
341 | |||
342 | local i = 0 | 360 | local i = 0 |
343 | for n = 1, level do | 361 | for n = 1, level do |
344 | for _, v in pairs(cases[n]) do | 362 | for _, v in pairs(cases[n]) do |
diff --git a/testes/locals.lua b/testes/locals.lua index 0de00a98..1b82dd7f 100644 --- a/testes/locals.lua +++ b/testes/locals.lua | |||
@@ -324,7 +324,7 @@ do | |||
324 | 324 | ||
325 | -- errors due to non-closable values | 325 | -- errors due to non-closable values |
326 | local function foo () | 326 | local function foo () |
327 | local <toclose> x = 34 | 327 | local <toclose> x = {} |
328 | end | 328 | end |
329 | local stat, msg = pcall(foo) | 329 | local stat, msg = pcall(foo) |
330 | assert(not stat and string.find(msg, "variable 'x'")) | 330 | assert(not stat and string.find(msg, "variable 'x'")) |
diff --git a/testes/math.lua b/testes/math.lua index c45a91ad..befce12e 100644 --- a/testes/math.lua +++ b/testes/math.lua | |||
@@ -270,7 +270,7 @@ else | |||
270 | end | 270 | end |
271 | 271 | ||
272 | do | 272 | do |
273 | local NaN = 0/0 | 273 | local <const> NaN = 0/0 |
274 | assert(not (NaN < 0)) | 274 | assert(not (NaN < 0)) |
275 | assert(not (NaN > minint)) | 275 | assert(not (NaN > minint)) |
276 | assert(not (NaN <= -9)) | 276 | assert(not (NaN <= -9)) |
@@ -767,7 +767,8 @@ assert(a == '10' and b == '20') | |||
767 | 767 | ||
768 | do | 768 | do |
769 | print("testing -0 and NaN") | 769 | print("testing -0 and NaN") |
770 | local mz, z = -0.0, 0.0 | 770 | local <const> mz = -0.0 |
771 | local <const> z = 0.0 | ||
771 | assert(mz == z) | 772 | assert(mz == z) |
772 | assert(1/mz < 0 and 0 < 1/z) | 773 | assert(1/mz < 0 and 0 < 1/z) |
773 | local a = {[mz] = 1} | 774 | local a = {[mz] = 1} |
@@ -775,17 +776,18 @@ do | |||
775 | a[z] = 2 | 776 | a[z] = 2 |
776 | assert(a[z] == 2 and a[mz] == 2) | 777 | assert(a[z] == 2 and a[mz] == 2) |
777 | local inf = math.huge * 2 + 1 | 778 | local inf = math.huge * 2 + 1 |
778 | mz, z = -1/inf, 1/inf | 779 | local <const> mz = -1/inf |
780 | local <const> z = 1/inf | ||
779 | assert(mz == z) | 781 | assert(mz == z) |
780 | assert(1/mz < 0 and 0 < 1/z) | 782 | assert(1/mz < 0 and 0 < 1/z) |
781 | local NaN = inf - inf | 783 | local <const> NaN = inf - inf |
782 | assert(NaN ~= NaN) | 784 | assert(NaN ~= NaN) |
783 | assert(not (NaN < NaN)) | 785 | assert(not (NaN < NaN)) |
784 | assert(not (NaN <= NaN)) | 786 | assert(not (NaN <= NaN)) |
785 | assert(not (NaN > NaN)) | 787 | assert(not (NaN > NaN)) |
786 | assert(not (NaN >= NaN)) | 788 | assert(not (NaN >= NaN)) |
787 | assert(not (0 < NaN) and not (NaN < 0)) | 789 | assert(not (0 < NaN) and not (NaN < 0)) |
788 | local NaN1 = 0/0 | 790 | local <const> NaN1 = 0/0 |
789 | assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN)) | 791 | assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN)) |
790 | local a = {} | 792 | local a = {} |
791 | assert(not pcall(rawset, a, NaN, 1)) | 793 | assert(not pcall(rawset, a, NaN, 1)) |
@@ -814,8 +816,8 @@ end | |||
814 | -- the first call after seed 1007 should return 0x7a7040a5a323c9d6 | 816 | -- the first call after seed 1007 should return 0x7a7040a5a323c9d6 |
815 | do | 817 | do |
816 | -- all computations assume at most 32-bit integers | 818 | -- all computations assume at most 32-bit integers |
817 | local h = 0x7a7040a5 -- higher half | 819 | local <const> h = 0x7a7040a5 -- higher half |
818 | local l = 0xa323c9d6 -- lower half | 820 | local <const> l = 0xa323c9d6 -- lower half |
819 | 821 | ||
820 | math.randomseed(1007) | 822 | math.randomseed(1007) |
821 | -- get the low 'intbits' of the 64-bit expected result | 823 | -- get the low 'intbits' of the 64-bit expected result |