This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/perf/optree.t: remove obsolete comment
[perl5.git] / t / perf / optree.t
CommitLineData
987c9691
FC
1#!./perl
2
009e0f19
DM
3# Use B to test that optimisations are not inadvertently removed,
4# by examining particular nodes in the optree.
987c9691 5
7bb1f299
DM
6use warnings;
7use strict;
8
987c9691
FC
9BEGIN {
10 chdir 't';
11 require './test.pl';
12 skip_all_if_miniperl("No B under miniperl");
13 @INC = '../lib';
14}
15
b243b193 16plan 695;
987c9691 17
412989c2 18use v5.10; # state
a5f48505
DM
19use B qw(svref_2object
20 OPpASSIGN_COMMON_SCALAR
21 OPpASSIGN_COMMON_RC1
22 OPpASSIGN_COMMON_AGG
7adc03cc
DM
23 OPpTRUEBOOL
24 OPpMAYBE_TRUEBOOL
a5f48505 25 );
2251d43b
FC
26
27
a5f48505
DM
28# Test that OP_AASSIGN gets the appropriate
29# OPpASSIGN_COMMON* flags set.
30#
31# Too few flags set is likely to cause code to misbehave;
32# too many flags set unnecessarily slows things down.
33# See also the tests in t/op/aassign.t
34
35for my $test (
36 # Each anon array contains:
37 # [
38 # expected flags:
39 # a 3 char string, each char showing whether we expect a
40 # particular flag to be set:
41 # '-' indicates any char not set, while
42 # 'S': char 0: OPpASSIGN_COMMON_SCALAR,
43 # 'R': char 1: OPpASSIGN_COMMON_RC1,
44 # 'A' char 2: OPpASSIGN_COMMON_AGG,
45 # code to eval,
46 # description,
47 # ]
48
49 [ "---", '() = (1, $x, my $y, @z, f($p))', 'no LHS' ],
50 [ "---", '(undef, $x, my $y, @z, ($a ? $b : $c)) = ()', 'no RHS' ],
51 [ "---", '(undef, $x, my $y, @z, ($a ? $b : $c)) = (1,2)', 'safe RHS' ],
52 [ "---", 'my @a = (1,2)', 'safe RHS: my array' ],
53 [ "---", 'my %h = (1,2)', 'safe RHS: my hash' ],
54 [ "---", 'my ($a,$b,$c,$d) = 1..6; ($a,$b) = ($c,$d);', 'non-common lex' ],
55 [ "---", '($x,$y) = (1,2)', 'pkg var LHS only' ],
56 [ "---", 'my $p; my ($x,$y) = ($p, $p)', 'my; dup lex var on RHS' ],
57 [ "---", 'my $p; my ($x,$y); ($x,$y) = ($p, $p)', 'dup lex var on RHS' ],
90ce4d05
DM
58 [ "---", 'my ($self) = @_', 'LHS lex scalar only' ],
59 [ "--A", 'my ($self, @rest) = @_', 'LHS lex mixed' ],
a5f48505
DM
60 [ "-R-", 'my ($x,$y) = ($p, $q)', 'pkg var RHS only' ],
61 [ "S--", '($x,$y) = ($p, $q)', 'pkg scalar both sides' ],
62 [ "--A", 'my (@a, @b); @a = @b', 'lex ary both sides' ],
63 [ "-R-", 'my ($x,$y,$z,@a); ($x,$y,$z) = @a ', 'lex vars to lex ary' ],
64 [ "--A", '@a = @b', 'pkg ary both sides' ],
65 [ "--A", 'my (%a,%b); %a = %b', 'lex hash both sides' ],
66 [ "--A", '%a = %b', 'pkg hash both sides' ],
67 [ "--A", 'my $x; @a = ($a[0], $a[$x])', 'common ary' ],
68 [ "--A", 'my ($x,@a); @a = ($a[0], $a[$x])', 'common lex ary' ],
69 [ "S-A", 'my $x; ($a[$x], $a[0]) = ($a[0], $a[$x])', 'common ary elems' ],
70 [ "S-A", 'my ($x,@a); ($a[$x], $a[0]) = ($a[0], $a[$x])',
71 'common lex ary elems' ],
72 [ "--A", 'my $x; my @a = @$x', 'lex ary may have stuff' ],
73 [ "-RA", 'my $x; my ($b, @a) = @$x', 'lex ary may have stuff' ],
74 [ "--A", 'my $x; my %a = @$x', 'lex hash may have stuff' ],
75 [ "-RA", 'my $x; my ($b, %a) = @$x', 'lex hash may have stuff' ],
76 [ "--A", 'my (@a,@b); @a = ($b[0])', 'lex ary and elem' ],
77 [ "S-A", 'my @a; ($a[1],$a[0]) = @a', 'lex ary and elem' ],
78 [ "--A", 'my @x; @y = $x[0]', 'pkg ary from lex elem' ],
808ce557
DM
79 [ "---", '(undef,$x) = f()', 'single scalar on LHS' ],
80 [ "---", '($x,$y) = ($x)', 'single scalar on RHS, no AGG' ],
81 [ "--A", '($x,@b) = ($x)', 'single scalar on RHS' ],
47a8f19b
DM
82 [ "--A", 'my @a; @a = (@a = split())', 'split a/a' ],
83 [ "--A", 'my (@a,@b); @a = (@b = split())', 'split a/b' ],
84 [ "---", 'my @a; @a = (split(), 1)', '(split(),1)' ],
85 [ "---", '@a = (split(//, @a), 1)', 'split(@a)' ],
86 [ "--A", 'my @a; my $ar = @a; @a = (@$ar = split())', 'a/ar split' ],
a5f48505 87) {
7bb1f299 88
a5f48505 89 my ($exp, $code, $desc) = @$test;
7bb1f299
DM
90 my $sub;
91 {
92 # package vars used in code snippets
93 our (@a, %a, @b, %b, $c, $p, $q, $x, $y, @y, @z);
94
95 $sub = eval "sub { $code }"
96 or die
97 "aassign eval('$code') failed: this test needs"
98 . "to be rewritten:\n$@"
99 }
a5f48505
DM
100
101 my $last_expr = svref_2object($sub)->ROOT->first->last;
2251d43b
FC
102 if ($last_expr->name ne 'aassign') {
103 die "Expected aassign but found ", $last_expr->name,
104 "; this test needs to be rewritten"
105 }
a5f48505
DM
106 my $got =
107 (($last_expr->private & OPpASSIGN_COMMON_SCALAR) ? 'S' : '-')
108 . (($last_expr->private & OPpASSIGN_COMMON_RC1) ? 'R' : '-')
109 . (($last_expr->private & OPpASSIGN_COMMON_AGG) ? 'A' : '-');
110 is $got, $exp, "OPpASSIGN_COMMON: $desc: '$code'";
2251d43b
FC
111}
112
113
114# join -> stringify/const
987c9691
FC
115
116for (['CONSTANT', sub { join "foo", $_ }],
117 ['$var' , sub { join $_ , $_ }],
118 ['$myvar' , sub { my $var; join $var, $_ }],
119) {
120 my($sep,$sub) = @$_;
121 my $last_expr = svref_2object($sub)->ROOT->first->last;
122 is $last_expr->name, 'stringify',
123 "join($sep, \$scalar) optimised to stringify";
124}
125
126for (['CONSTANT', sub { join "foo", "bar" }, 0, "bar" ],
127 ['CONSTANT', sub { join "foo", "bar", 3 }, 1, "barfoo3"],
128 ['$var' , sub { join $_ , "bar" }, 0, "bar" ],
129 ['$myvar' , sub { my $var; join $var, "bar" }, 0, "bar" ],
130) {
131 my($sep,$sub,$is_list,$expect) = @$_;
132 my $last_expr = svref_2object($sub)->ROOT->first->last;
133 my $tn = "join($sep, " . ($is_list?'list of constants':'const') . ")";
134 is $last_expr->name, 'const', "$tn optimised to constant";
135 is $sub->(), $expect, "$tn folded correctly";
136}
45742705
FC
137
138
6aa68307
FC
139# list+pushmark in list context elided out of the execution chain
140is svref_2object(sub { () = ($_, ($_, $_)) })
141 ->START # nextstate
142 ->next # pushmark
143 ->next # gvsv
144 ->next # should be gvsv, not pushmark
145 ->name, 'gvsv',
146 "list+pushmark in list context where list's elder sibling is a null";
147
148
f5b5c2a3
FC
149# nextstate multiple times becoming one nextstate
150
151is svref_2object(sub { 0;0;0;0;0;0;time })->START->next->name, 'time',
152 'multiple nextstates become one';
153
154
412989c2
FC
155# pad[ahs]v state declarations in void context
156
157is svref_2object(sub{state($foo,@fit,%far);state $bar;state($a,$b); time})
158 ->START->next->name, 'time',
159 'pad[ahs]v state declarations in void context';
160
161
d964400d
FC
162# pushmark-padsv-padav-padhv in list context --> padrange
163
164{
165 my @ops;
166 my $sub = sub { \my( $f, @f, %f ) };
167 my $op = svref_2object($sub)->START;
168 push(@ops, $op->name), $op = $op->next while $$op;
169 is "@ops", "nextstate padrange refgen leavesub", 'multi-type padrange'
170}
171
172
0298c760
FC
173# rv2[ahs]v in void context
174
175is svref_2object(sub { our($foo,@fit,%far); our $bar; our($a,$b); time })
176 ->START->next->name, 'time',
177 'rv2[ahs]v in void context';
178
179
45742705
FC
180# split to array
181
182for(['@pkgary' , '@_' ],
183 ['@lexary' , 'my @a; @a'],
184 ['my(@array)' , 'my(@a)' ],
185 ['local(@array)', 'local(@_)'],
186 ['@{...}' , '@{\@_}' ],
187){
188 my($tn,$code) = @$_;
189 my $sub = eval "sub { $code = split }";
190 my $split = svref_2object($sub)->ROOT->first->last;
191 is $split->name, 'split', "$tn = split swallows up the assignment";
192}
73f4c4fe
FC
193
194
195# stringify with join kid --> join
196is svref_2object(sub { "@_" })->ROOT->first->last->name, 'join',
197 'qq"@_" optimised from stringify(join(...)) to join(...)';
7adc03cc
DM
198
199
200# Check that certain ops, when in boolean context, have the
201# right private "is boolean" or "maybe boolean" flags set.
202#
203# A maybe flag is set when the context at the end of a chain of and/or/dor
204# ops isn't known till runtime, e.g.
205# sub f { ....; ((%h || $x) || $y)) }
206# If f() is called in void context, then %h can return a boolean value;
207# if in scalar context, %h must return a key count.
7adc03cc
DM
208
209for my $ops (
210 # op code op path flag maybe flag
211 [ 'rv2hv', '%pkg', [], OPpTRUEBOOL, OPpMAYBE_TRUEBOOL ],
212 [ 'rv2hv', 'scalar(%pkg)', [0], OPpTRUEBOOL, OPpMAYBE_TRUEBOOL ],
213 [ 'padhv', '%lex', [], OPpTRUEBOOL, OPpMAYBE_TRUEBOOL ],
214 [ 'padhv', 'scalar(%lex)', [0], OPpTRUEBOOL, OPpMAYBE_TRUEBOOL ],
215) {
216 my ($op_name, $op_code, $post_op_path, $bool_flag, $maybe_flag) = @$ops;
217
218 for my $test (
219 # 1st column: what to expect for each $context (void, scalar, unknown),
220 # 0: expect no flag
221 # 1: expect bool flag
222 # 2: expect maybe bool flag
223 # 9: skip test
b0e8c18f
DM
224 # 2nd column: path though the op subtree to the flagged op:
225 # 0 is first child, 1 is second child etc.
7adc03cc 226 # Will have @$post_op_path from above appended.
b0e8c18f 227 # 3rd column: code to execute: %s holds the code for the op
7adc03cc 228 #
b0e8c18f 229 # [V S U] PATH CODE
7adc03cc
DM
230
231 # INNER PLAIN
232
233 [ [0,0,0], [], '%s' ],
b243b193 234 [ [1,9,2], [0,0], 'if (%s) {$x}' ],
7adc03cc
DM
235 [ [1,9,1], [0,0], 'if (%s) {$x} else {$y}' ],
236 [ [1,9,2], [0,0], 'unless (%s) {$x}' ],
237
238 # INNER NOT
239
240 [ [1,1,1], [0], '!%s' ],
b0e8c18f 241 [ [1,9,1], [0,0,0], 'if (!%s) {$x}' ],
7adc03cc 242 [ [1,9,1], [0,0,0], 'if (!%s) {$x} else {$y}' ],
b0e8c18f 243 [ [1,9,1], [0,0,0], 'unless (!%s) {$x}' ],
7adc03cc
DM
244
245 # INNER COND
246
247 [ [1,1,1], [0,0,], '%s ? $p : $q' ],
248 [ [1,9,1], [0,0,0,0], 'if (%s ? $p : $q) {$x}' ],
249 [ [1,9,1], [0,0,0,0], 'if (%s ? $p : $q) {$x} else {$y}' ],
250 [ [1,9,1], [0,0,0,0], 'unless (%s ? $p : $q) {$x}' ],
251
252
253 # INNER OR LHS
254
255 [ [1,0,2], [0,0], '%s || $x' ],
b0e8c18f 256 [ [1,1,1], [0,0,0], '!(%s || $x)' ],
7adc03cc 257 [ [1,0,2], [0,1,0,0], '$y && (%s || $x)' ],
b243b193 258 [ [1,9,2], [0,0,0,0], 'if (%s || $x) {$x}' ],
b0e8c18f
DM
259 [ [1,9,1], [0,0,0,0], 'if (%s || $x) {$x} else {$y}' ],
260 [ [1,9,2], [0,0,0,0], 'unless (%s || $x) {$x}' ],
7adc03cc
DM
261
262 # INNER OR RHS
263
b0e8c18f
DM
264 [ [0,0,0], [0,1], '$x || %s' ],
265 [ [1,1,1], [0,0,1], '!($x || %s)' ],
266 [ [0,0,0], [0,1,0,1], '$y && ($x || %s)' ],
b243b193 267 [ [1,9,2], [0,0,0,1], 'if ($x || %s) {$x}' ],
b0e8c18f
DM
268 [ [1,9,1], [0,0,0,1], 'if ($x || %s) {$x} else {$y}' ],
269 [ [1,9,2], [0,0,0,1], 'unless ($x || %s) {$x}' ],
7adc03cc
DM
270
271 # INNER DOR LHS
272
273 [ [1,0,2], [0,0], '%s // $x' ],
b0e8c18f 274 [ [1,1,1], [0,0,0], '!(%s // $x)' ],
7adc03cc 275 [ [1,0,2], [0,1,0,0], '$y && (%s // $x)' ],
b243b193 276 [ [1,9,2], [0,0,0,0], 'if (%s // $x) {$x}' ],
b0e8c18f 277 [ [1,9,1], [0,0,0,0], 'if (%s // $x) {$x} else {$y}' ],
7adc03cc 278 [ [1,9,2], [0,0,0,0], 'unless (%s // $x) {$x}' ],
7adc03cc
DM
279
280 # INNER DOR RHS
281
b0e8c18f
DM
282 [ [0,0,0], [0,1], '$x // %s' ],
283 [ [1,1,1], [0,0,1], '!($x // %s)' ],
284 [ [0,0,0], [0,1,0,1], '$y && ($x // %s)' ],
b243b193 285 [ [1,9,2], [0,0,0,1], 'if ($x // %s) {$x}' ],
b0e8c18f
DM
286 [ [1,9,1], [0,0,0,1], 'if ($x // %s) {$x} else {$y}' ],
287 [ [1,9,2], [0,0,0,1], 'unless ($x // %s) {$x}' ],
7adc03cc
DM
288
289 # INNER AND LHS
290
b243b193 291 [ [1,0,2], [0,0], '%s && $x' ],
7adc03cc 292 [ [1,1,1], [0,0,0], '!(%s && $x)' ],
b243b193
DM
293 [ [1,0,2], [0,1,0,0], '$y || (%s && $x)' ],
294 [ [1,9,2], [0,0,0,0], 'if (%s && $x) {$x}' ],
7adc03cc 295 [ [1,9,1], [0,0,0,0], 'if (%s && $x) {$x} else {$y}' ],
b243b193 296 [ [1,9,2], [0,0,0,0], 'unless (%s && $x) {$x}' ],
7adc03cc
DM
297
298 # INNER AND RHS
299
b0e8c18f
DM
300 [ [0,0,0], [0,1], '$x && %s' ],
301 [ [1,1,1], [0,0,1], '!($x && %s)' ],
302 [ [0,0,0], [0,1,0,1], '$y || ($x && %s)' ],
b243b193 303 [ [1,9,2], [0,0,0,1], 'if ($x && %s) {$x}' ],
b0e8c18f
DM
304 [ [1,9,1], [0,0,0,1], 'if ($x && %s) {$x} else {$y}' ],
305 [ [1,9,2], [0,0,0,1], 'unless ($x && %s) {$x}' ],
7adc03cc 306
9d692a7f
DM
307 # INNER XOR LHS
308
309 # LHS of XOR is currently too hard to detect as
310 # being in boolean context
311
312 # INNER XOR RHS
313
314 [ [1,1,1], [1], '($x xor %s)' ],
315 [ [1,1,1], [0,1], '!($x xor %s)' ],
316 [ [1,1,1], [0,1,1], '$y || ($x xor %s)' ],
317 [ [1,9,1], [0,0,1], 'if ($x xor %s) {$x}' ],
318 [ [1,9,1], [0,0,1], 'if ($x xor %s) {$x} else {$y}' ],
319 [ [1,9,1], [0,0,1], 'unless ($x xor %s) {$x}' ],
320
321 # GREP
322
323 [ [1,1,1], [0,1,0], 'grep %s,1,2' ],
324 [ [1,1,1], [0,1,0,0], 'grep !%s,1,2' ],
325 [ [1,1,1], [0,1,0,0,1],'grep $y || %s,1,2' ],
326
327 # FLIP
328
329 [ [1,1,1], [0,0,0,0], '%s..$x' ],
330 [ [1,1,1], [0,0,0,0,0], '!%s..$x' ],
331 [ [1,1,1], [0,0,0,0,0,1], '($y || %s)..$x' ],
332
333 # FLOP
334
335 [ [1,1,1], [0,0,0,1], '$x..%s' ],
336 [ [1,1,1], [0,0,0,1,0], '$x..!%s' ],
337 [ [1,1,1], [0,0,0,1,0,1], '$x..($y || %s)' ],
338
7adc03cc
DM
339 ) {
340 my ($expects, $op_path, $code_fmt) = @$test;
341
342 for my $context (0,1,2) {
343 # 0: void
344 # 1: scalar
345 # 2: unknown
346 # 9: skip test (principally if() can't be in scalar context)
347
348 next if $expects->[$context] == 9;
349
350 my $base_code = sprintf $code_fmt, $op_code;
351 my $code = $base_code;
352 my @op_path = @$op_path;
353 push @op_path, @$post_op_path;
354
355 # where to find the expression in the top-level lineseq
356 my $seq_offset = -1;
357
358 if ($context == 0) {
359 $seq_offset -= 2;
360 $code .= "; 1";
361 }
362 elsif ($context == 1) {
363 $code = "\$r = ($code)";
364 unshift @op_path, 0;
365 }
366
367
368 my $sub;
369 {
370 our (%pkg);
371 my (%lex, $p, $q, $r, $x, $y);
372
373 no warnings 'void';
374 $sub = eval "sub { $code }"
375 or die
376 "eval'$code' failed: this test needs to be rewritten;\n"
377 . "Errors were:\n$@";
378 }
379
380 # find the expression subtree in the main lineseq of the sub
381 my $expr = svref_2object($sub)->ROOT->first;
382 my @ops;
383 my $next = $expr->first;
384 while ($$next) {
385 push @ops, $next;
386 $next = $next->sibling;
387 }
388 $expr = $ops[$seq_offset];
389
390 # search through the expr subtree looking for the named op -
391 # this assumes that for all the code examples above, the
392 # op is always in the LH branch
393 while (defined (my $p = shift @op_path)) {
394 $expr = $expr->first;
395 $expr = $expr->sibling while $p--;
396 }
397
398 if (!$expr || $expr->name ne $op_name) {
399 die "Can't find $op_name op in optree for '$code'; "
400 . "this test needs to be rewritten"
401 }
402
403 my $exp = $expects->[$context];
404 $exp = $exp == 0 ? 0
405 : $exp == 1 ? $bool_flag
406 : $maybe_flag;
407
408 my $got = ($expr->private & ($bool_flag | $maybe_flag));
409 my $cxt_name = ('void ', 'scalar ', 'unknown')[$context];
410 is $got, $exp, "boolean: $op_name $cxt_name '$base_code'";
411 }
412 }
413}
414