-
Notifications
You must be signed in to change notification settings - Fork 738
/
Copy pathIdiomTransformations.cpp
11385 lines (10234 loc) · 565 KB
/
IdiomTransformations.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 <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#include "codegen/CodeGenerator.hpp"
#include "env/FrontEnd.hpp"
#include "compile/Compilation.hpp"
#include "compile/SymbolReferenceTable.hpp"
#include "control/Options.hpp"
#include "control/Options_inlines.hpp"
#include "cs2/bitvectr.h"
#include "env/CompilerEnv.hpp"
#include "env/TRMemory.hpp"
#include "env/jittypes.h"
#include "il/AutomaticSymbol.hpp"
#include "il/Block.hpp"
#include "il/DataTypes.hpp"
#include "il/ILOpCodes.hpp"
#include "il/ILOps.hpp"
#include "il/ILProps.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/Cfg.hpp"
#include "infra/List.hpp"
#include "optimizer/IdiomRecognition.hpp"
#include "optimizer/IdiomRecognitionUtils.hpp"
#include "optimizer/Optimization_inlines.hpp"
#include "optimizer/Optimizer.hpp"
#include "optimizer/Structure.hpp"
#include "optimizer/UseDefInfo.hpp"
#include "ras/Debug.hpp"
#define OPT_DETAILS "O^O NEWLOOPREDUCER: "
#define DISPTRACE(OBJ) ((OBJ)->trace())
#define VERBOSE(OBJ) ((OBJ)->showMesssagesStdout())
#define PNEW new (PERSISTENT_NEW)
/** \brief
* Determines whether we should avoid transforming loops in java/lang/String due to functional issues when String
* compression is enabled.
*
* \param comp
* The compilation object.
*
* \return
* <c>true</c> if the transformation should be avoided, <c>false</c> otherwise.
*/
static bool avoidTransformingStringLoops(TR::Compilation* comp)
{
static bool cacheInitialized = false;
static bool cacheValue = false;
if (!cacheInitialized)
{
// TODO: This is a workaround for Java 829 functionality as we switched to using a byte[] backing array in String*.
// Remove this workaround once obsolete. Idiom recognition currently does not handle idioms involving char[] in a
// compressed string. String compression is technically a Java 9 feature, but for the sake of evaluating performance
// we need to be able to run our standard set of benchmarks, most of which do not work under Java 9 at the moment.
// This leaves us with the only option to run the respective benchmarks on Java 8 SR5, however in Java 8 SR5 the
// java/lang/String.value is of type char[] which will cause functional problems. To avoid these issues we will
// disable idiom recognition on Java 8 SR5 if String compression is enabled.
TR_OpaqueClassBlock* stringClass = comp->cg()->fej9()->getSystemClassFromClassName("java/lang/String", strlen("java/lang/String"), true);
if (stringClass != NULL)
{
// Only initialize the cache after we are certain java/lang/String has been resolved
cacheInitialized = true;
if (comp->cg()->fej9()->getInstanceFieldOffset(stringClass, "value", "[C") != ~0)
{
cacheValue = IS_STRING_COMPRESSION_ENABLED_VM(static_cast<TR_J9VMBase*>(comp->fe())->getJ9JITConfig()->javaVM);
}
}
}
return cacheValue;
}
//*****************************************************************************************
// It partially peels the loop body to align the top of the region
//*****************************************************************************************
bool
ChangeAlignmentOfRegion(TR_CISCTransformer *trans)
{
const bool disptrace = DISPTRACE(trans);
TR_CISCGraph *P = trans->getP();
TR_CISCGraph *T = trans->getT();
TR_CISCNode *pTop = P->getEntryNode()->getSucc(0);
TR_CISCNode *t;
TR_CISCNode *beforeLoop = NULL;
bool changed = false;
TR::Compilation * comp = trans->comp();
// Find actual pTop. Skip an optional node if there is no corresponding target node.
while (trans->getP2TRep(pTop) == NULL)
{
if (!pTop->isOptionalNode()) return changed;
pTop = pTop->getSucc(0);
}
// Try to find pTop in the predecessors of the loop body
for (t = T->getEntryNode(); t->isOutsideOfLoop();)
{
if (trans->analyzeT2P(t, pTop) & _T2P_MatchMask)
{
TR_CISCNode *chk;
for (chk = t->getSucc(0); chk->isOutsideOfLoop(); chk=chk->getSucc(0))
{
if (!chk->isNegligible() && trans->analyzeT2P(chk) == _T2P_NULL) break; // t is still invalid.
}
if (!chk->isOutsideOfLoop())
{
if (disptrace) traceMsg(comp, "ChangeAlignmentOfRegion : (t:%d p:%d) no need to change alignment\n",t->getID(),pTop->getID());
return changed; // Find pTop! Already aligned correctly
}
}
if (t->getNumSuccs() < 1)
{
if (disptrace) traceMsg(comp,"ChangeAlignmentOfRegion : #succs of tID:%d is 0\n", t->getID());
return changed; // cannot find either a loop body or pTop in the fallthrough path
}
beforeLoop = t;
switch(t->getOpcode())
{
case TR::lookup:
case TR::table:
int i;
for (i = t->getNumSuccs(); --i >= 0; )
{
TR_CISCNode *next_t = t->getSucc(i);
if (next_t->getOpcode() == TR::Case &&
next_t->getSucc(0) != T->getExitNode())
{
t = next_t->getSucc(0);
goto exit_switch;
}
}
// fall through
default:
t = t->getSucc(0);
break;
}
exit_switch:;
}
TR_ASSERT(beforeLoop, "error");
if (t->getOpcode() != TR::BBStart) return changed; // already aligned by this transformation before
t = t->getSucc(0); // Skip BBStart
int condT2P = trans->analyzeT2P(t, pTop);
if (condT2P & _T2P_MatchMask) return changed; // no need to change alignment
if (disptrace) traceMsg(comp,"ChangeAlignmentOfRegion : tTop %d, pTop %d\n",t->getID(),pTop->getID());
TR_CISCNodeRegion r(T->getNumNodes(), trans->trMemory()->heapMemoryRegion());
TR_CISCNode *firstNode = t;
TR_CISCNode *lastNode = NULL;
// Find the target node corresponding to pTop
int branchCount = 0;
for (;;)
{
if (condT2P != _T2P_NULL || !t->isNegligible()) lastNode = t;
t = t->getSucc(0);
if (t->getOpcode() == TR::BBEnd || t->getOpcode() == TR_exitnode || t == firstNode) return changed; // current limitation. peeling can be performed within the first BB of the body
if (t->getIlOpCode().isBranch())
if (++branchCount >= 2) return changed; // allow a single branch
condT2P = trans->analyzeT2P(t, pTop);
if (condT2P & _T2P_MatchMask)
break; // the target node corresponding to pTop is found
}
if (!lastNode) return changed; // the last node of the peeling region
TR_CISCNode *foundNode = lastNode->getSucc(0);
// Find the last non-negligible node
if (lastNode->isNegligible())
{
TR_CISCNode *lastNonNegligble = NULL;
for (t = firstNode; ;t = t->getSucc(0))
{
if (!t->isNegligible()) lastNonNegligble = t;
if (t == lastNode) break;
}
if (!lastNonNegligble) return changed;
lastNode = lastNonNegligble;
}
// Add nodes from firstNode to lastNode into the region r
if (disptrace) traceMsg(comp, "ChangeAlignmentOfRegion : foundNode %d, lastNode %d\n",foundNode->getID(),lastNode->getID());
if (branchCount > 0 &&
!lastNode->getIlOpCode().isBranch())
{
if (disptrace) traceMsg(comp, "Fail: there is a branch in the region. lastNode must be a branch node.\n");
return changed;
}
for (t = firstNode; ;t = t->getSucc(0))
{
r.append(t);
if (t == lastNode) break;
}
// analyze that all parents of every node in the region r are included in the region r.
ListIterator<TR_CISCNode> ri(&r);
for (t = ri.getFirst(); t; t = ri.getNext()) // each node in the region r
{
if (t->getOpcode() == TR::aload || t->getOpcode() == TR::iload)
{
bool noDefInR = true;
ListIterator<TR_CISCNode> chain(t->getChains());
TR_CISCNode *def;
for (def = chain.getFirst(); def; def = chain.getNext())
{
if (r.isIncluded(def))
{
noDefInR = false;
break;
}
}
if (noDefInR) continue; // If there is no def in r, it ignores this load node.
}
ListIterator<TR_CISCNode> pi(t->getParents());
TR_CISCNode *pn;
for (pn = pi.getFirst(); pn; pn = pi.getNext()) // each parent of t
{
if (!r.isIncluded(pn))
{
if (disptrace) traceMsg(comp,"ChangeAlignmentOfRegion : There is a parent(%d) of %d in the outside of the region\n", pn->getID(), t->getID());
return changed; // fail
}
}
}
/////////////////////////////
// From here, success path //
/////////////////////////////
T->duplicateListsDuplicator();
changed = true;
TR_CISCNode *from = r.getListHead()->getData();
TR_CISCNode *to = r.getListTail()->getData();
if (disptrace)
{
traceMsg(comp,"ChangeAlignmentOfRegion: Succ[0] of %d will be changed from %d to %d.\n",
beforeLoop->getID(),
beforeLoop->getSucc(0)->getID(),
foundNode->getID());
traceMsg(comp,"\tNodes from %d to %d will be added to BeforeInsertionList.\n",
from->getID(),to->getID());
}
TR_ASSERT(r.getListTail()->getData()->getIlOpCode().isTreeTop(), "error");
beforeLoop->replaceSucc(0, foundNode); // replace the loop entry with foundNode
TR_NodeDuplicator duplicator(comp);
for (t = ri.getFirst(); t; t = ri.getNext())
{
if (t->getIlOpCode().isTreeTop())
{
TR::Node *rep = t->getHeadOfTrNodeInfo()->_node;
if (disptrace)
{
traceMsg(comp,"add TR::Node 0x%p (tid:%d) to BeforeInsertionList.\n", rep, t->getID());
}
rep = duplicator.duplicateTree(rep);
if (t->getIlOpCode().isIf())
{
if (t->getOpcode() != rep->getOpCodeValue())
{
TR::TreeTop *ret;
for (ret = t->getHeadOfTreeTop()->getNextTreeTop();
ret->getNode()->getOpCodeValue() != TR::BBStart;
ret = ret->getNextTreeTop());
TR::Node::recreate(rep, (TR::ILOpCodes)t->getOpcode());
rep->setBranchDestination(ret);
}
}
trans->getBeforeInsertionList()->append(rep);
}
}
// Move the region ("from" - "to") to the last
trans->moveCISCNodes(from, to, NULL);
if (disptrace && changed)
{
traceMsg(comp,"After ChangeAlignmentOfRegion\n");
T->dump(comp->getOutFile(), comp);
}
return changed;
}
//*****************************************************************************************
// Analyze whether we can move the node n to immediately before the nodes in tgt.
// Both the node n and a node in tgt must be included in the list l.
// If the analysis fails, it will return NULL.
// Otherwise, it will return the target node, which must be included in the list tgt.
//*****************************************************************************************
TR_CISCNode *
analyzeMoveNodeForward(TR_CISCTransformer *trans, List<TR_CISCNode> *l, TR_CISCNode *n, List<TR_CISCNode> *tgt)
{
const bool disptrace = DISPTRACE(trans);
ListIterator<TR_CISCNode> ti(l);
TR_CISCNode *t;
TR_CISCNode *ret = NULL;
TR::Compilation * comp = trans->comp();
for (t = ti.getFirst(); t; t = ti.getNext())
{
if (t == n) break;
}
TR_ASSERT(t != NULL, "cannot find the node n in the list l!");
t = ti.getNext();
TR_ASSERT(t != NULL, "cannot find any node in tgt in the list l!");
if (tgt->find(t)) return NULL; // already moved
bool go = false;
if (n->isStoreDirect())
{
go = true;
}
else if (n->getNumChildren() == 2)
{
if (n->getIlOpCode().isAdd() ||
n->getIlOpCode().isSub() ||
n->getIlOpCode().isMul() ||
n->getIlOpCode().isLeftShift() ||
n->getIlOpCode().isRightShift() ||
n->getIlOpCode().isShiftLogical() ||
n->getIlOpCode().isAnd() ||
n->getIlOpCode().isOr() ||
n->getIlOpCode().isXor()) // Safe expressions
{
go = true;
if (n->getChild(0)->getOpcode() == TR_variable ||
n->getChild(1)->getOpcode() == TR_variable)
go = false; // not implemented yet.
}
}
else if (n->getNumChildren() == 1)
{
if (n->getIlOpCode().isConversion() ||
n->getIlOpCode().isNeg()) // Safe expressions
{
go = true;
if (n->getChild(0)->getOpcode() == TR_variable)
go = false; // not implemented yet.
}
}
else
{
if (n->getIlOpCode().isLoadConst())
{
go = true;
}
}
if (go)
{
List<TR_CISCNode> *chains = n->getChains();
List<TR_CISCNode> *parents = n->getParents();
TR_CISCNode *specialCareIf = trans->getP()->getSpecialCareNode(0);
bool generateCompensation0 = false;
while(true)
{
if (chains->find(t)) break; // it cannot be moved beyond its use/def.
if (parents->find(t)) break; // it cannot be moved beyond its parent.
if (t->getOpcode() == TR::BBStart)
{
TR::Block *block = t->getHeadOfTrNode()->getBlock();
if (block->getPredecessors().size() > 1) return NULL; // It currently analyzes within this BB.
}
if (t->getNumSuccs() >= 2 && specialCareIf)
{
bool fail = true;
TR_CISCNode *p = trans->getT2Phead(t);
if (p &&
p == specialCareIf &&
t->getSucc(1) == trans->getT()->getExitNode())
{
// add compensation code into AfterInsertionIdiomList and go ahead
TR::Node *trNode = n->getHeadOfTrNode();
if (trNode->getOpCode().isTreeTop())
{
if (trNode->getOpCode().isStoreDirect())
{
if (!generateCompensation0)
{
trans->getT()->duplicateListsDuplicator();
if (disptrace) traceMsg(comp,"analyzeMoveNodeForward: append the tree of 0x%p into AfterInsertionIdiomList\n", trNode);
trans->getAfterInsertionIdiomList(0)->append(trNode->duplicateTree());
}
fail = false;
generateCompensation0 = true;
}
// else, fail to move
}
else
{
fail = false;
}
}
if (fail) break; // It currently analyzes within this BB.
}
t = ti.getNext();
if (t == NULL) break; // cannot find any node in tgt in the list l.
ret = t;
if (tgt->find(t)) break; // find goal!
}
}
return ret;
}
//*****************************************************************************************
// It tries to reorder target nodes to match idiom nodes within each BB.
//*****************************************************************************************
bool
reorderTargetNodesInBB(TR_CISCTransformer *trans)
{
TR_CISCGraph *P = trans->getP();
TR_CISCGraph *T = trans->getT();
List<TR_CISCNode> *T2P = trans->getT2P(), *P2T = trans->getP2T(), *l;
TR_CISCNode *t, *p;
bool changed = false;
const bool disptrace = DISPTRACE(trans);
TR::Compilation * comp = trans->comp();
static int enable = -1;
if (enable < 0)
{
char *p = feGetEnv("DISABLE_REORDER");
enable = p ? 0 : 1;
}
if (!enable) return false;
TR_BitVector visited(T->getNumNodes(), comp->trMemory());
while(true)
{
ListIterator<TR_CISCNode> ti(T->getNodes());
int currentPID = 0x10000;
bool anyChanged = false;
for (t = ti.getFirst(); t; t = ti.getNext())
{
int tID = t->getID();
if (visited.isSet(tID)) continue;
visited.set(tID);
l = T2P + tID;
if (l->isEmpty()) // There is no idiom nodes corresponding to the node t
{
if (t->isNegligible())
{
continue; // skip the node t
}
else
{
break; // finish this analysis
}
}
int maxPid = -1;
ListIterator<TR_CISCNode> pi(l);
for (p = pi.getFirst(); p; p = pi.getNext())
{
if (p->getID() > maxPid) maxPid = p->getID();
}
if (maxPid >= 0)
{
if (maxPid <= currentPID)
{
currentPID = maxPid; // no problem
}
else
{
if (t->isOutsideOfLoop()) break; // reordering is currently supported only inside of the loop
// Try moving the node t forward
List<TR_CISCNode> *nextPlist = P2T+maxPid+1;
if (disptrace)
{
ListIterator<TR_CISCNode> nextTi(nextPlist);
TR_CISCNode *nextT;
traceMsg(comp,"reorderTargetNodesInBB: Try moving the tgt node %d forward until",tID);
for (nextT = nextTi.getFirst(); nextT; nextT = nextTi.getNext())
{
traceMsg(comp," %p(%d)",nextT,nextT->getID());
}
traceMsg(comp,"\n");
}
// Analyze whether we can move the node t to immediately before the nodes in nextPlist
List<TR_CISCNode> *dagList = T->getDagId2Nodes()+t->getDagID();
TR_CISCNode *tgt = analyzeMoveNodeForward(trans, dagList, t, nextPlist);
if (tgt)
{
T->duplicateListsDuplicator();
// OK, we can move the node t!
if (disptrace) traceMsg(comp,"We can move the node %d to %p(%d)\n",tID,tgt,tgt->getID());
anyChanged = changed = true;
trans->moveCISCNodes(t, t, tgt, "reorderTargetNodesInBB");
break;
}
}
}
}
if (!anyChanged) break;
}
if (disptrace && changed)
{
traceMsg(comp,"After reorderTargetNodesInBB\n");
T->dump(comp->getOutFile(), comp);
}
return changed;
}
//*****************************************************************************************
// It replicates a store instruction outside of the loop.
// It is specialized to those idioms that include TR_booltable
// Input: SpecialCareNode(0) - the TR_booltable in the idiom
// ImportantNode(1) - ificmpge for exiting the loop (optional)
//*****************************************************************************************
bool
moveStoreOutOfLoopForward(TR_CISCTransformer *trans)
{
TR_CISCGraph *P = trans->getP();
List<TR_CISCNode> *P2T = trans->getP2T();
TR_CISCNode *ixload, *aload, *iload;
TR::Compilation *comp = trans->comp();
TR_CISCNode *boolTable = P->getSpecialCareNode(0); // Note: The opcode isn't always TR_booltable.
TR_CISCNode *p = boolTable->getChild(0); // just before TR_booltable, such as b2i
TR_BitVector findBV(P->getNumNodes(), trans->trMemory(), stackAlloc);
findBV.set(boolTable->getID());
TR_CISCNode *optionalCmp = P->getImportantNode(1); // ificmpge
if (optionalCmp && (optionalCmp->getOpcode() == TR::ificmpge || optionalCmp->getOpcode() == TR_ifcmpall))
findBV.set(optionalCmp->getID());
ListIterator<TR_CISCNode> ti(P2T + p->getID());
TR_CISCNode *t;
TR_CISCNode *storedVariable = NULL;
bool success0 = false;
TR_ScratchList<TR_CISCNode> targetList(comp->trMemory());
for (t = ti.getFirst(); t; t = ti.getNext()) // for each target node corresponding to p
{
// t is a target node corresponding to p (just before TR_booltable)
ListIterator<TR_CISCNode> tParentIter(t->getParents());
TR_CISCNode *tParent;
for (tParent = tParentIter.getFirst(); tParent; tParent = tParentIter.getNext())
{
// checking whether tParent is a store instruction
if (tParent->isStoreDirect() &&
!tParent->isNegligible())
{
// checking whether all variables of stores are same.
if (!storedVariable) storedVariable = tParent->getChild(1);
else if (storedVariable != tParent->getChild(1))
{
if (DISPTRACE(trans)) traceMsg(comp, "moveStoreOutOfLoopForward failed because all variables of stores are not same.\n");
success0 = false;
goto endSpecial0; // FAIL!
}
// checking whether tParent will reach either boolTable or optionalCmp
if (checkSuccsSet(trans, tParent, &findBV))
{
success0 = true; // success for this t
break;
}
else
{
if (DISPTRACE(trans)) traceMsg(comp, "moveStoreOutOfLoopForward failed because tParent will not reach either boolTable or optionalCmp.\n");
success0 = false;
goto endSpecial0; // FAIL!
}
}
}
if (tParent) targetList.add(tParent); // add a store instruction
}
endSpecial0:
if (targetList.isEmpty())
{
if (DISPTRACE(trans)) traceMsg(comp, "moveStoreOutOfLoopForward failed because targetList is empty.\n");
success0 = false;
}
// check if descendants of p include an array load
if (!getThreeNodesForArray(p, &ixload, &aload, &iload, true))
{
if (DISPTRACE(trans)) traceMsg(comp, "moveStoreOutOfLoopForward failed because decendents of pid:%d don't include an array load.\n", p->getID());
success0 = false;
}
if (success0)
{
ixload = trans->getP2TRep(ixload);
aload = trans->getP2TRep(aload);
iload = trans->getP2TRep(iload);
if (DISPTRACE(trans)) traceMsg(comp, "moveStoreOutOfLoopForward: Target nodes ixload=%d, aload=%d, iload=%d\n",
ixload ? ixload->getID() : -1, aload ? aload->getID() : -1, iload ? iload->getID() : -1);
trans->getT()->duplicateListsDuplicator();
if (ixload && aload && iload && (iload->isLoadVarDirect() || iload->getOpcode() == TR_variable))
{
TR::Node *store;
TR::Node *conv;
TR::Node *storeDup0;
TR::Node *storeDup1;
TR::Node *convDup;
TR::Node *ixloadNode = ixload->getHeadOfTrNodeInfo()->_node;
TR::Node *iloadNode = iload->getHeadOfTrNodeInfo()->_node; // index
TR::Node *iloadm1Node = createOP2(comp, TR::isub,
TR::Node::createLoad(iloadNode, iloadNode->getSymbolReference()),
TR::Node::create(iloadNode, TR::iconst, 0, 1));
// prepare base[index]
TR::Node *arrayLoad0 = createArrayLoad(comp, trans->isGenerateI2L(),
ixloadNode,
aload->getHeadOfTrNodeInfo()->_node,
iloadNode,
ixloadNode->getSize());
// prepare base[index-1] (it may not be used.)
TR::Node *arrayLoad1 = createArrayLoad(comp, trans->isGenerateI2L(),
ixloadNode,
aload->getHeadOfTrNodeInfo()->_node,
iloadm1Node,
ixloadNode->getSize());
ti.set(&targetList);
t = ti.getFirst();
store = t->getHeadOfTrNodeInfo()->_node;
conv = store->getChild(0);
if (conv->getOpCode().isConversion())
{
convDup = TR::Node::create(conv->getOpCodeValue(), 1, arrayLoad0);
storeDup0 = TR::Node::createStore(store->getSymbolReference(), convDup);
convDup = TR::Node::create(conv->getOpCodeValue(), 1, arrayLoad1);
storeDup1 = TR::Node::createStore(store->getSymbolReference(), convDup);
}
else
{
storeDup0 = TR::Node::createStore(store->getSymbolReference(), arrayLoad0);
storeDup1 = TR::Node::createStore(store->getSymbolReference(), arrayLoad1);
}
trans->getAfterInsertionIdiomList(0)->append(storeDup0); // base[index]
trans->getAfterInsertionIdiomList(1)->append(storeDup1); // base[index-1] (it may not be used.)
if (VERBOSE(trans)) printf("%s moveStoreOutOfLoopForward\n", trans->getT()->getTitle());
if (DISPTRACE(trans)) traceMsg(comp, "moveStoreOutOfLoopForward adds %d into compensation code [0] and [1]\n", t->getID());
for (; t; t = ti.getNext()) t->setIsNegligible(); // set negligible to all stores
}
else
success0 = false;
}
return success0;
}
//*****************************************************************************************
// It analyzes redundant IAND. It is specialized to MEMCPYxxx2Byte, such as MEMCPYChar2Byte.
// Input: SpecialCareNode(*) - a set of conversions, such as i2b
//*****************************************************************************************
bool
IANDSpecialNodeTransformer(TR_CISCTransformer *trans)
{
TR_CISCGraph *P = trans->getP();
List<TR_CISCNode> *P2T = trans->getP2T();
TR::Compilation *comp = trans->comp();
int idx;
bool ret = false;
for (idx = 0; idx < MAX_SPECIALCARE_NODES; idx++)
{
TR_CISCNode *p = P->getSpecialCareNode(idx);
if (!p) break;
ListIterator<TR_CISCNode> ti(P2T + p->getID());
TR_CISCNode *t;
for (t = ti.getFirst(); t; t = ti.getNext())
{
if (t->getOpcode() != TR::i2b) continue; // not implemented yet for other OPs
TR_CISCNode *ch = t->getChild(0);
if (ch->isNegligible()) continue;
// example: the following two IANDs are redundant.
// dst = (byte)(((ch & 0xFF00) >> 8) & 0xFF)
// ^^^^^^^^ ^^^^^^
switch(ch->getOpcode())
{
case TR::iand:
if (!ch->getParents()->isSingleton() ||
!testIConst(ch, 1, 0xFF)) return false; // child(1) is "iconst 0xff"
ch->setIsNegligible(); // this IAND can be negligible!
ret = true;
ch = ch->getChild(0);
if (ch->getOpcode() != TR::ishr && ch->getOpcode() != TR::iushr) break;
// fall through if TR::ishr
case TR::ishr:
case TR::iushr:
if (!testIConst(ch, 1, 0x8)) break; // child(1) is "iconst 0x8"
ch = ch->getChild(0);
if (ch->getOpcode() != TR::iand) break;
if (!ch->getParents()->isSingleton() ||
!testIConst(ch, 1, 0xFF00)) return false; // child(1) is "iconst 0xFF00"
ch->setIsNegligible(); // this SHR can be negligible!
ret = true;
break;
}
}
}
return ret;
}
//////////////////////////////////////////////////////////////////////////
// utility routines
static void
findIndexLoad(TR::Node *aiaddNode, TR::Node *&index1, TR::Node *&index2, TR::Node *&topLevelIndex)
{
// iiload
// aiadd <-- aiaddNode
// aload
// isub
// imul
// iload <-- looking for the index
// iconst 4
// iconst -16
//
// -or-
// iiload
// aiadd
// aload
// isub
// iload
// iconst
//
// -or-
// iiload
// aiadd <-- aiaddNode
// aload
// isub
// imul
// iadd
// iload <-- looking for the index
// iload <-- looking for the index
// iconst 4
// iconst -16
//
// -or-
// iiload
// aiadd
// aload
// isub
// iadd
// iload <-- looking for the index
// iload <-- looking for the index
// iconst
//
index1 = NULL;
index2 = NULL;
topLevelIndex = NULL;
TR::Node *addOrSubNode = aiaddNode->getSecondChild();
if (addOrSubNode->getOpCode().isAdd() || addOrSubNode->getOpCode().isSub())
{
TR::Node *grandChild = NULL;
if (addOrSubNode->getFirstChild()->getOpCode().isMul())
grandChild = addOrSubNode->getFirstChild()->getFirstChild();
else
grandChild = addOrSubNode->getFirstChild();
if (grandChild->getOpCodeValue() == TR::i2l)
grandChild = grandChild->getFirstChild();
topLevelIndex = grandChild;
if (grandChild->getOpCode().hasSymbolReference())
{
index1 = grandChild;
}
else if (grandChild->getOpCode().isAdd() || grandChild->getOpCode().isSub())
{
TR::Node *grandGrandChild1 = grandChild->getFirstChild();
TR::Node *grandGrandChild2 = grandChild->getSecondChild();
while(grandGrandChild1->getOpCode().isAdd() || grandGrandChild1->getOpCode().isSub())
{
grandGrandChild2 = grandGrandChild1->getSecondChild();
grandGrandChild1 = grandGrandChild1->getFirstChild();
}
if (grandGrandChild1->getOpCode().hasSymbolReference())
{
index1 = grandGrandChild1;
}
if (grandGrandChild2->getOpCode().hasSymbolReference())
{
index2 = grandGrandChild2;
}
}
}
}
// get the iv thats involved in the looptest
//
static bool
usedInLoopTest(TR::Compilation *comp, TR::Node *loopTestNode, TR::SymbolReference *srcSymRef)
{
TR::Node *ivNode = loopTestNode->getFirstChild();
if (ivNode->getOpCode().isAdd() || ivNode->getOpCode().isSub())
ivNode = ivNode->getFirstChild();
if (ivNode->getOpCode().hasSymbolReference())
{
if (ivNode->getSymbolReference()->getReferenceNumber() == srcSymRef->getReferenceNumber())
return true;
}
else dumpOptDetails(comp, "iv %p in the loop test %p has no symRef?\n", ivNode, loopTestNode);
return false;
}
static bool
indexContainsArray(TR::Compilation *comp, TR::Node *index, vcount_t visitCount)
{
if (index->getVisitCount() == visitCount)
return false;
index->setVisitCount(visitCount);
if (comp->trace(OMR::idiomRecognition))
traceMsg(comp, "analyzing node %p\n", index);
if (index->getOpCode().hasSymbolReference() &&
index->getSymbolReference()->getSymbol()->isArrayShadowSymbol())
{
if (comp->trace(OMR::idiomRecognition))
traceMsg(comp, "found array node %p\n", index);
return true;
}
for (int32_t i = 0; i < index->getNumChildren(); i++)
if (indexContainsArray(comp, index->getChild(i), visitCount))
return true;
return false;
}
static bool
indexContainsArrayAccess(TR::Compilation *comp, TR::Node *aXaddNode)
{
if (comp->trace(OMR::idiomRecognition))
traceMsg(comp, "axaddnode %p\n", aXaddNode);
TR::Node *loadNode1, *loadNode2, *topLevelIndex;
findIndexLoad(aXaddNode, loadNode1, loadNode2, topLevelIndex);
// topLevelIndex now contains the actual expression q in a[q]
// if q contains another array access, then we cannot reduce
// this loop into an arraycopy
// ie. a[b[i]] do not represent linear array accesses
//
if (comp->trace(OMR::idiomRecognition))
traceMsg(comp, "aXaddNode %p topLevelIndex %p\n", aXaddNode, topLevelIndex);
vcount_t visitCount = comp->incOrResetVisitCount();
if (topLevelIndex)
return indexContainsArray(comp, topLevelIndex, visitCount);
return false;
}
// isIndexVariableInList checks whether the induction (index) variable symbol(s)
// from the given 'node' subtree is found inside 'nodeList'.
//
// Returns true if
// 1. one induction variable symbol is found in the list.
// Returns false if
// 1. no induction variables are found.
// 2. two induction variables found in 'node' tree are both in the list.
// i.e. a[i+j]
// i++;
// j++;
// In this case, the access pattern of the array would skip every
// other element.
static bool
isIndexVariableInList(TR::Node *node, List<TR::Node> *nodeList)
{
TR::Symbol *indexSymbol1 = NULL, *indexSymbol2 = NULL;
TR::Node *loadNode1, *loadNode2, *topLevelIndex;
findIndexLoad(node->getOpCode().isAdd() ? node : node->getFirstChild(),
loadNode1, loadNode2, topLevelIndex);
if (loadNode1)
indexSymbol1 = loadNode1->getSymbolReference()->getSymbol();
if (loadNode2)
indexSymbol2 = loadNode2->getSymbolReference()->getSymbol();
bool foundSymbol1 = false, foundSymbol2 = false;
if (indexSymbol1 || indexSymbol2)
{
// Search the node list for the index symbol(s).
ListIterator<TR::Node> li(nodeList);
TR::Node *store;
for (store = li.getFirst(); store; store = li.getNext())
{
TR::Symbol *storeSymbol = store->getSymbolReference()->getSymbol();
if (indexSymbol1 == storeSymbol)
foundSymbol1 = true;
if (indexSymbol2 && indexSymbol2 == storeSymbol)
foundSymbol2 = true;
}
}
// Return true only if either one symbol is found, but not both.
return foundSymbol1 ^ foundSymbol2;
}
// for the memCmp transformer
//
static bool
indicesAndStoresAreConsistent(TR::Compilation *comp, TR::Node *lhsSrcNode, TR::Node *rhsSrcNode, TR_CISCNode *lhsNode, TR_CISCNode *rhsNode)
{
// lhs and rhs indicate the two arrays involved in the comparison test
//
//
TR_ScratchList<TR::Node> variableList(comp->trMemory());
if (lhsNode)
variableList.add(lhsNode->getHeadOfTrNode());
if (rhsNode && rhsNode != lhsNode)
variableList.add(rhsNode->getHeadOfTrNode());
return (isIndexVariableInList(lhsSrcNode, &variableList) &&
isIndexVariableInList(rhsSrcNode, &variableList));
}
static TR::Node* getArrayBase(TR::Node *node)
{
if (node->getOpCode().hasSymbolReference() &&
node->getSymbolReference()->getSymbol()->isArrayShadowSymbol())
{
node = node->getFirstChild();
if (node->getOpCode().isArrayRef()) node = node->getFirstChild();
if (node->getOpCode().isIndirect()) node = node->getFirstChild();
return node;
}
return NULL;
}
static bool
areArraysInvariant(TR::Compilation *comp, TR::Node *inputNode, TR::Node *outputNode, TR_CISCGraph *T)
{
if (T)
{
TR::Node *aNode = getArrayBase(inputNode);
TR::Node *bNode = getArrayBase(outputNode);
if (comp->trace(OMR::idiomRecognition))
traceMsg(comp, "aNode = %p bNode = %p\n", aNode, bNode);
if (aNode && aNode->getOpCode().isLoadDirect() &&
bNode && bNode->getOpCode().isLoadDirect())
{
TR_CISCNode *aCNode = T->getCISCNode(aNode);
TR_CISCNode *bCNode = T->getCISCNode(bNode);
if (comp->trace(OMR::idiomRecognition))
traceMsg(comp, "aC = %p %d bC = %p %d\n", aCNode, aCNode->getID(), bCNode, bCNode->getID());
if (aCNode && bCNode)
{
ListIterator<TR_CISCNode> aDefI(aCNode->getChains());
ListIterator<TR_CISCNode> bDefI(bCNode->getChains());
TR_CISCNode *ch;
for (ch = aDefI.getFirst(); ch; ch = aDefI.getNext())
{