-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathOMRCFGSimplifier.cpp
1427 lines (1216 loc) · 52.6 KB
/
OMRCFGSimplifier.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, 2019 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/CFGSimplifier.hpp"
#include <algorithm>
#include <stddef.h>
#include "codegen/CodeGenerator.hpp"
#include "compile/Compilation.hpp"
#include "env/StackMemoryRegion.hpp"
#include "env/TRMemory.hpp"
#include "il/Block.hpp"
#include "il/ILOpCodes.hpp"
#include "il/ILOps.hpp"
#include "il/Node.hpp"
#include "il/Node_inlines.hpp"
#include "il/StaticSymbol.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/List.hpp"
#include "infra/CfgEdge.hpp"
#include "infra/CfgNode.hpp"
#include "optimizer/Optimization.hpp"
#include "optimizer/Optimization_inlines.hpp"
#include "optimizer/Optimizer.hpp"
#include "optimizer/TransformUtil.hpp"
#include "ras/DebugCounter.hpp"
#include "infra/Checklist.hpp"
// Set to 0 to disable the special-case pattern matching using the
// s390 condition code.
#define ALLOW_SIMPLIFY_COND_CODE_BOOLEAN_STORE 1
#define OPT_DETAILS "O^O CFG SIMPLIFICATION: "
TR::Optimization* OMR::CFGSimplifier::create(TR::OptimizationManager *manager)
{
return new (manager->allocator()) TR::CFGSimplifier(manager);
}
OMR::CFGSimplifier::CFGSimplifier(TR::OptimizationManager *manager)
: TR::Optimization(manager)
{}
int32_t OMR::CFGSimplifier::perform()
{
if (trace())
traceMsg(comp(), "Starting CFG Simplification\n");
bool anySuccess = false;
{
TR::StackMemoryRegion stackMemoryRegion(*trMemory());
_cfg = comp()->getFlowGraph();
if (_cfg != NULL)
{
for (TR::CFGNode *cfgNode = _cfg->getFirstNode(); cfgNode; cfgNode = cfgNode->getNext())
{
_block = toBlock(cfgNode);
anySuccess |= simplify();
}
}
// Any transformations invalidate use/def and value number information
//
if (anySuccess)
{
optimizer()->setUseDefInfo(NULL);
optimizer()->setValueNumberInfo(NULL);
}
} // stackMemoryRegion scope
if (trace())
{
traceMsg(comp(), "\nEnding CFG Simplification\n");
comp()->dumpMethodTrees("\nTrees after CFG Simplification\n");
}
return 1; // actual cost
}
bool OMR::CFGSimplifier::simplify()
{
// Can't simplify the entry or exit blocks
//
if (_block->getEntry() == NULL)
return false;
// Find the first two successors
//
_succ1 = _succ2 = NULL;
_next2 = NULL;
if (_block->getSuccessors().empty())
{
_next1 = NULL;
}
else
{
_succ1 = _block->getSuccessors().front();
_next1 = toBlock(_succ1->getTo());
if (_block->getSuccessors().size() > 1)
{
_succ2 = *(++(_block->getSuccessors().begin()));
_next2 = toBlock(_succ2->getTo());
}
}
return simplifyIfStructure();
}
bool OMR::CFGSimplifier::simplifyIfStructure()
{
if (trace())
traceMsg(comp(), "Attempting if simpliciaton on block_%d\n", _block->getNumber());
// There must be exactly two successors, and they must be real blocks
//
if (_next1 == NULL || _next2 == NULL)
return false;
if (_succ2 == NULL)
return false;
if (_block->getSuccessors().size() > 2)
return false;
if (_next1->getEntry() == NULL || _next2->getEntry() == NULL)
return false;
// The successors must have only this block as their predecessor, and must
// have a common successor.
//
bool needToDuplicateTree = false;
if (_next1->getPredecessors().empty())
return false;
if (!(_next1->getPredecessors().front()->getFrom() == _block && (_next1->getPredecessors().size() == 1)))
needToDuplicateTree = true;
if (_next2->getPredecessors().empty())
return false;
if (!(_next2->getPredecessors().front()->getFrom() == _block && (_next2->getPredecessors().size() == 1)))
needToDuplicateTree = true;
// This block must end in a compare-and-branch which can be converted to a
// boolean compare, or a branch using the condition code.
//
TR::TreeTop *compareTreeTop = getLastRealTreetop(_block);
TR::Node *compareNode = compareTreeTop->getNode();
if (!compareNode->getOpCode().isIf())
return false;
if (compareNode->isNopableInlineGuard())
//don't simplify nopable guards
return false;
// ... and so one of the successors must be the fall-through successor. Make
// _next1 be the fall-through successor.
//
TR::Block *b = getFallThroughBlock(_block);
if (b != _next1)
{
TR_ASSERT(b == _next2, "CFG Simplifier");
_next2 = _next1;
_next1 = b;
}
return simplifyIfPatterns(needToDuplicateTree);
}
bool OMR::CFGSimplifier::simplifyIfPatterns(bool needToDuplicateTree)
{
return simplifyBooleanStore(needToDuplicateTree)
|| simplifyNullToException(needToDuplicateTree)
|| simplifySimpleStore(needToDuplicateTree)
|| simplifyCondStoreSequence(needToDuplicateTree)
|| simplifyInstanceOfTestToCheckcast(needToDuplicateTree)
;
}
bool OMR::CFGSimplifier::hasExceptionPoint(TR::Block *block, TR::TreeTop *end)
{
return true;
if (!block->getExceptionSuccessors().empty())
return true;
for (TR::TreeTop *itr = block->getEntry(); itr && itr != end; itr = itr->getNextTreeTop())
{
if (itr->getNode()->exceptionsRaised() != 0)
return true;
}
return false;
}
bool OMR::CFGSimplifier::simplifyInstanceOfTestToCheckcast(bool needToDuplicateTree)
{
static char *disableSimplifyInstanceOfTestToCheckcast = feGetEnv("TR_disableSimplifyInstanceOfTestToCheckcast");
if (disableSimplifyInstanceOfTestToCheckcast != NULL)
return false;
if (comp()->getOSRMode() == TR::involuntaryOSR)
return false;
if (_block->isCatchBlock())
return false;
if (trace())
traceMsg(comp(), "Start simplifyInstanceOfTestToCheckcast block_%d\n", _block->getNumber());
// This block must end in an ifacmpeq or ifacmpne against aconst NULL
TR::TreeTop *compareTreeTop = getLastRealTreetop(_block);
TR::Node *compareNode = compareTreeTop->getNode();
if (compareNode->getOpCodeValue() != TR::ificmpeq
&& compareNode->getOpCodeValue() != TR::ificmpne)
return false;
if (trace())
traceMsg(comp(), " Found an ificmp[eq/ne] n%dn\n", compareNode->getGlobalIndex());
if (compareNode->getSecondChild()->getOpCodeValue() != TR::iconst
|| compareNode->getSecondChild()->getInt() != 0)
return false;
if (trace())
traceMsg(comp(), " Found an ificmp[eq/ne] against zero n%dn\n", compareNode->getGlobalIndex());
if (compareNode->getFirstChild()->getOpCodeValue() != TR::instanceof)
return false;
if (trace())
traceMsg(comp(), " Found an ificmp[eq/ne] of an instanceof against 0 n%dn\n", compareNode->getGlobalIndex());
if (compareNode->getFirstChild()->getSecondChild()->getOpCodeValue() != TR::loadaddr)
return false;
if (trace())
traceMsg(comp(), " Found an ificmp[eq/new] of an instanceof a constant class against zero n%dn\n", compareNode->getGlobalIndex());
TR::Block *throwBlock = NULL, *fallthroughBlock = NULL;
if (compareNode->getOpCodeValue() == TR::ificmpeq)
{
if (_next2->getLastRealTreeTop()->getNode()->getNumChildren() != 1
|| _next2->getLastRealTreeTop()->getNode()->getFirstChild()->getOpCodeValue() != TR::athrow)
return false;
if (trace())
traceMsg(comp(), " Found an ificmpeq of an instanceof against zero which throws on taken size\n");
throwBlock = _next2;
fallthroughBlock = _next1;
} else {
if (_next1->getLastRealTreeTop()->getNode()->getNumChildren() != 1
|| _next1->getLastRealTreeTop()->getNode()->getFirstChild()->getOpCodeValue() != TR::athrow)
return false;
if (trace())
traceMsg(comp(), " Found an ificmpne of an instance of against zero which throws on the fallthrough path\n");
throwBlock = _next1;
fallthroughBlock = _next2;
}
if (!performTransformation(comp(), "%sReplace ificmp of instanceof with throw failure with checkcastAndNULLCHK in block_%d\n", OPT_DETAILS, _block->getNumber()))
return false;
_cfg->invalidateStructure();
TR::Node *objNode = compareNode->getFirstChild()->getFirstChild();
TR::Node *classNode = compareNode->getFirstChild()->getSecondChild();
TR::Block *catchBlock = TR::Block::createEmptyBlock(compareNode, comp(), throwBlock->getFrequency());
catchBlock->setHandlerInfo(0, comp()->getInlineDepth(), 0, comp()->getCurrentMethod(), comp());
TR::Node *gotoNode = TR::Node::create(compareNode, TR::Goto, 0);
gotoNode->setBranchDestination(throwBlock->getEntry());
catchBlock->append(TR::TreeTop::create(comp(), gotoNode));
TR::TreeTop *lastTree = comp()->findLastTree();
catchBlock->getExit()->join(lastTree->getNextTreeTop());
lastTree->join(catchBlock->getEntry());
TR::Node *checkcastAndNULLCHKNode = TR::Node::createWithSymRef(compareNode->getFirstChild(), TR::checkcastAndNULLCHK, 2, comp()->getSymRefTab()->findOrCreateCheckCastSymbolRef(comp()->getMethodSymbol()));
TR_Pair<TR_ByteCodeInfo, TR::Node> *bcInfo = new (trHeapMemory()) TR_Pair<TR_ByteCodeInfo, TR::Node> (&compareNode->getFirstChild()->getByteCodeInfo(), checkcastAndNULLCHKNode);
comp()->getCheckcastNullChkInfo().push_front(bcInfo);
checkcastAndNULLCHKNode->setAndIncChild(0, objNode);
checkcastAndNULLCHKNode->setAndIncChild(1, classNode);
if (trace())
traceMsg(comp(), "Remove compareTreeTop n%dn\n", compareTreeTop->getNode()->getGlobalIndex());
TR::TransformUtil::removeTree(comp(), compareTreeTop);
TR::TreeTop *checkcastAndNULLCHKTree = TR::TreeTop::create(comp(), checkcastAndNULLCHKNode);
if (trace())
traceMsg(comp(), "Create checkcastAndNULLCHK Node n%dn\n", checkcastAndNULLCHKNode->getGlobalIndex());
_block->append(checkcastAndNULLCHKTree);
if (hasExceptionPoint(_block, checkcastAndNULLCHKTree))
_block = _block->split(checkcastAndNULLCHKTree, _cfg, true, false);
_cfg->addNode(catchBlock);
_cfg->addExceptionEdge(_block, catchBlock);
_cfg->addEdge(catchBlock, throwBlock);
_cfg->removeEdge(_block, throwBlock);
if (_block->getNextBlock() != fallthroughBlock) {
TR::Node *gotoNode = TR::Node::create(checkcastAndNULLCHKNode, TR::Goto, 0);
gotoNode->setBranchDestination(fallthroughBlock->getEntry());
_block->append(TR::TreeTop::create(comp(), gotoNode));
}
if (trace())
traceMsg(comp(), "End simplifyInstanceOfTestToCheckcast.\n");
TR::DebugCounter::incStaticDebugCounter(comp(), TR::DebugCounter::debugCounterName(comp(), "cfgSimpCheckcast/(%s)", comp()->signature()));
return true;
}
static bool containsIndirectOperationImpl(TR::Node *node, TR::NodeChecklist *visited, int32_t depth)
{
if (visited->contains(node))
return false;
if (depth == 0)
return true;
visited->add(node);
if (!(node->getOpCode().isArithmetic() && !node->getOpCode().isDiv())
&& !node->getOpCode().isLoadVarDirect()
&& !node->getOpCode().isTernary()
&& !node->getOpCode().isLoadConst())
return true;
if (node->getOpCode().hasSymbolReference()
&& !node->getSymbolReference()->getSymbol()->isAutoOrParm())
return true;
for (int i = 0; i < node->getNumChildren(); ++i)
{
if (containsIndirectOperationImpl(node->getChild(i), visited, depth-1))
return true;
}
return false;
}
static bool containsIndirectOperation(TR::Compilation *comp, TR::TreeTop *treetop)
{
TR::NodeChecklist visited(comp);
return containsIndirectOperationImpl(treetop->getNode()->getFirstChild(), &visited, 3);
}
bool OMR::CFGSimplifier::simplifyCondStoreSequence(bool needToDuplicateTree)
{
if (!(comp()->cg()->getSupportsTernary()))
return false;
if (trace())
traceMsg(comp(), "Start simplifyCondStoreSequence block_%d\n", _block->getNumber());
TR::TreeTop *compareTree = _block->getLastRealTreeTop();
TR::Node *compareNode = compareTree->getNode();
bool triangle2 = _next2->getSuccessors().size() == 1
&& _next2->getExceptionSuccessors().size() == 0
&& toBlock(_next2->getSuccessors().front()->getTo()) == _next1;
bool triangle1 = _next1->getSuccessors().size () == 1
&& _next1->getExceptionSuccessors().size() == 0
&& toBlock(_next1->getSuccessors().front()->getTo()) == _next2;
if (trace())
{
traceMsg(comp(), " block%d triangle1: %d triangle2: %d\n", _block->getNumber(), triangle1, triangle2);
}
if (!triangle1 || triangle2) { return false; }
TR::Block *toCheck = triangle1 ? _next1 : _next2;
TR::TreeTop *treeCursor = toCheck->getEntry()->getNextTreeTop();
int32_t count = 0;
while (treeCursor->getNode()->getOpCode().isStoreDirect()
&& !treeCursor->getNode()->getOpCode().isWrtBar()
&& !containsIndirectOperation(comp(), treeCursor))
{
if (!treeCursor->getNode()->getDataType().isIntegral()
&& !treeCursor->getNode()->getDataType().isAddress())
return false;
if (trace())
traceMsg(comp(), " Store node n%dn data type checks out\n", treeCursor->getNode()->getGlobalIndex());
if (!treeCursor->getNode()->getSymbolReference()->getSymbol()->isAutoOrParm())
return false;
if (trace())
traceMsg(comp(), " Store node n%dn symRef checks out\n", treeCursor->getNode()->getGlobalIndex());
treeCursor = treeCursor->getNextTreeTop();
count++;
}
if (treeCursor->getNode()->getOpCodeValue() != TR::BBEnd || count < 2)
return false;
if (!performTransformation(comp(), "%sReplace conditional stores in block_%d with stores of appropriate ternary at nodes\n", OPT_DETAILS, toCheck->getNumber()))
return false;
_cfg->invalidateStructure();
TR::Node *condition = TR::Node::create(compareNode, compareNode->getOpCode().convertIfCmpToCmp(), 2,
compareNode->getFirstChild(),
compareNode->getSecondChild());
treeCursor = toCheck->getEntry()->getNextTreeTop();
while (treeCursor->getNode()->getOpCode().isStoreDirect()
&& !treeCursor->getNode()->getOpCode().isWrtBar())
{
TR::Node *storeNode = treeCursor->getNode();
TR::Node *trueValue = triangle1 ? TR::Node::createWithSymRef(comp()->il.opCodeForDirectLoad(storeNode->getDataType()), 0, storeNode->getSymbolReference()) : (needToDuplicateTree ? storeNode->getFirstChild()->duplicateTree() : storeNode->getFirstChild());
TR::Node *falseValue = triangle1 ? (needToDuplicateTree ? storeNode->getFirstChild()->duplicateTree() : storeNode->getFirstChild()) : TR::Node::createWithSymRef(comp()->il.opCodeForDirectLoad(storeNode->getDataType()), 0, storeNode->getSymbolReference());
TR::Node *select = TR::Node::create(storeNode, comp()->il.opCodeForTernarySelect(storeNode->getDataType()), 3);
if (trace())
traceMsg(comp(), "Created ternary node n%dn\n", select->getGlobalIndex());
select->setAndIncChild(0, condition);
select->setAndIncChild(1, trueValue);
select->setAndIncChild(2, falseValue);
TR::TreeTop *insTree = TR::TreeTop::create(comp(), TR::Node::createWithSymRef(storeNode, storeNode->getOpCodeValue(), 1, select, storeNode->getSymbolReference()));
if (storeNode->getOpCodeValue() == TR::astore && storeNode->isHeapificationStore())
insTree->getNode()->setHeapificationStore(true);
compareTree->insertBefore(insTree);
treeCursor = treeCursor->getNextTreeTop();
}
if (triangle1) {
_cfg->removeEdge(_block, _next1);
if (_block->getNextBlock() != _next2)
{
TR::Node *gotoNode = TR::Node::create(compareNode, TR::Goto, 0);
gotoNode->setBranchDestination(_next2->getEntry());
_block->append(TR::TreeTop::create(comp(), gotoNode));
}
} else {
_cfg->removeEdge(_block, _next2);
if (_block->getNextBlock() != _next2)
{
TR::Node *gotoNode = TR::Node::create(compareNode, TR::Goto, 0);
gotoNode->setBranchDestination(_next1->getEntry());
_block->append(TR::TreeTop::create(comp(), gotoNode));
}
}
TR::TransformUtil::removeTree(comp(), compareTree);
if (trace())
traceMsg(comp(), "End simplifyCondStoreSequence.\n");
TR::DebugCounter::incStaticDebugCounter(comp(), TR::DebugCounter::debugCounterName(comp(), "cfgSimpMovSeq/%d/(%s)", count, comp()->signature()));
return true;
}
bool OMR::CFGSimplifier::simplifySimpleStore(bool needToDuplicateTree)
{
if (!(comp()->cg()->getSupportsTernary()))
return false;
if (trace())
traceMsg(comp(), "Start simplifySimpleStore block_%d\n", _block->getNumber());
TR::TreeTop *compareTree = _block->getLastRealTreeTop();
TR::Node *compareNode = compareTree->getNode();
bool triangle2 = _next2->getSuccessors().size() == 1
&& _next2->getExceptionSuccessors().size() == 0
&& toBlock(_next2->getSuccessors().front()->getTo()) == _next1;
bool triangle1 = _next1->getSuccessors().size () == 1
&& _next1->getExceptionSuccessors().size() == 0
&& toBlock(_next1->getSuccessors().front()->getTo()) == _next2;
bool diamond = _next2->getSuccessors().size() == 1
&& _next2->getExceptionSuccessors().size() == 0
&& _next1->getSuccessors().size() == 1
&& _next1->getExceptionSuccessors().size() == 0
&& toBlock(_next1->getSuccessors().front()->getTo()) == toBlock(_next2->getSuccessors().front()->getTo());
if (trace())
{
traceMsg(comp(), " block%d triangle1: %d triangle2: %d diamond: %d\n", _block->getNumber(), triangle1, triangle2, diamond);
}
static char *disableSimplifySimpleStoreTriangle = feGetEnv("TR_disableSimplifySimpleStoreTriangle");
if ((triangle1 || triangle2) && disableSimplifySimpleStoreTriangle != NULL)
return false;
static char *disableSimplifySimpleStoreDiamond = feGetEnv("TR_disableSimplifySimpleStoreDiamond");
if ((diamond) && disableSimplifySimpleStoreDiamond != NULL)
return false;
if (!triangle1 && !triangle2 && !diamond)
return false;
if (trace())
traceMsg(comp(), " compareTree has correctType\n");
TR::TreeTop *treeCursor = NULL;
TR::Node *trueValue = NULL, *falseValue = NULL, *storeNode = NULL;
bool isHeapificationStore = false;
if (triangle2 || diamond)
{
treeCursor =_next2->getEntry()->getNextTreeTop();
if (!treeCursor->getNode()->getOpCode().isStoreDirect()
|| treeCursor->getNode()->getOpCode().isWrtBar()
|| containsIndirectOperation(comp(), treeCursor))
return false;
if (trace())
traceMsg(comp(), " Take side has an appropriate store as the first tree\n");
storeNode = treeCursor->getNode();
isHeapificationStore = storeNode->getOpCodeValue() == TR::astore && storeNode->isHeapificationStore();
if (treeCursor->getNextTreeTop()->getNode()->getOpCodeValue() != TR::BBEnd
&& treeCursor->getNextTreeTop()->getNode()->getOpCodeValue() != TR::Goto)
return false;
trueValue = treeCursor->getNode()->getFirstChild();
if (trace())
traceMsg(comp(), " Taken side checks out\n");
}
if (triangle1 || diamond)
{
treeCursor = _next1->getEntry()->getNextTreeTop();
if (!treeCursor->getNode()->getOpCode().isStoreDirect()
|| treeCursor->getNode()->getOpCode().isWrtBar()
|| containsIndirectOperation(comp(), treeCursor))
return false;
if (trace())
traceMsg(comp(), " Fallthrough side has an appropriate store as the first tree\n");
if (storeNode != NULL
&& treeCursor->getNode()->getSymbolReference()->getReferenceNumber() != storeNode->getSymbolReference()->getReferenceNumber())
return false;
storeNode = treeCursor->getNode();
isHeapificationStore = storeNode->getOpCodeValue() == TR::astore && ((diamond && isHeapificationStore && storeNode->isHeapificationStore()) || (!diamond && storeNode->isHeapificationStore()));
if (trace())
traceMsg(comp(), " Fallthrough side is storing to the same symeref\n");
traceMsg(comp(), "Next tree n%dn\n", treeCursor->getNextTreeTop()->getNode()->getGlobalIndex());
if (treeCursor->getNextTreeTop()->getNode()->getOpCodeValue() != TR::BBEnd
&& treeCursor->getNextTreeTop()->getNode()->getOpCodeValue() != TR::Goto)
return false;
falseValue = treeCursor->getNode()->getFirstChild();
if (trace())
traceMsg(comp(), " Fallthrough checks out\n");
}
if (!storeNode->getDataType().isIntegral()
&& !storeNode->getDataType().isAddress())
return false;
if (trace())
traceMsg(comp(), " StoreNode data type checks out\n");
if (!diamond && !storeNode->getSymbolReference()->getSymbol()->isAutoOrParm())
return false;
if (trace())
traceMsg(comp(), " StoreNode symRef checks out\n");
if (!performTransformation(comp(), "%sReplace conditional store with store of an appropriate ternary at node [%p]\n", OPT_DETAILS, compareNode))
return false;
_cfg->invalidateStructure();
TR::Node *select = TR::Node::create(storeNode, comp()->il.opCodeForTernarySelect(storeNode->getDataType()), 3);
select->setAndIncChild(0,
TR::Node::create(compareNode, compareNode->getOpCode().convertIfCmpToCmp(), 2,
compareNode->getFirstChild(),
compareNode->getSecondChild()));
select->setAndIncChild(1,
trueValue ? (needToDuplicateTree ? trueValue->duplicateTree() : trueValue) : TR::Node::createWithSymRef(comp()->il.opCodeForDirectLoad(storeNode->getDataType()), 0, storeNode->getSymbolReference()));
select->setAndIncChild(2,
falseValue ? (needToDuplicateTree? falseValue->duplicateTree() : falseValue) : TR::Node::createWithSymRef(comp()->il.opCodeForDirectLoad(storeNode->getDataType()), 0, storeNode->getSymbolReference()));
TR::TreeTop *cmov = TR::TreeTop::create(comp(), TR::Node::createWithSymRef(storeNode, storeNode->getOpCodeValue(), 1, select, storeNode->getSymbolReference()));
compareTree->insertBefore(cmov);
if (isHeapificationStore)
cmov->getNode()->setHeapificationStore(true);
if (trace())
traceMsg(comp(), "End simplifySimpleStore. New ternary node is n%dn\n", select->getGlobalIndex());
TR::Block *dest;
if (diamond) {
dest = toBlock(_next1->getSuccessors().front()->getTo());
_cfg->addEdge(_block, dest);
_cfg->removeEdge(_block, _next1);
_cfg->removeEdge(_block, _next2);
} else if (triangle2) {
dest = _next1;
_cfg->removeEdge(_block, _next2);
} else if (triangle1) {
dest = _next2;
_cfg->removeEdge(_block, _next1);
}
if (_block->getNextBlock() != dest)
{
TR::Node *gotoNode = TR::Node::create(compareNode, TR::Goto, 0);
gotoNode->setBranchDestination(dest->getEntry());
_block->append(TR::TreeTop::create(comp(), gotoNode));
}
TR::TransformUtil::removeTree(comp(), compareTree);
TR::DebugCounter::incStaticDebugCounter(comp(), TR::DebugCounter::debugCounterName(comp(), "cfgSimpCMOV/%s/(%s)", (diamond ? "diamond" : (triangle1 ? "triangle1" : "triangle2")), comp()->signature()));
return true;
}
bool OMR::CFGSimplifier::simplifyNullToException(bool needToDuplicateTree)
{
static char *disableSimplifyExplicitNULLTest = feGetEnv("TR_disableSimplifyExplicitNULLTest");
static char *disableSimplifyNullToException = feGetEnv("TR_disableSimplifyNullToException");
if (disableSimplifyExplicitNULLTest != NULL || disableSimplifyNullToException != NULL)
return false;
if (comp()->getOSRMode() == TR::involuntaryOSR)
return false;
if (trace())
traceMsg(comp(), "Start simplifyNullToException\n");
// This block must end in an ifacmpeq or ifacmpne against aconst NULL
TR::TreeTop *compareTreeTop = getLastRealTreetop(_block);
TR::Node *compareNode = compareTreeTop->getNode();
if (compareNode->getOpCodeValue() != TR::ifacmpeq
&& compareNode->getOpCodeValue() != TR::ifacmpne)
return false;
if (trace())
traceMsg(comp(), " Found an ifacmp[eq/ne] n%dn\n", compareNode->getGlobalIndex());
if (compareNode->getSecondChild()->getOpCodeValue() != TR::aconst
|| compareNode->getSecondChild()->getAddress() != 0)
return false;
// _next1 is fall through so grab the block where the value is NULL
TR::Block *nullBlock = compareNode->getOpCodeValue() == TR::ifacmpeq ? _next2 : _next1;
if (trace())
traceMsg(comp(), " Matched nullBlock %d\n", nullBlock->getNumber());
// we want code sequence ending in a throw (any throw will do)
TR::Node *lastRootNode = nullBlock->getLastRealTreeTop()->getNode();
if (lastRootNode->getNumChildren() < 1
|| lastRootNode->getFirstChild()->getOpCodeValue() != TR::athrow)
return false;
if (!performTransformation(comp(), "%sReplace ifacmpeq/ifacmpne of NULL node n%dn [%p] to a blcok ending in throw with a NULLCHK to a catch which goes to block_%d\n", OPT_DETAILS, compareNode->getGlobalIndex(), compareNode, nullBlock->getNumber()))
return false;
_cfg->invalidateStructure();
TR::DebugCounter::incStaticDebugCounter(comp(), TR::DebugCounter::debugCounterName(comp(), "cfgSimpNULLCHK/nullToException/(%s)", comp()->signature()));
TR::Block *compareBlock = _block;
if (hasExceptionPoint(compareBlock, compareTreeTop))
compareBlock = compareBlock->split(compareTreeTop, _cfg, true, false);
if (compareBlock->getNextBlock() == nullBlock) {
TR::Node *gotoNode = TR::Node::create(compareNode, TR::Goto, 0);
gotoNode->setBranchDestination((nullBlock == _next1 ? _next2 : _next1)->getEntry());
compareBlock->append(TR::TreeTop::create(comp(), gotoNode));
}
TR::Node *nullchkNode = TR::Node::createWithSymRef(TR::NULLCHK, 1, 1, TR::Node::create(compareNode, TR::PassThrough, 1, compareNode->getFirstChild()), comp()->getSymRefTab()->findOrCreateNullCheckSymbolRef(comp()->getMethodSymbol()));
if (trace())
traceMsg(comp(), "End simplifyNullToException. New NULLCHK node is n%dn\n", nullchkNode->getGlobalIndex());
compareTreeTop->insertBefore(TR::TreeTop::create(comp(), nullchkNode));
TR::Block *catchBlock = TR::Block::createEmptyBlock(compareNode, comp(), nullBlock->getFrequency());
catchBlock->setHandlerInfo(0, comp()->getInlineDepth(), 0, comp()->getCurrentMethod(), comp());
TR::Node *gotoNode = TR::Node::create(compareNode, TR::Goto, 0);
gotoNode->setBranchDestination(nullBlock->getEntry());
catchBlock->append(TR::TreeTop::create(comp(), gotoNode));
TR::TreeTop *lastTree = comp()->findLastTree();
catchBlock->getExit()->join(lastTree->getNextTreeTop());
lastTree->join(catchBlock->getEntry());
_cfg->addNode(catchBlock);
_cfg->addExceptionEdge(compareBlock, catchBlock);
_cfg->addEdge(catchBlock, nullBlock);
_cfg->removeEdge(compareBlock, nullBlock);
TR::TransformUtil::removeTree(comp(), compareTreeTop);
return true;
}
// Look for pattern of the form:
//
// if (cond)
// x = 0;
// else
// x = y;
//
// This can be simplified to remove the control flow if the condition can
// be represented by a "cmp" opcode.
//
// Also look specifically for the following pattern using the S390 condition code:
//
// if (conditionCode)
// x = 0;
// else
// x = y;
// if (some cond involving x) goto someLabel
//
// Return "true" if any transformations were made.
//
bool OMR::CFGSimplifier::simplifyBooleanStore(bool needToDuplicateTree)
{
if (!(comp()->cg()->getSupportsTernary()))
return false;
if (trace())
traceMsg(comp(), "Start simplifyBooleanStore\n");
if (_next1->getSuccessors().empty())
return false;
if (_next1->getSuccessors().size() != 1)
return false;
if (_next2->getSuccessors().empty())
return false;
if (_next2->getSuccessors().size() != 1)
return false;
if (_next1->getSuccessors().front()->getTo() != _next2->getSuccessors().front()->getTo())
return false;
if (trace())
traceMsg(comp(), " Control flow checks out\n");
TR::Block *joinBlock = toBlock(_next1->getSuccessors().front()->getTo());
// This block must end in a compare-and-branch which can be converted to a
// boolean compare, or a branch using the condition code.
//
TR::TreeTop *compareTreeTop = getLastRealTreetop(_block);
TR::Node *compareNode = compareTreeTop->getNode();
bool isBranchOnCondCode = false;
if (compareNode->getOpCode().convertIfCmpToCmp() == TR::BadILOp)
return false;
if (trace())
traceMsg(comp(), " Found a Compare node n%dn\n", compareNode->getGlobalIndex());
// The trees of each successor block must consist of a single store.
//
TR::TreeTop *store1TreeTop = getNextRealTreetop(_next1->getEntry());
if (store1TreeTop == NULL || getNextRealTreetop(store1TreeTop) != NULL)
return false;
TR::Node *store1 = store1TreeTop->getNode();
if (!store1->getOpCode().isStore())
return false;
if (trace())
traceMsg(comp(), " Successor block_%d is single store\n", _next1->getNumber());
TR::TreeTop *store2TreeTop = getNextRealTreetop(_next2->getEntry());
if (store2TreeTop == NULL || getNextRealTreetop(store2TreeTop) != NULL)
return false;
TR::Node *store2 = store2TreeTop->getNode();
if (!store2->getOpCode().isStore())
return false;
if (trace())
traceMsg(comp(), " Successor block_%d is single store\n", _next2->getNumber());
// The stores must be integer stores to the same variable
//
if (store1->getOpCodeValue() != store2->getOpCodeValue())
return false;
if (!store1->getOpCode().isInt() && !store1->getOpCode().isByte())
return false;
if (store1->getSymbolReference()->getSymbol() != store2->getSymbolReference()->getSymbol())
return false;
if (trace())
traceMsg(comp(), " Store nodes opcode and symref checks out\n");
// Indirect stores must have the same base
//
int32_t valueIndex;
if (store1->getOpCode().isIndirect())
{
valueIndex = 1;
// - TODO
///return false;
}
else
{
valueIndex = 0;
}
TR::Node *value1 = store1->getChild(valueIndex);
TR::Node *value2 = store2->getChild(valueIndex);
if (valueIndex == 1) // indirect store, check base objects
{
TR::Node *base1 = store1->getFirstChild();
TR::Node *base2 = store2->getFirstChild();
if (!base1->getOpCode().hasSymbolReference() || !base2->getOpCode().hasSymbolReference())
return false;
if (base1->getSymbolReference()->getReferenceNumber() != base2->getSymbolReference()->getReferenceNumber())
return false;
if (trace())
traceMsg(comp(), " Indirect store base node opcode and symref checks out\n");
}
// The value on one of the stores must be zero. There is a special case if
// one of the values is 0 and the other is 1.
//
bool booleanValue = false;
bool swapCompare = false;
if (value1->getOpCode().isLoadConst())
{
int32_t int1 = value1->getInt();
if (value2->getOpCode().isLoadConst())
{
int32_t int2 = value2->getInt();
if (int1 == 1)
{
if (int2 == 0)
{
booleanValue = true;
swapCompare = true;
}
else
return false;
}
else if (int1 == 0)
{
if (int2 == 1)
booleanValue = true;
else
swapCompare = true;
}
else if (int2 != 0)
return false;
}
// Is this code really valid when the trees guarded by the if rely on the condition checked in the 'if' (e.g. we could have an indirect access without any checking in guarded code, because the test checked if the value was NULL, in which case by performing the simplification we could end up with a crash when the object is dereferenced)
//else if (int1 == 0)
// swapCompare = true;
else
{
return false;
}
}
// Is this code really valid when the trees guarded by the if rely on the condition checked in the 'if' (e.g. we could have an indirect access without any checking in guarded code, because the test checked if the value was NULL, in which case by performing the simplification we could end up with a crash when the object is dereferenced)
//else if (!(value2->getOpCode().isLoadConst() && value2->getInt() == 0))
// return false;
else
return false;
if (trace())
traceMsg(comp(), " Comparison values check out\n");
#if ALLOW_SIMPLIFY_COND_CODE_BOOLEAN_STORE
if (isBranchOnCondCode)
return simplifyCondCodeBooleanStore(joinBlock, compareNode, store1, store2);
#else
if (isBranchOnCondCode)
return false;
#endif
// The stores can be simplified
//
// The steps are:
// 1) Add an edge from the first block to the join block
// 2) Set up the istore to replace the compare, e.g.
// replace
// ificmpeq
// with
// istore
// cmpeq
// 3) Remove the 2 blocks containing the stores
// 4) Insert a goto from the first block to the join block if necessary
//
// TODO - support going to non-fallthrough join block
//
if (!performTransformation(comp(), "%sReplace compare-and-branch node [%p] with boolean compare\n", OPT_DETAILS, compareNode))
return false;
_cfg->addEdge(TR::CFGEdge::createEdge(_block, joinBlock, trMemory()));
// Re-use the store with the non-trivial value - for boolean store it doesn't
// matter which store we re-use.
//
needToDuplicateTree = true; // to avoid setting store node to NULL
TR::TreeTop *storeTreeTop;
TR::Node *storeNode;
if (swapCompare)
{
if (needToDuplicateTree)
{
storeTreeTop = NULL;
storeNode = store2->duplicateTree();
}
else
{
storeTreeTop = store2TreeTop;
storeNode = store2;
}
// Set up the new opcode for the compare node
//
TR::Node::recreate(compareNode, compareNode->getOpCode().getOpCodeForReverseBranch());
}
else
{
if (needToDuplicateTree)
{
storeTreeTop = NULL;
storeNode = store1->duplicateTree();
}
else
{
storeTreeTop = store1TreeTop;
storeNode = store1;
}
}
TR::Node *value = storeNode->getChild(valueIndex);
TR::Node::recreate(compareNode, compareNode->getOpCode().convertIfCmpToCmp());
TR::Node *node1;
TR::ILOpCodes convertOpCode = TR::BadILOp;
if (booleanValue)
{
// If the result is a boolean value (i.e. either a 0 or 1 is being stored
// as a result of the compare), the trees are changed from:
// ificmpxx --> L1
// ...
// ...
// istore
// x
// iconst 0
// ...
// goto L2
// L1:
// istore
// x
// iconst 1
// L2:
//
// to:
// istore
// x
// possible i2x conversion
// icmpxx
// ...
// Separate the original value on the store from the store node
//
value->recursivelyDecReferenceCount();
// The compare node will create a byte value. This must be converted to the
// type expected on the store.
//
int32_t size = storeNode->getOpCode().getSize();
if (size == 4)
{
storeNode->setChild(valueIndex, compareNode);
compareNode->incReferenceCount();
}
else
{
if (size == 1)
convertOpCode = TR::i2b;
else if (size == 2)
convertOpCode = TR::i2s;
else if (size == 8)
convertOpCode = TR::i2l;
node1 = TR::Node::create(convertOpCode, 1, compareNode);