/
optimize.c
9581 lines (8317 loc) · 323 KB
/
optimize.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Racket
Copyright (c) 2004-2016 PLT Design Inc.
Copyright (c) 1995-2001 Matthew Flatt
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA.
libscheme
Copyright (c) 1994 Brent Benson
All rights reserved.
*/
/* This file implements bytecode optimization.
See "eval.c" for an overview of compilation passes. */
#include "schpriv.h"
#include "schrunst.h"
#include "schmach.h"
/* Controls for inlining algorithm: */
#define OPT_ESTIMATE_FUTURE_SIZES 1
#define OPT_DISCOURAGE_EARLY_INLINE 1
#define OPT_LIMIT_FUNCTION_RESIZE 0
#define OPT_BRANCH_ADDS_NO_SIZE 1
#define OPT_DELAY_GROUP_PROPAGATE 0
#define OPT_PRE_OPTIMIZE_FOR_CROSS_MODULE(size_override) (size_override)
#define MAX_PROC_INLINE_SIZE 256
#define CROSS_MODULE_INLINE_SIZE 8
/* Various kinds of fuel ensure that
the compiler doesn't go into a loop
or take non-linear time */
#define INITIAL_INLINING_FUEL 32
#define INITIAL_FLATTENING_FUEL 16
struct Optimize_Info
{
MZTAG_IF_REQUIRED
short flags;
struct Optimize_Info *next;
int original_frame, new_frame;
Scheme_Object *consts;
Comp_Prefix *cp;
int init_kclock;
/* Compilation context, used for unresolving for cross-module inlining: */
Scheme_Env *env;
Scheme_Object *insp;
/* Propagated up and down the chain: */
int size;
int vclock; /* virtual clock that ticks for a side effect, a branch,
or a dependency on an earlier side-effect (such as a
previous guard on an unsafe operation's argument);
the clock is only compared between binding sites and
uses, so we can rewind the clock at a join after an
increment that models a branch (if the branch is not
taken or doesn't increment the clock) */
int aclock; /* virtual clock that ticks for allocation without side effects,
for constraining the reordering of operations that might
capture a continuation */
int kclock; /* virtual clock that ticks for a potential continuation capture,
for constraining the movement of allocation operations */
int sclock; /* virtual clock that ticks when space consumption is potentially observed */
int psize;
short inline_fuel, flatten_fuel;
char letrec_not_twice, enforce_const, use_psize, has_nonleaf;
Scheme_Hash_Table *top_level_consts;
int maybe_values_argument; /* triggers an approximation for clock increments */
/* Set by expression optimization: */
int single_result, preserves_marks; /* negative means "tentative", due to fixpoint in progress */
int escapes; /* flag to signal that the expression always escapes. When escapes is 1, it's assumed
that single_result and preserves_marks are also 1, and that it's not necessary to
use optimize_ignored before including the expression. */
int lambda_depth; /* counts nesting depth under `lambda`s */
int used_toplevel; /* tracks whether any non-local variables or syntax-object literals are used */
Scheme_Hash_Table *uses; /* used variables, accumulated for closures */
Scheme_IR_Local *transitive_use_var; /* set when optimizing a letrec-bound procedure
to record variables that were added to `uses` */
Scheme_Object *context; /* for logging */
Scheme_Logger *logger;
Scheme_Hash_Tree *types; /* maps position (from this frame) to predicate */
int no_types; /* disables use of type info */
};
typedef struct Optimize_Info_Sequence {
int init_flatten_fuel, min_flatten_fuel;
} Optimize_Info_Sequence;
static void merge_lambda_arg_types(Scheme_Lambda *lam1, Scheme_Lambda *lam2);
static void check_lambda_arg_types_registered(Scheme_Lambda *lam, int app_count);
static int lambda_body_size_plus_info(Scheme_Lambda *lam, int check_assign,
Optimize_Info *info, int *is_leaf);
static int lambda_has_top_level(Scheme_Lambda *lam);
static int wants_local_type_arguments(Scheme_Object *rator, int argpos);
static void add_types_for_f_branch(Scheme_Object *t, Optimize_Info *info, int fuel);
static void register_use(Scheme_IR_Local *var, Optimize_Info *info);
static Scheme_Object *optimize_info_lookup_lambda(Scheme_Object *var);
static Scheme_Object *optimize_info_propagate_local(Scheme_Object *var);
static void optimize_info_used_top(Optimize_Info *info);
static Scheme_Object *optimize_get_predicate(Optimize_Info *info, Scheme_Object *var, int ignore_no_types);
static void add_type(Optimize_Info *info, Scheme_Object *var, Scheme_Object *pred);
static void merge_types(Optimize_Info *src_info, Optimize_Info *info, Scheme_Hash_Tree *skip_vars);
static Scheme_Object *lookup_constant_proc(Optimize_Info *info, Scheme_Object *rand);
static Scheme_Object *expr_implies_predicate(Scheme_Object *expr, Optimize_Info *info);
static Scheme_Object *do_expr_implies_predicate(Scheme_Object *expr, Optimize_Info *info,
int *_involves_k_cross, int fuel,
Scheme_Hash_Tree *ignore_vars);
static int produces_local_type(Scheme_Object *rator, int argc);
static int optimize_any_uses(Optimize_Info *info, Scheme_IR_Let_Value *at_irlv, int n);
static void optimize_uses_of_mutable_imply_early_alloc(Scheme_IR_Let_Value *at_irlv, int n);
static void propagate_used_variables(Optimize_Info *info);
static int env_uses_toplevel(Optimize_Info *frame);
static Scheme_IR_Local *clone_variable(Scheme_IR_Local *var);
static void increment_use_count(Scheme_IR_Local *var, int as_rator);
static Optimize_Info *optimize_info_add_frame(Optimize_Info *info, int orig, int current, int flags);
static void optimize_info_done(Optimize_Info *info, Optimize_Info *parent);
static void register_transitive_uses(Scheme_IR_Local *var, Optimize_Info *info);
static void optimize_info_seq_init(Optimize_Info *info, Optimize_Info_Sequence *info_seq);
static void optimize_info_seq_step(Optimize_Info *info, Optimize_Info_Sequence *info_seq);
static void optimize_info_seq_done(Optimize_Info *info, Optimize_Info_Sequence *info_seq);
static Scheme_Object *estimate_closure_size(Scheme_Object *e);
static Scheme_Object *no_potential_size(Scheme_Object *value);
static Scheme_Object *optimize_lets(Scheme_Object *form, Optimize_Info *info, int for_inline, int context);
static Scheme_Object *optimize_clone(int single_use, Scheme_Object *obj, Optimize_Info *info, Scheme_Hash_Tree *var_map, int as_rator);
XFORM_NONGCING static int relevant_predicate(Scheme_Object *pred);
XFORM_NONGCING static int predicate_implies(Scheme_Object *pred1, Scheme_Object *pred2);
XFORM_NONGCING static int predicate_implies_not(Scheme_Object *pred1, Scheme_Object *pred2);
static int single_valued_noncm_expression(Scheme_Object *expr, int fuel);
static Scheme_Object *optimize_ignored(Scheme_Object *e, Optimize_Info *info,
int expected_vals, int maybe_omittable,
int fuel);
static Scheme_Object *equivalent_exprs(Scheme_Object *a, Scheme_Object *b,
Optimize_Info *a_info, Optimize_Info *b_info, int context);
static int movable_expression(Scheme_Object *expr, Optimize_Info *info,
int cross_lambda, int cross_k, int cross_s,
int check_space, int fuel);
#define SCHEME_LAMBDAP(vals_expr) (SAME_TYPE(SCHEME_TYPE(vals_expr), scheme_ir_lambda_type) \
|| SAME_TYPE(SCHEME_TYPE(vals_expr), scheme_case_lambda_sequence_type))
#define SCHEME_WILL_BE_LAMBDAP(v) SAME_TYPE(SCHEME_TYPE(v), scheme_will_be_lambda_type)
#define SCHEME_WILL_BE_LAMBDA_SIZE(v) SCHEME_PINT_VAL(v)
#define SCHEME_WILL_BE_LAMBDA(v) SCHEME_IPTR_VAL(v)
static int lambda_body_size(Scheme_Object *o, int less_args);
typedef struct Scheme_Once_Used {
Scheme_Object so;
Scheme_Object *expr;
Scheme_IR_Local *var;
int vclock; /* record clocks at binding site */
int aclock;
int kclock;
int sclock;
int spans_k; /* potentially captures a continuation */
int moved;
} Scheme_Once_Used;
static Scheme_Once_Used *make_once_used(Scheme_Object *val, Scheme_IR_Local *var,
int vclock, int aclock, int kclock, int sclock, int spans_k);
static ROSYM Scheme_Hash_Tree *empty_eq_hash_tree;
#ifdef MZ_PRECISE_GC
static void register_traversers(void);
#endif
void scheme_init_optimize()
{
REGISTER_SO(empty_eq_hash_tree);
empty_eq_hash_tree = scheme_make_hash_tree(SCHEME_hashtr_eq);
#ifdef MZ_PRECISE_GC
register_traversers();
#endif
}
/*========================================================================*/
/* logging */
/*========================================================================*/
static void note_match(int actual, int expected, Optimize_Info *warn_info)
{
if (!warn_info || (expected == -1))
return;
if (actual != expected) {
scheme_log(warn_info->logger,
SCHEME_LOG_WARNING,
0,
"warning%s: %d values produced when %d expected",
scheme_optimize_context_to_string(warn_info->context),
actual, expected);
}
}
char *scheme_optimize_context_to_string(Scheme_Object *context)
/* Convert a context to a string that is suitable for use in logging */
{
if (context) {
Scheme_Object *mod, *func;
const char *ctx, *prefix, *mctx, *mprefix;
char *all;
int clen, plen, mclen, mplen, len;
if (SCHEME_PAIRP(context)) {
func = SCHEME_CAR(context);
mod = SCHEME_CDR(context);
} else if (SAME_TYPE(SCHEME_TYPE(context), scheme_module_type)) {
func = scheme_false;
mod = context;
} else {
func = context;
mod = scheme_false;
}
if (SAME_TYPE(SCHEME_TYPE(func), scheme_ir_lambda_type)) {
Scheme_Object *name;
name = ((Scheme_Lambda *)func)->name;
if (name) {
if (SCHEME_VECTORP(name)) {
Scheme_Object *port;
int print_width = 1024;
intptr_t plen;
port = scheme_make_byte_string_output_port();
scheme_write_proc_context(port, print_width,
SCHEME_VEC_ELS(name)[0],
SCHEME_VEC_ELS(name)[1], SCHEME_VEC_ELS(name)[2],
SCHEME_VEC_ELS(name)[3], SCHEME_VEC_ELS(name)[4],
SCHEME_TRUEP(SCHEME_VEC_ELS(name)[6]));
ctx = scheme_get_sized_byte_string_output(port, &plen);
prefix = " in: ";
} else {
ctx = scheme_get_proc_name(func, &len, 0);
prefix = " in: ";
}
} else {
ctx = "";
prefix = "";
}
} else {
ctx = "";
prefix = "";
}
if (SAME_TYPE(SCHEME_TYPE(mod), scheme_module_type)) {
mctx = scheme_display_to_string(((Scheme_Module *)mod)->modsrc, NULL);
mprefix = " in module: ";
} else {
mctx = "";
mprefix = "";
}
clen = strlen(ctx);
plen = strlen(prefix);
mclen = strlen(mctx);
mplen = strlen(mprefix);
if (!clen && !mclen)
return "";
all = scheme_malloc_atomic(clen + plen + mclen + mplen + 1);
memcpy(all, prefix, plen);
memcpy(all + plen, ctx, clen);
memcpy(all + plen + clen, mprefix, mplen);
memcpy(all + plen + clen + mplen, mctx, mclen);
all[clen + plen + mclen + mplen] = 0;
return all;
} else
return "";
}
char *scheme_optimize_info_context(Optimize_Info *info)
{
return scheme_optimize_context_to_string(info->context);
}
Scheme_Logger *scheme_optimize_info_logger(Optimize_Info *info)
{
return info->logger;
}
/*========================================================================*/
/* utils */
/*========================================================================*/
static void set_optimize_mode(Scheme_IR_Local *var)
{
MZ_ASSERT(SAME_TYPE(var->so.type, scheme_ir_local_type));
memset(&var->optimize, 0, sizeof(var->optimize));
var->mode = SCHEME_VAR_MODE_OPTIMIZE;
}
#define SCHEME_PRIM_IS_UNSAFE_NONMUTATING (SCHEME_PRIM_IS_UNSAFE_FUNCTIONAL | SCHEME_PRIM_IS_UNSAFE_OMITABLE)
int scheme_is_functional_nonfailing_primitive(Scheme_Object *rator, int num_args, int expected_vals)
/* A call to a functional, non-failing primitive (i.e., it accepts any argument)
can be discarded if its results are ignored.
Return 2 => true, and results are a constant when arguments are constants. */
{
if (SCHEME_PRIMP(rator)
&& (SCHEME_PRIM_PROC_OPT_FLAGS(rator) & (SCHEME_PRIM_IS_OMITABLE_ANY | SCHEME_PRIM_IS_UNSAFE_NONMUTATING))
&& (num_args >= ((Scheme_Primitive_Proc *)rator)->mina)
&& (num_args <= ((Scheme_Primitive_Proc *)rator)->mu.maxa)
&& ((expected_vals < 0)
|| ((expected_vals == 1) && !(SCHEME_PRIM_PROC_FLAGS(rator) & SCHEME_PRIM_IS_MULTI_RESULT))
|| (SAME_OBJ(scheme_values_proc, rator)
&& (expected_vals == num_args)))) {
if (SAME_OBJ(scheme_values_proc, rator))
return 2;
return 1;
} else
return 0;
}
static Scheme_Object *get_struct_proc_shape(Scheme_Object *rator, Optimize_Info *info, int prop_ok)
/* Determines whether `rator` is known to be a struct accessor, etc. */
{
Scheme_Object *c;
if (info
&& (info->top_level_consts || info->cp->inline_variants)
&& SAME_TYPE(SCHEME_TYPE(rator), scheme_ir_toplevel_type)) {
int pos;
pos = SCHEME_TOPLEVEL_POS(rator);
c = NULL;
if (info->top_level_consts)
c = scheme_hash_get(info->top_level_consts, scheme_make_integer(pos));
if (!c && info->cp->inline_variants)
c = scheme_hash_get(info->cp->inline_variants, scheme_make_integer(pos));
if (c && (SAME_TYPE(SCHEME_TYPE(c), scheme_struct_proc_shape_type)
|| (prop_ok && SAME_TYPE(SCHEME_TYPE(c), scheme_struct_prop_proc_shape_type)))) {
return c;
}
}
return NULL;
}
int scheme_is_struct_functional(Scheme_Object *rator, int num_args, Optimize_Info *info, int vals)
/* Determines whether `rator` is a functional, non-failing struct operation */
{
Scheme_Object *c;
if ((vals == 1) || (vals == -1)) {
c = get_struct_proc_shape(rator, info, 1);
if (c) {
if (SAME_TYPE(SCHEME_TYPE(c), scheme_struct_proc_shape_type)) {
int mode = (SCHEME_PROC_SHAPE_MODE(c) & STRUCT_PROC_SHAPE_MASK);
int field_count = (SCHEME_PROC_SHAPE_MODE(c) >> STRUCT_PROC_SHAPE_SHIFT);
if (((num_args == 1) && (mode == STRUCT_PROC_SHAPE_PRED))
|| ((num_args == field_count) && (mode == STRUCT_PROC_SHAPE_CONSTR))) {
return 1;
}
} else if (SAME_TYPE(SCHEME_TYPE(c), scheme_struct_prop_proc_shape_type)) {
if ((SCHEME_PROP_PROC_SHAPE_MODE(c) == STRUCT_PROP_PROC_SHAPE_PRED)
&& (num_args == 1))
return 1;
}
}
}
return 0;
}
static Scheme_Object *extract_specialized_proc(Scheme_Object *le, Scheme_Object *default_val)
/* Look through `(procedure-specialize <e>)` to get `<e>` */
{
if (SAME_TYPE(SCHEME_TYPE(le), scheme_application2_type)) {
Scheme_App2_Rec *app = (Scheme_App2_Rec *)le;
if (SAME_OBJ(app->rator, scheme_procedure_specialize_proc)) {
if (SCHEME_PROCP(app->rand) || SCHEME_LAMBDAP(app->rand))
return app->rand;
}
}
return default_val;
}
int scheme_omittable_expr(Scheme_Object *o, int vals, int fuel, int flags,
Optimize_Info *opt_info, Optimize_Info *warn_info)
/* Checks whether the bytecode `o` returns `vals` values with no
side-effects and without pushing and using continuation marks.
A -1 for `vals` means that any return count is ok.
Also used with fully resolved expression by `module' to check
for "functional" bodies, in which case `flags` includes
`OMITTABLE_RESOLVED`.
The `opt_info` argument is used only to access module-level
information, not local bindings.
If `warn_info` is supplied, complain when a mismatch is detected.
We rely on the letrec-check pass to avoid omitting early references
to letrec-bound variables, but `flags` can include `OMITTABLE_KEEP_VARS`
to keep all variable references.
If flags includes `OMITTABLE_KEEP_MUTABLE_VARS`, then references
to mutable variables are kept, which allows this function to be
a conservative approximation for "reorderable". */
{
Scheme_Type vtype;
/* FIXME: can overflow the stack */
try_again:
vtype = SCHEME_TYPE(o);
if ((vtype > _scheme_ir_values_types_)
|| ((vtype == scheme_ir_local_type)
&& !(flags & OMITTABLE_KEEP_VARS)
&& (!(flags & OMITTABLE_KEEP_MUTABLE_VARS)
|| !SCHEME_VAR(o)->mutated))
|| ((vtype == scheme_local_type)
&& !(flags & OMITTABLE_KEEP_VARS)
&& !(SCHEME_GET_LOCAL_FLAGS(o) == SCHEME_LOCAL_CLEAR_ON_READ))
|| ((vtype == scheme_local_unbox_type)
&& !(flags & (OMITTABLE_KEEP_VARS | OMITTABLE_KEEP_MUTABLE_VARS))
&& !(SCHEME_GET_LOCAL_FLAGS(o) == SCHEME_LOCAL_CLEAR_ON_READ))
|| (vtype == scheme_lambda_type)
|| (vtype == scheme_ir_lambda_type)
|| (vtype == scheme_inline_variant_type)
|| (vtype == scheme_case_lambda_sequence_type)
|| (vtype == scheme_quote_syntax_type)
|| (vtype == scheme_varref_form_type)
|| (vtype == scheme_ir_quote_syntax_type)) {
note_match(1, vals, warn_info);
return ((vals == 1) || (vals < 0));
}
if (vtype == scheme_toplevel_type) {
note_match(1, vals, warn_info);
if (!(flags & OMITTABLE_KEEP_VARS) && (flags & OMITTABLE_RESOLVED) && ((vals == 1) || (vals < 0))) {
if (SCHEME_TOPLEVEL_FLAGS(o) & SCHEME_TOPLEVEL_FLAGS_MASK)
return 1;
else
return 0;
}
}
if (vtype == scheme_ir_toplevel_type) {
note_match(1, vals, warn_info);
if ((vals == 1) || (vals < 0)) {
if (!(flags & OMITTABLE_KEEP_VARS)
&& ((SCHEME_TOPLEVEL_FLAGS(o) & SCHEME_TOPLEVEL_FLAGS_MASK) >= SCHEME_TOPLEVEL_READY))
return 1;
else if ((SCHEME_TOPLEVEL_FLAGS(o) & SCHEME_TOPLEVEL_FLAGS_MASK) >= SCHEME_TOPLEVEL_FIXED)
return 1;
else
return 0;
}
}
if (vtype == scheme_branch_type) {
Scheme_Branch_Rec *b;
b = (Scheme_Branch_Rec *)o;
return (scheme_omittable_expr(b->test, 1, fuel - 1, flags, opt_info, warn_info)
&& scheme_omittable_expr(b->tbranch, vals, fuel - 1, flags, opt_info, warn_info)
&& scheme_omittable_expr(b->fbranch, vals, fuel - 1, flags, opt_info, warn_info));
}
if (vtype == scheme_let_one_type) {
Scheme_Let_One *lo = (Scheme_Let_One *)o;
return (scheme_omittable_expr(lo->value, 1, fuel - 1, flags, opt_info, warn_info)
&& scheme_omittable_expr(lo->body, vals, fuel - 1, flags, opt_info, warn_info));
}
if (vtype == scheme_let_void_type) {
Scheme_Let_Void *lv = (Scheme_Let_Void *)o;
/* recognize (letrec ([x <omittable>]) ...): */
MZ_ASSERT(flags & OMITTABLE_RESOLVED);
if (SAME_TYPE(SCHEME_TYPE(lv->body), scheme_let_value_type)) {
Scheme_Let_Value *lv2 = (Scheme_Let_Value *)lv->body;
if ((lv2->count == 1)
&& (lv2->position == 0)
&& scheme_omittable_expr(lv2->value, 1, fuel - 1, flags, opt_info, warn_info)) {
o = lv2->body;
} else
o = lv->body;
} else
o = lv->body;
goto try_again;
}
if (vtype == scheme_ir_let_header_type) {
/* recognize another (let ([x <omittable>]) ...) pattern: */
Scheme_IR_Let_Header *lh = (Scheme_IR_Let_Header *)o;
int i;
MZ_ASSERT(!(flags & OMITTABLE_RESOLVED));
o = lh->body;
for (i = 0; i < lh->num_clauses; i++) {
Scheme_IR_Let_Value *lv = (Scheme_IR_Let_Value *)o;
if (!scheme_omittable_expr(lv->value, lv->count, fuel - 1, flags, opt_info, warn_info))
return 0;
o = lv->body;
}
goto try_again;
}
if (vtype == scheme_letrec_type) {
MZ_ASSERT(flags & OMITTABLE_RESOLVED);
o = ((Scheme_Letrec *)o)->body;
goto try_again;
}
if (vtype == scheme_application_type) {
Scheme_App_Rec *app = (Scheme_App_Rec *)o;
if ((app->num_args >= 4) && (app->num_args <= 11)
&& SAME_OBJ(scheme_make_struct_type_proc, app->args[0])) {
note_match(5, vals, warn_info);
}
if (scheme_is_functional_nonfailing_primitive(app->args[0], app->num_args, vals)
|| scheme_is_struct_functional(app->args[0], app->num_args, opt_info, vals)
|| ((SCHEME_APPN_FLAGS(app) & APPN_FLAG_OMITTABLE) && !(flags & OMITTABLE_IGNORE_APPN_OMIT))) {
int i;
for (i = app->num_args; i--; ) {
if (!scheme_omittable_expr(app->args[i + 1], 1, fuel - 1, flags, opt_info, warn_info))
return 0;
}
return 1;
} else if (SCHEME_PRIMP(app->args[0])) {
if (!(SCHEME_PRIM_PROC_FLAGS(app->args[0]) & SCHEME_PRIM_IS_MULTI_RESULT)) {
note_match(1, vals, warn_info);
} else if (SAME_OBJ(scheme_values_proc, app->args[0])) {
note_match(app->num_args, vals, warn_info);
}
}
if (!SAME_OBJ(scheme_make_struct_type_proc, app->args[0]))
return 0;
}
if (vtype == scheme_application2_type) {
Scheme_App2_Rec *app = (Scheme_App2_Rec *)o;
if (scheme_is_functional_nonfailing_primitive(app->rator, 1, vals)
|| scheme_is_struct_functional(app->rator, 1, opt_info, vals)
|| ((SCHEME_APPN_FLAGS(app) & APPN_FLAG_OMITTABLE) && !(flags & OMITTABLE_IGNORE_APPN_OMIT))) {
if (scheme_omittable_expr(app->rand, 1, fuel - 1, flags, opt_info, warn_info))
return 1;
} else if (SAME_OBJ(app->rator, scheme_make_vector_proc)
&& (vals == 1 || vals == -1)
&& (SCHEME_INTP(app->rand)
&& (SCHEME_INT_VAL(app->rand) >= 0))
&& IN_FIXNUM_RANGE_ON_ALL_PLATFORMS(SCHEME_INT_VAL(app->rand))) {
return 1;
} else if (SAME_OBJ(app->rator, scheme_procedure_specialize_proc)) {
if ((vals == 1 || vals == -1) && extract_specialized_proc(o, NULL))
return 1;
} else if (SCHEME_PRIMP(app->rator)) {
if (!(SCHEME_PRIM_PROC_FLAGS(app->rator) & SCHEME_PRIM_IS_MULTI_RESULT)
|| SAME_OBJ(scheme_values_proc, app->rator)) {
note_match(1, vals, warn_info);
}
}
if (!SAME_OBJ(scheme_make_struct_type_property_proc, app->rator))
return 0;
}
if (vtype == scheme_application3_type) {
Scheme_App3_Rec *app = (Scheme_App3_Rec *)o;
if (scheme_is_functional_nonfailing_primitive(app->rator, 2, vals)
|| scheme_is_struct_functional(app->rator, 2, opt_info, vals)
|| ((SCHEME_APPN_FLAGS(app) & APPN_FLAG_OMITTABLE) && !(flags & OMITTABLE_IGNORE_APPN_OMIT))) {
if (scheme_omittable_expr(app->rand1, 1, fuel - 1, flags, opt_info, warn_info)
&& scheme_omittable_expr(app->rand2, 1, fuel - 1, flags, opt_info, warn_info))
return 1;
} else if (SAME_OBJ(app->rator, scheme_make_vector_proc)
&& (vals == 1 || vals == -1)
&& (SCHEME_INTP(app->rand1)
&& (SCHEME_INT_VAL(app->rand1) >= 0)
&& IN_FIXNUM_RANGE_ON_ALL_PLATFORMS(SCHEME_INT_VAL(app->rand1)))
&& scheme_omittable_expr(app->rand2, 1, fuel - 1, flags, opt_info, warn_info)) {
return 1;
} else if (SCHEME_PRIMP(app->rator)) {
if (!(SCHEME_PRIM_PROC_FLAGS(app->rator) & SCHEME_PRIM_IS_MULTI_RESULT)) {
note_match(1, vals, warn_info);
} else if (SAME_OBJ(scheme_values_proc, app->rator)) {
note_match(2, vals, warn_info);
}
}
if (!SAME_OBJ(scheme_make_struct_type_property_proc, app->rator))
return 0;
}
/* check for (set! x x) */
if (vtype == scheme_set_bang_type) {
Scheme_Set_Bang *sb = (Scheme_Set_Bang *)o;
if (SAME_TYPE(scheme_local_type, SCHEME_TYPE(sb->var))
&& SAME_TYPE(scheme_local_type, SCHEME_TYPE(sb->val))
&& (SCHEME_LOCAL_POS(sb->var) == SCHEME_LOCAL_POS(sb->val)))
return 1;
else if (SAME_TYPE(scheme_ir_local_type, SCHEME_TYPE(sb->var))
&& SAME_OBJ(sb->var, sb->val))
return 1;
}
/* check for struct-type declaration: */
if (!(flags & OMITTABLE_IGNORE_MAKE_STRUCT_TYPE)) {
Scheme_Object *auto_e;
int auto_e_depth;
auto_e = scheme_is_simple_make_struct_type(o, vals,
(((flags & OMITTABLE_RESOLVED) ? CHECK_STRUCT_TYPE_RESOLVED : 0)
| CHECK_STRUCT_TYPE_ALWAYS_SUCCEED
| CHECK_STRUCT_TYPE_DELAY_AUTO_CHECK),
&auto_e_depth,
NULL, NULL,
(opt_info ? opt_info->top_level_consts : NULL),
((opt_info && opt_info->cp) ? opt_info->cp->inline_variants : NULL),
NULL, NULL, 0, NULL, NULL, NULL,
5);
if (auto_e) {
if (scheme_omittable_expr(auto_e, 1, fuel - 1, flags, opt_info, warn_info))
return 1;
}
}
/* check for struct-type property declaration: */
if (!(flags & OMITTABLE_IGNORE_MAKE_STRUCT_TYPE)) {
if (scheme_is_simple_make_struct_type_property(o, vals,
(((flags & OMITTABLE_RESOLVED) ? CHECK_STRUCT_TYPE_RESOLVED : 0)
| CHECK_STRUCT_TYPE_ALWAYS_SUCCEED),
NULL,
(opt_info ? opt_info->top_level_consts : NULL),
((opt_info && opt_info->cp) ? opt_info->cp->inline_variants : NULL),
NULL, NULL, 0, NULL, NULL,
5))
return 1;
}
return 0;
}
static Scheme_Object *ensure_single_value(Scheme_Object *e)
/* Wrap `e` so that it either produces a single value or fails */
{
Scheme_App2_Rec *app2;
if (single_valued_noncm_expression(e, 5))
return e;
app2 = MALLOC_ONE_TAGGED(Scheme_App2_Rec);
app2->iso.so.type = scheme_application2_type;
app2->rator = scheme_values_proc;
app2->rand = e;
SCHEME_APPN_FLAGS(app2) |= (APPN_FLAG_IMMED | APPN_FLAG_SFS_TAIL);
return (Scheme_Object *)app2;
}
static Scheme_Object *do_make_discarding_sequence(Scheme_Object *e1, Scheme_Object *e2,
Optimize_Info *info,
int ignored, int rev)
/* Evaluate `e1` then `e2` (or opposite order if rev), and each must
produce a single value. The result of `e1` is ignored and the
result is `e2` --- except that `e2` is ignored, too, if
`ignored`. */
{
if (ignored)
e2 = optimize_ignored(e2, info, 1, 0, 5);
e2 = ensure_single_value(e2);
if (scheme_omittable_expr(e1, 1, 5, 0, info, NULL))
return e2;
e1 = ensure_single_value(optimize_ignored(e1, info, 1, 0, 5));
if (ignored && scheme_omittable_expr(e2, 1, 5, 0, info, NULL))
return e1;
/* use `begin` instead of `begin0` if we can swap the order: */
if (rev && movable_expression(e2, info, 0, 1, 1, 0, 50))
rev = 0;
return scheme_make_sequence_compilation(scheme_make_pair((rev ? e2 : e1),
scheme_make_pair((rev ? e1 : e2), scheme_null)),
rev ? -1 : 1,
0);
}
static Scheme_Object *make_discarding_sequence(Scheme_Object *e1, Scheme_Object *e2,
Optimize_Info *info)
{
return do_make_discarding_sequence(e1, e2, info, 0, 0);
}
static Scheme_Object *make_discarding_reverse_sequence(Scheme_Object *e1, Scheme_Object *e2,
Optimize_Info *info)
{
return do_make_discarding_sequence(e1, e2, info, 0, 1);
}
static Scheme_Object *make_discarding_sequence_3(Scheme_Object *e1, Scheme_Object *e2, Scheme_Object *e3,
Optimize_Info *info)
{
return make_discarding_sequence(e1, make_discarding_sequence(e2, e3, info), info);
}
static Scheme_Object *make_discarding_app_sequence(Scheme_App_Rec *appr, int result_pos, Scheme_Object *result,
Optimize_Info *info)
/* Generalize do_make_discarding_sequence() to a sequence of argument
expressions, where `result_pos` is the position of the returned
argument. If `result_pos` is -1, then all argument results will be
ignored. If `result`, then it is used as the result after all
arguments are evaluated.*/
{
int i;
Scheme_Object *l = scheme_null;
result_pos = result_pos + 1;
if (result)
l = scheme_make_pair(result, l);
for (i = appr->num_args; i; i--) {
Scheme_Object *e;
e = appr->args[i];
e = ensure_single_value(e);
if (i == result_pos) {
if (SCHEME_NULLP(l)) {
l = scheme_make_pair(e, scheme_null);
} else {
l = scheme_make_sequence_compilation(scheme_make_pair(e, l), -1, 0);
l = scheme_make_pair(l, scheme_null);
}
} else {
e = optimize_ignored(e, info, 1, 1, 5);
if (e)
l = scheme_make_pair(e, l);
}
}
if (SCHEME_NULLP(l))
return scheme_void;
if (SCHEME_NULLP(SCHEME_CDR(l)))
return SCHEME_CAR(l);
return scheme_make_sequence_compilation(l, 1, 0);
}
static Scheme_Object *optimize_ignored(Scheme_Object *e, Optimize_Info *info,
int expected_vals, int maybe_omittable,
int fuel)
/* Simplify an expression whose result will be ignored. The
`expected_vals` is 1 or -1. If `maybe_omittable`, the result can be
NULL to indicate that it can be omitted. */
{
if (maybe_omittable) {
if (scheme_omittable_expr(e, expected_vals, 5, 0, info, NULL))
return NULL;
}
if (fuel) {
/* We could do a lot more here, but for now, we just avoid purely
functional, always successful operations --- especially allocating ones. */
switch (SCHEME_TYPE(e)) {
case scheme_application2_type:
{
Scheme_App2_Rec *app = (Scheme_App2_Rec *)e;
if (!SAME_OBJ(app->rator, scheme_values_proc)) /* `values` is probably here to ensure a single result */
if (scheme_is_functional_nonfailing_primitive(app->rator, 1, expected_vals))
return do_make_discarding_sequence(app->rand, scheme_void, info, 1, 0);
/* (make-vector <num>) => <void> */
if (SAME_OBJ(app->rator, scheme_make_vector_proc)
&& (SCHEME_INTP(app->rand)
&& (SCHEME_INT_VAL(app->rand) >= 0))
&& IN_FIXNUM_RANGE_ON_ALL_PLATFORMS(SCHEME_INT_VAL(app->rand)))
return (maybe_omittable ? NULL : scheme_void);
}
break;
case scheme_application3_type:
{
Scheme_App3_Rec *app = (Scheme_App3_Rec *)e;
if (scheme_is_functional_nonfailing_primitive(app->rator, 2, expected_vals))
return do_make_discarding_sequence(app->rand1,
do_make_discarding_sequence(app->rand2,
scheme_void,
info,
1, 0),
info,
1, 0);
/* (make-vector <num> <expr>) => <expr> */
if (SAME_OBJ(app->rator, scheme_make_vector_proc)
&& (SCHEME_INTP(app->rand1)
&& (SCHEME_INT_VAL(app->rand1) >= 0))
&& IN_FIXNUM_RANGE_ON_ALL_PLATFORMS(SCHEME_INT_VAL(app->rand1))) {
Scheme_Object *val;
val = ensure_single_value(app->rand2);
return optimize_ignored(val, info, 1, maybe_omittable, 5);
}
}
break;
case scheme_application_type:
{
Scheme_App_Rec *app = (Scheme_App_Rec *)e;
if (scheme_is_functional_nonfailing_primitive(app->args[0], app->num_args, expected_vals))
return make_discarding_app_sequence(app, -1, NULL, info);
}
break;
case scheme_branch_type:
{
Scheme_Branch_Rec *b = (Scheme_Branch_Rec *)e;
Scheme_Object *tb, *fb;
tb = optimize_ignored(b->tbranch, info, expected_vals, 1, fuel - 1);
fb = optimize_ignored(b->fbranch, info, expected_vals, 1, fuel - 1);
if (tb || fb) {
b->tbranch = tb ? tb : scheme_false;
b->fbranch = fb ? fb : scheme_false;
return (Scheme_Object*)b;
} else {
Scheme_Object *val;
val = ensure_single_value(b->test);
return optimize_ignored(val, info, 1, maybe_omittable, 5);
}
}
break;
}
}
return e;
}
static Scheme_Object *make_sequence_2(Scheme_Object *a, Scheme_Object *b)
{
return scheme_make_sequence_compilation(scheme_make_pair(a, scheme_make_pair(b, scheme_null)), 1, 0);
}
static Scheme_Object *make_discarding_first_sequence(Scheme_Object *e1, Scheme_Object *e2,
Optimize_Info *info)
/* Like make_discarding_sequence(), but second expression is not constrained to
a single result. */
{
e1 = optimize_ignored(e1, info, 1, 1, 5);
if (!e1)
return e2;
e1 = ensure_single_value(e1);
return make_sequence_2(e1, e2);
}
static Scheme_Object *make_application_2(Scheme_Object *a, Scheme_Object *b, Optimize_Info *info)
{
return scheme_make_application(scheme_make_pair(a, scheme_make_pair(b, scheme_null)), info);
}
static Scheme_Object *make_application_3(Scheme_Object *a, Scheme_Object *b, Scheme_Object *c,
Optimize_Info *info)
{
return scheme_make_application(scheme_make_pair(a, scheme_make_pair(b, scheme_make_pair(c, scheme_null))),
info);
}
static Scheme_Object *replace_tail_inside(Scheme_Object *alt, Scheme_Object *inside, Scheme_Object *orig)
/* Installs a new expression in the result position of various forms, such as `begin`;
extract_tail_inside() needs to be consistent with this function */
{
if (inside) {
switch (SCHEME_TYPE(inside)) {
case scheme_sequence_type:
if (((Scheme_Sequence *)inside)->count)
((Scheme_Sequence *)inside)->array[((Scheme_Sequence *)inside)->count-1] = alt;
else
scheme_signal_error("internal error: strange inside replacement");
break;
case scheme_ir_let_header_type:
((Scheme_IR_Let_Header *)inside)->body = alt;
break;
case scheme_ir_let_value_type:
((Scheme_IR_Let_Value *)inside)->body = alt;
break;
default:
scheme_signal_error("internal error: strange inside replacement");
}
return orig;
}
return alt;
}
static void extract_tail_inside(Scheme_Object **_t2, Scheme_Object **_inside)
/* Looks through various forms, like `begin` to extract a result expression;
replace_tail_inside() needs to be consistent with this function */
{
while (1) {
if (SAME_TYPE(SCHEME_TYPE(*_t2), scheme_ir_let_header_type)) {
Scheme_IR_Let_Header *head = (Scheme_IR_Let_Header *)*_t2;
int i;
*_inside = *_t2;
*_t2 = head->body;
for (i = head->num_clauses; i--; ) {
*_inside = *_t2;
*_t2 = ((Scheme_IR_Let_Value *)*_t2)->body;
}
} else if (SAME_TYPE(SCHEME_TYPE(*_t2), scheme_sequence_type)) {
Scheme_Sequence *seq = (Scheme_Sequence *)*_t2;
if (seq->count) {
*_inside = *_t2;
*_t2 = seq->array[seq->count-1];
} else
break;
} else
break;
}
}
Scheme_Object *scheme_optimize_extract_tail_inside(Scheme_Object *t2)
{
Scheme_Object *inside;
extract_tail_inside(&t2, &inside);
return t2;
}
/*========================================================================*/
/* detecting `make-struct-type` calls and struct shapes */
/*========================================================================*/
static int is_inspector_call(Scheme_Object *a)
/* Does `a` produce an inspector? */
{
if (SAME_TYPE(SCHEME_TYPE(a), scheme_application_type)) {
Scheme_App_Rec *app = (Scheme_App_Rec *)a;
if (!app->num_args
&& (SAME_OBJ(app->args[0], scheme_current_inspector_proc)
|| SAME_OBJ(app->args[0], scheme_make_inspector_proc)))
return 1;
}
return 0;
}
static int is_proc_spec_proc(Scheme_Object *p, int init_field_count)
/* Does `p` produce a good `prop:procedure` value? */
{
Scheme_Type vtype;
if (SCHEME_INTP(p)
&& (SCHEME_INT_VAL(p) >= 0)
&& (SCHEME_INT_VAL(p) < init_field_count))
return 1;
if (SCHEME_PROCP(p)) {
p = scheme_get_or_check_arity(p, -1);
if (SCHEME_INTP(p)) {
return (SCHEME_INT_VAL(p) >= 1);
} else if (SCHEME_STRUCTP(p)
&& scheme_is_struct_instance(scheme_arity_at_least, p)) {
p = ((Scheme_Structure *)p)->slots[0];
if (SCHEME_INTP(p))
return (SCHEME_INT_VAL(p) >= 1);
}
return 0;
}
vtype = SCHEME_TYPE(p);
if ((vtype == scheme_lambda_type) || (vtype == scheme_ir_lambda_type)) {
if (((Scheme_Lambda *)p)->num_params >= 1)
return 1;
}