-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathLocalDeadStoreElimination.cpp
1108 lines (981 loc) · 41.1 KB
/
LocalDeadStoreElimination.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 (c) 2000, 2021 IBM Corp. and others
*
* 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 http://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] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#include "optimizer/LocalDeadStoreElimination.hpp"
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#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 "env/CompilerEnv.hpp"
#include "il/AliasSetInterface.hpp"
#include "il/AutomaticSymbol.hpp"
#include "il/DataTypes.hpp"
#include "il/Block.hpp"
#include "il/ILOpCodes.hpp"
#include "il/ILOps.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/BitVector.hpp"
#include "infra/Checklist.hpp"
#include "infra/List.hpp"
#include "optimizer/Optimization.hpp"
#include "optimizer/Optimization_inlines.hpp"
#include "optimizer/Optimizer.hpp"
TR::Optimization *TR::LocalDeadStoreElimination::create(TR::OptimizationManager *manager)
{
return new (manager->allocator()) TR::LocalDeadStoreElimination(manager);
}
TR::LocalDeadStoreElimination::LocalDeadStoreElimination(TR::OptimizationManager *manager)
: TR::Optimization(manager)
{}
void
TR::LocalDeadStoreElimination::setIsFirstReferenceToNode(TR::Node *parent, int32_t index, TR::Node *node)
{
parent ? node->setLocalIndex(1) : node->setLocalIndex(0);
}
bool
TR::LocalDeadStoreElimination::isFirstReferenceToNode(TR::Node *parent, int32_t index, TR::Node *node)
{
return (node->getLocalIndex() <= 1);
}
int32_t TR::LocalDeadStoreElimination::perform()
{
if (trace())
traceMsg(comp(), "Starting LocalDeadStoreElimination\n");
TR::TreeTop *tt, *exitTreeTop;
for (tt = comp()->getStartTree(); tt; tt = exitTreeTop->getNextTreeTop())
{
exitTreeTop = tt->getExtendedBlockExitTreeTop();
transformBlock(tt, exitTreeTop);
}
eliminateDeadObjectInitializations();
if (_treesChanged)
requestDeadTreesCleanup();
if (trace())
traceMsg(comp(), "\nEnding LocalDeadStoreElimination\n");
return 1;
}
int32_t TR::LocalDeadStoreElimination::performOnBlock(TR::Block *block)
{
if (block->getEntry())
{
transformBlock(block->getEntry(), block->getEntry()->getExtendedBlockExitTreeTop());
postPerformOnBlocks();
}
return 0;
}
void TR::LocalDeadStoreElimination::prePerformOnBlocks()
{
TR::TreeTop *currentTree = comp()->getStartTree();
comp()->incVisitCount();
//
// Local index is set to reference count below
// As we walk backwards in the main analysis, the local index will keep getting decremented
// giving us an idea about how many more references exist to a particular node
//
while (currentTree)
{
setupReferenceCounts(currentTree->getNode());
currentTree = currentTree->getNextTreeTop();
}
comp()->incVisitCount();
_treesChanged = false;
}
void TR::LocalDeadStoreElimination::postPerformOnBlocks()
{
}
bool TR::LocalDeadStoreElimination::isNonRemovableStore(TR::Node *storeNode, bool &seenIdentityStore)
{
TR::Node *currentNode = _curTree->getNode();
TR::SymbolReference *symRef = storeNode->getSymbolReference();
int32_t symRefNum = symRef->getReferenceNumber();
bool nonRemovableStore = false;
/*
* Stores that are children of resolve checks cannot be removed, since
* the check still has to be done.
*/
if (currentNode->getOpCode().isResolveCheck())
nonRemovableStore = true;
// NOTE: The sym->usedInNestedProc() check for LocalDeadStoreElimination is too conservative as the
// seenIdenticalStore check can legally eliminate even nested symbols
// i.e.
// istore "a" <- even with nested "a" this store can be eliminated due to the redefine of "a" below
// x
// istore "a"
// y
// The usedInNestedProc check is to prevent a auto/parm store to a nested symbol in a nested routine being
// removed when there is only a return and no uses from the store to the end of the block:
// istore "b" // not legal to remove this store as the auto/parm 'escapes' to the parent (owning) routine as they share a stack
// z
// return
if (storeNode->dontEliminateStores(true))
nonRemovableStore = true;
seenIdentityStore = isIdentityStore(storeNode);
// although it's ok to remove 2 consecutive same stores no matter what
// eg. pinning array stores from GRA live range splitting
// except for placeholder stores for monexit, we need to see those in order to pop the live monitor stack the correct number of times
if (!storeNode->getSymbolReference()->getSymbol()->holdsMonitoredObject())
{
TR::Node *prevStoreNode = _curTree->getPrevTreeTop()->getNode()->getStoreNode();
if (prevStoreNode &&
!storeNode->getOpCode().isIndirect() && !prevStoreNode->getOpCode().isIndirect() &&
storeNode->getFirstChild() == prevStoreNode->getFirstChild() &&
storeNode->getSymbolReference() == prevStoreNode->getSymbolReference())
{
seenIdentityStore = true;
nonRemovableStore = false;
}
}
return nonRemovableStore;
}
void TR::LocalDeadStoreElimination::transformBlock(TR::TreeTop * entryTree, TR::TreeTop * exitTree)
{
_curTree = entryTree;
_blockContainsReturn = false;
_treesAnchored = false;
comp()->setCurrentBlock(entryTree->getEnclosingBlock());
/**
* Count the number of stores to allocate the right size data structures
*/
int32_t numStores = 0;
while (!(_curTree == exitTree))
{
if (_curTree->getNode()->getStoreNode())
numStores++;
_curTree = _curTree->getNextTreeTop();
}
StoreNodeTable storeNodeTable(0, comp()->trMemory()->currentStackRegion());
TR_BitVector deadSymbolReferences(comp()->trMemory()->currentStackRegion());
_storeNodes = &storeNodeTable;
int32_t symRefCount = comp()->getSymRefCount();
comp()->incOrResetVisitCount();
int32_t i;
TR::Node *currentNode;
_curTree = exitTree;
while (!(_curTree == entryTree))
{
currentNode = _curTree->getNode();
if (comp()->getVisitCount() == MAX_VCOUNT - 2) // Inc visit count will assert at MAX_VCOUNT - 1, so this gives us
// the one extra incVisitCount that might occur if we remove a store tree.
{
if (trace())
traceMsg(comp(), "Bailing out of local deadstore to avoid visit count overflow\n");
break;
}
/*
* Special case for when the last instruction in the block is a return.
* In this case we can simply get rid of any trailing stores to local
* variables in this block as they cannot be accessed outside
* this method.
*/
if (currentNode->getOpCode().isReturn())
{
_blockContainsReturn = true;
for (int32_t symRefNumber = comp()->getSymRefTab()->getIndexOfFirstSymRef(); symRefNumber < symRefCount; symRefNumber++)
{
TR::SymbolReference *symRef = comp()->getSymRefTab()->getSymRef(symRefNumber);
TR::Symbol *sym = symRef ? symRef->getSymbol() : 0;
if (sym && (sym->isAuto() || sym->isParm())) {
deadSymbolReferences.set(symRefNumber);
}
}
}
bool removedTree = false;
TR::Node *storeNode = currentNode->getStoreNode();
if (storeNode)
{
bool seenIdentityStore = isIdentityStore(storeNode);
bool nonRemovableStore = isNonRemovableStore(storeNode, seenIdentityStore);
TR::SymbolReference *symRef = storeNode->getSymbolReference();
int32_t symRefNum = symRef->getReferenceNumber();
// the logic below is:
// when nonRemovableStore=false then removal of the store can happen if one of the following conditions is true:
// 1) seenIdentityStore=true
// 2) haven't seen a use and not volatile and one of the following is also true
// a) the block contains a return and not nested and sym is an auto or parm (i.e. val doesn't escape)
// b) an identical store has been seen
if (!nonRemovableStore &&
(seenIdentityStore || // 1)
(deadSymbolReferences.get(symRefNum) && // 2)
!symRef->getSymbol()->isVolatile() &&
((_blockContainsReturn && // a)
(symRef->getSymbol()->isAuto() ||
symRef->getSymbol()->isParm())) ||
seenIdenticalStore(storeNode))))) // b)
{
removedTree = true;
_curTree = removeStoreTree(_curTree);
}
else
{
adjustStoresInfo(storeNode, deadSymbolReferences);
}
}
else if (currentNode->getNumChildren() > 0 &&
(currentNode->getFirstChild()->getOpCode().isCall() ||
currentNode->getFirstChild()->getOpCode().isStore()))
{
adjustStoresInfo(currentNode->getFirstChild(), deadSymbolReferences);
}
// Change made to accommodate branches in mid (extended) basic blocks
// Cannot remove stores based on a store that appears after a branch
//
if (currentNode->getOpCode().isBranch() ||
currentNode->getOpCode().isJumpWithMultipleTargets() ||
(currentNode->getOpCodeValue() == TR::treetop && currentNode->getFirstChild()->getOpCode().isJumpWithMultipleTargets()))
{
_blockContainsReturn = false;
deadSymbolReferences.empty();
_storeNodes->clear();
}
if (!removedTree)
{
comp()->incVisitCount();
examineNode(NULL, 0, currentNode, deadSymbolReferences);
}
_curTree = _curTree->getPrevTreeTop();
} // end while (!(_curTree == entryTree))
if (_treesAnchored)
{
_curTree = entryTree;
while (!(_curTree == exitTree))
{
currentNode = _curTree->getNode();
TR::Node *firstChild = currentNode->getOpCodeValue() == TR::treetop ? currentNode->getFirstChild() : 0;
bool callWithSideEffects=false;
if(firstChild && firstChild->getOpCode().isCall())
{
callWithSideEffects=true;
if (firstChild->getSymbolReference()->getSymbol()->isResolvedMethod() &&
firstChild->getSymbolReference()->getSymbol()->castToResolvedMethodSymbol()->isSideEffectFree())
callWithSideEffects = false;
}
if (firstChild &&
!(callWithSideEffects ||
firstChild->getOpCodeValue() == TR::New ||
firstChild->getOpCodeValue() == TR::newarray ||
firstChild->getOpCodeValue() == TR::anewarray ||
firstChild->getOpCodeValue() == TR::multianewarray ||
firstChild->getOpCode().isCheckCast() ||
firstChild->getOpCodeValue() == TR::instanceof ||
firstChild->getOpCodeValue() == TR::monent ||
firstChild->getOpCodeValue() == TR::monexit))
{
if (firstChild->getReferenceCount() == 0 &&
performTransformation(comp(), "%sRemoving Dead Anchor : %s [0x%p]\n", optDetailString(), currentNode->getOpCode().getName(), currentNode))
{
_treesChanged = true;
optimizer()->prepareForTreeRemoval(_curTree);
TR::TreeTop *nextTree = _curTree->getNextTreeTop();
TR::TreeTop *prevTree = _curTree->getPrevTreeTop();
prevTree->setNextTreeTop(nextTree);
nextTree->setPrevTreeTop(prevTree);
_curTree = nextTree;
continue;
}
}
_curTree = _curTree->getNextTreeTop();
}
}
}
TR::TreeTop *TR::LocalDeadStoreElimination::removeStoreTree(TR::TreeTop *treeTop)
{
// If all the conditions for a store are met then
// get rid of the store operation
//
_treesChanged = true;
comp()->incVisitCount();
TR::Node *nodeInTreeTop = treeTop->getNode();
TR::Node *storeNode = nodeInTreeTop->getStoreNode();
if ((storeNode != nodeInTreeTop) && (nodeInTreeTop->getOpCodeValue() == TR::NULLCHK))
{
TR::TreeTop *nullCheckTree = TR::TreeTop::create(comp(), nodeInTreeTop);
TR::Node *referenceNode = nodeInTreeTop->getNullCheckReference();
TR::Node *passThroughNode = TR::Node::create(TR::PassThrough, 1, referenceNode);
nullCheckTree->getNode()->setChild(0, passThroughNode);
nullCheckTree->getNode()->setReferenceCount(0);
nullCheckTree->getNode()->setNumChildren(1);
passThroughNode->setReferenceCount(1);
setIsFirstReferenceToNode(NULL, 0, nullCheckTree->getNode());
setIsFirstReferenceToNode(nullCheckTree->getNode(), 0, passThroughNode);
treeTop->getPrevTreeTop()->join(nullCheckTree);
nullCheckTree->join(treeTop);
}
else if ((storeNode != nodeInTreeTop) && (nodeInTreeTop->getOpCodeValue() == TR::BNDCHKwithSpineCHK))
{
TR::TreeTop *spineCheckTree = TR::TreeTop::create(comp(), nodeInTreeTop);
TR::Node *arrayAccessNode = nodeInTreeTop->getFirstChild();
TR::Node *replacementNode = TR::Node::createConstZeroValue(storeNode, storeNode->getDataType());
spineCheckTree->getNode()->setAndIncChild(0, replacementNode);
spineCheckTree->getNode()->setReferenceCount(0);
setIsFirstReferenceToNode(NULL, 0, spineCheckTree->getNode());
setIsFirstReferenceToNode(spineCheckTree->getNode(), 0, replacementNode);
treeTop->getPrevTreeTop()->join(spineCheckTree);
spineCheckTree->join(treeTop);
}
// Entire node may not be removable as nodes may have values
// that would change if the node is allowed to swing down to its
// next use (if any symbols in the value are written in the
// intervening treetops).
//
if (isEntireNodeRemovable(storeNode))
{
if (performTransformation(comp(), "%sRemoving Dead Store : %s [0x%p]\n", optDetailString(), storeNode->getOpCode().getName(), storeNode))
{
storeNode->setReferenceCount(1);
optimizer()->prepareForNodeRemoval(storeNode);
storeNode->recursivelyDecReferenceCount();
TR::TreeTop *nextTree = treeTop->getNextTreeTop();
TR::TreeTop *prevTree = treeTop->getPrevTreeTop();
prevTree->setNextTreeTop(nextTree);
nextTree->setPrevTreeTop(prevTree);
treeTop = nextTree;
}
}
else
{
if (performTransformation(comp(), "%sAnchoring rhs of store : %s [0x%p] in a treetop\n", optDetailString(), storeNode->getOpCode().getName(), storeNode))
{
bool removedTree = false;
TR::TreeTop *curTree = NULL;
// if the store is dead, remove the corresponding compressRefs node as well
//
if (comp()->useAnchors())
{
// FIXME: perhaps need to walk down the extended block
// looking for the corresponding anchor node
//
curTree = treeTop->getNextTreeTop();
TR::Node *translateNode = NULL;
for (; curTree->getNode()->getOpCodeValue() != TR::BBEnd; curTree = curTree->getNextTreeTop())
if (curTree->getNode()->getOpCode().isAnchor() &&
curTree->getNode()->getFirstChild() == storeNode)
{
translateNode = curTree->getNode();
break;
}
if (translateNode)
{
dumpOptDetails(comp(), "removing corresponding translation [%p] for [%p]\n", translateNode, storeNode);
if (translateNode->getFirstChild()->getReferenceCount() > 1)
{
removedTree = true;
translateNode->recursivelyDecReferenceCount();
//translateNode->getFirstChild()->decReferenceCount();
TR::TreeTop *nextTree = curTree->getNextTreeTop();
TR::TreeTop *previousTree = curTree->getPrevTreeTop();
previousTree->setNextTreeTop(nextTree);
nextTree->setPrevTreeTop(previousTree);
}
else
{
translateNode->decReferenceCount();
translateNode->getSecondChild()->decReferenceCount();
curTree->setNode(storeNode);
}
}
}
TR::TreeTop *prevTree = treeTop->getPrevTreeTop();
TR::NodeChecklist visited(comp());
for (int32_t j=0;j<storeNode->getNumChildren();j++)
{
getAnchorNode(storeNode, j, storeNode->getChild(j), treeTop, visited);
}
optimizer()->prepareForNodeRemoval(storeNode);
_treesAnchored = true;
TR::TreeTop *nextTree = treeTop->getNextTreeTop();
if ((curTree != treeTop) ||
!removedTree)
{
if (nodeInTreeTop->getOpCode().isAnchor()||
(nodeInTreeTop->getOpCode().isCheck() &&
!nodeInTreeTop->getOpCode().isNullCheck()))
treeTop->getNode()->recursivelyDecReferenceCount();
else
storeNode->recursivelyDecReferenceCount();
TR::TreeTop *previousTree = treeTop->getPrevTreeTop();
previousTree->setNextTreeTop(nextTree);
nextTree->setPrevTreeTop(previousTree);
//treeTop = prevTree->getNextTreeTop();
}
treeTop = nextTree;
}
}
return treeTop;
}
// Look for identity stores, i.e. stores whose value to be stored is a load of
// the same object.
//
bool TR::LocalDeadStoreElimination::isIdentityStore(TR::Node *storeNode)
{
// Now see if this is an identity store. If it is there are two cases:
// 1) This is the first use of the load node - the store can be removed
// immediately.
// 2) There is a previous use of the load node - the store must be added
// to the list of pending stores until we can prove there is no
// intervening store that kills the load.
//
TR::Node *storedValue = NULL;
int32_t storedValueChildIndex = -1;
if (storeNode->getOpCode().isIndirect())
{
storedValueChildIndex = 1;
storedValue = storeNode->getSecondChild();
}
else
{
storedValueChildIndex = 0;
storedValue = storeNode->getFirstChild();
}
if (!storedValue->getOpCode().hasSymbolReference())
return false;
if (storeNode->getSymbolReference() == NULL)
return false;
if (storedValue->getSymbolReference() == NULL)
return false;
if (storedValue->getSymbol() != storeNode->getSymbol())
return false;
if ((storeNode->getOpCode().isIndirect() && !storedValue->getOpCode().isIndirect()) ||
(!storeNode->getOpCode().isIndirect() && storedValue->getOpCode().isIndirect()))
return false;
if (storedValue->getSymbol()->isVolatile())
return false;
if (!storedValue->getOpCode().isLoadVar())
return false;
if (storeNode->getOpCode().isIndirect() &&
storeNode->getFirstChild() != storedValue->getFirstChild())
return false;
if (storeNode->getSymbolReference()->getOffset() != storedValue->getSymbolReference()->getOffset())
return false;
#ifdef J9_PROJECT_SPECIFIC
if (storeNode->getType().isBCD() && !storeNode->isDecimalSizeAndShapeEquivalent(storedValue))
return false;
#endif
// This is an identity store
//
if ((storedValue->getReferenceCount() == 1) || isFirstReferenceToNode(storeNode, storedValueChildIndex, storedValue))
return true;
bool doChecksForAnchors = false;
TR::ILOpCodes anchorOp = TR::BadILOp;
if (comp()->useCompressedPointers() &&
(storedValue->getOpCodeValue() == TR::aloadi) &&
storedValue->getReferenceCount() == 2)
{
doChecksForAnchors = true;
anchorOp = TR::compressedRefs;
}
if (doChecksForAnchors)
{
// search the prev treetop for the anchor, give up if not found
//
// compressedRefs
// load
// store
// ==> load
//
TR::TreeTop *prevTree = _curTree->getPrevTreeTop();
if ((prevTree->getNode()->getOpCodeValue() == anchorOp) &&
(prevTree->getNode()->getFirstChild() == storedValue))
return true;
}
return false;
}
void TR::LocalDeadStoreElimination::examineNode(TR::Node *parent, int32_t childNum, TR::Node *node, TR_BitVector &deadSymbolReferences)
{
if (!isFirstReferenceToNode(parent, childNum, node))
{
node->setLocalIndex(node->getLocalIndex()-1);
return;
}
// Process children
//
for (int32_t i = 0 ; i < node->getNumChildren(); i++)
{
examineNode(node, i, node->getChild(i), deadSymbolReferences);
}
if (!node->getOpCode().hasSymbolReference())
return;
TR::SymbolReference *symRef = node->getSymbolReference();
if (node->getOpCode().isLoadVar() ||
(node->getOpCodeValue() == TR::loadaddr))
{
deadSymbolReferences.reset(symRef->getReferenceNumber());
if (symRef->sharesSymbol())
{
symRef->getUseDefAliases().getAliasesAndSubtractFrom(deadSymbolReferences);
}
killStoreNodes(node);
}
if (node->getOpCode().isCall() ||
node->getOpCode().isCheck() ||
node->getOpCodeValue() == TR::New ||
node->getOpCodeValue() == TR::newarray ||
node->getOpCodeValue() == TR::anewarray ||
node->getOpCodeValue() == TR::multianewarray ||
///node->getOpCodeValue() == TR::checkcast ||
node->getOpCode().isCheckCast() ||
node->getOpCodeValue() == TR::instanceof ||
node->getOpCodeValue() == TR::monent ||
node->getOpCodeValue() == TR::monexit ||
node->mightHaveVolatileSymbolReference())
{
int32_t symRefNum = symRef->getReferenceNumber();
deadSymbolReferences.reset(symRefNum);
symRef->getUseonlyAliases().getAliasesAndSubtractFrom(deadSymbolReferences);
killStoreNodes(node);
bool isCallDirect = node->getOpCode().isCallDirect();
if (symRef->sharesSymbol())
{
symRef->getUseDefAliases(isCallDirect).getAliasesAndSubtractFrom(deadSymbolReferences);
}
}
}
// Simple check to see if a node is removable or not
// Only checks the reference counts of the children (> 1 or not)
// An enhancement must be made here to check if the reference count > 1
// actually can have side effects; i.e. check if it can be allowed
// to swing down to its next use regardless. Thats more expensive and
// I have added a later cleanup pass in LocalOpts that would in fact
// clean up such anchored values created by any opt in general
//
// enhancement made to see if reference counts of children arise out of this node only
// count of edges will equal the accumulated reference count
// using externalReferenceCount = (accumulated reference count) - (edge count traversing tree from this edge)
// node is removable if externalReferenceCount is zero
//
bool TR::LocalDeadStoreElimination::isEntireNodeRemovable(TR::Node *storeNode)
{
if (storeNode->getReferenceCount() > 1)
return false;
TR::Node *storeValue = storeNode->getFirstChild();
rcount_t externalReferenceCount = 0;
setExternalReferenceCountToTree(storeValue, &externalReferenceCount);
return (externalReferenceCount == 0);
}
void TR::LocalDeadStoreElimination::setExternalReferenceCountToTree(TR::Node *node, rcount_t *externalReferenceCount)
{
vcount_t visitCount = comp()->getVisitCount();
// accumulate edge counts
(*externalReferenceCount)--;
if (visitCount == node->getVisitCount())
return;
node->setVisitCount(visitCount);
// accumulate reference counts
(*externalReferenceCount) += node->getReferenceCount();
// Process children
//
for (int32_t i = 0 ; i < node->getNumChildren(); i++)
setExternalReferenceCountToTree(node->getChild(i), externalReferenceCount);
return;
}
bool TR::LocalDeadStoreElimination::seenIdenticalStore(TR::Node *node)
{
for (auto it = _storeNodes->rbegin(); it != _storeNodes->rend(); ++it) {
TR::Node *storeNode = *it;
if (!storeNode) continue;
// the same store node could be commoned in a previous tree
// e.g.
// store
// treetop
// ==> store
// or
// store
// compressedRefs
// ==> store
// or
// store
// fieldAccess
// ==> store
// in this case, the store is not dead and cannot be removed
//
if (storeNode == node)
{
if (trace())
traceMsg(comp(), "seenIdentical nodes %p and %p\n", node, storeNode);
return false;
}
if (areLhsOfStoresSyntacticallyEquivalent(storeNode, node))
return true;
else if ((node->getSymbolReference()->getReferenceNumber() == storeNode->getSymbolReference()->getReferenceNumber()))
return false;
}
return false;
}
bool TR::LocalDeadStoreElimination::areLhsOfStoresSyntacticallyEquivalent(TR::Node *node1, TR::Node *node2)
{
int32_t childAdjust;
int32_t firstNonCheckChildOfFirstNode = 0;
if (node1->getNumChildren() > 0)
{
childAdjust = (node1->getOpCode().isWrtBar()) ? 2 : 1;
firstNonCheckChildOfFirstNode = node1->getNumChildren() - childAdjust;
}
int32_t firstNonCheckChildOfSecondNode = 0;
if (node2->getNumChildren() > 0)
{
childAdjust = (node2->getOpCode().isWrtBar()) ? 2 : 1;
firstNonCheckChildOfSecondNode = node2->getNumChildren() - childAdjust;
}
if (!(firstNonCheckChildOfFirstNode == firstNonCheckChildOfSecondNode))
return false;
TR::ILOpCode &opCode1 = node1->getOpCode();
if (opCode1.hasSymbolReference())
{
if (!((node1->getOpCodeValue() == node2->getOpCodeValue()
&& node1->getSymbolReference()->getReferenceNumber() == node2->getSymbolReference()->getReferenceNumber())))
return false;
}
for (int32_t i = 0; i < firstNonCheckChildOfFirstNode; i++)
{
if (node1->getChild(i) != node2->getChild(i))
return false;
}
return true;
}
void TR::LocalDeadStoreElimination::adjustStoresInfo(TR::Node *node, TR_BitVector &deadSymbolReferences)
{
// Gen the information for this node if it is a store; mark the symbol
// being stored into as unused so that any dead store encountered above
// can be removed if it does not feed a use.
//
if (node->getOpCode().isStore() && !(node->getSymbolReference()->getSymbol()->isAutoOrParm() && node->storedValueIsIrrelevant()))
{
TR::SymbolReference *symRef = node->getSymbolReference();
TR::Symbol *sym = symRef->getSymbol();
deadSymbolReferences.set(symRef->getReferenceNumber());
_storeNodes->push_back(node);
}
else if (node->getOpCode().isCall() ||
node->getOpCodeValue() == TR::monent ||
node->getOpCodeValue() == TR::monexit ||
(node->isGCSafePointWithSymRef() && comp()->getOptions()->realTimeGC()) ||
node->mightHaveVolatileSymbolReference())
{
bool isCallDirect = false;
if (node->getOpCode().isCallDirect())
isCallDirect = true;
if (node->getSymbolReference()->sharesSymbol())
{
node->getSymbolReference()->getUseDefAliases(isCallDirect).getAliasesAndSubtractFrom(deadSymbolReferences);
}
killStoreNodes(node);
}
}
// Create treetops for each of the nodes having reference counts > 0
// and add them in to the list of treetops
//
TR::Node* TR::LocalDeadStoreElimination::getAnchorNode(TR::Node *parentNode, int32_t nodeIndex, TR::Node *node, TR::TreeTop *treeTop, TR::NodeChecklist& visited)
{
if (!visited.contains(node))
visited.add(node);
if (node->getReferenceCount() > 1)
{
TR::TreeTop *prevTree = treeTop->getPrevTreeTop();
TR::TreeTop *anchorTreeTop = TR::TreeTop::create(comp(), TR::Node::create(node, TR::treetop, 1));
anchorTreeTop->getNode()->setAndIncChild(0, node);
setIsFirstReferenceToNode(NULL, 0, anchorTreeTop->getNode());
// first refs may be tracked by parent nodes so we must transfer the first ref flag from
// the original parent (parentNode) to the newly created treetop parent node
if (isFirstReferenceToNode(parentNode, nodeIndex, node))
setIsFirstReferenceToNode(anchorTreeTop->getNode(), 0, node);
anchorTreeTop->join(treeTop);
prevTree->join(anchorTreeTop);
return node;
}
else
{
// TR_FirstNodeReferenceTracker (when used) will mark the earliest reference of a tree commoned node (> 1 ref in same tree)
// as the first overall reference therefore visit children nodes from first (0) to last (node->getNumChildren()-1)
// so the first reference flag is propagated correctly with setIsFirstReferenceToNode in the code just above.
// When not using TR_FirstNodeReferenceTracker the future use count is decremented below to handle this case.
// NOTE: TR_FirstNodeReferenceTracker is now permanently disabled.
for (int i = 0; i < node->getNumChildren(); i++) // visit from 0->numChildren-1 see above comment
{
TR::Node *child = node->getChild(i);
if (!visited.contains(child))
{
getAnchorNode(node, i, child, treeTop, visited);
}
else
{
// child->decReferenceCount();
if (child->getLocalIndex() > 1)
{
// A tree commoned node (> 1 ref in same tree) must have its future use count decremented here
// so the anchored node previously created in this function will be correctly recognized as a first
// reference in examineNode()
child->decLocalIndex();
}
}
}
}
return NULL;
}
void TR::LocalDeadStoreElimination::setupReferenceCounts(TR::Node *node)
{
node->setVisitCount(comp()->getVisitCount());
node->setLocalIndex(node->getReferenceCount());
for (int i = 0; i < node->getNumChildren(); i++)
{
TR::Node *child = node->getChild(i);
if (child->getVisitCount() != comp()->getVisitCount())
{
setupReferenceCounts(child);
}
}
}
void TR::LocalDeadStoreElimination::eliminateDeadObjectInitializations()
{
//
// Assign an index to each local allocation symbol.
//
TR::SymbolReferenceTable *symRefTab = comp()->getSymRefTab();
int32_t symRefCount = comp()->getSymRefCount();
int32_t i;
int32_t numSymbols = 1;
for (i = symRefTab->getIndexOfFirstSymRef(); i < symRefCount; i++)
{
TR::SymbolReference *symRef = symRefTab->getSymRef(i);
if (symRef)
{
TR::Symbol *sym = symRef->getSymbol();
if (sym)
{
if (sym->isLocalObject() &&
sym->getLocalObjectSymbol()->getOpCodeKind() == TR::New)
sym->setLocalIndex(numSymbols++);
else
sym->setLocalIndex(0);
}
}
}
//if (numSymbols == 1)
// return;
LDSBitVector usedLocalObjectSymbols(comp()->trMemory()->currentStackRegion());
TR::TreeTop *tt;
// Go through the trees and find locally allocated objects
// used only for initialization
//
vcount_t visitCount = comp()->incVisitCount();
for (tt = comp()->getStartTree(); tt; tt = tt->getNextTreeTop())
findLocallyAllocatedObjectUses(usedLocalObjectSymbols, NULL, -1, tt->getNode(), visitCount);
visitCount = comp()->incVisitCount();
//TR::Node *currentNew = NULL;
TR_ScratchList<TR::Node> currentNews(trMemory()), removedNews(trMemory());
for (tt = comp()->getStartTree(); tt; tt = tt->getNextTreeTop())
{
TR::Node *node = tt->getNode();
TR::Node *storeNode = node->getStoreNode();
bool removableZeroStore = false;
bool removableLocalObjectStore = false;
TR::Node *currentNewNode = NULL;
if (storeNode &&
storeNode->getOpCode().isStoreIndirect() &&
!storeNode->getSymbolReference()->isUnresolved())
{
if (storeNode->getFirstChild()->getOpCode().hasSymbolReference() &&
storeNode->getFirstChild()->getSymbolReference()->getSymbol()->isLocalObject() &&
(storeNode->getFirstChild()->getSymbolReference()->getSymbol()->getLocalObjectSymbol()->getOpCodeKind() == TR::New) &&
!usedLocalObjectSymbols.get(storeNode->getFirstChild()->getSymbolReference()->getSymbol()->getLocalIndex()))
removableLocalObjectStore = true;
else if (currentNews.find(storeNode->getFirstChild()) ||
(storeNode->getFirstChild()->getOpCode().isArrayRef() &&
currentNews.find(storeNode->getFirstChild()->getFirstChild()) &&
storeNode->getFirstChild()->getSecondChild()->getOpCode().isLoadConst()))
{
currentNewNode = storeNode->getFirstChild();
if (currentNewNode->getOpCode().isArrayRef())
currentNewNode = currentNewNode->getFirstChild();
TR::Node *valueNode = storeNode->getSecondChild();
if(valueNode->isConstZeroValue())
{
//dumpOptDetails(comp(), "Removable zero store %p\n", storeNode);
removableZeroStore = true;
}
else
{
//dumpOptDetails(comp(), "Removing new %p because of cause node %p\n", currentNewNode, node);
currentNews.remove(currentNewNode);
if (!removedNews.find(currentNewNode))
removedNews.add(currentNewNode);
}
}
}
if (examineNewUsesForKill(node, storeNode, ¤tNews, &removedNews, NULL, -1, visitCount))
{
if (!removableZeroStore)
{
currentNews.remove(currentNewNode);
if (!removedNews.find(currentNewNode))
removedNews.add(currentNewNode);
//dumpOptDetails(comp(), "Removing new %p because of cause node %p\n", currentNewNode, node);
}
}
if (removableZeroStore)
{
TR::SymbolReference *symRef = currentNewNode->getSymbolReference();
TR_ExtraInfoForNew *initInfo = symRef->getExtraInfo();
if (initInfo)
{
if (initInfo->zeroInitSlots ||
(initInfo->numZeroInitSlots > 0))
{
int32_t offset = -1;
if (storeNode->getFirstChild()->getOpCode().isArrayRef())
{
if (comp()->target().is64Bit())
{
if (storeNode->getFirstChild()->getSecondChild()->getLongInt() > INT_MAX)
removableZeroStore = false;
else
offset = storeNode->getSymbolReference()->getOffset() +
(int32_t)storeNode->getFirstChild()->getSecondChild()->getLongInt() -
TR::Compiler->om.contiguousArrayHeaderSizeInBytes();
}
else
offset = storeNode->getSymbolReference()->getOffset() +
storeNode->getFirstChild()->getSecondChild()->getInt() -
TR::Compiler->om.contiguousArrayHeaderSizeInBytes();
}
else
offset = storeNode->getSymbolReference()->getOffset() - fe()->getObjectHeaderSizeInBytes();
if (removableZeroStore)
{
if (initInfo->zeroInitSlots &&
!initInfo->zeroInitSlots->get(offset/4))
{
initInfo->zeroInitSlots->set(offset/4);
initInfo->numZeroInitSlots++;
if (storeNode->getOpCode().getSize() > 4)
{
initInfo->zeroInitSlots->set((offset/4)+1);
initInfo->numZeroInitSlots++;
}
}
}
}
else
removableZeroStore = false;
}
else
removableZeroStore = false;
}