forked from eclipse/omr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OMRValuePropagation.cpp
8146 lines (7176 loc) · 305 KB
/
OMRValuePropagation.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, 2022 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/ValuePropagation.hpp"
#include "optimizer/GlobalValuePropagation.hpp"
#include <algorithm>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "codegen/CodeGenerator.hpp"
#include "env/FrontEnd.hpp"
#include "codegen/RecognizedMethods.hpp"
#include "compile/Compilation.hpp"
#include "compile/Method.hpp"
#include "compile/ResolvedMethod.hpp"
#include "compile/SymbolReferenceTable.hpp"
#include "compile/VirtualGuard.hpp"
#include "control/Options.hpp"
#include "control/Options_inlines.hpp"
#include "cs2/bitvectr.h"
#include "env/CompilerEnv.hpp"
#include "env/IO.hpp"
#include "env/PersistentInfo.hpp"
#include "env/TRMemory.hpp"
#include "env/jittypes.h"
#include "il/AliasSetInterface.hpp"
#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/ParameterSymbol.hpp"
#include "il/ResolvedMethodSymbol.hpp"
#include "il/Symbol.hpp"
#include "il/SymbolReference.hpp"
#include "il/TreeTop.hpp"
#include "il/TreeTop_inlines.hpp"
#include "infra/Array.hpp"
#include "infra/Assert.hpp"
#include "infra/Bit.hpp"
#include "infra/BitVector.hpp"
#include "infra/Cfg.hpp"
#include "infra/Link.hpp"
#include "infra/List.hpp"
#include "infra/CfgEdge.hpp"
#include "infra/CfgNode.hpp"
#include "optimizer/Inliner.hpp"
#include "optimizer/Optimization.hpp"
#include "optimizer/Optimization_inlines.hpp"
#include "optimizer/Optimizations.hpp"
#include "optimizer/Optimizer.hpp"
#include "optimizer/Structure.hpp"
#include "optimizer/Reachability.hpp"
#include "optimizer/UseDefInfo.hpp"
#include "optimizer/VPConstraint.hpp"
#include "optimizer/TransformUtil.hpp"
#include "ras/Debug.hpp"
#ifdef J9_PROJECT_SPECIFIC
#include "env/ClassTableCriticalSection.hpp"
#include "env/CHTable.hpp"
#include "env/PersistentCHTable.hpp"
#include "runtime/RuntimeAssumptions.hpp"
#include "runtime/J9Profiler.hpp"
#include "runtime/J9ValueProfiler.hpp"
#endif
#define OPT_DETAILS "O^O VALUE PROPAGATION: "
#define NEED_WRITE_BARRIER 1
#define NEED_ARRAYSTORE_CHECK 2
#define NEED_ARRAYSTORE_CHECK_AND_WRITE_BARRIER 3
#define STRING_ARRAY_SIZE 100
extern TR::TreeTop *createStoresForArraycopyChildren(TR::Compilation *comp, TR::TreeTop *arrayTree, TR::SymbolReference * &srcObjRef, TR::SymbolReference * &dstObjRef, TR::SymbolReference * &srcRef, TR::SymbolReference * &dstRef, TR::SymbolReference * &lenRef);
void collectArraylengthNodes(TR::Node *node, vcount_t visitCount, List<TR::Node> *arraylengthNodes)
{
if (node->getVisitCount() == visitCount)
return;
node->setVisitCount(visitCount);
if (node->getOpCode().isArrayLength())
{
arraylengthNodes->add(node);
}
int32_t childNum;
for (childNum=0;childNum < node->getNumChildren();childNum++)
collectArraylengthNodes(node->getChild(childNum), visitCount, arraylengthNodes);
}
OMR::ValuePropagation::Relationship *OMR::ValuePropagation::createRelationship(int32_t relative, TR::VPConstraint *constraint)
{
Relationship *rel = _relationshipCache.pop();
if (!rel)
rel = new (trStackMemory()) Relationship;
rel->relative = relative;
rel->constraint = constraint;
rel->setNext(NULL);
return rel;
}
void OMR::ValuePropagation::freeRelationship(Relationship *rel)
{
_relationshipCache.add(rel);
}
void OMR::ValuePropagation::freeRelationships(TR_LinkHead<Relationship> &list)
{
Relationship *cur, *next;
for (cur = list.getFirst(); cur; cur = next)
{
next = cur->getNext();
freeRelationship(cur);
}
list.setFirst(NULL);
}
OMR::ValuePropagation::StoreRelationship *OMR::ValuePropagation::createStoreRelationship(TR::Symbol *symbol, Relationship *firstRel)
{
StoreRelationship *rel = _storeRelationshipCache.pop();
if (!rel)
rel = new (trStackMemory()) StoreRelationship;
rel->symbol = symbol;
rel->relationships.setFirst(firstRel);
rel->setNext(NULL);
return rel;
}
void OMR::ValuePropagation::freeStoreRelationship(StoreRelationship *rel)
{
freeRelationships(rel->relationships);
_storeRelationshipCache.add(rel);
}
void OMR::ValuePropagation::freeStoreRelationships(TR_LinkHead<StoreRelationship> &list)
{
StoreRelationship *cur, *next;
for (cur = list.getFirst(); cur; cur = next)
{
next = cur->getNext();
freeStoreRelationship(cur);
}
list.setFirst(NULL);
}
void OMR::ValuePropagation::freeValueConstraints(ValueConstraints &valueConstraints)
{
_vcHandler.empty(valueConstraints);
}
OMR::ValuePropagation::ValueConstraint *OMR::ValuePropagation::copyValueConstraints(ValueConstraints &valueConstraints)
{
return _vcHandler.copyAll(valueConstraints);
}
void OMR::ValuePropagation::addConstraint(TR::VPConstraint *constraint, int32_t hash)
{
ConstraintsHashTableEntry *entry = new (trStackMemory()) ConstraintsHashTableEntry;
entry->constraint = constraint;
entry->next = _constraintsHashTable[hash];
_constraintsHashTable[hash] = entry;
}
void OMR::ValuePropagation::addLoopDef(TR::Node *node)
{
// If the loop def entry does not already exist, create it
//
int32_t hash = (((uintptr_t)node) >> 2) % VP_HASH_TABLE_SIZE;
LoopDefsHashTableEntry *entry;
for (entry = _loopDefsHashTable[hash]; entry; entry = entry->next)
{
if (entry->node == node)
return;
}
entry = new (trStackMemory()) LoopDefsHashTableEntry;
entry->node = node;
entry->region = NULL;
entry->next = _loopDefsHashTable[hash];
_loopDefsHashTable[hash] = entry;
}
OMR::ValuePropagation::LoopDefsHashTableEntry *OMR::ValuePropagation::findLoopDef(TR::Node *node)
{
// Find the loop def entry
//
int32_t hash = (((uintptr_t)node) >> 2) % VP_HASH_TABLE_SIZE;
LoopDefsHashTableEntry *entry;
for (entry = _loopDefsHashTable[hash]; entry; entry = entry->next)
{
if (entry->node == node)
return entry;
}
return NULL;
}
static bool worthPropagatingConstraints(TR::Compilation *comp, bool isGlobalPropagation)
{
if ((!isGlobalPropagation && (comp->getMethodHotness() > cold)) || (comp->getMethodHotness() > warm))
return true;
return false;
}
bool OMR::ValuePropagation::propagateConstraint(TR::Node *node, int32_t valueNumber, Relationship *first, Relationship *rel, ValueConstraints *valueConstraints)
{
// Go through the relationships list for this value number and propagate the
// effects of the given new relationship to the others.
//
// Return true if the propagation was successful, false if constraints could
// not be intersected.
//
// Make sure the propagation recursion is not getting too deep
//
_propagationDepth++;
if (_propagationDepth > _maxPropagationDepth)
{
_reachedMaxRelationDepth = true;
if (trace())
traceMsg(comp(), "===>Reached Max Relational Propagation Depth: %d\n", _propagationDepth);
///TR_ASSERT(0, "Reached max relation depth %p\n", _propagationDepth);
}
TR::VPConstraint *c;
Relationship *cur = first;
for (cur = first; cur && worthPropagatingConstraints(comp(), _isGlobalPropagation); cur = cur->getNext())
{
if (cur->relative == rel->relative)
continue;
if (rel->relative == AbsoluteConstraint)
{
// Propagate new absolute constraint to existing relative constraint
//
TR_ASSERT(cur->constraint->asRelation(), "assertion failure");
c = cur->constraint->asRelation()->propagateAbsoluteConstraint(rel->constraint, cur->relative, this);
if (c)
{
c = addConstraintToList(node, cur->relative, AbsoluteConstraint, c, valueConstraints);
if (!c)
return false;
}
}
else if (cur->relative == AbsoluteConstraint)
{
// Propagate existing absolute constraint to new relative constraint
//
TR_ASSERT(rel->constraint->asRelation(), "assertion failure");
c = rel->constraint->asRelation()->propagateAbsoluteConstraint(cur->constraint, rel->relative, this);
if (c)
{
c = addConstraintToList(node, rel->relative, AbsoluteConstraint, c, valueConstraints);
if (!c)
return false;
}
}
else
{
// Propagate relative constraint to others
//
TR_ASSERT(cur->constraint->asRelation(), "assertion failure");
TR_ASSERT(rel->constraint->asRelation(), "assertion failure");
c = cur->constraint->asRelation()->propagateRelativeConstraint(rel->constraint->asRelation(), cur->relative, rel->relative, this);
if (c)
{
c = addConstraintToList(node, cur->relative, rel->relative, c, valueConstraints);
if (!c)
return false;
}
}
}
// If this is a relative constraint, propagate the same relationship to the
// relative.
//
if (rel->relative != AbsoluteConstraint)
{
TR_ASSERT(rel->constraint->asRelation(), "assertion failure");
if (!addConstraintToList(node, rel->relative, valueNumber, rel->constraint->asRelation()->getComplement(this), valueConstraints))
return false;
}
_propagationDepth--;
return true;
}
// Add or modify a relationship for a value number in a value constraint list.
// If the list is null, add the relationship as a global constraint on the value
// number.
// Return the relationship if it is new or changed from the existing one.
//
TR::VPConstraint *OMR::ValuePropagation::addConstraintToList(TR::Node *node, int32_t valueNumber, int32_t relative, TR::VPConstraint *constraint, ValueConstraints *valueConstraints, bool replaceExisting)
{
// If we are really adding a global constraint, go do that.
//
if (!valueConstraints)
{
TR_ASSERT(!replaceExisting, "assertion failure");
return addGlobalConstraint(node, valueNumber, constraint, relative);
}
TR::VPConstraint *c = NULL;
Relationship *rel = NULL, *prevRel = NULL;
bool newOrChanged = false;
TR::VPConstraint *existingGlobalConstraint = NULL;
// Apply global constraints to the new constraint.
// If the new constraint is the same as an existing global constraint there
// is nothing to do.
//
GlobalConstraint *gc = findGlobalConstraint(valueNumber);
if (gc)
{
for (rel = gc->constraints.getFirst(); rel; rel = rel->getNext())
{
if (rel->relative == relative)
{
c = constraint->intersect(rel->constraint, this);
// FIXME:the code below will propagate relative block constraints to
// the global constraints list; but does not yet work correctly.
// commented for now.
#if 0
if (c == NULL)
return c;
else if (c == rel->constraint)
{
if (relative == AbsoluteConstraint)
return c;
else
break;
}
#else
if (c == rel->constraint || c == NULL)
{
// intersection failed
if (c == NULL && removeConstraints())
{
///setIntersectionFailed(true);
///removeConstraints(valueNumber, NULL);
}
if (c != NULL)
{
existingGlobalConstraint = c;
constraint = c;
break;
}
else
return c;
}
#endif
constraint = c;
break;
}
}
}
// Find or insert the value constraint
//
ValueConstraint *cur = _vcHandler.find(valueNumber, *valueConstraints);
// Find the relationship. The list is ordered by relative value number.
//
if (!existingGlobalConstraint)
{
if (!cur)
cur = _vcHandler.findOrCreate(valueNumber, *valueConstraints);
// Find the relationship. The list is ordered by relative value number.
//
int32_t numRelatives = 0;
for (rel = cur->relationships.getFirst(), prevRel = NULL; rel && rel->relative < relative; prevRel = rel, rel = rel->getNext())
{ numRelatives++; }
// If the relationship does not exist, we must have traversed the whole list
// Make sure that our lists are not getting too long -
//
static const char *p = feGetEnv("TR_VPMaxRelDepth");
static const int32_t maxRelDepth = p ? atoi(p) : 64;
if (!rel && numRelatives > maxRelDepth)
{
_reachedMaxRelationDepth = true;
if (trace())
traceMsg(comp(), "===>Reached Max Relational Propagation Depth: %d\n", numRelatives);
}
// If the relationship does not exist, create it.
//
if (!rel || rel->relative > relative)
{
rel = createRelationship(relative, constraint);
cur->relationships.insertAfter(prevRel, rel);
newOrChanged = true;
c = constraint;
}
// If the relationship already exists see if it can be further constrained
// by the new constraint
//
else
{
c = replaceExisting ? constraint : constraint->intersect(rel->constraint, this);
if (!c)
{
// intersection failed
if (removeConstraints())
{
///setIntersectionFailed(true);
////removeConstraints(valueNumber, valueConstraints);
}
return NULL; // can't intersect the constraints
}
if (c != rel->constraint)
{
// Replace the constraint with the intersected value
//
rel->constraint = c;
newOrChanged = true;
}
}
}
// If there are store relationships corresponding to the one being added
// or changed, apply the new constraint to them too.
//
StoreRelationship *store;
if (cur)
{
for (store = cur->storeRelationships.getFirst(); store; store = store->getNext())
{
if (isUnreachableStore(store))
continue;
TR::VPConstraint *storeConstraint = NULL;
Relationship *storeRel, *prev;
TR::Symbol *storeSym = store->symbol;
// don't update store constraints for symbols
// that don't match
//
bool updateStoreConstraint = true;
if (node && node->getOpCode().hasSymbolReference() &&
(node->getSymbol() != storeSym))
updateStoreConstraint = false;
if (updateStoreConstraint)
{
for (storeRel = store->relationships.getFirst(), prev = NULL; storeRel; prev = storeRel, storeRel = storeRel->getNext())
{
if (storeRel->relative > relative)
{
storeRel = NULL;
break;
}
else if (storeRel->relative == relative)
{
storeConstraint = constraint->intersect(storeRel->constraint, this);
break;
}
}
if (!storeRel)
{
storeRel = createRelationship(relative, constraint);
store->relationships.insertAfter(prev, storeRel);
// check for the special class constraint
//
///storeConstraint = constraint->intersect(constraint, this);
//
//FIXME: ugly, uncomment line above and comment this 'if' block
if (TR::VPConstraint::isSpecialClass((uintptr_t)constraint->getClass()))
{
TR_ASSERT(constraint->asClass(), "special class constraint must be VPClass");
traceMsg(comp(), "found special class constraint!\n");
// remove the type information
storeConstraint = TR::VPClass::create(this, NULL, constraint->getClassPresence(), constraint->getPreexistence(), constraint->getArrayInfo(), constraint->getObjectLocation());
}
else
storeConstraint = constraint;
}
TR_ASSERT(!replaceExisting, "assertion failure");
if (storeConstraint)
{
if (storeConstraint != storeRel->constraint)
{
storeRel->constraint = storeConstraint;
if (trace() && node)
{
traceMsg(comp(), " %s [%p] gets new store constraint:", node->getOpCode().getName(), node);
storeRel->print(this, valueNumber, 1);
}
}
}
else
{
// This store value is unreachable on this path
//
//FIXME: this is aggressive and does not handle
//stores in loops. this snippet of code is only
//correct for acyclic regions.
///if (lastTimeThrough())
/// setUnreachableStore(store);
}
} // updateStoreConstraint
} // store = cur->storeRelationships.getFirst(); store; store = store->getNext()
} // if (cur)
if (existingGlobalConstraint)
return existingGlobalConstraint;
if (newOrChanged)
{
if (trace() && node)
{
traceMsg(comp(), " n%in %s gets new constraint:", node->getGlobalIndex(), node->getOpCode().getName());
rel->print(this, valueNumber, 1);
traceMsg(comp(), "type of constraint - longConstraint: %p, intConstraint: %p, shortConstraint: %p\n", rel->constraint->asLongConstraint(), rel->constraint->asIntConstraint(), rel->constraint->asShortConstraint());
}
// Propagate the effects of the constraint to other constraints
// associated with this value number
//
if (valueNumber < _firstUnresolvedSymbolValueNumber)
{
TR_ASSERT(!replaceExisting, "should be merging real value numbers");
bool propFailed = false;
if (!propagateConstraint(node, valueNumber, cur->relationships.getFirst(), rel, valueConstraints))
propFailed = true;
static const bool relPropGlobal = feGetEnv("TR_enableVPRelPropGlobal") != NULL;
if (relPropGlobal)
{
// Propagate according to any global relative constraints as well,
// but keeping the resulting constraints local in valueConstraints.
GlobalConstraint *globalConstraint = findGlobalConstraint(valueNumber);
if (!propFailed && globalConstraint != NULL)
{
auto firstReln = globalConstraint->constraints.getFirst();
if (!propagateConstraint(node, valueNumber, firstReln, rel, valueConstraints))
propFailed = true;
}
}
if (propFailed)
{
// propagation failed
if (removeConstraints())
{
////setIntersectionFailed(true);
////removeConstraints(valueNumber, valueConstraints);
}
// reset the propagationDepth here
//
_propagationDepth = 0;
return NULL;
}
}
}
return c;
}
TR::VPConstraint *OMR::ValuePropagation::addGlobalConstraint(TR::Node *node, TR::VPConstraint *constraint, TR::Node *relative)
{
// No global constraints for local value propagation
//
if (!_isGlobalPropagation)
return addBlockConstraint(node, constraint, relative);
int32_t relativeVN = relative ? getValueNumber(relative) : AbsoluteConstraint;
return addGlobalConstraint(node, getValueNumber(node), constraint, relativeVN);
}
TR::VPConstraint *OMR::ValuePropagation::addGlobalConstraint(TR::Node *node, int32_t valueNumber, TR::VPConstraint *constraint, int32_t relative)
{
TR_ASSERT(_isGlobalPropagation, "Local VP can't add global constraint");
// Add the relationship into the relationship list for the value
//
GlobalConstraint *gc = findGlobalConstraint(valueNumber);
if (!gc)
gc = createGlobalConstraint(valueNumber);
int32_t numRelatives = 0;
Relationship *rel, *prev;
for (rel = gc->constraints.getFirst(), prev = NULL;
rel && rel->relative < relative;
prev = rel, rel = rel->getNext())
{ numRelatives++; }
bool newOrChanged = false;
static const char *p = feGetEnv("TR_VPMaxRelDepth");
static const int32_t maxRelDepth = p ? atoi(p) : 64;
if (!rel && numRelatives > maxRelDepth)
{
_reachedMaxRelationDepth = true;
if (trace())
traceMsg(comp(), "===>Reached Max Relational Propagation Depth: %d\n", numRelatives);
}
if (!rel || rel->relative > relative)
{
rel = createRelationship(relative, constraint);
gc->constraints.insertAfter(prev, rel);
newOrChanged = true;
}
// See if the constraint can be further constrained by the new constraint
//
// When we finally fix ValuePropagation to fully support unsigned
// arithmetic, we can get rid of the c in the assume and following if)
TR::VPConstraint *c = constraint->intersect(rel->constraint, this);
// enable this code now since unsigned types are supported in VP
//TR_ASSERT(c, "Cannot intersect constraints");
// intersection failed
if (!c && removeConstraints())
{
////setIntersectionFailed(true);
////removeConstraints(valueNumber, NULL);
return c;
}
if (trace() && c == NULL)
traceMsg(comp(), "Cannot intersect constraints on %s [%p]", node->getOpCode().getName(), node);
// The assume below is informational in nature and should therefore not be triggered if production builds include assumes (as on WCode or binopt).
// This condition should rarely show up in WCode or binopt (as Simplifier transformations will homogenize the types) but can happen, for example, when an aggregate symbol is
// referenced in one place on an oload and another as an iload. MG: Update assert?
TR_ASSERT(c, "Cannot intersect constraints on %s [%p]", node->getOpCode().getName(), node);
if (c && c != rel->constraint)
{
// Replace the constraint with the intersected value
//
rel->constraint = c;
newOrChanged = true;
}
if (newOrChanged)
{
if (trace() && node)
{
traceMsg(comp(), " %s [%p] gets new global constraint:", node->getOpCode().getName(), node);
rel->print(this, valueNumber, 1);
}
// Propagate the effects of the constraint to other constraints
// associated with this value number
//
bool propagateOK = propagateConstraint(node, valueNumber, gc->constraints.getFirst(), rel, NULL);
// propagation of constraints failed
if (!propagateOK)
{
if (removeConstraints())
{
////setIntersectionFailed(true);
///removeConstraints(valueNumber, NULL);
return c;
}
// reset the propagationDepth here
//
_propagationDepth = 0;
}
TR_ASSERT(propagateOK, "Cannot intersect constraints");
}
else if (c)
{
if (trace() && node)
{
traceMsg(comp(), " %s [%p] found existing global constraint value number %d (%p): ", node->getOpCode().getName(), node, valueNumber, c);
c->print(comp(), comp()->getOutFile());
traceMsg(comp(),"\n");
}
}
return c;
}
TR::VPConstraint *OMR::ValuePropagation::addEdgeConstraint(TR::Node *node, TR::VPConstraint *constraint, EdgeConstraints *constraints, TR::Node *relative)
{
if (!_isGlobalPropagation)
return constraint;
int32_t valueNumber = getValueNumber(node);
int32_t relativeVN = relative ? getValueNumber(relative) : AbsoluteConstraint;
constraint = addConstraintToList(node, valueNumber, relativeVN, constraint, &constraints->valueConstraints);
if (!constraint && removeConstraints())
{
///setIntersectionFailed(true);
removeConstraints(valueNumber, &constraints->valueConstraints, false);
}
return constraint;
}
TR::VPConstraint *OMR::ValuePropagation::addBlockConstraint(TR::Node *node, TR::VPConstraint *constraint, TR::Node *relative, bool mustBeValid)
{
if (!constraint)
return NULL;
int32_t valueNumber = getValueNumber(node);
int32_t relativeVN = relative ? getValueNumber(relative) : AbsoluteConstraint;
constraint = addConstraintToList(node, valueNumber, relativeVN, constraint, &_curConstraints);
// intersection failed
if (!constraint && removeConstraints())
{
///setIntersectionFailed(true);
removeConstraints(valueNumber, &_curConstraints, true);
return constraint;
}
///TR_ASSERT(constraint || !mustBeValid, "Cannot intersect constraints");
return constraint;
}
TR::VPConstraint *OMR::ValuePropagation::addBlockOrGlobalConstraint(TR::Node *node, TR::VPConstraint *constraint, bool isGlobal, TR::Node *relative)
{
if (isGlobal)
return addGlobalConstraint(node, constraint, relative);
return addBlockConstraint(node, constraint, relative);
}
void OMR::ValuePropagation::mergeRelationships(TR_LinkHead<Relationship> &fromList, TR_LinkHead<Relationship> &toList,
int32_t valueNumber, bool preserveFrom, StoreRelationship *mergingStore, List<TR::Symbol> *storeSymbols,
bool inBothLists)
{
if (trace())
traceMsg(comp(), "Merging relationships for value number: %i\n", valueNumber);
// Merge (i.e. logical OR) the "from" list into the "to" list.
// Both lists are ordered by relation number.
// A relationship that only exists in one of the lists is assumed
// to be generalized in the other list.
// See if there are global constraints for this value number. A
// merged constraint need not be kept in the "to" list if it is already a
// global constraint.
//
Relationship *global = NULL;
GlobalConstraint *gc = findGlobalConstraint(valueNumber);
if (gc)
global = gc->constraints.getFirst();
Relationship *from = fromList.getFirst();
bool isFromEmpty = fromList.isEmpty();
Relationship *to = toList.getFirst();
bool isToEmpty = toList.isEmpty();
Relationship *prev = NULL;
Relationship *rel;
TR::VPConstraint *newConstraint;
// allow store relationships to survive control flow merges
//
bool preserveStoreRelationships = true; //comp()->fe()->aggressiveSmallAppOpts();
if (!preserveFrom)
fromList.setFirst(NULL);
while (from || to)
{
if (!to || (from && from->relative < to->relative))
{
// The "from" value does not exist in the "to" list.
// Do not add it to the "to" list
//
if (preserveStoreRelationships &&
mergingStore &&
(from->relative == AbsoluteConstraint) &&
(!inBothLists /* || !isToEmpty */)) // commented out the !isToEmpty because it is not guaranteed to be safe to take an absolute constraint that only exists on one side
{
rel = createRelationship(from->relative, from->constraint);
rel->setNext(to);
to = rel;
}
rel = from->getNext();
if (!preserveFrom)
freeRelationship(from);
from = rel;
continue;
}
if (!from || from->relative > to->relative)
{
// The "to" value does not exist int the "from" list.
// Remove it from the "to" list
//
rel = to->getNext();
if (!preserveStoreRelationships ||
!mergingStore ||
(to->relative != AbsoluteConstraint) ||
(inBothLists /* && isFromEmpty */)) // but do not remove the storeRel
{
toList.removeAfter(prev, to);
freeRelationship(to);
}
to = rel;
continue;
}
if (from->constraint == to->constraint)
{
// Constraints are the same - move on
//
prev = to;
to = to->getNext();
rel = from->getNext();
if (!preserveFrom)
freeRelationship(from);
from = rel;
continue;
}
if (trace())
{
traceMsg(comp(), "Attempting merge from: ");
from->print(this);
traceMsg(comp(), "\n merge To: ");
to->print(this);
traceMsg(comp(), "\n");
}
newConstraint = to->constraint->merge(from->constraint, this);
rel = from->getNext();
if (!preserveFrom)
freeRelationship(from);
from = rel;
if (newConstraint)
{
// If the new merged constraint is the same as the corresponding
// global constraint there is no need to add it as a local constraint
//
while (global && global->relative < to->relative)
global = global->getNext();
if (global && global->relative == to->relative &&
global->constraint == newConstraint)
newConstraint = NULL;
}
// If merging the constraints resulted in no constraint at all remove the
// entry from the "to" list.
//
if (!newConstraint)
{
rel = to->getNext();
toList.removeAfter(prev, to);
freeRelationship(to);
to = rel;
}
// Otherwise replace the existing constraint with the new constraint.
//
else
{
to->constraint = newConstraint;
prev = to;
to = to->getNext();
}
}
}
void OMR::ValuePropagation::mergeStoreRelationships(ValueConstraint *fromvc, ValueConstraint *tovc, bool preserveFrom)
{
// Merge (i.e. logical OR) the store relationships in the "from" list into
// the "to" list.
// Both lists are ordered by symbol address.
// Note that the "from" value constraint may be null
StoreRelationship *from = fromvc ? fromvc->storeRelationships.getFirst() : NULL;
StoreRelationship *to = tovc->storeRelationships.getFirst();
StoreRelationship *prev = NULL;
StoreRelationship *store;
int32_t valueNumber = tovc->getValueNumber();
TR_ScratchList<TR::Symbol> storeSymbols(comp()->trMemory());
storeSymbols.deleteAll();
bool inBothLists = false;
if (fromvc && !preserveFrom)
fromvc->storeRelationships.setFirst(NULL);
// Find the back-edge constraints just in case they are needed
//
ValueConstraint *backEdgeStores = NULL;
if (_loopInfo && _loopInfo->_backEdgeConstraints)
{
backEdgeStores = _vcHandler.find(valueNumber, _loopInfo->_backEdgeConstraints->valueConstraints);
}
// If only one list has store constraints for a given symbol, one of two
// things can happen:
// 1) If this is the second time through a loop and there is a back-edge
// constraint for the symbol, use it as the missing constraint
// 2) Otherwise make the store constraint generalized.
//
// If both lists have store constraints for a symbol, the relationship lists
// are merged.
//
prev = NULL;
while (from || to)
{
TR_LinkHead<Relationship> *fromRel, *toRel;
TR::Symbol *symbol;
bool preserve = preserveFrom;
if (!from || (to && from->symbol > to->symbol))
{
if (isUnreachableStore(to))
{
// From is missing and to is unreachable - ignore the store
//
store = to->getNext();
tovc->storeRelationships.removeAfter(prev, to);
freeStoreRelationship(to);
to = store;
continue;
}
symbol = to->symbol;
fromRel = NULL;
if (backEdgeStores)
{
store = findStoreRelationship(backEdgeStores->storeRelationships, symbol);
if (store)
{
// this is probably a typo...we should actually
// be interested in the relationships associated
// with the store instead
///fromRel = &backEdgeStores->relationships;
fromRel = &store->relationships;
preserve = true;
}
}
toRel = &to->relationships;
prev = to;
to = to->getNext();
}
else if (!to || from->symbol < to->symbol)
{
if (isUnreachableStore(from))
{
// To is missing and from is unreachable - ignore the store
//
store = from->getNext();
if (!preserveFrom)
freeStoreRelationship(from);
from = store;
continue;
}
symbol = from->symbol;
store = createStoreRelationship(symbol, NULL);
if (preserveFrom)
store->relationships.setFirst(copyRelationships(from->relationships.getFirst()));
else
{
store->relationships.setFirst(from->relationships.getFirst());
from->relationships.setFirst(NULL);
}
tovc->storeRelationships.insertAfter(prev, store);
prev = store;
fromRel = NULL;
if (backEdgeStores)
{
store = findStoreRelationship(backEdgeStores->storeRelationships, symbol);
if (store)
{
fromRel = &backEdgeStores->relationships;
preserve = true;
}