-
Notifications
You must be signed in to change notification settings - Fork 405
Expand file tree
/
Copy pathconstfold.cpp
More file actions
3079 lines (2748 loc) · 117 KB
/
constfold.cpp
File metadata and controls
3079 lines (2748 loc) · 117 KB
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
// Copyright Contributors to the Open Shading Language project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/AcademySoftwareFoundation/OpenShadingLanguage
#include <vector>
#include <cmath>
#include <cstdlib>
#include <OpenImageIO/fmath.h>
#include <OpenImageIO/sysutil.h>
#include "oslexec_pvt.h"
#include "opcolor.h"
#include "runtimeoptimize.h"
#include <OSL/dual.h>
#include <OSL/oslnoise.h>
using namespace OSL;
using namespace OSL::pvt;
// names of ops we'll be using frequently
static ustring u_nop ("nop"),
u_assign ("assign"),
u_aassign ("aassign"),
u_compassign ("compassign"),
u_mxcompassign ("mxcompassign"),
u_add ("add"),
u_sub ("sub"),
u_mul ("mul"),
u_sqrt ("sqrt"),
u_inversesqrt ("inversesqrt"),
u_cbrt ("cbrt"),
u_if ("if"),
u_eq ("eq"),
u_return ("return"),
u_error ("error"),
u_fmterror("%s"),
u_fmt_range_check("Index [%d] out of range %s[0..%d]: %s:%d (group %s, layer %d %s, shader %s)");
static ustring u_cell ("cell"), u_cellnoise ("cellnoise");
OSL_NAMESPACE_ENTER
namespace pvt { // OSL::pvt
inline bool
equal_consts (const Symbol &A, const Symbol &B)
{
return (&A == &B ||
(equivalent (A.typespec(), B.typespec()) &&
!memcmp (A.data(), B.data(), A.typespec().simpletype().size())));
}
inline bool
unequal_consts (const Symbol &A, const Symbol &B)
{
return (equivalent (A.typespec(), B.typespec()) &&
memcmp (A.data(), B.data(), A.typespec().simpletype().size()));
}
int constfold_none(RuntimeOptimizer& /*rop*/, int /*opnum*/)
{
return 0;
}
DECLFOLDER(constfold_add)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
Symbol &B (*rop.inst()->argsymbol(op.firstarg()+2));
if (rop.is_zero(A)) {
// R = 0 + B => R = B
rop.turn_into_assign (op, rop.inst()->arg(op.firstarg()+2),
"0 + A => A");
return 1;
}
if (rop.is_zero(B)) {
// R = A + 0 => R = A
rop.turn_into_assign (op, rop.inst()->arg(op.firstarg()+1),
"A + 0 => A");
return 1;
}
if (A.is_constant() && B.is_constant()) {
if (A.typespec().is_int() && B.typespec().is_int()) {
int cind = rop.add_constant (A.get_int() + B.get_int());
rop.turn_into_assign (op, cind, "const + const");
return 1;
} else if (A.typespec().is_float() && B.typespec().is_float()) {
int cind = rop.add_constant (A.get_float() + B.get_float());
rop.turn_into_assign (op, cind, "const + const");
return 1;
} else if (A.typespec().is_triple_or_float() && B.typespec().is_triple_or_float()) {
Symbol &R (*rop.inst()->argsymbol(op.firstarg()+0));
Vec3 result = A.coerce_vec3() + B.coerce_vec3();
int cind = rop.add_constant (R.typespec(), &result);
rop.turn_into_assign (op, cind, "const + const");
return 1;
}
}
return 0;
}
DECLFOLDER(constfold_sub)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
Symbol &B (*rop.inst()->argsymbol(op.firstarg()+2));
if (rop.is_zero(B)) {
// R = A - 0 => R = A
rop.turn_into_assign (op, rop.inst()->arg(op.firstarg()+1),
"A - 0 => A");
return 1;
}
// R = A - B, if both are constants, => R = C
if (A.is_constant() && B.is_constant()) {
if (A.typespec().is_int() && B.typespec().is_int()) {
int cind = rop.add_constant (A.get_int() - B.get_int());
rop.turn_into_assign (op, cind, "const - const");
return 1;
} else if (A.typespec().is_float() && B.typespec().is_float()) {
int cind = rop.add_constant (A.get_float() - B.get_float());
rop.turn_into_assign (op, cind, "const - const");
return 1;
} else if (A.typespec().is_triple_or_float() && B.typespec().is_triple_or_float()) {
Symbol &R (*rop.inst()->argsymbol(op.firstarg()+0));
Vec3 result = A.coerce_vec3() - B.coerce_vec3();
int cind = rop.add_constant (R.typespec(), &result);
rop.turn_into_assign (op, cind, "const - const");
return 1;
}
}
// R = A - A => R = 0 even if not constant!
if (&A == &B) {
rop.turn_into_assign_zero (op, "A - A => 0");
}
return 0;
}
DECLFOLDER(constfold_mul)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
Symbol &B (*rop.inst()->argsymbol(op.firstarg()+2));
if (rop.is_one(A)) {
// R = 1 * B => R = B
rop.turn_into_assign (op, rop.inst()->arg(op.firstarg()+2),
"1 * A => A");
return 1;
}
if (rop.is_zero(A)) {
// R = 0 * B => R = 0
rop.turn_into_assign (op, rop.inst()->arg(op.firstarg()+1),
"0 * A => 0");
return 1;
}
if (rop.is_one(B)) {
// R = A * 1 => R = A
rop.turn_into_assign (op, rop.inst()->arg(op.firstarg()+1),
"A * 1 => A");
return 1;
}
if (rop.is_zero(B)) {
// R = A * 0 => R = 0
rop.turn_into_assign (op, rop.inst()->arg(op.firstarg()+2),
"A * 0 => 0");
return 1;
}
if (A.is_constant() && B.is_constant()) {
if (A.typespec().is_int() && B.typespec().is_int()) {
int cind = rop.add_constant (A.get_int() * B.get_int());
rop.turn_into_assign (op, cind, "const * const");
return 1;
} else if (A.typespec().is_float() && B.typespec().is_float()) {
int cind = rop.add_constant (A.get_float() * B.get_float());
rop.turn_into_assign (op, cind, "const * const");
return 1;
} else if (A.typespec().is_triple_or_float() && B.typespec().is_triple_or_float()) {
Symbol &R (*rop.inst()->argsymbol(op.firstarg()+0));
Vec3 result = A.coerce_vec3() * B.coerce_vec3();
int cind = rop.add_constant (R.typespec(), &result);
rop.turn_into_assign (op, cind, "const * const");
return 1;
}
}
return 0;
}
DECLFOLDER(constfold_div)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
Symbol &B (*rop.inst()->argsymbol(op.firstarg()+2));
if (rop.is_one(B)) {
// R = A / 1 => R = A
rop.turn_into_assign (op, rop.inst()->arg(op.firstarg()+1),
"A / 1 => A");
return 1;
}
if (rop.is_zero(B) && (B.typespec().is_float() ||
B.typespec().is_triple() || B.typespec().is_int())) {
// R = A / 0 => R = 0 because of OSL div by zero rule
rop.turn_into_assign_zero (op, "A / 0 => 0 (by OSL division rules)");
return 1;
}
if (A.is_constant() && B.is_constant()) {
int cind = -1;
if (A.typespec().is_int() && B.typespec().is_int()) {
cind = rop.add_constant (A.get_int() / B.get_int());
} else if (A.typespec().is_int_or_float() && B.typespec().is_int_or_float()) {
cind = rop.add_constant (A.coerce_float() / B.coerce_float());
} else if (A.typespec().is_triple_or_float() && B.typespec().is_triple_or_float()) {
Symbol &R (*rop.inst()->argsymbol(op.firstarg()+0));
Vec3 result = A.coerce_vec3() / B.coerce_vec3();
cind = rop.add_constant (R.typespec(), &result);
}
if (cind >= 0) {
rop.turn_into_assign (op, cind, "const / const");
return 1;
}
}
return 0;
}
DECLFOLDER(constfold_mod)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
Symbol &B (*rop.inst()->argsymbol(op.firstarg()+2));
if (rop.is_zero(A)) {
// R = 0 % B => R = 0
rop.turn_into_assign_zero (op, "0 % A => 0");
return 1;
}
if (rop.is_zero(B)) {
// R = A % 0 => R = 0
rop.turn_into_assign_zero (op, "A % 0 => 0");
return 1;
}
if (A.is_constant() && B.is_constant() &&
A.typespec().is_int() && B.typespec().is_int()) {
int a = A.get_int();
int b = B.get_int();
int cind = rop.add_constant (b ? (a % b) : 0);
rop.turn_into_assign (op, cind, "const % const");
return 1;
}
return 0;
}
DECLFOLDER(constfold_dot)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
Symbol &B (*rop.inst()->argsymbol(op.firstarg()+2));
// Dot with (0,0,0) -> 0
if (rop.is_zero(A) || rop.is_zero(B)) {
rop.turn_into_assign_zero (op, "dot(a,(0,0,0)) => 0");
return 1;
}
// dot(const,const) -> const
if (A.is_constant() && B.is_constant()) {
OSL_DASSERT(A.typespec().is_triple() && B.typespec().is_triple());
float result = A.get_vec3().dot(B.get_vec3());
int cind = rop.add_constant (TypeDesc::TypeFloat, &result);
rop.turn_into_assign (op, cind, "dot(const,const)");
return 1;
}
return 0;
}
DECLFOLDER(constfold_neg)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
if (A.is_constant()) {
if (A.typespec().is_int()) {
int result = - A.get_int();
int cind = rop.add_constant (A.typespec(), &result);
rop.turn_into_assign (op, cind, "-const");
return 1;
} else if (A.typespec().is_float()) {
float result = - A.get_float();
int cind = rop.add_constant (A.typespec(), &result);
rop.turn_into_assign (op, cind, "-const");
return 1;
} else if (A.typespec().is_triple()) {
Vec3 result = - A.get_vec3();
int cind = rop.add_constant (A.typespec(), &result);
rop.turn_into_assign (op, cind, "-const");
return 1;
}
}
return 0;
}
DECLFOLDER(constfold_abs)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
if (A.is_constant()) {
if (A.typespec().is_int()) {
int result = std::abs(A.get_int());
int cind = rop.add_constant (A.typespec(), &result);
rop.turn_into_assign (op, cind, "abs(const)");
return 1;
} else if (A.typespec().is_float()) {
float result = std::abs(A.get_float());
int cind = rop.add_constant (A.typespec(), &result);
rop.turn_into_assign (op, cind, "abs(const)");
return 1;
} else if (A.typespec().is_triple()) {
Vec3 result = A.get_vec3();
result.x = std::abs(result.x);
result.y = std::abs(result.y);
result.z = std::abs(result.z);
int cind = rop.add_constant (A.typespec(), &result);
rop.turn_into_assign (op, cind, "abs(const)");
return 1;
}
}
return 0;
}
DECLFOLDER(constfold_eq)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
Symbol &B (*rop.inst()->argsymbol(op.firstarg()+2));
if (A.is_constant() && B.is_constant()) {
bool val = false;
if (equivalent (A.typespec(), B.typespec())) {
val = equal_consts (A, B);
} else if (A.typespec().is_float() && B.typespec().is_int()) {
val = (A.get_float() == B.get_int());
} else if (A.typespec().is_int() && B.typespec().is_float()) {
val = (A.get_int() == B.get_float());
} else {
return 0; // unhandled cases
}
// Turn the 'eq R A B' into 'assign R X' where X is 0 or 1.
static const int int_zero = 0, int_one = 1;
int cind = rop.add_constant (TypeDesc::TypeInt,
val ? &int_one : &int_zero);
rop.turn_into_assign (op, cind, "const == const");
return 1;
}
return 0;
}
DECLFOLDER(constfold_neq)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
Symbol &B (*rop.inst()->argsymbol(op.firstarg()+2));
if (A.is_constant() && B.is_constant()) {
bool val = false;
if (equivalent (A.typespec(), B.typespec())) {
val = ! equal_consts (A, B);
} else if (A.typespec().is_float() && B.typespec().is_int()) {
val = (A.get_float() != B.get_int());
} else if (A.typespec().is_int() && B.typespec().is_float()) {
val = (A.get_int() != B.get_float());
} else {
return 0; // unhandled case
}
// Turn the 'neq R A B' into 'assign R X' where X is 0 or 1.
static const int int_zero = 0, int_one = 1;
int cind = rop.add_constant (TypeDesc::TypeInt,
val ? &int_one : &int_zero);
rop.turn_into_assign (op, cind, "const != const");
return 1;
}
return 0;
}
DECLFOLDER(constfold_lt)
{
static const int int_zero = 0, int_one = 1;
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
Symbol &B (*rop.inst()->argsymbol(op.firstarg()+2));
const TypeSpec &ta (A.typespec());
const TypeSpec &tb (B.typespec());
if (A.is_constant() && B.is_constant()) {
// Turn the 'leq R A B' into 'assign R X' where X is 0 or 1.
bool val = false;
if (ta.is_float() && tb.is_float()) {
val = (A.get_float() < B.get_float());
} else if (ta.is_float() && tb.is_int()) {
val = (A.get_float() < B.get_int());
} else if (ta.is_int() && tb.is_float()) {
val = (A.get_int() < B.get_float());
} else if (ta.is_int() && tb.is_int()) {
val = (A.get_int() < B.get_int());
} else {
return 0; // unhandled case
}
int cind = rop.add_constant (TypeDesc::TypeInt,
val ? &int_one : &int_zero);
rop.turn_into_assign (op, cind, "const < const");
return 1;
}
return 0;
}
DECLFOLDER(constfold_le)
{
static const int int_zero = 0, int_one = 1;
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
Symbol &B (*rop.inst()->argsymbol(op.firstarg()+2));
const TypeSpec &ta (A.typespec());
const TypeSpec &tb (B.typespec());
if (A.is_constant() && B.is_constant()) {
// Turn the 'leq R A B' into 'assign R X' where X is 0 or 1.
bool val = false;
if (ta.is_float() && tb.is_float()) {
val = (A.get_float() <= B.get_float());
} else if (ta.is_float() && tb.is_int()) {
val = (A.get_float() <= B.get_int());
} else if (ta.is_int() && tb.is_float()) {
val = (A.get_int() <= B.get_float());
} else if (ta.is_int() && tb.is_int()) {
val = (A.get_int() <= B.get_int());
} else {
return 0; // unhandled case
}
int cind = rop.add_constant (TypeDesc::TypeInt,
val ? &int_one : &int_zero);
rop.turn_into_assign (op, cind, "const <= const");
return 1;
}
return 0;
}
DECLFOLDER(constfold_gt)
{
static const int int_zero = 0, int_one = 1;
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
Symbol &B (*rop.inst()->argsymbol(op.firstarg()+2));
const TypeSpec &ta (A.typespec());
const TypeSpec &tb (B.typespec());
if (A.is_constant() && B.is_constant()) {
// Turn the 'gt R A B' into 'assign R X' where X is 0 or 1.
bool val = false;
if (ta.is_float() && tb.is_float()) {
val = (A.get_float() > B.get_float());
} else if (ta.is_float() && tb.is_int()) {
val = (A.get_float() > B.get_int());
} else if (ta.is_int() && tb.is_float()) {
val = (A.get_int() > B.get_float());
} else if (ta.is_int() && tb.is_int()) {
val = (A.get_int() > B.get_int());
} else {
return 0; // unhandled case
}
int cind = rop.add_constant (TypeDesc::TypeInt,
val ? &int_one : &int_zero);
rop.turn_into_assign (op, cind, "const > const");
return 1;
}
return 0;
}
DECLFOLDER(constfold_ge)
{
static const int int_zero = 0, int_one = 1;
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
Symbol &B (*rop.inst()->argsymbol(op.firstarg()+2));
const TypeSpec &ta (A.typespec());
const TypeSpec &tb (B.typespec());
if (A.is_constant() && B.is_constant()) {
// Turn the 'leq R A B' into 'assign R X' where X is 0 or 1.
bool val = false;
if (ta.is_float() && tb.is_float()) {
val = (A.get_float() >= B.get_float());
} else if (ta.is_float() && tb.is_int()) {
val = (A.get_float() >= B.get_int());
} else if (ta.is_int() && tb.is_float()) {
val = (A.get_int() >= B.get_float());
} else if (ta.is_int() && tb.is_int()) {
val = (A.get_int() >= B.get_int());
} else {
return 0; // unhandled case
}
int cind = rop.add_constant (TypeDesc::TypeInt,
val ? &int_one : &int_zero);
rop.turn_into_assign (op, cind, "const >= const");
return 1;
}
return 0;
}
DECLFOLDER(constfold_or)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
Symbol &B (*rop.inst()->argsymbol(op.firstarg()+2));
if (A.is_constant() && B.is_constant()) {
OSL_DASSERT(A.typespec().is_int() && B.typespec().is_int());
bool val = A.get_int() || B.get_int();
// Turn the 'or R A B' into 'assign R X' where X is 0 or 1.
static const int int_zero = 0, int_one = 1;
int cind = rop.add_constant (TypeDesc::TypeInt,
val ? &int_one : &int_zero);
rop.turn_into_assign (op, cind, "const || const");
return 1;
}
return 0;
}
DECLFOLDER(constfold_and)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
Symbol &B (*rop.inst()->argsymbol(op.firstarg()+2));
if (A.is_constant() && B.is_constant()) {
// Turn the 'and R A B' into 'assign R X' where X is 0 or 1.
OSL_DASSERT(A.typespec().is_int() && B.typespec().is_int());
bool val = A.get_int() && B.get_int();
static const int int_zero = 0, int_one = 1;
int cind = rop.add_constant (TypeDesc::TypeInt,
val ? &int_one : &int_zero);
rop.turn_into_assign (op, cind, "const && const");
return 1;
}
return 0;
}
DECLFOLDER(constfold_bitand)
{
Opcode &op (rop.op(opnum));
Symbol &A (*rop.opargsym(op, 1));
Symbol &B (*rop.opargsym(op, 2));
if (A.is_constant() && B.is_constant()) {
// Turn the 'bitand R A B' into 'assign R X'.
OSL_DASSERT(A.typespec().is_int() && B.typespec().is_int());
int cind = rop.add_constant (A.get_int() & B.get_int());
rop.turn_into_assign (op, cind, "const & const");
return 1;
}
return 0;
}
DECLFOLDER(constfold_bitor)
{
Opcode &op (rop.op(opnum));
Symbol &A (*rop.opargsym(op, 1));
Symbol &B (*rop.opargsym(op, 2));
if (A.is_constant() && B.is_constant()) {
// Turn the 'bitor R A B' into 'assign R X'.
OSL_DASSERT(A.typespec().is_int() && B.typespec().is_int());
int cind = rop.add_constant (A.get_int() | B.get_int());
rop.turn_into_assign (op, cind, "const | const");
return 1;
}
return 0;
}
DECLFOLDER(constfold_xor)
{
Opcode &op (rop.op(opnum));
Symbol &A (*rop.opargsym(op, 1));
Symbol &B (*rop.opargsym(op, 2));
if (A.is_constant() && B.is_constant()) {
// Turn the 'xor R A B' into 'assign R X'.
OSL_DASSERT(A.typespec().is_int() && B.typespec().is_int());
int cind = rop.add_constant (A.get_int() ^ B.get_int());
rop.turn_into_assign (op, cind, "const ^ const");
return 1;
}
return 0;
}
DECLFOLDER(constfold_compl)
{
Opcode &op (rop.op(opnum));
Symbol &A (*rop.opargsym(op, 1));
if (A.is_constant()) {
// Turn the 'compl R A' into 'assign R X'.
OSL_DASSERT(A.typespec().is_int());
int cind = rop.add_constant (~(A.get_int()));
rop.turn_into_assign (op, cind, "~const");
return 1;
}
return 0;
}
DECLFOLDER(constfold_if)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &C (*rop.inst()->argsymbol(op.firstarg()+0));
if (C.is_constant()) {
int result = -1; // -1 == we don't know
if (C.typespec().is_int())
result = (C.get_int() != 0);
else if (C.typespec().is_float())
result = (C.get_float() != 0.0f);
else if (C.typespec().is_triple())
result = (C.get_vec3() != Vec3(0,0,0));
else if (C.typespec().is_string()) {
ustring s = C.get_string();
result = (s.length() != 0);
}
int changed = 0;
if (result > 0) {
changed += rop.turn_into_nop (op.jump(0), op.jump(1), "elide 'else'");
changed += rop.turn_into_nop (op, "elide 'else'");
} else if (result == 0) {
changed += rop.turn_into_nop (opnum, op.jump(0), "elide 'if'");
}
return changed;
}
// Eliminate 'if' that contains no statements to execute
int jump = op.farthest_jump ();
bool only_nops = true;
for (int i = opnum+1; i < jump && only_nops; ++i)
only_nops &= (rop.inst()->ops()[i].opname() == u_nop);
if (only_nops) {
rop.turn_into_nop (op, "'if' with no body");
return 1;
}
return 0;
}
// Is an array known to have all elements having the same value?
static bool
array_all_elements_equal (const Symbol &s)
{
TypeDesc t = s.typespec().simpletype();
size_t size = t.elementsize();
size_t n = t.numelements();
for (size_t i = 1; i < n; ++i)
if (memcmp ((const char *)s.data(), (const char *)s.data()+i*size, size))
return false;
return true;
}
DECLFOLDER(constfold_aref)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol &R (*rop.inst()->argsymbol(op.firstarg()+0));
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
Symbol &Index (*rop.inst()->argsymbol(op.firstarg()+2));
OSL_DASSERT(A.typespec().is_array() && Index.typespec().is_int());
// Try to turn R=A[I] into R=C if A and I are const.
if (A.is_constant() && Index.is_constant()) {
TypeSpec elemtype = A.typespec().elementtype();
OSL_ASSERT (equivalent(elemtype, R.typespec()));
const int length = A.typespec().arraylength();
const int orig_index = Index.get_int(), index = OIIO::clamp(orig_index, 0, length - 1);
OSL_DASSERT(index >=0 && index < length);
int cind = rop.add_constant (elemtype,
(char *)A.data() + index*elemtype.simpletype().size());
rop.turn_into_assign (op, cind, "aref const fold: const_array[const]");
if (rop.inst()->master()->range_checking() && index != orig_index) {
// the original index was out of range, and the user cares about reporting errors
const int args_to_add[] = {
rop.add_constant(u_fmt_range_check),
rop.add_constant(orig_index),
rop.add_constant(A.unmangled()),
rop.add_constant(length - 1),
rop.add_constant(op.sourcefile()),
rop.add_constant(op.sourceline()),
rop.add_constant(rop.group().name()),
rop.add_constant(rop.layer()),
rop.add_constant(rop.inst()->layername()),
rop.add_constant(ustring(rop.inst()->shadername()))
};
rop.insert_code(opnum, u_error, args_to_add,
RuntimeOptimizer::RecomputeRWRanges,
RuntimeOptimizer::GroupWithNext);
Opcode &newop (rop.inst()->ops()[opnum]);
newop.argreadonly(0);
}
return 1;
}
// Even if the index isn't constant, we still know the answer if all
// the array elements are equal!
if (A.is_constant() && array_all_elements_equal(A)) {
TypeSpec elemtype = A.typespec().elementtype();
OSL_ASSERT (equivalent(elemtype, R.typespec()));
int cind = rop.add_constant (elemtype, (char *)A.data());
rop.turn_into_assign (op, cind, "aref of elements-equal array");
return 1;
}
return 0;
}
DECLFOLDER(constfold_arraylength)
{
Opcode &op (rop.inst()->ops()[opnum]);
OSL_MAYBE_UNUSED Symbol &R (*rop.inst()->argsymbol(op.firstarg()+0));
Symbol &A (*rop.inst()->argsymbol(op.firstarg()+1));
OSL_DASSERT (R.typespec().is_int() && A.typespec().is_array());
// Try to turn R=arraylength(A) into R=C if the array length is known
int len = A.typespec().is_unsized_array() ? A.initializers()
: A.typespec().arraylength();
if (len > 0) {
int cind = rop.add_constant (TypeSpec(TypeDesc::INT), &len);
rop.turn_into_assign (op, cind, "const fold arraylength");
return 1;
}
return 0;
}
DECLFOLDER(constfold_aassign)
{
// Array assignment
Opcode &op (rop.inst()->ops()[opnum]);
Symbol *R (rop.inst()->argsymbol(op.firstarg()+0));
Symbol *I (rop.inst()->argsymbol(op.firstarg()+1));
Symbol *C (rop.inst()->argsymbol(op.firstarg()+2));
if (! I->is_constant() || !C->is_constant())
return 0; // not much we can do if not assigning constants
OSL_DASSERT (R->typespec().is_array() && I->typespec().is_int());
TypeSpec elemtype = R->typespec().elementtype();
if (elemtype.is_closure())
return 0; // don't worry about closures
TypeDesc elemsimpletype = elemtype.simpletype();
// Look for patterns where all array elements are assigned in
// succession within the same block, in which case we can turn the
// result into a constant!
int len = R->typespec().arraylength();
if (len <= 0)
return 0; // don't handle arrays of unknown length
int elemsize = (int)elemsimpletype.size();
std::vector<int> index_assigned (len, -1);
std::vector<char> filled_values (elemsize * len); // constant storage
char *fill = (char *)&filled_values[0];
int num_assigned = 0;
int opindex = opnum;
int highestop = opindex;
for ( ; ; ) {
Opcode &opi (rop.inst()->ops()[opindex]);
if (opi.opname() != u_aassign)
break; // not a successive aassign op
Symbol *Ri (rop.inst()->argsymbol(opi.firstarg()+0));
if (Ri != R)
break; // not a compassign to the same variable
Symbol *Ii (rop.inst()->argsymbol(opi.firstarg()+1));
Symbol *Ci (rop.inst()->argsymbol(opi.firstarg()+2));
if (! Ii->is_constant() || !Ci->is_constant())
break; // not assigning constants
int indexval = Ii->get_int();
if (indexval < 0 || indexval >= len)
break; // out of range index; let runtime deal with it
if (equivalent(elemtype, Ci->typespec())) {
// equivalent types
memcpy (fill + indexval*elemsize, Ci->data(), elemsize);
} else if (elemtype.is_float() && Ci->typespec().is_int()) {
// special case of float[i] = int
((float *)fill)[indexval] = Ci->coerce_float();
} else {
break; // a case we don't handle
}
if (index_assigned[indexval] < 0)
++num_assigned;
index_assigned[indexval] = opindex;
highestop = opindex;
opindex = rop.next_block_instruction(opindex);
if (! opindex)
break;
}
if (num_assigned == len) {
// woo-hoo! we had a succession of constant aassign ops to the
// same variable, filling in all indices. Turn the whole shebang
// into a single assignment.
int cind = rop.add_constant (R->typespec(), fill);
rop.turn_into_assign (op, cind, "replaced element-by-element assignment");
rop.turn_into_nop (opnum+1, highestop+1, "replaced element-by-element assignment");
return highestop+1-opnum;
}
return 0;
}
DECLFOLDER(constfold_compassign)
{
// Component assignment
Opcode &op (rop.inst()->ops()[opnum]);
Symbol *R (rop.inst()->argsymbol(op.firstarg()+0));
Symbol *I (rop.inst()->argsymbol(op.firstarg()+1));
Symbol *C (rop.inst()->argsymbol(op.firstarg()+2));
if (! I->is_constant() || !C->is_constant())
return 0; // not much we can do if not assigning constants
OSL_DASSERT (R->typespec().is_triple() && I->typespec().is_int() &&
(C->typespec().is_float() || C->typespec().is_int()));
// We are obviously not assigning to a constant, but it could be
// that at this point in our current block, the value of A is known,
// and that will show up as a block alias.
int Aalias = rop.block_alias (rop.inst()->arg(op.firstarg()+0));
Symbol *AA = rop.inst()->symbol(Aalias);
// N.B. symbol returns NULL if Aalias is < 0
// Try to simplify A[I]=C if we already know the old value of A as a
// constant. We can turn it into A[I] = N, where N is the old A but with
// the Ith component set to C. If it turns out that the old A[I] == C,
// and thus the assignment doesn't change A's value, we can eliminate
// the assignment entirely.
if (AA && AA->is_constant()) {
OSL_DASSERT (AA->typespec().is_triple());
int index = I->get_int();
if (index < 0 || index >= 3) {
// We are indexing a const triple out of range. But this
// isn't necessarily a reportable error, because it may be a
// code path that will never be taken. Punt -- don't
// optimize this op, leave it to the execute-time range
// check to catch, if indeed it is a problem.
return 0;
}
float *aa = (float *)AA->data();
float c = C->coerce_float();
if (aa[index] == c) {
// If the component assignment doesn't change that component,
// just omit the op entirely.
rop.turn_into_nop (op, "useless compassign");
return 1;
}
// If the previous value of the triple was a constant, and we're
// assigning a new constant to one component (and the index is
// also a constant), just turn it into an assignment of a new
// constant triple.
Vec3 newval (aa[0], aa[1], aa[2]);
newval[index] = c;
int cind = rop.add_constant (AA->typespec(), &newval);
rop.turn_into_assign (op, cind, "fold compassign");
return 1;
}
// Look for patterns where all three components are assigned in
// succession within the same block, in which case we can turn the
// result into a constant!
int index_assigned[3] = { -1, -1, -1 };
float filled_values[3];
int num_assigned = 0;
int opindex = opnum;
int highestop = opindex;
for ( ; ; ) {
Opcode &opi (rop.inst()->ops()[opindex]);
if (opi.opname() != u_compassign)
break; // not a successive compassign op
Symbol *Ri (rop.inst()->argsymbol(opi.firstarg()+0));
if (Ri != R)
break; // not a compassign to the same variable
Symbol *Ii (rop.inst()->argsymbol(opi.firstarg()+1));
Symbol *Ci (rop.inst()->argsymbol(opi.firstarg()+2));
if (! Ii->is_constant() || !Ci->is_constant())
break; // not assigning constants
int indexval = Ii->get_int();
if (indexval < 0 || indexval >= 3)
break; // out of range index; let runtime deal with it
filled_values[indexval] = Ci->coerce_float();
if (index_assigned[indexval] < 0)
++num_assigned;
index_assigned[indexval] = opindex;
highestop = opindex;
opindex = rop.next_block_instruction(opindex);
if (! opindex)
break;
}
if (num_assigned == 3) {
// woo-hoo! we had a succession of constant compassign ops to the
// same variable, filling in all indices. Turn the whole shebang
// into a single assignment.
int cind = rop.add_constant (R->typespec(), filled_values);
rop.turn_into_assign (op, cind, "replaced element-by-element assignment");
rop.turn_into_nop (opnum+1, highestop+1, "replaced element-by-element assignment");
return highestop+1-opnum;
}
return 0;
}
DECLFOLDER(constfold_mxcompassign)
{
// Matrix component assignment
Opcode &op (rop.inst()->ops()[opnum]);
Symbol *R (rop.inst()->argsymbol(op.firstarg()+0));
Symbol *J (rop.inst()->argsymbol(op.firstarg()+1));
Symbol *I (rop.inst()->argsymbol(op.firstarg()+2));
Symbol *C (rop.inst()->argsymbol(op.firstarg()+3));
if (! J->is_constant() || ! I->is_constant() || !C->is_constant())
return 0; // not much we can do if not assigning constants
OSL_DASSERT (R->typespec().is_matrix() &&
J->typespec().is_int() && I->typespec().is_int() &&
(C->typespec().is_float() || C->typespec().is_int()));
// We are obviously not assigning to a constant, but it could be
// that at this point in our current block, the value of A is known,
// and that will show up as a block alias.
int Aalias = rop.block_alias (rop.inst()->arg(op.firstarg()+0));
Symbol *AA = rop.inst()->symbol(Aalias);
// N.B. symbol returns NULL if Aalias is < 0
// Try to simplify A[J,I]=C if we already know the old value of A as a
// constant. We can turn it into A[J,I] = N, where N is the old A but with
// the designated component set to C. If it turns out that the old
// A[J,I] == C, and thus the assignment doesn't change A's value, we can
// eliminate the assignment entirely.
if (AA && AA->is_constant()) {
OSL_DASSERT (AA->typespec().is_matrix());
int jndex = J->get_int();
int index = I->get_int();
if (index < 0 || index >= 3 || jndex < 0 || jndex >= 3) {
// We are indexing a const matrix out of range. But this
// isn't necessarily a reportable error, because it may be a
// code path that will never be taken. Punt -- don't
// optimize this op, leave it to the execute-time range
// check to catch, if indeed it is a problem.
return 0;
}
Matrix44 *aa = (Matrix44 *)AA->dataptr();
float c = C->coerce_float();
if ((*aa)[jndex][index] == c) {
// If the component assignment doesn't change that component,
// just omit the op entirely.
rop.turn_into_nop (op, "useless mxcompassign");
return 1;
}
// If the previous value of the matrix was a constant, and we're
// assigning a new constant to one component (and the index is
// also a constant), just turn it into an assignment of a new
// constant triple.
Matrix44 newval = *aa;
newval[jndex][index] = c;
int cind = rop.add_constant (AA->typespec(), &newval);