-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathOMRCfg.cpp
3289 lines (2812 loc) · 114 KB
/
OMRCfg.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 "infra/Cfg.hpp"
#include <algorithm>
#include <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "compile/Compilation.hpp"
#include "control/Options.hpp"
#include "control/Options_inlines.hpp"
#include "env/TRMemory.hpp"
#include "env/PersistentInfo.hpp"
#include "il/Block.hpp"
#include "il/ILOpCodes.hpp"
#include "il/ILOps.hpp"
#include "il/LabelSymbol.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/Cfg.hpp"
#include "infra/deque.hpp"
#include "infra/Link.hpp"
#include "infra/List.hpp"
#include "infra/Stack.hpp"
#include "infra/CfgEdge.hpp"
#include "infra/CfgNode.hpp"
#include "optimizer/Optimizer.hpp"
#include "optimizer/Structure.hpp"
#include "optimizer/StructuralAnalysis.hpp"
#include "ras/Debug.hpp"
#ifdef __MVS__
#include <stdlib.h>
#endif
#define OPT_DETAILS "O^O LOCAL OPTS: "
TR::CFG *
OMR::CFG::self() {
return static_cast<TR::CFG*>(this);
}
const char*
OMR::CFG::blockFrequencyNames[NUMBER_BLOCK_FREQUENCIES] =
{
"UNKNOWN_COLD_BLOCK_COUNT",
"VERSIONED_COLD_BLOCK_COUNT",
"UNRESOLVED_COLD_BLOCK_COUNT",
"CATCH_COLD_BLOCK_COUNT",
"INTERP_CALLEE_COLD_BLOCK_COUNT",
"REVERSE_ARRAYCOPY_COLD_BLOCK_COUNT"
};
TR::CFGNode *
OMR::CFG::addNode(TR::CFGNode *n, TR_RegionStructure *parent, bool isEntryInParent)
{
_nodes.add(n);
n->setNumber(allocateNodeNumber());
if (parent &&
getStructure())
{
TR::Block *block = n->asBlock();
if (block)
{
TR_BlockStructure *blockStructure = block->getStructureOf();
TR_StructureSubGraphNode *blockNode = NULL;
if (!blockStructure)
blockStructure = new (structureMemoryRegion()) TR_BlockStructure(comp(), block->getNumber(), block);
else
{
TR_StructureSubGraphNode *node;
TR_RegionStructure::Cursor si(*parent);
for (node = si.getCurrent(); node != NULL; node = si.getNext())
{
if (node->getStructure() == blockStructure)
{
blockNode = node;
break;
}
}
}
blockStructure->setNumber(n->getNumber());
if (!blockNode)
{
blockNode = new (structureMemoryRegion()) TR_StructureSubGraphNode(blockStructure);
if (!isEntryInParent)
{
parent->addSubNode(blockNode);
}
else
{
//TR_ASSERT(0, "Adding node as entry in structure\n");
setStructure(NULL);
}
}
blockNode->setNumber(n->getNumber());
}
else
TR_ASSERT(0, "Trying to add block structure without creating a block\n");
}
return n;
}
void
OMR::CFG::addEdge(TR::CFGEdge *e)
{
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
{
traceMsg(comp(),"\nAdding edge %d-->%d:\n", e->getFrom()->getNumber(), e->getTo()->getNumber());
}
_numEdges++;
// Tell the control tree to modify the structures containing this edge
//
if (getStructure() != NULL)
{
getStructure()->addEdge(e, false);
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
{
traceMsg(comp(),"\nStructures after adding edge %d-->%d:\n", e->getFrom()->getNumber(), e->getTo()->getNumber());
comp()->getDebug()->print(comp()->getOutFile(), _rootStructure, 6);
}
}
}
TR::CFGEdge *
OMR::CFG::addEdge(TR::CFGNode *f, TR::CFGNode *t)
{
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
{
traceMsg(comp(),"\nAdding real edge %d-->%d:\n", f->getNumber(), t->getNumber());
}
TR_ASSERT(!f->hasExceptionSuccessor(t), "adding a non exception edge when there's already an exception edge");
TR::CFGEdge * e = TR::CFGEdge::createEdge(f, t, _internalMemoryRegion);
addEdge(e);
return e;
}
void
OMR::CFG::addExceptionEdge(
TR::CFGNode *f,
TR::CFGNode *t)
{
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
{
traceMsg(comp(),"\nAttempting to add exception edge %d-->%d:\n", f->getNumber(), t->getNumber());
}
TR::Block * newCatchBlock = toBlock(t);
for (auto e = f->getExceptionSuccessors().begin(); e != f->getExceptionSuccessors().end(); ++e)
{
TR::Block * existingCatchBlock = toBlock((*e)->getTo());
if (newCatchBlock == existingCatchBlock) return;
// OSR exception edges are special and we do not want any 'optimization' done to them
// from the following special checks
if (newCatchBlock->isOSRCatchBlock() || existingCatchBlock->isOSRCatchBlock()) continue;
// If the existing catch block is going to be considered first and it catches everything that
// the new catch block does then the new catch block isn't reaching from the 'f' block
//
// Catch block 'A' is considered before catch block 'B' if 'A' is from a greater inline depth
// or 'A' and 'B' are from the same inline depth and 'A' handler index is less than 'B's.
//
int32_t existingDepth = existingCatchBlock->getInlineDepth();
int32_t newDepth = newCatchBlock->getInlineDepth();
if (existingDepth < newDepth ||
(existingDepth == newDepth && existingCatchBlock->getHandlerIndex() > newCatchBlock->getHandlerIndex()))
continue;
// The existing catch block is going to be considered first. Don't add an edge to the new catch
// block if the existing one catches everything that the new one catches.
//
/////void * newEC = newCatchBlock->getExceptionClass(), * existingEC = existingCatchBlock->getExceptionClass();
if (existingCatchBlock->getCatchType() == 0 ||
/////(newEC && existingEC && isInstanceOf(newEC, existingEC)) ||
(existingDepth == newDepth && existingCatchBlock->getCatchType() == newCatchBlock->getCatchType()))
{
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
{
traceMsg(comp(),"\nAddition of exception edge aborted - existing catch alredy handles this case!");
}
return;
}
}
addExceptionEdgeUnchecked(f, t);
}
void
OMR::CFG::addExceptionEdgeUnchecked(
TR::CFGNode *f,
TR::CFGNode *t)
{
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
{
traceMsg(comp(),"\nAdding exception edge %d-->%d:\n", f->getNumber(), t->getNumber());
}
TR_ASSERT(!f->hasSuccessor(t), "adding an exception edge when there's already a non exception edge");
TR::CFGEdge* e = TR::CFGEdge::createExceptionEdge(f,t, _internalMemoryRegion);
_numEdges++;
// Tell the control tree to modify the structures containing this edge
if (getStructure() != NULL)
{
getStructure()->addEdge(e, true);
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
{
traceMsg(comp(),"\nStructures after adding exception edge %d-->%d:\n", f->getNumber(), t->getNumber());
comp()->getDebug()->print(comp()->getOutFile(), _rootStructure, 6);
}
}
}
void
OMR::CFG::addSuccessorEdges(TR::Block * block)
{
TR::Node * node = block->getLastRealTreeTop()->getNode();
switch (node->getOpCode().getOpCodeValue())
{
case TR::table:
case TR::lookup:
{
vcount_t visitCount = comp()->incVisitCount();
int32_t n = node->getCaseIndexUpperBound();
for (int32_t i = 1; i < n; ++i)
{
TR::TreeTop * target = node->getChild(i)->getBranchDestination();
TR::Block * targetBlock = target->getNode()->getBlock();
if (targetBlock->getVisitCount() != visitCount)
{
addEdge(block, targetBlock);
targetBlock->setVisitCount(visitCount);
}
}
break;
}
case TR::ificmpeq: case TR::ificmpne: case TR::ificmplt: case TR::ificmpge: case TR::ificmpgt: case TR::ificmple:
case TR::ifiucmplt: case TR::ifiucmpge: case TR::ifiucmpgt: case TR::ifiucmple:
case TR::iflcmpeq: case TR::iflcmpne: case TR::iflcmplt: case TR::iflcmpge: case TR::iflcmpgt: case TR::iflcmple:
case TR::iffcmpeq: case TR::iffcmpne: case TR::iffcmplt: case TR::iffcmpge: case TR::iffcmpgt: case TR::iffcmple:
case TR::ifdcmpeq: case TR::ifdcmpne: case TR::ifdcmplt: case TR::ifdcmpge: case TR::ifdcmpgt: case TR::ifdcmple:
case TR::ifbcmpeq: case TR::ifbcmpne: case TR::ifbcmplt: case TR::ifbcmpge: case TR::ifbcmpgt: case TR::ifbcmple:
case TR::ifscmpeq: case TR::ifscmpne: case TR::ifscmplt: case TR::ifscmpge: case TR::ifscmpgt: case TR::ifscmple:
case TR::ifsucmplt: case TR::ifsucmpge: case TR::ifsucmpgt: case TR::ifsucmple:
case TR::ifacmpeq: case TR::ifacmpne:
case TR::iffcmpequ: case TR::iffcmpneu: case TR::iffcmpltu: case TR::iffcmpgeu: case TR::iffcmpgtu: case TR::iffcmpleu:
case TR::ifdcmpequ: case TR::ifdcmpneu: case TR::ifdcmpltu: case TR::ifdcmpgeu: case TR::ifdcmpgtu: case TR::ifdcmpleu:
{
TR::Block * branchBlock = node->getBranchDestination()->getNode()->getBlock();
addEdge(block, branchBlock);
TR::Block * nextBlock = block->getExit()->getNextTreeTop()->getNode()->getBlock();
if (branchBlock != nextBlock)
addEdge(block, nextBlock);
break;
}
case TR::Goto:
addEdge(block, node->getBranchDestination()->getNode()->getBlock());
break;
case TR::ireturn: case TR::lreturn: case TR::freturn: case TR::dreturn: case TR::areturn: case TR::Return:
case TR::athrow:
addEdge(block, getEnd());
break;
case TR::NULLCHK:
if (node->getFirstChild()->getOpCodeValue() == TR::athrow)
addEdge(block, getEnd());
else
addEdge(block, block->getExit()->getNextTreeTop()->getNode()->getBlock());
break;
default:
{
if (block->getExit()->getNextTreeTop())
addEdge(block, block->getExit()->getNextTreeTop()->getNode()->getBlock());
else
addEdge(block, getEnd());
}
}
}
void
OMR::CFG::join(TR::Block * b1, TR::Block * b2)
{
if (b2) b1->getExit()->join(b2->getEntry());
self()->addSuccessorEdges(b1);
}
void
OMR::CFG::insertBefore(TR::Block * b1, TR::Block * b2)
{
self()->addNode(b1);
self()->join(b1, b2);
}
void
OMR::CFG::copyExceptionSuccessors(
TR::CFGNode *from,
TR::CFGNode *to,
bool (*predicate)(TR::CFGEdge *))
{
for (auto e1 = from->getExceptionSuccessors().begin(); e1 != from->getExceptionSuccessors().end(); ++e1)
{
if (predicate(*e1))
{
self()->addExceptionEdge(to, toBlock((*e1)->getTo()));
}
}
}
TR_Structure *
OMR::CFG::invalidateStructure()
{
setStructure(NULL);
TR::Region::reset(_structureMemoryRegion, comp()->trMemory()->heapMemoryRegion());
return getStructure();
}
TR_Structure *
OMR::CFG::setStructure(TR_Structure *p)
{
if (_rootStructure && !p)
{
dumpOptDetails(comp(), " (Invalidating structure)\n");
}
return (_rootStructure = p);
}
/**
* Default predicate for copyExceptionSuccessors.
*/
bool OMR::alwaysTrue(TR::CFGEdge * e)
{
return true;
}
TR_OrderedExceptionHandlerIterator::TR_OrderedExceptionHandlerIterator(TR::Block * tryBlock, TR::Region &workingMemoryRegion)
{
if (tryBlock->getExceptionSuccessors().empty())
_dim = 0;
else
{
int32_t handlerDim = 1, inlineDim = 1;
for (auto e = tryBlock->getExceptionSuccessors().begin(); e != tryBlock->getExceptionSuccessors().end(); ++e)
{
TR::Block * b = toBlock((*e)->getTo());
if (!b->isOSRCatchBlock())
{
TR_ASSERT(b->getHandlerIndex()!=-1, "exception handler index is not defined" );
if (b->getHandlerIndex() >= handlerDim)
handlerDim = b->getHandlerIndex() + 1;
if (b->getInlineDepth() >= inlineDim)
inlineDim = b->getInlineDepth() + 1;
}
}
_dim = handlerDim * inlineDim;
_handlers = (TR::Block **)workingMemoryRegion.allocate(_dim*sizeof(TR::Block *));
memset(_handlers, 0, _dim*sizeof(TR::Block *));
for (auto e = tryBlock->getExceptionSuccessors().begin(); e != tryBlock->getExceptionSuccessors().end(); ++e)
{
TR::Block * b = toBlock((*e)->getTo());
if (!b->isOSRCatchBlock())
{
TR_ASSERT((_handlers[((inlineDim - b->getInlineDepth() - 1) * handlerDim) + b->getHandlerIndex()] == NULL), "handler entry is not NULL\n");
_handlers[((inlineDim - b->getInlineDepth() - 1) * handlerDim) + b->getHandlerIndex()] = b;
}
}
}
}
TR::Block *
TR_OrderedExceptionHandlerIterator::getFirst()
{
_index = 0;
return getCurrent();
}
TR::Block *
TR_OrderedExceptionHandlerIterator::getNext()
{
++_index;
return getCurrent();
}
TR::Block *
TR_OrderedExceptionHandlerIterator::getCurrent()
{
TR::Block * handler = 0;
while (_index < _dim && (handler = _handlers[_index]) == 0)
++_index;
return handler;
}
TR::CFGEdge::CFGEdge(TR::CFGNode *pF, TR::CFGNode *pT)
: _pFrom(pF), _pTo(pT), _visitCount(0), _frequency(0), _id(-1)
{}
TR::CFGEdge * TR::CFGEdge::createEdge (TR::CFGNode *pF, TR::CFGNode *pT, TR_Memory* trMemory, TR_AllocationKind allocKind)
{
TR::CFGEdge * newEdge = new (trMemory, allocKind) TR::CFGEdge(pF, pT);
pF->addSuccessor(newEdge);
pT->addPredecessor(newEdge);
if (pT->getFrequency() >= 0)
newEdge->setFrequency(pT->getFrequency());
if ((pF->getFrequency() >= 0) && (pF->getFrequency() < newEdge->getFrequency()))
newEdge->setFrequency(pF->getFrequency());
return newEdge;
}
TR::CFGEdge * TR::CFGEdge::createEdge (TR::CFGNode *pF, TR::CFGNode *pT, TR::Region ®ion)
{
TR::CFGEdge * newEdge = new (region) TR::CFGEdge(pF, pT);
pF->addSuccessor(newEdge);
pT->addPredecessor(newEdge);
if (pT->getFrequency() >= 0)
newEdge->setFrequency(pT->getFrequency());
if ((pF->getFrequency() >= 0) && (pF->getFrequency() < newEdge->getFrequency()))
newEdge->setFrequency(pF->getFrequency());
return newEdge;
}
TR::CFGEdge * TR::CFGEdge::createExceptionEdge (TR::CFGNode *pF, TR::CFGNode *pT, TR_Memory* trMemory, TR_AllocationKind allocKind)
{
TR::CFGEdge * newEdge = new (trMemory, allocKind) TR::CFGEdge(pF, pT);
pF->addExceptionSuccessor(newEdge);
pT->addExceptionPredecessor(newEdge);
return newEdge;
}
TR::CFGEdge * TR::CFGEdge::createExceptionEdge (TR::CFGNode *pF, TR::CFGNode *pT, TR::Region ®ion)
{
TR::CFGEdge * newEdge = new (region) TR::CFGEdge(pF, pT);
pF->addExceptionSuccessor(newEdge);
pT->addExceptionPredecessor(newEdge);
return newEdge;
}
void TR::CFGEdge::setFrom(TR::CFGNode *pF)
{
_pFrom = pF;
pF->addSuccessor(this);
}
void TR::CFGEdge::setTo(TR::CFGNode *pT)
{
_pTo = pT;
pT->addPredecessor(this);
}
void TR::CFGEdge::setExceptionFrom(TR::CFGNode *pF)
{
_pFrom = pF;
pF->addExceptionSuccessor(this);
}
void TR::CFGEdge::setExceptionTo(TR::CFGNode *pT)
{
_pTo = pT;
pT->addExceptionPredecessor(this);
}
void TR::CFGEdge::setFromTo(TR::CFGNode *pF, TR::CFGNode *pT)
{
setFrom(pF);
setTo(pT);
}
void TR::CFGEdge::setExceptionFromTo(TR::CFGNode *pF, TR::CFGNode *pT)
{
_pFrom = pF;
_pTo = pT;
pF->addExceptionSuccessor(this);
pT->addExceptionPredecessor(this);
}
static int32_t getTotalEdgesFrequency(TR::CFGEdgeList& edgesList)
{
int32_t totalFreq = 0;
for (auto edge = edgesList.begin(); edge != edgesList.end(); ++edge)
totalFreq += (*edge)->getFrequency();
return totalFreq;
}
bool OMR::CFG::updateBlockFrequency(TR::Block *block, int32_t newFreq)
{
int16_t oldFreq = block->getFrequency();
if (newFreq != oldFreq && newFreq >= 0)
{
if (comp()->getOption(TR_TraceBFGeneration))
traceMsg(comp(), "updated block %d freq from %d to %d\n", block->getNumber(), oldFreq, newFreq);
block->setFrequency(newFreq);
return true;
}
else
return false;
}
void OMR::CFG::updateBlockFrequencyFromEdges(TR::Block *block)
{
int32_t totalPredFreq = getTotalEdgesFrequency(block->getPredecessors()) + getTotalEdgesFrequency(block->getExceptionPredecessors());
int32_t totalSuccFreq = getTotalEdgesFrequency(block->getSuccessors()) + getTotalEdgesFrequency(block->getExceptionSuccessors());
int32_t maxNewFreq = std::min(totalPredFreq, totalSuccFreq);
// currently only lower frequency
if (maxNewFreq < block->getFrequency())
updateBlockFrequency(block, maxNewFreq);
}
TR::CFGNode::CFGNode(TR_Memory * m)
: _nodeNumber(-1),
_visitCount(0),
_frequency(-1),
_forwardTraversalIndex(-1),
_backwardTraversalIndex(-1),
_region(m->heapMemoryRegion()),
_successors(m->heapMemoryRegion()),
_predecessors(m->heapMemoryRegion()),
_exceptionSuccessors(m->heapMemoryRegion()),
_exceptionPredecessors(m->heapMemoryRegion())
{
}
TR::CFGNode::CFGNode(int32_t n, TR_Memory * m)
: _nodeNumber(n),
_visitCount(0),
_frequency(-1),
_forwardTraversalIndex(-1),
_backwardTraversalIndex(-1),
_region(m->heapMemoryRegion()),
_successors(m->heapMemoryRegion()),
_predecessors(m->heapMemoryRegion()),
_exceptionSuccessors(m->heapMemoryRegion()),
_exceptionPredecessors(m->heapMemoryRegion())
{
}
TR::CFGNode::CFGNode(TR::Region ®ion)
: _nodeNumber(-1),
_visitCount(0),
_frequency(-1),
_forwardTraversalIndex(-1),
_backwardTraversalIndex(-1),
_region(region),
_successors(region),
_predecessors(region),
_exceptionSuccessors(region),
_exceptionPredecessors(region)
{
}
TR::CFGNode::CFGNode(int32_t n, TR::Region ®ion)
: _nodeNumber(n),
_visitCount(0),
_frequency(-1),
_forwardTraversalIndex(-1),
_backwardTraversalIndex(-1),
_region(region),
_successors(region),
_predecessors(region),
_exceptionSuccessors(region),
_exceptionPredecessors(region)
{
}
// This method needs to be non-inlined so that the home for TR::CFGNode's vft is
// established.
//
void TR::CFGNode::removeFromCFG(TR::Compilation *c)
{
}
TR::TreeTop * OMR::CFG::findLastTreeTop()
{
TR::Block *cursorBlock = getStart()->getSuccessors().front()->getTo()->asBlock();
TR::Block *prevCursorBlock = NULL;
while (cursorBlock)
{
prevCursorBlock = cursorBlock;
cursorBlock = cursorBlock->getNextBlock();
}
if (prevCursorBlock)
return prevCursorBlock->getExit();
return NULL;
}
TR::CFGEdge *TR::CFGNode::getEdge(TR::CFGNode *n)
{
TR_SuccessorIterator ei(this);
for (TR::CFGEdge * edge = ei.getFirst(); edge != NULL; edge = ei.getNext())
{
if (edge->getTo() == n)
return edge;
}
return NULL;
}
template <typename FUNC>
TR::CFGEdge * TR::CFGNode::getEdgeMatchingNodeInAList (TR::CFGNode * n, TR::CFGEdgeList& list, FUNC blockGetter)
{
for (auto edge = list.begin(); edge != list.end(); ++edge)
{
if (blockGetter(*edge) == n)
return *edge;
}
return NULL;
}
TR::CFGEdge * TR::CFGNode::getSuccessorEdge(TR::CFGNode * n)
{
return getEdgeMatchingNodeInAList(n, getSuccessors(), toBlockGetter);
}
TR::CFGEdge * TR::CFGNode::getExceptionSuccessorEdge(TR::CFGNode * n)
{
return getEdgeMatchingNodeInAList(n, getExceptionSuccessors(), toBlockGetter);
}
TR::CFGEdge * TR::CFGNode::getPredecessorEdge(TR::CFGNode * n)
{
return getEdgeMatchingNodeInAList(n, getPredecessors(), fromBlockGetter);
}
TR::CFGEdge * TR::CFGNode::getExceptionPredecessorEdge(TR::CFGNode * n)
{
return getEdgeMatchingNodeInAList(n, getExceptionPredecessors(), fromBlockGetter);
}
bool TR::CFGNode::hasSuccessor(TR::CFGNode * n)
{
return getSuccessorEdge(n) != NULL;
}
bool TR::CFGNode::hasExceptionSuccessor(TR::CFGNode * n)
{
return getExceptionSuccessorEdge(n) != NULL;
}
bool TR::CFGNode::hasPredecessor(TR::CFGNode * n)
{
return getPredecessorEdge(n) != NULL;
}
bool TR::CFGNode::hasExceptionPredecessor(TR::CFGNode * n)
{
return getExceptionPredecessorEdge(n) != NULL;
}
TR::CFGNode *OMR::CFG::removeNode(TR::CFGNode *node)
{
if (node->nodeIsRemoved())
return 0;
_nodes.remove(node);
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
traceMsg(comp(),"\nRemoving node %d\n", node->getNumber());
node->removeFromCFG(comp());
// Remove the exception successors first, so that try/finally structures get
// cleaned up in the right order. Nobody else cares about the order.
//
while (!node->getExceptionSuccessors().empty())
removeEdge(node->getExceptionSuccessors().front());
while (!node->getSuccessors().empty())
removeEdge(node->getSuccessors().front());
// All predecessors must have been removed otherwise the CFG will be inconsistent.
//
TR_ASSERT(node->getPredecessors().empty(),
"CFG, removing a node that still has predecessors");
TR_ASSERT(node->getExceptionPredecessors().empty(),
"CFG, removing a node that still has predecessors");
node->removeNode();
return node;
}
// test if 'to' is an unreachable orphan
//
OMR::CFG::OrphanType
OMR::CFG::unreachableOrphan(TR::CFG *cfg, TR::CFGNode *from, TR::CFGNode *to)
{
// If the "to" node is orphaned by removing this edge, remove it from the
// CFG as long as it is not the exit node - it is valid for the exit node
// to be unreachable.
//
if ((to->getPredecessors().empty() && to->getExceptionPredecessors().empty() &&
to != cfg->getEnd()) ||
(to->getExceptionPredecessors().empty() && (to->getPredecessors().size() == 1) &&
to->getPredecessors().front()->getFrom() == to) ||
(to->getPredecessors().empty() && (to->getExceptionPredecessors().size() == 1) &&
to->getExceptionPredecessors().front()->getFrom() == to))
{
return IsOrphanedNode;
}
// If we have structure, and 'to' is the entry node of a cyclic region
// and as a result of removing this edge we have made 'to' unreachable, except
// from backedges originating from within 'to' then recognize 'to' to be orphaned
//
if (cfg->getStructure())
{
TR_RegionStructure *parent = from->asBlock()->getStructureOf()->findCommonParent(to->asBlock()->getStructureOf(), cfg);
TR_StructureSubGraphNode *toSubNode = parent->findSubNodeInRegion(to->getNumber());
TR_ASSERT(toSubNode, "cannot have cfg edge a->b in region p where b is not a direct subnode of p");
TR_RegionStructure *toRegion = toSubNode->getStructure()->asRegion();
if (toRegion)
{
bool onlyBackEdges = true;
TR_PredecessorIterator pit(to);
TR::CFGEdge *edge;
for (edge = pit.getFirst(); edge; edge = pit.getNext())
{
if (!toRegion->contains(edge->getFrom()->asBlock()->getStructureOf()) &&
(edge->getFrom() != from))
{
onlyBackEdges = false;
break;
}
}
if (onlyBackEdges)
return IsOrphanedRegion;
}
}
return IsParented;
}
/*
This Function looks for an edge in the successor/predecessor OR
exceptionSuccessor/exceptionPredecessor Lists from the 'from' and 'to' Nodes of
the edge If the edge is not found in either combination, we return false
*/
bool OMR::CFG::removeEdge(TR::CFGEdge *edge)
{
bool blocksWereRemoved = false;
TR::CFGNode *from = edge->getFrom();
TR::CFGNode *to = edge->getTo();
TR::StackMemoryRegion stackMemoryRegion(*trMemory());
_mightHaveUnreachableBlocks = true;
bool found = false;
if (std::find(from->getSuccessors().begin(), from->getSuccessors().end(), edge) != from->getSuccessors().end()) {
found = true;
from->getSuccessors().remove(edge);
}
else if (std::find(from->getExceptionSuccessors().begin(), from->getExceptionSuccessors().end(), edge) != from->getExceptionSuccessors().end()) {
found = true;
from->getExceptionSuccessors().remove(edge);
}
if (std::find(to->getPredecessors().begin(), to->getPredecessors().end(), edge) != to->getPredecessors().end()) {
found = true;
to->getPredecessors().remove(edge);
}
else if (std::find(to->getExceptionPredecessors().begin(), to->getExceptionPredecessors().end(), edge) != to->getExceptionPredecessors().end()) {
found = true;
to->getExceptionPredecessors().remove(edge);
}
if (!found)
return false;
_numEdges--;
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
traceMsg(comp(), "\nRemoving edge %d-->%d (depth %d):\n", from->getNumber(), to->getNumber(), _removeEdgeNestingDepth);
TR_ScratchList<TR::CFGNode> nodesToBeRemoved(trMemory());
bool doWalk = false;
TR_BitVector *blocksVisited = NULL;
OrphanType orphan = unreachableOrphan(self(), from, to);
if (orphan != IsParented)
{
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
{
traceMsg(comp(),"\nblock_%d is an orphan now with type=%d:\n", to->getNumber(), orphan);
}
{
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
traceMsg(comp(), "\nAdding node %d to nodesToBeRemoved from %d\n", to->getNumber(), from->getNumber());
nodesToBeRemoved.add(to);
blocksVisited = new (trStackMemory()) TR_BitVector(getNextNodeNumber(), trMemory(), stackAlloc);
blocksWereRemoved = true;
doWalk = true;
}
}
// Tell the control tree to modify the structures containing this edge
//
if (getStructure())
{
TR_Structure *fromStruct = toBlock(from)->getStructureOf();
TR_Structure *toStruct = toBlock(to)->getStructureOf();
if (fromStruct && toStruct)
{
toStruct->removeEdge(fromStruct, toStruct);
}
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
{
traceMsg(comp(),"\nStructures changed after removing edge %d-->%d:\n", from->getNumber(), to->getNumber());
}
}
if (doWalk)
{
TR_Queue<TR::CFGNode> nodeq(trMemory());
nodeq.enqueue(to);
blocksVisited->empty();
do
{
from = nodeq.dequeue();
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
traceMsg(comp(), "\ndo walk for node %d\n", from->getNumber());
if (blocksVisited->get(from->getNumber()))
continue;
// if the node is already removed, donot process
//
if(from->nodeIsRemoved())
continue;
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
traceMsg(comp(), "Processing unreachable node %d\n", from->getNumber());
blocksVisited->set(from->getNumber());
TR_SuccessorIterator edgesIt(from);
for (TR::CFGEdge * e = edgesIt.getFirst(); e; e = edgesIt.getNext())
{
to = e->getTo();
_numEdges--;
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
traceMsg(comp(), "\n2Removing edge %d-->%d (depth %d):\n", from->getNumber(), to->getNumber(), _removeEdgeNestingDepth);
if (std::find(from->getSuccessors().begin(), from->getSuccessors().end(), e) != from->getSuccessors().end())
from->getSuccessors().remove(e);
else
from->getExceptionSuccessors().remove(e);
if (std::find(to->getPredecessors().begin(), to->getPredecessors().end(), e) != to->getPredecessors().end())
to->getPredecessors().remove(e);
else
to->getExceptionPredecessors().remove(e);
// break cycles by not adding 'to' that is
// already in the removed list
//
orphan = unreachableOrphan(self(), from, to);
if (orphan != IsParented &&
!blocksVisited->get(to->getNumber()) &&
!to->nodeIsRemoved())
{
{
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
traceMsg(comp(), "\nAdding node %d to nodesToBeRemoved from %d\n", to->getNumber(), from->getNumber());
if (orphan == IsOrphanedNode)
nodesToBeRemoved.add(to);
blocksWereRemoved = true;
nodeq.enqueue(to);
}
}
// Tell the control tree to modify the structures containing this edge
//
if (getStructure())
{
TR_Structure *fromStruct = toBlock(from)->getStructureOf();
TR_Structure *toStruct = toBlock(to)->getStructureOf();
if (fromStruct && toStruct)
toStruct->removeEdge(fromStruct, toStruct);
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
{
traceMsg(comp(),"\nStructure changed after removing edge %d-->%d:\n", from->getNumber(), to->getNumber());
///comp()->getDebug()->print(comp()->getOutFile(), getStructure(), 6);
}
}
}
} while (!nodeq.isEmpty());
// finally remove all the unreachable nodes
//
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
traceMsg(comp(),"\nNow actually removing nodes\n");
ListIterator<TR::CFGNode> nodesIt(&nodesToBeRemoved);
for (TR::CFGNode *n = nodesIt.getFirst(); n; n = nodesIt.getNext())
{
{
if (_nodes.remove(n))
{
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
traceMsg(comp(),"\nRemoved node %d\n", n->getNumber());
n->removeFromCFG(comp());
n->removeNode();
}
}
}
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
traceMsg(comp(), "\n_doesHaveUnreachableBlocks %d\n", _doesHaveUnreachableBlocks);
if (!_ignoreUnreachableBlocks && _doesHaveUnreachableBlocks)
{
removeUnreachableBlocks();
blocksWereRemoved = true;
}
}
return blocksWereRemoved;
}
bool OMR::CFG::removeEdge(TR::CFGEdge *edge, bool recursiveImpl)
{
TR::CFGNode *from = edge->getFrom();
TR::CFGNode *to = edge->getTo();
if (comp()->getOption(TR_TraceAddAndRemoveEdge))
{
traceMsg(comp(), "\nRemoving edge %d-->%d (depth %d):\n", from->getNumber(), to->getNumber(), _removeEdgeNestingDepth);
}