-
Notifications
You must be signed in to change notification settings - Fork 748
/
Copy pathIdiomRecognitionUtils.cpp
1565 lines (1437 loc) · 63.9 KB
/
IdiomRecognitionUtils.cpp
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 IBM Corp. and others 2000
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include "optimizer/IdiomRecognitionUtils.hpp"
#include <algorithm>
#include <stddef.h>
#include <stdint.h>
#include "codegen/CodeGenerator.hpp"
#include "env/FrontEnd.hpp"
#include "compile/Compilation.hpp"
#include "env/CompilerEnv.hpp"
#include "env/TRMemory.hpp"
#include "il/Block.hpp"
#include "il/DataTypes.hpp"
#include "il/ILOpCodes.hpp"
#include "il/ILOps.hpp"
#include "il/Node.hpp"
#include "il/Node_inlines.hpp"
#include "il/Symbol.hpp"
#include "il/SymbolReference.hpp"
#include "il/TreeTop.hpp"
#include "il/TreeTop_inlines.hpp"
#include "infra/Assert.hpp"
#include "infra/BitVector.hpp"
#include "infra/List.hpp"
#include "infra/TRCfgEdge.hpp"
#include "optimizer/IdiomRecognition.hpp"
#include "optimizer/Structure.hpp"
#include "optimizer/TranslateTable.hpp"
#include "optimizer/TransformUtil.hpp"
/************************************/
/************ Utilities *************/
/************************************/
void
dump256Bytes(uint8_t *t, TR::Compilation * comp)
{
int i;
traceMsg(comp, " | 0 1 2 3 4 5 6 7 8 9 A B C D E F\n");
traceMsg(comp, "--+--------------------------------");
for (i = 0; i < 256; i++)
{
if ((i % 16) == 0)
{
traceMsg(comp, "\n%02X|",i);
}
traceMsg(comp, "%2x",t[i]);
}
traceMsg(comp, "\n");
}
/******************************************************/
/************ Utilities for making idioms *************/
/******************************************************/
//*****************************************************************************************
// create the idiom for "v = src1 op val"
//*****************************************************************************************
TR_PCISCNode *
createIdiomIOP2VarInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, int opcode, TR_PCISCNode *v, TR_PCISCNode *src1, TR_PCISCNode *val)
{
TR_PCISCNode *n0, *n1;
TR_ASSERT(v->getOpcode() == TR_variable || v->getOpcode() == TR::iload, "Error!");
n0 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), opcode, v->getOpcode() == TR_variable ? TR::NoType : TR::Int32, tgt->incNumNodes(), dagId, 1, 2, pred); tgt->addNode(n0);
n1 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::istore, TR::Int32, tgt->incNumNodes(), dagId, 1, 2, n0); tgt->addNode(n1);
n0->setChildren(src1, val);
n1->setChildren(n0, v->getOpcode() == TR::iload ? v->getChild(0) : v);
n0->setIsChildDirectlyConnected();
n1->setIsChildDirectlyConnected();
n0->setIsSuccDirectlyConnected();
return n1;
}
//*****************************************************************************************
// create the idiom for "v = v op val"
//*****************************************************************************************
TR_PCISCNode *
createIdiomIOP2VarInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, int opcode, TR_PCISCNode *v, TR_PCISCNode *val)
{
return createIdiomIOP2VarInLoop(tgt, ctrl, dagId, pred, opcode, v, v, val);
}
//*****************************************************************************************
// create the idiom for "v = src1 - val"
//*****************************************************************************************
TR_PCISCNode *
createIdiomDecVarInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, TR_PCISCNode *v, TR_PCISCNode *src1, TR_PCISCNode *subval)
{
return createIdiomIOP2VarInLoop(tgt, ctrl, dagId, pred, TR::isub, v, src1, subval);
}
//*****************************************************************************************
// create the idiom for "v = src1 + val"
//*****************************************************************************************
TR_PCISCNode *
createIdiomIncVarInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, TR_PCISCNode *v, TR_PCISCNode *src1, TR_PCISCNode *addval)
{
return createIdiomIOP2VarInLoop(tgt, ctrl, dagId, pred, TR::iadd, v, src1, addval);
}
//*****************************************************************************************
// create the idiom for "v = v - val"
//*****************************************************************************************
TR_PCISCNode *
createIdiomDecVarInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, TR_PCISCNode *v, TR_PCISCNode *subval)
{
return createIdiomDecVarInLoop(tgt, ctrl, dagId, pred, v, v, subval);
}
//*****************************************************************************************
// create the idiom for "v = v + val"
//*****************************************************************************************
TR_PCISCNode *
createIdiomIncVarInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, TR_PCISCNode *v, TR_PCISCNode *addval)
{
return createIdiomIncVarInLoop(tgt, ctrl, dagId, pred, v, v, addval);
}
//*****************************************************************************************
// create "iconst val" or "lconst val". Use lconst for a 64-bit environment
//*****************************************************************************************
TR_PCISCNode *
createIdiomArrayRelatedConst(TR_PCISCGraph *tgt, int32_t ctrl, uint16_t id, int dagId, int32_t val)
{
TR_PCISCNode *ret;
uint32_t opcode = (ctrl & CISCUtilCtl_64Bit) ? TR::lconst : TR::iconst;
ret = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), opcode, opcode == TR::lconst ? TR::Int64 : TR::Int32, id, dagId, 0, 0, val);
tgt->addNode(ret);
return ret;
}
//*****************************************************************************************
// create "iconst -sizeof(array header)" or "lconst -sizeof(array header)".
// Use lconst for a 64-bit environment.
//*****************************************************************************************
TR_PCISCNode *
createIdiomArrayHeaderConst(TR_PCISCGraph *tgt, int32_t ctrl, uint16_t id, int dagId, TR::Compilation *c)
{
return createIdiomArrayRelatedConst(tgt, ctrl, id, dagId, -(int32_t)TR::Compiler->om.contiguousArrayHeaderSizeInBytes());
}
//*****************************************************************************************
// when the environment is under 64-bit, it'll insert "i2l" to the node.
//*****************************************************************************************
TR_PCISCNode *
createIdiomI2LIfNecessary(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode **pred, TR_PCISCNode *node)
{
TR_PCISCNode *ret = node;
if ((ctrl & (CISCUtilCtl_64Bit|CISCUtilCtl_NoI2L)) == (CISCUtilCtl_64Bit|0))
{
ret = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::i2l, TR::Int64, tgt->incNumNodes(), dagId, 1, 1, *pred, node); tgt->addNode(ret);
*pred = ret;
}
return ret;
}
#if 0
//*****************************************************************************************
// It creates an address tree of "index part" for a byte array. (e.g. index-header)
// It may insert I2L depending on the given flag.
//
// "InLoop" means that it sets the given dagId to all nodes in this subroutine.
//*****************************************************************************************
TR_PCISCNode *
createIdiomByteArrayAddressIndexTreeInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, TR_PCISCNode *index, TR_PCISCNode *cmah)
{
TR_PCISCNode *n0;
TR_PCISCNode *parentIndex;
if (ctrl & CISCUtilCtl_64Bit)
{
if (ctrl & CISCUtilCtl_NoI2L)
{
n0 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::lsub, TR::Int64, tgt->incNumNodes(), dagId, 1, 2, pred); tgt->addNode(n0);
parentIndex = n0;
}
else
{
parentIndex = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::i2l, TR::Int64, tgt->incNumNodes(), dagId, 1, 1, pred); tgt->addNode(parentIndex);
n0 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::lsub, TR::Int64, tgt->incNumNodes(), dagId, 1, 2, parentIndex); tgt->addNode(n0);
n0->setChild(parentIndex);
n0->setIsChildDirectlyConnected();
}
}
else
{
n0 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::isub, TR::Int32, tgt->incNumNodes(), dagId, 1, 2, pred); tgt->addNode(n0);
parentIndex = n0;
}
parentIndex->setChild(index);
if ((ctrl & CISCUtilCtl_ChildDirectConnected) || index->getOpcode() == TR_variable || index->getOpcode() == TR_arrayindex)
parentIndex->setIsChildDirectlyConnected();
n0->setChild(1, cmah);
return n0;
}
//*****************************************************************************************
// It creates an effective address tree for a byte array. (e.g. base+(index-header))
// It may insert I2L depending on the given flag.
//
// "InLoop" means that it sets the given dagId to all nodes in this subroutine.
//*****************************************************************************************
TR_PCISCNode *
createIdiomByteArrayAddressInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, TR_PCISCNode *base, TR_PCISCNode *index, TR_PCISCNode *cmah)
{
TR_PCISCNode *n0, *n1;
n0 = createIdiomByteArrayAddressIndexTreeInLoop(tgt, ctrl, dagId, pred, index, cmah);
n1 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), (ctrl & CISCUtilCtl_64Bit) ? TR::aladd : TR::aiadd, TR::Address, tgt->incNumNodes(), dagId, 1, 2, n0); tgt->addNode(n1);
n1->setChildren(base, n0);
if (base->getOpcode() == TR_variable || base->getOpcode() == TR_arraybase)
n1->setIsChildDirectlyConnected();
return n1;
}
#endif
//*****************************************************************************************
// It creates an address tree of "index part" for a non-byte array. (e.g. index*4-header)
// It may insert I2L depending on the given flag.
//
// "InLoop" means that it sets the given dagId to all nodes in this subroutine.
//*****************************************************************************************
TR_PCISCNode *
createIdiomArrayAddressIndexTreeInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, TR_PCISCNode *index, TR_PCISCNode *cmah, TR_PCISCNode *constForMul)
{
TR_PCISCNode *n0, *mul;
if (ctrl & CISCUtilCtl_64Bit)
{
TR_PCISCNode *i2l;
if (ctrl & CISCUtilCtl_NoI2L)
{
mul = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::lmul, TR::Int64, tgt->incNumNodes(), dagId, 1, 2, pred); tgt->addNode(mul);
i2l = mul;
}
else
{
i2l = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::i2l, TR::Int64, tgt->incNumNodes(), dagId, 1, 1, pred); tgt->addNode(i2l);
mul = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::lmul, TR::Int64, tgt->incNumNodes(), dagId, 1, 2, i2l); tgt->addNode(mul);
mul->setIsChildDirectlyConnected();
mul->setChild(i2l);
}
n0 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::lsub, TR::Int64, tgt->incNumNodes(), dagId, 1, 2, mul); tgt->addNode(n0);
i2l->setChild(index);
switch(index->getOpcode())
{
case TR_variable:
case TR_arrayindex:
i2l->setIsChildDirectlyConnected();
break;
}
n0->setIsChildDirectlyConnected();
}
else
{
mul = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::imul, TR::Int32, tgt->incNumNodes(), dagId, 1, 2, pred); tgt->addNode(mul);
n0 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::isub, TR::Int32, tgt->incNumNodes(), dagId, 1, 2, mul); tgt->addNode(n0);
mul->setChild(index);
n0->setIsChildDirectlyConnected();
switch(index->getOpcode())
{
case TR_variable:
case TR_arrayindex:
mul->setIsChildDirectlyConnected();
break;
}
}
mul->setChild(1, constForMul);
n0->setChildren(mul, cmah);
return n0;
}
//*****************************************************************************************
// It creates an effective address tree for a non-byte array. (e.g. base+(index*4-header))
// It may insert I2L depending on the given flag.
//
// "InLoop" means that it sets the given dagId to all nodes in this subroutine.
//*****************************************************************************************
TR_PCISCNode *
createIdiomArrayAddressInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, TR_PCISCNode *base, TR_PCISCNode *index, TR_PCISCNode *cmah, TR_PCISCNode *constForMul)
{
TR_PCISCNode *n0, *n1;
n0 = createIdiomArrayAddressIndexTreeInLoop(tgt, ctrl, dagId, pred, index, cmah, constForMul);
n1 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), (ctrl & CISCUtilCtl_64Bit) ? TR::aladd : TR::aiadd, TR::Address, tgt->incNumNodes(), dagId, 1, 2, n0); tgt->addNode(n1);
n1->setChildren(base, n0);
if (base->getOpcode() == TR_variable || base->getOpcode() == TR_arraybase)
n1->setIsChildDirectlyConnected();
return n1;
}
//*****************************************************************************************
// It creates an effective address tree for an array using the given index tree. (e.g. base+indexTree)
//
// "InLoop" means that it sets the given dagId to all nodes in this subroutine.
//*****************************************************************************************
TR_PCISCNode *
createIdiomArrayAddressInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, TR_PCISCNode *base, TR_PCISCNode *indexTree)
{
TR_PCISCNode *n1;
n1 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), (ctrl & CISCUtilCtl_64Bit) ? TR::aladd : TR::aiadd, TR::Address, tgt->incNumNodes(), dagId, 1, 2, pred); tgt->addNode(n1);
n1->setChildren(base, indexTree);
return n1;
}
#if 0
//*****************************************************************************************
// It creates a byte array load.
// "InLoop" means that it sets the given dagId to all nodes in this subroutine.
//*****************************************************************************************
TR_PCISCNode *
createIdiomByteArrayLoadInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, TR_PCISCNode *base, TR_PCISCNode *index, TR_PCISCNode *cmah)
{
TR_PCISCNode *n1, *n2;
n1 = createIdiomByteArrayAddressInLoop(tgt, ctrl, dagId, pred, base, index, cmah);
n2 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::bloadi, TR::Int8, tgt->incNumNodes(), dagId, 1, 1, n1); tgt->addNode(n2);
n2->setChild(n1);
n2->setIsChildDirectlyConnected();
return n2;
}
//*****************************************************************************************
// It creates a byte array store from the given address and storeval trees.
// "InLoop" means that it sets the given dagId to all nodes in this subroutine.
//*****************************************************************************************
TR_PCISCNode *
createIdiomByteArrayStoreBodyInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, TR_PCISCNode *addr, TR_PCISCNode *storeval)
{
TR_PCISCNode *n2, *i2b;
if (ctrl & CISCUtilCtl_NoConversion)
{
i2b = storeval;
}
else
{
i2b = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), (ctrl & CISCUtilCtl_AllConversion) ? TR_conversion : (TR_CISCOps)TR::i2b,
TR::Int8,
tgt->incNumNodes(), dagId, 1, 1, pred); tgt->addNode(i2b);
pred = i2b;
i2b->setChild(storeval);
}
n2 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::bstorei, TR::Int8, tgt->incNumNodes(), dagId, 1, 2, pred); tgt->addNode(n2);
n2->setChildren(addr, i2b);
return n2;
}
//*****************************************************************************************
// It creates a byte array store from the given base, index, cmah, and storeval trees.
// "InLoop" means that it sets the given dagId to all nodes in this subroutine.
//*****************************************************************************************
TR_PCISCNode *
createIdiomByteArrayStoreInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, TR_PCISCNode *base, TR_PCISCNode *index, TR_PCISCNode *cmah, TR_PCISCNode *storeval)
{
TR_PCISCNode *n1, *n2;
n1 = createIdiomByteArrayAddressInLoop(tgt, ctrl, dagId, pred, base, index, cmah);
n2 = createIdiomByteArrayStoreBodyInLoop(tgt, ctrl, dagId, n1, n1, storeval);
n2->setIsChildDirectlyConnected();
return n2;
}
#endif
//*****************************************************************************************
// It creates a char array load.
// "InLoop" means that it sets the given dagId to all nodes in this subroutine.
//*****************************************************************************************
TR_PCISCNode *
createIdiomCharArrayLoadInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, TR_PCISCNode *base, TR_PCISCNode *index, TR_PCISCNode *cmah, TR_PCISCNode *const2)
{
return createIdiomArrayLoadInLoop(tgt, ctrl, dagId, pred, TR::sloadi, TR::Int16, base, index, cmah, const2);
}
//*****************************************************************************************
// It creates a non-byte array load.
// "InLoop" means that it sets the given dagId to all nodes in this subroutine.
//*****************************************************************************************
TR_PCISCNode *
createIdiomArrayLoadInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, int opcode, TR::DataType dataType, TR_PCISCNode *base, TR_PCISCNode *index, TR_PCISCNode *cmah, TR_PCISCNode *mulconst)
{
TR_PCISCNode *n1, *n2;
n1 = createIdiomArrayAddressInLoop(tgt, ctrl, dagId, pred, base, index, cmah, mulconst);
n2 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), opcode, dataType, tgt->incNumNodes(), dagId, 1, 1, n1); tgt->addNode(n2);
n2->setChild(n1);
n2->setIsChildDirectlyConnected();
return n2;
}
//*****************************************************************************************
// It creates a non-byte array store from the given address and storeval trees.
// "InLoop" means that it sets the given dagId to all nodes in this subroutine.
//*****************************************************************************************
TR_PCISCNode *
createIdiomArrayStoreBodyInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, int opcode, TR::DataType dataType, TR_PCISCNode *addr, TR_PCISCNode *storeval)
{
TR_PCISCNode *n2, *i2c;
if (ctrl & CISCUtilCtl_NoConversion)
{
i2c = storeval;
}
else
{
i2c = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(),
(opcode == TR::sstorei) ? (TR_CISCOps)TR::i2s : TR_conversion,
(opcode == TR::sstorei) ? TR::Int16 : TR::NoType,
tgt->incNumNodes(), dagId, 1, 1, pred); tgt->addNode(i2c);
i2c->setIsOptionalNode();
pred = i2c;
i2c->setChild(storeval);
}
n2 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), opcode, dataType, tgt->incNumNodes(), dagId, 1, 2, pred); tgt->addNode(n2);
n2->setChildren(addr, i2c);
return n2;
}
//*****************************************************************************************
// It creates a char array store from the given address and storeval trees.
// "InLoop" means that it sets the given dagId to all nodes in this subroutine.
//*****************************************************************************************
TR_PCISCNode *
createIdiomCharArrayStoreBodyInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, TR_PCISCNode *addr, TR_PCISCNode *storeval)
{
return createIdiomArrayStoreBodyInLoop(tgt, ctrl, dagId, pred, TR::sstorei, TR::Int16, addr, storeval);
}
//*****************************************************************************************
// It creates a char array store from the given base, index, cmah, and storeval trees.
// "InLoop" means that it sets the given dagId to all nodes in this subroutine.
//*****************************************************************************************
TR_PCISCNode *
createIdiomArrayStoreInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, int opcode, TR::DataType dataType, TR_PCISCNode *base, TR_PCISCNode *index, TR_PCISCNode *cmah, TR_PCISCNode *const2, TR_PCISCNode *storeval)
{
TR_PCISCNode *n1, *n2;
n1 = createIdiomArrayAddressInLoop(tgt, ctrl, dagId, pred, base, index, cmah, const2);
n2 = createIdiomArrayStoreBodyInLoop(tgt, ctrl, dagId, n1, opcode, dataType, n1, storeval);
n2->setIsChildDirectlyConnected();
return n2;
}
//*****************************************************************************************
// It creates a char array store from the given base, index, cmah, and storeval trees.
// "InLoop" means that it sets the given dagId to all nodes in this subroutine.
//*****************************************************************************************
TR_PCISCNode *
createIdiomCharArrayStoreInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, TR_PCISCNode *base, TR_PCISCNode *index, TR_PCISCNode *cmah, TR_PCISCNode *const2, TR_PCISCNode *storeval)
{
return createIdiomArrayStoreInLoop(tgt, ctrl, dagId, pred, TR::sstorei, TR::Int16, base, index, cmah, const2, storeval);
}
//*****************************************************************************************
// It creates a byte array load particularly for "sun/io/CharToByteSingleByte.JITintrinsicConvert".
// The base address is given via java.nio.DirectByteBuffer.
//
// "InLoop" means that it sets the given dagId to all nodes in this subroutine.
//*****************************************************************************************
TR_PCISCNode *
createIdiomByteDirectArrayLoadInLoop(TR_PCISCGraph *tgt, int32_t ctrl, int dagId, TR_PCISCNode *pred, TR_PCISCNode *base, TR_PCISCNode *index)
{
TR_PCISCNode *n2;
TR_PCISCNode *n0;
TR_PCISCNode *parentIndex;
if (ctrl & CISCUtilCtl_64Bit)
{
if (ctrl & CISCUtilCtl_NoI2L)
{
n0 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::ladd, TR::Int64, tgt->incNumNodes(), dagId, 1, 2, pred); tgt->addNode(n0);
parentIndex = n0;
}
else
{
parentIndex = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::i2l, TR::Int64, tgt->incNumNodes(), dagId, 1, 1, pred); tgt->addNode(parentIndex);
n0 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::ladd, TR::Int64, tgt->incNumNodes(), dagId, 1, 2, parentIndex); tgt->addNode(n0);
n0->setChild(parentIndex);
n0->setIsChildDirectlyConnected();
}
parentIndex->setChild(index);
if ((ctrl & CISCUtilCtl_ChildDirectConnected) || index->getOpcode() == TR_variable || index->getOpcode() == TR_arrayindex)
parentIndex->setIsChildDirectlyConnected();
}
else
{
parentIndex = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::l2i, TR::Int32, tgt->incNumNodes(), dagId, 1, 1, pred); tgt->addNode(parentIndex);
parentIndex->setChild(base);
parentIndex->setIsChildDirectlyConnected();
base = parentIndex;
n0 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::iadd, TR::Int32, tgt->incNumNodes(), dagId, 1, 2, parentIndex); tgt->addNode(n0);
n0->setChild(index);
if ((ctrl & CISCUtilCtl_ChildDirectConnected) || index->getOpcode() == TR_variable || index->getOpcode() == TR_arrayindex)
n0->setIsChildDirectlyConnected();
}
n0->setChild(1, base);
n2 = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::bloadi, TR::Int8, tgt->incNumNodes(), dagId, 1, 1, n0); tgt->addNode(n2);
n2->setChild(n0);
n2->setIsChildDirectlyConnected();
return n2;
}
//*****************************************************************************************
// It creates the idiom for "divide by 10" or the corresponding multiplication idiom
// based on the given flag.
//
// "InLoop" means that it sets the given dagId to all nodes in this subroutine.
//*****************************************************************************************
TR_PCISCNode *
createIdiomIDiv10InLoop(TR_PCISCGraph *tgt, int32_t ctrl, bool isDiv2Mul, int dagId, TR_PCISCNode *pred, TR_PCISCNode *src1, TR_PCISCNode *src2, TR_PCISCNode *c2, TR_PCISCNode *c31)
{
TR_PCISCNode *ret;
if (isDiv2Mul)
{
TR_PCISCNode *nmul= new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::imulh, TR::Int32 ,tgt->incNumNodes(), 1, 1, 2, pred, src1, src2); tgt->addNode(nmul);
TR_PCISCNode *nshr= new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::ishr, TR::Int32 ,tgt->incNumNodes(), 1, 1, 2, nmul, nmul, c2); tgt->addNode(nshr);
TR_PCISCNode *ushr= new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::iushr, TR::Int32 ,tgt->incNumNodes(), 1, 1, 2, nshr, src1, c31); tgt->addNode(ushr); // optional
ret = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::iadd, TR::Int32 ,tgt->incNumNodes(), 1, 1, 2, ushr, nshr, ushr); tgt->addNode(ret); // optional
ushr->setIsOptionalNode();
ushr->setSkipParentsCheck();
ret->setIsOptionalNode();
}
else
{
ret = new (PERSISTENT_NEW) TR_PCISCNode(tgt->trMemory(), TR::idiv, TR::Int32 ,tgt->incNumNodes(), 1, 1, 2, pred, src1, src2); tgt->addNode(ret);
}
return ret;
}
//*****************************************************************************************
// Search the node "target" starting from "start" within this basic block
//*****************************************************************************************
bool
searchNodeInBlock(TR_CISCNode *start,
TR_CISCNode *target)
{
TR_CISCNode *t = start;
while (t->getNumSuccs() == 1 && t->getPreds()->isSingleton())
{
if (t == target) return true;
t = t->getSucc(0);
if (t == start) break; // maybe, this condition never happen.
}
return false;
}
//*****************************************************************************************
// Check if all successors of t will reach any node included in pBV.
//*****************************************************************************************
bool
checkSuccsSet(TR_CISCTransformer *const trans,
TR_CISCNode *t,
TR_BitVector *const pBV)
{
List<TR_CISCNode> *T2P = trans->getT2P();
TR_CISCNode *p;
while (t->getNumSuccs() == 1)
{
t = t->getSucc(0);
if (!t->isNegligible())
{
ListIterator<TR_CISCNode> li(T2P + t->getID());
for (p = li.getFirst(); p; p = li.getNext()) if (pBV->isSet(p->getID())) return true; // found the pattern node in pBV
return false; // cannot find the pattern node.
}
}
for (int i = t->getNumSuccs(); --i >= 0; )
{
TR_CISCNode *tn = t->getSucc(i);
if (!tn->isNegligible())
{
ListIterator<TR_CISCNode> li(T2P + tn->getID());
for (p = li.getFirst(); p; p = li.getNext()) if (pBV->isSet(p->getID())) break; // found the pattern node in pBV
if (!p) return false; // cannot find the pattern node.
}
else
{
if (!checkSuccsSet(trans, tn, pBV)) return false;
}
}
return true;
}
//*****************************************************************************************
// It searches an iload in the tree "q", and it returns the iload
//*****************************************************************************************
static TR_CISCNode *
searchIload(TR_CISCNode *q, bool allowArrayIndex)
{
while(true)
{
bool fail = false;
if (q->getOpcode() == TR::i2l)
{
q = q->getChild(0); // allow only TR::iload or TR_variable
fail = true;
}
if (q->getOpcode() == TR::iload || q->getOpcode() == TR_variable) return q;
if (allowArrayIndex && q->getOpcode() == TR_arrayindex) return q;
if (fail || q->getOpcode() == TR::lload || q->getNumChildren() < 1) return 0;
q = q->getChild(0);
}
return 0;
}
//*****************************************************************************************
// It searches an array load in the tree "top", and it returns the type of an array load,
// a base variable, and an index variable.
//*****************************************************************************************
bool
getThreeNodesForArray(TR_CISCNode *top, TR_CISCNode **ixloadORstore, TR_CISCNode **aload, TR_CISCNode **iload, bool allowArrayIndex)
{
TR_CISCNode *p;
// search ixloadORstore
p = top;
while(true)
{
if (p->getNumChildren() == 0) return false;
if (p->getIlOpCode().isLoadIndirect() || p->getIlOpCode().isStoreIndirect() ||
p->getOpcode() == TR_inbload || p->getOpcode() == TR_inbstore ||
p->getOpcode() == TR_indload || p->getOpcode() == TR_indstore ||
p->getOpcode() == TR_ibcload || p->getOpcode() == TR_ibcstore)
{
*ixloadORstore = p;
break;
}
p = p->getChild(0);
}
p = p->getChild(0);
TR_CISCNode *q;
switch(p->getOpcode())
{
case TR::aladd:
case TR::aiadd:
// search aload
q = p->getChild(0);
while(true)
{
if (q->getOpcode() == TR::aload || q->getOpcode() == TR_variable || q->getOpcode() == TR_arraybase)
{
*aload = q;
break;
}
if (q->getNumChildren() != 1) return false;
q = q->getChild(0);
}
// search iload
if ((q = searchIload(p->getChild(1), allowArrayIndex)) == 0) return false;
*iload = q;
break;
case TR::ladd:
case TR::iadd:
// search iload
if ((q = searchIload(p->getChild(1), allowArrayIndex)))
{
*iload = q;
q = p->getChild(0);
}
else if ((q = searchIload(p->getChild(0), allowArrayIndex)))
{
*iload = q;
q = p->getChild(1);
}
else
{
return false;
}
// search aload
while(true)
{
if (q->getOpcode() == TR::lload || q->getOpcode() == TR_variable)
{
*aload = q;
break;
}
if (q->getOpcode() == TR::iload || q->getNumChildren() != 1) return false;
q = q->getChild(0);
}
break;
default:
return false;
}
return true;
}
//*****************************************************************************************
// It returns isDcrement and modification length by analyzing if-opcode
//*****************************************************************************************
bool
testExitIF(int opcode, bool *isDecrement, int32_t *modLength, int32_t *modStartIdx)
{
switch(opcode)
{
case TR::ificmplt:
if (isDecrement) *isDecrement = true;
if (modLength) *modLength = 1;
if (modStartIdx) *modStartIdx = 0;
return true;
case TR::ificmple:
if (isDecrement) *isDecrement = true;
if (modLength) *modLength = 0;
if (modStartIdx) *modStartIdx = 1;
return true;
case TR::ificmpgt:
if (isDecrement) *isDecrement = false;
if (modLength) *modLength = 1;
if (modStartIdx) *modStartIdx = 0;
return true;
case TR::ificmpge:
if (isDecrement) *isDecrement = false;
if (modLength) *modLength = 0;
if (modStartIdx) *modStartIdx = 0;
return true;
}
return false;
}
/********************************************************************/
/************ Utilities for analyzing or making TR::Node *************/
/********************************************************************/
//*****************************************************************************************
// It returns the n'th child is "iconst value".
//*****************************************************************************************
bool
testIConst(TR_CISCNode *n, int idx, int32_t value)
{
TR_CISCNode *ch = n->getChild(idx);
return (ch->getOpcode() == TR::iconst && ch->getOtherInfo() == value);
}
//*****************************************************************************************
// It inserts i2l based on the given flag.
//*****************************************************************************************
TR::Node*
createI2LIfNecessary(TR::Compilation *comp, bool is64bit, TR::Node *child)
{
return is64bit ? TR::Node::create(TR::i2l, 1, child) : child;
}
//*****************************************************************************************
// It converts from store to load.
//*****************************************************************************************
TR::Node*
createLoad(TR::Node *baseNode)
{
return baseNode->getOpCode().isStoreDirect() ? TR::Node::createLoad(baseNode, baseNode->getSymbolReference()) :
baseNode->duplicateTree();
}
//*****************************************************************************************
// It creates a load instruction, and it inserts i2l based on the given flag.
//*****************************************************************************************
TR::Node*
createLoadWithI2LIfNecessary(TR::Compilation *comp, bool is64bit, TR::Node *indexNode)
{
TR_ASSERT(indexNode->getOpCode().hasSymbolReference(), "parameter error");
TR::Node *iload = createLoad(indexNode);
return createI2LIfNecessary(comp, is64bit, iload);
}
//*****************************************************************************************
// It creates a node of the constant value of the array header size.
//*****************************************************************************************
TR::Node*
createArrayHeaderConst(TR::Compilation *comp, bool is64bit, TR::Node *baseNode)
{
TR::Node *c2;
if (is64bit)
{
c2 = TR::Node::create(baseNode, TR::lconst, 0);
c2->setLongInt(-(int32_t)TR::Compiler->om.contiguousArrayHeaderSizeInBytes());
}
else
{
c2 = TR::Node::create(baseNode, TR::iconst, 0, -(int32_t)TR::Compiler->om.contiguousArrayHeaderSizeInBytes());
}
return c2;
}
//*****************************************************************************************
// It creates an effective address tree for the top of the given array.
//*****************************************************************************************
TR::Node*
createArrayTopAddressTree(TR::Compilation *comp, bool is64bit, TR::Node *baseNode)
{
TR::Node *aload = createLoad(baseNode);
return TR::TransformUtil::generateFirstArrayElementAddressTrees(comp, aload);
}
//*****************************************************************************************
// It converts from store to load, and it inserts i2l based on the given flag.
//*****************************************************************************************
TR::Node*
convertStoreToLoadWithI2LIfNecessary(TR::Compilation *comp, bool is64bit, TR::Node *indexNode)
{
return (indexNode->getOpCode().isStoreDirect()) ?
createLoadWithI2LIfNecessary(comp, is64bit, indexNode) :
createI2LIfNecessary(comp, is64bit, indexNode->getReferenceCount() ?
indexNode->duplicateTree() : indexNode);
}
//*****************************************************************************************
// It converts from store to load.
//*****************************************************************************************
TR::Node*
convertStoreToLoad(TR::Compilation *comp, TR::Node *indexNode)
{
return (indexNode->getOpCode().isStoreDirect()) ?
TR::Node::createLoad(indexNode, indexNode->getSymbolReference()) :
indexNode->getReferenceCount() ? indexNode->duplicateTree() : indexNode;
}
//*****************************************************************************************
// It creates the byte size from an element size. (i.e. ret = index * multiply)
//*****************************************************************************************
TR::Node*
createBytesFromElement(TR::Compilation *comp, bool is64bit, TR::Node *indexNode, int multiply)
{
TR::Node *top, *c2;
top = convertStoreToLoadWithI2LIfNecessary(comp, is64bit, indexNode);
if (is64bit)
{
if (multiply > 1)
{
c2 = TR::Node::create(indexNode, TR::lconst, 0); // c2 is used temporary
c2->setLongInt(multiply);
top = TR::Node::create(TR::lmul, 2, top, c2);
}
}
else
{
if (multiply > 1)
{
c2 = TR::Node::create(indexNode, TR::iconst, 0, multiply); // c2 is used temporary
top = TR::Node::create(TR::imul, 2, top, c2);
}
}
return top;
}
//*****************************************************************************************
// It creates an index address tree for the given indexNode and size.
//*****************************************************************************************
TR::Node*
createIndexOffsetTree(TR::Compilation *comp, bool is64bit, TR::Node *indexNode, int multiply)
{
TR::Node *c1 = createBytesFromElement(comp, is64bit, indexNode, multiply);
return TR::TransformUtil::generateConvertArrayElementIndexToOffsetTrees(comp, c1, NULL, multiply);
}
//*****************************************************************************************
// It creates an effective address tree for the given baseNode, indexNode and size.
//*****************************************************************************************
TR::Node*
createArrayAddressTree(TR::Compilation *comp, bool is64bit, TR::Node *baseNode, TR::Node *indexNode, int multiply)
{
if (indexNode->getOpCodeValue() == TR::iconst && indexNode->getInt() == 0)
{
return createArrayTopAddressTree(comp, is64bit, baseNode);
}
else
{
TR::Node *c2 = createIndexOffsetTree(comp, is64bit, indexNode, multiply);
TR::Node *aload = createLoad(baseNode);
return TR::TransformUtil::generateArrayElementAddressTrees(comp, aload, c2);
}
}
//*****************************************************************************************
// It creates an array load tree for the given load type, baseNode, indexNode and size.
//*****************************************************************************************
TR::Node*
createArrayLoad(TR::Compilation *comp, bool is64bit, TR::Node *ixload, TR::Node *baseNode, TR::Node *indexNode, int multiply)
{
TR::Node *top, *c1;
if (comp->useCompressedPointers() &&
(ixload->getDataType() == TR::Address))
multiply = multiply >> 1;
c1 = createArrayAddressTree(comp, is64bit, baseNode, indexNode, multiply);
TR::SymbolReference *symref=ixload->getSymbolReference();
top = TR::Node::createWithSymRef(ixload, ixload->getOpCodeValue(), 1, symref);
top->setAndIncChild(0, c1);
return top;
}
//*****************************************************************************************
// It replaces the array index in the address tree.
//*****************************************************************************************
TR::Node *
replaceIndexInAddressTree(TR::Compilation *comp, TR::Node *tree, TR::SymbolReference *symRef, TR::Node *newNode)
{
TR::Node *orgTree = tree;
if (tree->getOpCode().isIndirect()) tree = tree->getChild(0);
if (tree->getOpCode().isAdd())
{
tree = tree->getChild(1);
while(true)
{
TR::Node *parent = tree;
if (tree->getOpCodeValue() == TR::iadd)
{
TR::Node *ch1 = tree->getChild(1);
if (ch1->getOpCodeValue() == TR::iload)
{
if (ch1->getSymbolReference() == symRef)
{
parent->getAndDecChild(1);
parent->setAndIncChild(1, newNode);
return orgTree;
}
}
}
tree = tree->getChild(0);
if (!tree) break;
if (tree->getOpCodeValue() == TR::iload)
{
if (tree->getSymbolReference() == symRef)
{
parent->getAndDecChild(0);
parent->setAndIncChild(0, newNode);
return orgTree;
}
else
{
return NULL;
}
}
}
}
return NULL;
}
//*****************************************************************************************
// It modifies the array header constant in the given tree
//*****************************************************************************************
TR::Node *
modifyArrayHeaderConst(TR::Compilation *comp, TR::Node *tree, int32_t offset)
{
if (offset == 0) return tree;
if (!tree->getOpCode().isAdd()) tree = tree->getFirstChild();
if (tree->getOpCodeValue() == TR::aiadd || tree->getOpCodeValue() == TR::aladd)
{
TR::Node *addOrSubNode = tree->getSecondChild();
TR::Node *constLoad = addOrSubNode->getSecondChild();
if (addOrSubNode->getOpCode().isSub())
{
offset = -offset;
}
else if (!addOrSubNode->getOpCode().isAdd())