-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathLocalAnalysis.cpp
1107 lines (936 loc) · 37.4 KB
/
LocalAnalysis.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/LocalAnalysis.hpp"
#include <stdint.h>
#include <string.h>
#include "codegen/CodeGenerator.hpp"
#include "env/FrontEnd.hpp"
#include "compile/Compilation.hpp"
#include "compile/Method.hpp"
#include "compile/ResolvedMethod.hpp"
#include "compile/SymbolReferenceTable.hpp"
#include "cs2/bitvectr.h"
#include "env/TRMemory.hpp"
#include "env/jittypes.h"
#include "il/Block.hpp"
#include "il/DataTypes.hpp"
#include "il/ILOpCodes.hpp"
#include "il/ILOps.hpp"
#include "il/MethodSymbol.hpp"
#include "il/Node.hpp"
#include "il/Node_inlines.hpp"
#include "il/ResolvedMethodSymbol.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 "infra/CfgEdge.hpp"
#include "optimizer/Optimizer.hpp"
#include "ras/Debug.hpp"
class TR_OpaqueClassBlock;
bool TR_LocalAnalysis::isSupportedNodeForFieldPrivatization(TR::Node *node, TR::Compilation *comp, TR::Node *parent)
{
// types of stores that may be supported by field privatization
bool isSupportedStoreNode =
node->getOpCode().isStore() && node->getOpCodeValue() != TR::astorei;
return isSupportedNode(node, comp, parent, isSupportedStoreNode);
}
bool TR_LocalAnalysis::isSupportedNode(TR::Node *node, TR::Compilation *comp, TR::Node *parent, bool isSupportedStoreNode)
{
return isSupportedNodeForFunctionality(node, comp, parent, isSupportedStoreNode) &&
isSupportedNodeForPREPerformance(node, comp, parent);
}
bool TR_LocalAnalysis::isSupportedNodeForPREPerformance(TR::Node *node, TR::Compilation *comp, TR::Node *parent)
{
TR::SymbolReference *symRef = node->getOpCode().hasSymbolReference()?node->getSymbolReference():NULL;
if (node->getOpCode().isStore() && symRef &&
symRef->getSymbol()->isAutoOrParm())
{
//dumpOptDetails("Returning false for store %p\n", node);
return false;
}
if (node->getOpCode().isLoadConst() && !comp->cg()->isMaterialized(node))
{
return false;
}
if (node->getOpCode().hasSymbolReference() &&
(node->getSymbolReference() == comp->getSymRefTab()->findJavaLangClassFromClassSymbolRef()))
{
return false;
}
return true;
}
bool TR_LocalAnalysis::isSupportedNodeForFunctionality(TR::Node *node, TR::Compilation *comp, TR::Node *parent, bool isSupportedStoreNode)
{
#if 0
// This needs to be tested more. The idea is to check parent when making PRE decision
if (node->getOpCode().isLoadConst() &&
(parent->getOpCode().isStore()))
return true;
#endif
if (parent &&
(parent->getOpCodeValue() == TR::Prefetch) &&
(node->getOpCodeValue() == TR::aloadi))
return false;
if (node->isThisPointer() && !node->isNonNull())
return false;
if ((node->getOpCodeValue() == TR::a2l) || (node->getOpCodeValue() == TR::a2i))
return false;
if (node->getOpCode().isSpineCheck() /* &&
node->getFirstChild()->getOpCode().isStore() */ )
return false;
if (comp->requiresSpineChecks() &&
node->getOpCode().hasSymbolReference() &&
node->getSymbol()->isArrayShadowSymbol())
return false;
if (node->getOpCode().isCall() &&
!node->getSymbolReference()->isUnresolved() &&
node->getSymbol()->castToMethodSymbol()->isPureFunction() &&
(node->getDataType() != TR::NoType))
return true;
if (node->getOpCode().hasSymbolReference() &&
(node->getSymbolReference()->isSideEffectInfo() ||
node->getSymbolReference()->isOverriddenBitAddress() ||
node->getSymbolReference()->isUnresolved()))
return false;
if (node->isDataAddrPointer())
return false;
if (isSupportedOpCode(node->getOpCode(), comp) || isSupportedStoreNode || node->getOpCode().isLoadConst())
{
if (node->getDataType() == TR::Address)
{
if (!node->addressPointsAtObject())
return false;
}
return true;
}
return false;
}
TR_LocalAnalysisInfo::TR_LocalAnalysisInfo(TR::Compilation *c, bool t)
: _compilation(c), _trace(t), _trMemory(c->trMemory())
{
_numNodes = -1;
#if 0 // somehow stops PRE from happening
// We are going to increment visit count for every tree so can reach max
// for big methods quickly. Perhaps can improve containsCall() in the future.
comp()->resetVisitCounts(0);
#endif
if (comp()->getVisitCount() > HIGH_VISIT_COUNT)
{
_compilation->resetVisitCounts(1);
dumpOptDetails(comp(), "\nResetting visit counts for this method before LocalAnalysisInfo\n");
}
TR::CFG *cfg = comp()->getFlowGraph();
_numBlocks = cfg->getNextNodeNumber();
TR_ASSERT(_numBlocks > 0, "Local analysis, node numbers not assigned");
// Allocate information on the stack. It is the responsibility of the user
// of this class to determine the life of the information by using jitStackMark
// and jitStackRelease.
//
//_blocksInfo = (TR::Block **) trMemory()->allocateStackMemory(_numBlocks*sizeof(TR::Block *));
//memset(_blocksInfo, 0, _numBlocks*sizeof(TR::Block *));
TR::TreeTop *currentTree = comp()->getStartTree();
// Only do this if not done before; typically this would be done in the
// first call to this method through LocalTransparency and would NOT
// need to be re-done by LocalAnticipatability.
//
if (_numNodes < 0)
{
_optimizer = comp()->getOptimizer();
int32_t numBuckets;
int32_t numNodes = comp()->getNodeCount();
if (numNodes < 10)
numBuckets = 1;
else if (numNodes < 100)
numBuckets = 7;
else if (numNodes < 500)
numBuckets = 31;
else if (numNodes < 3000)
numBuckets = 127;
else if (numNodes < 6000)
numBuckets = 511;
else
numBuckets = 1023;
// Allocate hash table for matching expressions
//
HashTable hashTable(numBuckets, comp());
_hashTable = &hashTable;
// Null checks are handled differently as the criterion for
// commoning a null check is different than that used for
// other nodes; for a null check, the null check reference is
// important (and not the actual indirect access itself)
//
_numNullChecks = 0;
while (currentTree)
{
if (currentTree->getNode()->getOpCodeValue() == TR::NULLCHK)
//////if (currentTree->getNode()->getOpCode().isNullCheck())
_numNullChecks++;
currentTree = currentTree->getNextTreeTop();
}
if (_numNullChecks == 0)
_nullCheckNodesAsArray = NULL;
else
{
_nullCheckNodesAsArray = (TR::Node**)trMemory()->allocateStackMemory(_numNullChecks*sizeof(TR::Node*));
memset(_nullCheckNodesAsArray, 0, _numNullChecks*sizeof(TR::Node*));
}
currentTree = comp()->getStartTree();
int32_t symRefCount = comp()->getSymRefCount();
_checkSymbolReferences = new (trStackMemory()) TR_BitVector(symRefCount, trMemory(), stackAlloc);
_numNodes = 1;
_numNullChecks = 0;
// This loop counts all the nodes that are going to take part in PRE.
// This is a computation intensive loop as we check if the node that
// is syntactically equivalent to a given node has been seen before
// and if so we use the local index of the original node (that
// is syntactically equivalent to the given node). Could be improved
// in complexity with value numbering at some stage.
//
_visitCount = comp()->incVisitCount();
while (currentTree)
{
TR::Node *firstNodeInTree = currentTree->getNode();
TR::ILOpCode *opCode = &firstNodeInTree->getOpCode();
if (((firstNodeInTree->getOpCodeValue() == TR::treetop) ||
(comp()->useAnchors() && firstNodeInTree->getOpCode().isAnchor())) &&
(firstNodeInTree->getFirstChild()->getOpCode().isStore()))
{
firstNodeInTree->setLocalIndex(-1);
if (comp()->useAnchors() && firstNodeInTree->getOpCode().isAnchor())
firstNodeInTree->getSecondChild()->setLocalIndex(-1);
firstNodeInTree = firstNodeInTree->getFirstChild();
opCode = &firstNodeInTree->getOpCode();
}
// This call finds nodes with opcodes that are supported by PRE
// in this subtree; this accounts for all opcodes other than stores/checks
// which are handled later on below
//
bool firstNodeInTreeHasCallsInStoreLhs = false;
countSupportedNodes(firstNodeInTree, NULL, firstNodeInTreeHasCallsInStoreLhs);
if ((opCode->isStore() && !firstNodeInTree->getSymbolReference()->getSymbol()->isAutoOrParm()) ||
opCode->isCheck())
{
int32_t oldExpressionOnRhs = hasOldExpressionOnRhs(firstNodeInTree);
//
// Return value 0 denotes that the node contains some sub-expression
// that cannot participate in PRE; e.g. a call or a new
//
// Return value -1 denotes that the node can participate in PRE
// but did not match with any existing expression seen so far
//
// Any other return value (should be positive always) denotes that
// the node can participate in PRE and has been matched with a seen
// expression having local index == return value
//
if (oldExpressionOnRhs == -1)
{
if (trace())
{
traceMsg(comp(), "\nExpression #%d is : \n", _numNodes);
comp()->getDebug()->print(comp()->getOutFile(), firstNodeInTree, 6, true);
}
firstNodeInTree->setLocalIndex(_numNodes++);
}
else
firstNodeInTree->setLocalIndex(oldExpressionOnRhs);
if (opCode->isCheck() &&
(firstNodeInTree->getFirstChild()->getOpCode().isStore() &&
!firstNodeInTree->getFirstChild()->getSymbolReference()->getSymbol()->isAutoOrParm()))
{
int oldExpressionOnRhs = hasOldExpressionOnRhs(firstNodeInTree->getFirstChild());
if (oldExpressionOnRhs == -1)
{
if (trace())
{
traceMsg(comp(), "\nExpression #%d is : \n", _numNodes);
comp()->getDebug()->print(comp()->getOutFile(), firstNodeInTree->getFirstChild(), 6, true);
}
firstNodeInTree->getFirstChild()->setLocalIndex(_numNodes++);
}
else
firstNodeInTree->getFirstChild()->setLocalIndex(oldExpressionOnRhs);
}
}
else
firstNodeInTree->setLocalIndex(-1);
currentTree = currentTree->getNextTreeTop();
}
}
_supportedNodesAsArray = (TR::Node**)trMemory()->allocateStackMemory(_numNodes*sizeof(TR::Node*));
memset(_supportedNodesAsArray, 0, _numNodes*sizeof(TR::Node*));
_checkExpressions = new (trStackMemory()) TR_BitVector(_numNodes, trMemory(), stackAlloc);
//_checkExpressions.init(_numNodes, trMemory(), stackAlloc);
// This loop goes through the trees and collects the nodes
// that would take part in PRE. Each node has its local index set to
// the bit position that it occupies in the bit vector analyses.
//
currentTree = comp()->getStartTree();
_visitCount = comp()->incVisitCount();
while (currentTree)
{
TR::Node *firstNodeInTree = currentTree->getNode();
TR::ILOpCode *opCode = &firstNodeInTree->getOpCode();
if (((firstNodeInTree->getOpCodeValue() == TR::treetop) ||
(comp()->useAnchors() && firstNodeInTree->getOpCode().isAnchor())) &&
(firstNodeInTree->getFirstChild()->getOpCode().isStore()))
{
firstNodeInTree = firstNodeInTree->getFirstChild();
opCode = &firstNodeInTree->getOpCode();
}
collectSupportedNodes(firstNodeInTree, NULL);
if ((opCode->isStore() && !firstNodeInTree->getSymbolReference()->getSymbol()->isAutoOrParm()) ||
opCode->isCheck())
{
if (opCode->isCheck())
{
_checkSymbolReferences->set(firstNodeInTree->getSymbolReference()->getReferenceNumber());
_checkExpressions->set(firstNodeInTree->getLocalIndex());
}
if (!_supportedNodesAsArray[firstNodeInTree->getLocalIndex()])
_supportedNodesAsArray[firstNodeInTree->getLocalIndex()] = firstNodeInTree;
if (opCode->isCheck() &&
firstNodeInTree->getFirstChild()->getOpCode().isStore() &&
!firstNodeInTree->getFirstChild()->getSymbolReference()->getSymbol()->isAutoOrParm() &&
!_supportedNodesAsArray[firstNodeInTree->getFirstChild()->getLocalIndex()])
_supportedNodesAsArray[firstNodeInTree->getFirstChild()->getLocalIndex()] = firstNodeInTree->getFirstChild();
}
currentTree = currentTree->getNextTreeTop();
}
//initialize(toBlock(cfg->getStart()));
}
TR_LocalAnalysis::TR_LocalAnalysis(TR_LocalAnalysisInfo &info, bool trace)
: _lainfo(info), _trace(trace)
{
_registersScarce = false; //comp()->cg()->areAssignableGPRsScarce();
}
void TR_LocalAnalysis::initializeLocalAnalysis(bool isSparse, bool lock)
{
_info = (TR_LocalAnalysisInfo::LAInfo*) trMemory()->allocateStackMemory(_lainfo._numBlocks*sizeof(TR_LocalAnalysisInfo::LAInfo));
memset(_info, 0, _lainfo._numBlocks*sizeof(TR_LocalAnalysisInfo::LAInfo));
TR::BitVector blocksSeen(comp()->allocator());
initializeBlocks(toBlock(comp()->getFlowGraph()->getStart()), blocksSeen);
int32_t i;
for (i = 0; i < _lainfo._numBlocks; i++)
{
_info[i]._analysisInfo = allocateContainer(getNumNodes());
_info[i]._downwardExposedAnalysisInfo = allocateContainer(getNumNodes());
_info[i]._downwardExposedStoreAnalysisInfo = allocateContainer(getNumNodes());
}
}
void TR_LocalAnalysis::initializeBlocks(TR::Block *block, TR::BitVector &blocksSeen)
{
_info[block->getNumber()]._block = block;
blocksSeen[block->getNumber()] = true;
TR::Block *next;
for (auto nextEdge = block->getSuccessors().begin(); nextEdge != block->getSuccessors().end(); ++nextEdge)
{
next = toBlock((*nextEdge)->getTo());
if (!blocksSeen.ValueAt(next->getNumber()))
initializeBlocks(next, blocksSeen);
}
for (auto nextEdge = block->getExceptionSuccessors().begin(); nextEdge != block->getExceptionSuccessors().end(); ++nextEdge)
{
next = toBlock((*nextEdge)->getTo());
if (!blocksSeen.ValueAt(next->getNumber()))
initializeBlocks(next, blocksSeen);
}
}
// Checks for syntactic equivalence and returns the side-table index
// of the syntactically equivalent node if it found one; else it returns
// -1 signifying that this is the first time any node similar syntactically
// to this node has been seen. Adds the node to the hash table if seen for the
// first time.
//
//
int TR_LocalAnalysisInfo::hasOldExpressionOnRhs(TR::Node *node, bool recalcContainsCall, bool storeLhsContainsCall)
{
//
// Get the relevant portion of the subtree
// for this node; this is different for a null check
// as its null check reference is the only
// sub-expression that matters
//
TR::Node *relevantSubtree = NULL;
if (node->getOpCodeValue() == TR::NULLCHK)
relevantSubtree = node->getNullCheckReference();
else
relevantSubtree = node;
// containsCall checks whether the relevant node has some
// sub-expression that cannot be commoned, e.g. call or a new
//
bool nodeContainsCall;
if (!recalcContainsCall && (relevantSubtree == node))
{
// can use pre-calculated value of containsCall and storeLhsContainsCall, to avoid visitCount overflow
nodeContainsCall = node->containsCall();
}
else
{
storeLhsContainsCall = false;
nodeContainsCall = containsCall(relevantSubtree, storeLhsContainsCall);
}
if (nodeContainsCall)
{
//
// If the node is not a store, a call-like sub-expression is inadmissable;
// if the node is a store, a call-like sub-expression is allowed on the RHS
// of the store as this does not inhibit privatization in any way as
// the private temp store's RHS simply points at original RHS. But if a call-like
// sub-expression is present in the LHS of the store, that is inadmissable
//
if (!node->getOpCode().isStore() ||
storeLhsContainsCall)
return 0;
}
bool seenIndirectStore = false;
#ifdef J9_PROJECT_SPECIFIC
bool seenIndirectBCDStore = false;
#endif
bool seenWriteBarrier = false;
int32_t storeNumChildren = node->getNumChildren();
// Need to save the node's isStoreIndirect flag before the node
// is recreated below. The recreation is done for convenience
// of syntactic comparison between loads and stores of the same field.
// Since a store node is transformed to a load, isStoreIndirect
// of the original store node will become false after the recreation.
bool isStoreIndirect = node->getOpCode().isStoreIndirect();
// If node is a null check, compare the
// null check reference only to establish syntactic equivalence
//
if (node->getOpCodeValue() == TR::NULLCHK)
/////if (node->getOpCode().isNullCheck())
{
int32_t k;
for (k=0;k<_numNullChecks;k++)
{
if (!(_nullCheckNodesAsArray[k] == NULL))
{
if (areSyntacticallyEquivalent(_nullCheckNodesAsArray[k]->getNullCheckReference(), node->getNullCheckReference()))
return _nullCheckNodesAsArray[k]->getLocalIndex();
}
}
_nullCheckNodesAsArray[_numNullChecks++] = node;
}
else
{
//
// If this node is a global store, then equivalence check is different.
// We try to give a store to field (or static) o.f the same index as
// a load of o.f. This is so that privatization happens for fields/statics.
// So the store's opcode value is changed temporarily to be a load before
// syntactic equivalence is checked; this enables matching stores/loads to
// same global symbol.
//
if (node->getOpCode().isStore() &&
!node->getSymbolReference()->getSymbol()->isAutoOrParm())
{
if (node->getOpCode().isWrtBar())
seenWriteBarrier = true;
#ifdef J9_PROJECT_SPECIFIC
seenIndirectBCDStore = node->getType().isBCD();
#endif
TR::Node::recreate(node, _compilation->il.opCodeForCorrespondingLoadOrStore(node->getOpCodeValue()));
if (isStoreIndirect)
{
node->setNumChildren(1);
}
else
{
node->setNumChildren(0);
}
#ifdef J9_PROJECT_SPECIFIC
if (seenIndirectBCDStore)
node->setBCDStoreIsTemporarilyALoad(true);
#endif
seenIndirectStore = true;
}
int32_t hashValue = _hashTable->hash(node);
HashTable::Cursor cursor(_hashTable, hashValue);
TR::Node *other;
bool isOtherStoreIndirect = false;
for (other = cursor.firstNode(); other; other = cursor.nextNode())
{
// Convert other node's opcode to be a load temporarily
// (only for syntactic equivalence check; see explanation above)
// to enable matching global stores/loads.
//
bool seenOtherIndirectStore = false;
#ifdef J9_PROJECT_SPECIFIC
bool seenOtherIndirectBCDStore = false;
#endif
bool seenOtherWriteBarrier = false;
int32_t otherStoreNumChildren = other->getNumChildren();
if (other->getOpCode().isStore() &&
!other->getSymbolReference()->getSymbol()->isAutoOrParm())
{
if (other->getOpCode().isWrtBar())
seenOtherWriteBarrier = true;
// Need to save the node's isStoreIndirect flag before the node
// is recreated below. The recreation is done for convenience
// of syntactic comparison between loads and stores of the same field.
// Since a store node is transformed to a load, isStoreIndirect
// of the original store node will become false after the recreation.
isOtherStoreIndirect = other->getOpCode().isStoreIndirect();
#ifdef J9_PROJECT_SPECIFIC
seenOtherIndirectBCDStore = other->getType().isBCD();
#endif
TR::Node::recreate(other, _compilation->il.opCodeForCorrespondingLoadOrStore(other->getOpCodeValue()));
if (isOtherStoreIndirect)
{
other->setNumChildren(1);
}
else
{
other->setNumChildren(0);
}
#ifdef J9_PROJECT_SPECIFIC
if (seenOtherIndirectBCDStore)
other->setBCDStoreIsTemporarilyALoad(true);
#endif
seenOtherIndirectStore = true;
}
bool areSame = areSyntacticallyEquivalent(node, other);
// Restore the other node's state to what it was originally
// (if it was a global store)
//
if (seenOtherWriteBarrier)
{
other->setNumChildren(otherStoreNumChildren);
if (otherStoreNumChildren == 3)
TR::Node::recreate(other, TR::awrtbari);
else
TR::Node::recreate(other, TR::awrtbar);
}
else if (seenOtherIndirectStore)
{
other->setNumChildren(otherStoreNumChildren);
#ifdef J9_PROJECT_SPECIFIC
if (seenOtherIndirectBCDStore)
other->setBCDStoreIsTemporarilyALoad(false);
#endif
TR::Node::recreate(other, _compilation->il.opCodeForCorrespondingLoadOrStore(other->getOpCodeValue()));
}
if (areSame)
{
if (seenWriteBarrier)
{
node->setNumChildren(storeNumChildren);
if (storeNumChildren == 3)
TR::Node::recreate(node, TR::awrtbari);
else
TR::Node::recreate(node, TR::awrtbar);
}
else if (seenIndirectStore)
{
node->setNumChildren(storeNumChildren);
#ifdef J9_PROJECT_SPECIFIC
if (seenIndirectBCDStore)
node->setBCDStoreIsTemporarilyALoad(false);
#endif
TR::Node::recreate(node, _compilation->il.opCodeForCorrespondingLoadOrStore(node->getOpCodeValue()));
}
return other->getLocalIndex();
}
}
// No match from existing nodes in the hash table;
// add this node to the hash table.
//
_hashTable->add(node, hashValue);
}
// Restore this node's state to what it was before
// (if it was a global store)
//
if (seenWriteBarrier)
{
node->setNumChildren(storeNumChildren);
if (storeNumChildren == 3)
TR::Node::recreate(node, TR::awrtbari);
else
TR::Node::recreate(node, TR::awrtbar);
}
else if (seenIndirectStore)
{
node->setNumChildren(storeNumChildren);
#ifdef J9_PROJECT_SPECIFIC
if (seenIndirectBCDStore)
node->setBCDStoreIsTemporarilyALoad(false);
#endif
TR::Node::recreate(node, _compilation->il.opCodeForCorrespondingLoadOrStore(node->getOpCodeValue()));
}
return -1;
}
bool TR_LocalAnalysisInfo::isCallLike(TR::Node *node) {
TR::ILOpCode &opCode = node->getOpCode();
TR::ILOpCodes opCodeValue = opCode.getOpCodeValue();
if ((opCode.isCall() && !node->isPureCall()) ||
opCodeValue == TR::New ||
opCodeValue == TR::newarray ||
opCodeValue == TR::anewarray ||
opCodeValue == TR::multianewarray)
return true;
//if (debug("preventUnresolvedPRE"))
{
if (node->hasUnresolvedSymbolReference())
return true;
}
if (node->getOpCode().hasSymbolReference())
{
if (!node->getSymbolReference()->getSymbol()->isTransparent() ||
(node->getSymbolReference()->getSymbol()->isMethodMetaData() &&
!node->getSymbolReference()->getSymbol()->isImmutableField()))
return true;
if (node->getSymbolReference()->isSideEffectInfo() ||
node->getSymbolReference()->isOverriddenBitAddress())
return true;
if (node->isThisPointer() && !node->isNonNull())
return true;
if (comp()->requiresSpineChecks() &&
//node->getOpCode().hasSymbolReference() &&
node->getSymbol()->isArrayShadowSymbol())
{
//containsCallInStoreLhs = true;
return true;
}
// Note that this change is only needed because javaLangClassFromClass loads are being skipped for performance (see earlier code in this file) and
// we want to avoid strange side effects of a parent being commoned but a child is not a candidate for commoning.
//
if (node->getOpCode().hasSymbolReference() &&
(node->getSymbolReference() == comp()->getSymRefTab()->findJavaLangClassFromClassSymbolRef()))
{
return true;
}
}
return false;
}
// Check if this node contains a call or call-like opcode that
// cannot be moved (duplicated) by PRE
//
// This method iterates over the children of the call, so it needs a visit count mechanism
// to avoid processing nodes twice. However, the trees are already in a similar situation so
// the node visit counts must not be corrupted for those iterations.
// Simply incrementing for each use of containsCall will not work, for large programs the visit
// count will overflow.
// To get around this, we use two visit counts:
// if the old visit count is the current one, the new visit count is old visit count + 2
// otherwise, the new visit count is old visit count + 1
// At the end of processing the node tree is revisited and the visit counts reset to the old
// visit count or to zero.
// It is guaranteed that there is room for old visit count + 2, that is checked before all
// this starts.
//
bool TR_LocalAnalysisInfo::containsCall(TR::Node *node, bool &containsCallInStoreLhs)
{
bool result = containsCallInTree(node, containsCallInStoreLhs);
containsCallResetVisitCounts(node);
return result;
}
bool TR_LocalAnalysisInfo::containsCallInTree(TR::Node *node, bool &containsCallInStoreLhs)
{
vcount_t visitCount = node->getVisitCount();
if (visitCount == _visitCount+1 || visitCount == _visitCount+2)
return false;
if (visitCount == _visitCount)
visitCount = 2;
else
visitCount = 1;
node->setVisitCount(_visitCount + visitCount);
if (isCallLike(node))
return true;
int32_t i;
for (i = 0;i < node->getNumChildren();i++)
{
bool childHasCalls = containsCallInTree(node->getChild(i), containsCallInStoreLhs);
if (childHasCalls)
{
if (node->getOpCode().isStoreIndirect() &&
(i == 0))
containsCallInStoreLhs = true;
return true;
}
}
return false;
}
void TR_LocalAnalysisInfo::containsCallResetVisitCounts(TR::Node *node)
{
int32_t i;
i = node->getVisitCount();
if (i == _visitCount+2)
i = _visitCount;
else if (i == _visitCount+1)
i = 0;
else
return;
node->setVisitCount(i);
for (i = 0; i < node->getNumChildren(); i++)
containsCallResetVisitCounts(node->getChild(i));
}
//
// Returns true if the two subtrees are identical syntactically; only special case
// is TR::aiadd (or TR::aladd) which may not participate in PRE; expressions containing
// the TR::aiadd (or TR::aladd) are matched correctly though by the special case code.
//
bool TR_LocalAnalysisInfo::areSyntacticallyEquivalent(TR::Node *node1, TR::Node *node2)
{
if (!_optimizer->areNodesEquivalent(node1,node2))
return false;
if (!(node1->getNumChildren() == node2->getNumChildren()))
return false;
if (node1 == node2)
return true;
int32_t i;
for (i = 0;i < node1->getNumChildren();i++)
{
if (node1->getChild(i)->getLocalIndex() != node2->getChild(i)->getLocalIndex())
return false;
else
{
if (node1->getChild(i)->getLocalIndex() == MAX_SCOUNT)
{
if ((node1->getChild(i)->getOpCode().isLoadConst() || node1->getChild(i)->getOpCode().isLoadVarDirect()) && (node2->getChild(i)->getOpCode().isLoadConst() || node2->getChild(i)->getOpCode().isLoadVarDirect()))
{
if (!_optimizer->areNodesEquivalent(node1->getChild(i), node2->getChild(i)))
return false;
}
else if ((node1->getChild(i)->getOpCode().isArrayRef()) && (node2->getChild(i)->getOpCode().isArrayRef()))
{
for (int32_t j = 0;j < node1->getChild(i)->getNumChildren();j++)
{
TR::Node *grandChild1 = node1->getChild(i)->getChild(j);
TR::Node *grandChild2 = node2->getChild(i)->getChild(j);
if (grandChild1->getLocalIndex() != grandChild2->getLocalIndex())
return false;
else
{
if (grandChild1->getLocalIndex() == MAX_SCOUNT)
{
if ((grandChild1->getOpCode().isLoadConst() || grandChild1->getOpCode().isLoadVarDirect()) && (grandChild2->getOpCode().isLoadConst() || grandChild2->getOpCode().isLoadVarDirect()))
{
if (!_optimizer->areNodesEquivalent(grandChild1, grandChild2))
return false;
}
else
return false;
}
else if (grandChild1->getLocalIndex() == 0)
return false;
}
}
}
else
return false;
}
else if (node1->getChild(i)->getLocalIndex() == 0)
return false;
}
}
return true;
}
int32_t TR_LocalAnalysisInfo::HashTable::hash(TR::Node *node)
{
// Hash on the opcode and value numbers of the children
//
uint32_t h, g;
int32_t numChildren = node->getNumChildren();
h = (node->getOpCodeValue() << 4) + numChildren;
g = 0;
for (int32_t i = numChildren-1; i >= 0; i--)
{
TR::Node *child = node->getChild(i);
h <<= 4;
if (child->getOpCode().hasSymbolReference())
h += (int32_t)(intptr_t)child->getSymbolReference()->getSymbol();
else
h++;
g = h & 0xF0000000;
h ^= g >> 24;
}
return (h ^ g) % _numBuckets;
}
TR_LocalAnalysisInfo::HashTable::Cursor::Cursor(HashTable *table, int32_t bucket)
: _table(*table), _bucket(bucket), _chunk(table->_buckets[bucket]), _index(0)
{
}
TR::Node *TR_LocalAnalysisInfo::HashTable::Cursor::firstNode()
{
_chunk = _table._buckets[_bucket];
if (!_chunk) return NULL;
_index = 0;
return _chunk->_nodes[_index];
}
TR::Node *TR_LocalAnalysisInfo::HashTable::Cursor::nextNode()
{
while (_chunk)
{
if (_index < NODES_PER_CHUNK-1)
{
_index++;
TR::Node *node = _chunk->_nodes[_index];
if (node)
return node;
}
_chunk = _chunk->_next;
_index = -1;
}
return NULL;
}
// Counts nodes that involved in PRE that are not stores or checks.
// These nodes require temps.
//
bool TR_LocalAnalysisInfo::countSupportedNodes(TR::Node *node, TR::Node *parent, bool &containsCallInStoreLhs)
{
if (_visitCount == node->getVisitCount())
{
return false;
}
node->setVisitCount(_visitCount);
node->setContainsCall(false);
if (isCallLike(node))
{
node->setContainsCall(true);
// would return here
}
bool flag = false;
TR::ILOpCode &opCode = node->getOpCode();
int n = node->getNumChildren();
int32_t i;
for (i = 0; i < n; i++)
{
TR::Node *child = node->getChild(i);
bool childHasCallsInStoreLhs = false;
if (countSupportedNodes(child, node, childHasCallsInStoreLhs))
flag = true;
if (childHasCallsInStoreLhs)
containsCallInStoreLhs = true;
if (child->containsCall())
{
if (node->getOpCode().isStoreIndirect() && (i == 0))
containsCallInStoreLhs = true;
node->setContainsCall(true);
}
}
if (TR_LocalAnalysis::isSupportedNode(node, _compilation, parent))
{
int oldExpressionOnRhs = hasOldExpressionOnRhs(node, false, containsCallInStoreLhs);
if (oldExpressionOnRhs == -1)
{
if (trace())
{
traceMsg(comp(), "\nExpression #%d is : \n",_numNodes);
_compilation->getDebug()->print(_compilation->getOutFile(), node, 6, true);
}
flag = true;
node->setLocalIndex(_numNodes);
_numNodes++;
}
else
node->setLocalIndex(oldExpressionOnRhs);
}