-
Notifications
You must be signed in to change notification settings - Fork 746
/
Copy pathJ9Simplifier.cpp
1259 lines (1118 loc) · 49.9 KB
/
J9Simplifier.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 "omrport.h"
#include "optimizer/Simplifier.hpp"
#include "optimizer/J9SimplifierHelpers.hpp"
#include "optimizer/J9SimplifierHandlers.hpp"
#include "codegen/CodeGenerator.hpp"
#include "compile/Compilation.hpp"
#include "il/Block.hpp"
#include "il/Node.hpp"
#include "il/Node_inlines.hpp"
#include "optimizer/Optimization_inlines.hpp"
bool
J9::Simplifier::isLegalToUnaryCancel(TR::Node *node, TR::Node *firstChild, TR::ILOpCodes opcode)
{
if (!OMR::Simplifier::isLegalToUnaryCancel(node, firstChild, opcode))
return false;
if (node->getOpCode().isConversionWithFraction() &&
firstChild->getOpCode().isConversionWithFraction() &&
(node->getDecimalFraction() != firstChild->getDecimalFraction()))
{
// illegal to fold a pattern like:
// pd2f frac=5
// f2pd frac=0
// f
// to just 'f' because the extra digits that should be introduced by
// the frac=5 in the parent will be lost
if (trace())
traceMsg(comp(),"disallow unaryCancel of node %p and firstChild %p due to mismatch of decimal fractions (%d != %d)\n",
node,firstChild,node->getDecimalFraction(),firstChild->getDecimalFraction());
return false;
}
if (firstChild->getOpCodeValue() == opcode &&
node->getType().isBCD() &&
firstChild->getType().isBCD() &&
firstChild->getFirstChild()->getType().isBCD() &&
node->hasIntermediateTruncation())
{
// illegal to fold if there is an intermediate (firstChild) truncation:
// zd2pd p=4 0034
// pd2zd p=2 34
// pdx p=4 1234
// if folding is performed to remove zd2pd/pd2zd then the result will
// be 1234 instead of 0034
if (trace())
traceMsg(comp(),"disallow unaryCancel of node %p and firstChild %p due to intermediate truncation of node\n",node,firstChild);
return false;
}
else if (firstChild->getOpCodeValue() == opcode &&
node->getType().isBCD() &&
!firstChild->getType().isBCD())
{
int32_t nodeP = node->getDecimalPrecision();
int32_t childP = TR::DataType::getMaxPackedDecimalPrecision();
int32_t grandChildP = firstChild->getFirstChild()->getDecimalPrecision();
if (node->hasSourcePrecision())
childP = node->getSourcePrecision();
else if (firstChild->getDataType().canGetMaxPrecisionFromType())
childP = firstChild->getDataType().getMaxPrecisionFromType();
if (childP < nodeP && childP < grandChildP)
{
if (trace())
traceMsg(comp(),"disallow unaryCancel of node %p and firstChild %p due to intermediate truncation of node\n",node,firstChild);
return false;
}
}
else if (firstChild->getOpCodeValue() == opcode &&
!node->getType().isBCD() &&
!firstChild->getType().isBCD())
{
if (node->getDataType().canGetMaxPrecisionFromType() &&
firstChild->getDataType().canGetMaxPrecisionFromType() &&
node->getDataType().getMaxPrecisionFromType() > firstChild->getDataType().getMaxPrecisionFromType())
{
if (trace())
traceMsg(comp(),"disallow unaryCancel of node %p and firstChild %p due to intermediate truncation of node\n",node,firstChild);
return false;
}
}
return true;
}
TR::Node *
J9::Simplifier::unaryCancelOutWithChild(TR::Node *node, TR::Node *firstChild, TR::TreeTop *anchorTree, TR::ILOpCodes opcode, bool anchorChildren)
{
TR::Node *grandChild = OMR::Simplifier::unaryCancelOutWithChild(node, firstChild, anchorTree, opcode, anchorChildren);
if (grandChild == NULL)
return NULL;
TR_RawBCDSignCode alwaysGeneratedSign = comp()->cg()->alwaysGeneratedSign(node);
if (node->getType().isBCD() &&
grandChild->getType().isBCD() &&
(node->getDecimalPrecision() != grandChild->getDecimalPrecision() || alwaysGeneratedSign != raw_bcd_sign_unknown))
{
// must maintain the top level node's precision when replacing with the grandchild
// (otherwise if the parent of the node is call it will pass a too small or too big value)
TR::Node *origOrigGrandChild = grandChild;
if (node->getDecimalPrecision() != grandChild->getDecimalPrecision())
{
TR::Node *origGrandChild = grandChild;
grandChild = TR::Node::create(TR::ILOpCode::modifyPrecisionOpCode(grandChild->getDataType()), 1, origGrandChild);
origGrandChild->decReferenceCount(); // inc'd an extra time when creating modPrecision node above
grandChild->incReferenceCount();
grandChild->setDecimalPrecision(node->getDecimalPrecision());
dumpOptDetails(comp(), "%sCreate %s [" POINTER_PRINTF_FORMAT "] to reconcile precision mismatch between node %s [" POINTER_PRINTF_FORMAT "] grandChild %s [" POINTER_PRINTF_FORMAT "] (%d != %d)\n",
optDetailString(),
grandChild->getOpCode().getName(),
grandChild,
node->getOpCode().getName(),
node,
origOrigGrandChild->getOpCode().getName(),
origOrigGrandChild,
node->getDecimalPrecision(),
origOrigGrandChild->getDecimalPrecision());
}
// if the top level was always setting a particular sign code (e.g. ud2pd) then must maintain this side-effect here when cancelling
if (alwaysGeneratedSign != raw_bcd_sign_unknown)
{
TR::Node *origGrandChild = grandChild;
TR::ILOpCodes setSignOp = TR::ILOpCode::setSignOpCode(grandChild->getDataType());
TR_ASSERT(setSignOp != TR::BadILOp,"could not find setSignOp for type %s on %s (%p)\n",
grandChild->getDataType().toString(),grandChild->getOpCode().getName(),grandChild);
grandChild = TR::Node::create(setSignOp, 2,
origGrandChild,
TR::Node::iconst(origGrandChild, TR::DataType::getValue(alwaysGeneratedSign)));
origGrandChild->decReferenceCount(); // inc'd an extra time when creating setSign node above
grandChild->incReferenceCount();
grandChild->setDecimalPrecision(origGrandChild->getDecimalPrecision());
dumpOptDetails(comp(), "%sCreate %s [" POINTER_PRINTF_FORMAT "] to preserve setsign side-effect between node %s [" POINTER_PRINTF_FORMAT "] grandChild %s [" POINTER_PRINTF_FORMAT "] (sign=0x%x)\n",
optDetailString(),
grandChild->getOpCode().getName(),
grandChild,
node->getOpCode().getName(),
node,
origOrigGrandChild->getOpCode().getName(),
origOrigGrandChild,
TR::DataType::getValue(alwaysGeneratedSign));
}
}
return grandChild;
}
bool
J9::Simplifier::isRecognizedPowMethod(TR::Node *node)
{
TR::Symbol *symbol = node->getSymbol();
TR::MethodSymbol *methodSymbol = symbol ? symbol->castToMethodSymbol() : NULL;
return (methodSymbol &&
(methodSymbol->getRecognizedMethod() == TR::java_lang_Math_pow ||
methodSymbol->getRecognizedMethod() == TR::java_lang_StrictMath_pow));
}
bool
J9::Simplifier::isRecognizedAbsMethod(TR::Node * node)
{
TR::Symbol *symbol = node->getSymbol();
TR::MethodSymbol *methodSymbol = symbol ? symbol->castToMethodSymbol() : NULL;
return (methodSymbol &&
(methodSymbol->getRecognizedMethod() == TR::java_lang_Math_abs_D ||
methodSymbol->getRecognizedMethod() == TR::java_lang_Math_abs_F ||
methodSymbol->getRecognizedMethod() == TR::java_lang_Math_abs_I));
}
bool
J9::Simplifier::isRecognizedObjectComparisonNonHelper(TR::Node *node, TR::SymbolReferenceTable::CommonNonhelperSymbol &nonHelperSymbol)
{
bool isRecognized = false;
if (node->getOpCode().isCall())
{
if (comp()->getSymRefTab()->isNonHelper(node->getSymbolReference(),
TR::SymbolReferenceTable::objectEqualityComparisonSymbol))
{
isRecognized = true;
nonHelperSymbol = TR::SymbolReferenceTable::objectEqualityComparisonSymbol;
}
else if (comp()->getSymRefTab()->isNonHelper(node->getSymbolReference(),
TR::SymbolReferenceTable::objectInequalityComparisonSymbol))
{
isRecognized = true;
nonHelperSymbol = TR::SymbolReferenceTable::objectInequalityComparisonSymbol;
}
}
return isRecognized;
}
TR::Node *
J9::Simplifier::foldAbs(TR::Node *node)
{
TR::Node * childNode = NULL;
if (node->getNumChildren() == 1)
childNode = node->getFirstChild();
else if (node->getNumChildren() == 2)
{
TR_ASSERT(node->getFirstChild()->getOpCodeValue() == TR::loadaddr, "The first child of abs is either the value or loadaddr");
childNode = node->getSecondChild();
}
if (childNode &&
(childNode->isNonNegative() || (node->getReferenceCount()==1)) &&
performTransformation(comp(), "%sFolded abs for postive argument on node [%p]\n", optDetailString(), node))
{
TR::TreeTop::create(comp(), _curTree->getPrevTreeTop(),
TR::Node::create(TR::treetop, 1, childNode));
node = replaceNode(node, childNode, _curTree);
_alteredBlock = true;
}
return node;
}
TR::Node *
J9::Simplifier::simplifyiCallMethods(TR::Node * node, TR::Block * block)
{
if (isRecognizedAbsMethod(node))
{
node = foldAbs(node);
}
else if (isRecognizedPowMethod(node))
{
static char *skipit = feGetEnv("TR_NOMATHRECOG");
if (skipit != NULL) return node;
int32_t numChildren = node->getNumChildren();
// call can have 2 or 3 args. In both cases, the last two are
// the parameters of interest.
TR::Node *expNode = node->getChild(numChildren-1);
TR::Node *valueNode = node->getChild(numChildren-2);
// In Java strictmath.pow(), if both arguments are integers,
// then the result is exactly equal to the mathematical result
// of raising the first argument to the power of the second argument
// if that result can in fact be represented exactly as a double value.
//(In the foregoing descriptions, a floating-point value is considered
// to be an integer if and only if it is finite and a fixed point of
// the method ceil or, equivalently, a fixed point of the method floor.
// A value is a fixed point of a one-argument method if and only if
// the result of applying the method to the value is equal to the value.)
if (valueNode->getOpCodeValue() == TR::dconst &&
expNode->getOpCodeValue() == TR::dconst &&
valueNode->getDouble() == 10.0 && expNode->getDouble() == 4.0)
{
foldDoubleConstant(node, 10000.0, (TR::Simplifier *) this);
}
}
else
{
TR::SymbolReferenceTable::CommonNonhelperSymbol nonHelperSymbol;
if (isRecognizedObjectComparisonNonHelper(node, nonHelperSymbol))
{
TR::Node *lhs = node->getChild(0);
const bool lhsNull =
lhs->getOpCodeValue() == TR::aconst
&& lhs->getConstValue() == 0;
TR::Node *rhs = node->getChild(1);
const bool rhsNull =
rhs->getOpCodeValue() == TR::aconst
&& rhs->getConstValue() == 0;
// If either operand is null, no need to use the equality/inequality comparison
// helper, as direct comparison of the two references will suffice. Also, if both operands
// are the same node, no need to use the comparison helper - the references must be
// equal. Fold both cases to use acmpeq or acmpne which might be further simplified
//
if (lhsNull || rhsNull || lhs == rhs)
{
const bool isEqualityComparison = (nonHelperSymbol == TR::SymbolReferenceTable::objectEqualityComparisonSymbol);
if (performTransformation(
comp(),
"%sChanging n%un from %s to %s\n",
optDetailString(),
node->getGlobalIndex(),
comp()->getSymRefTab()->getNonHelperSymbolName(nonHelperSymbol),
isEqualityComparison ? "acmpeq" : "acmpne"))
{
const char *counterName = TR::DebugCounter::debugCounterName(comp(), "vt-helper/simplifier-xformed/acmp/(%s)/bc=%d",
comp()->signature(), node->getByteCodeIndex());
TR::DebugCounter::incStaticDebugCounter(comp(), counterName);
TR::Node::recreate(node, isEqualityComparison ? TR::acmpeq : TR::acmpne);
node = simplify(node, block);
}
}
}
}
return node;
}
/**
* Current implementation of matchAndSimplifyTimeMethods focuses on java time functions.
*/
TR::Node *
J9::Simplifier::simplifylCallMethods(TR::Node * node, TR::Block * block)
{
TR::MethodSymbol * methodSymbol = node->getSymbol()->getMethodSymbol();
if (methodSymbol)
{
if ((methodSymbol->getRecognizedMethod() == TR::java_lang_System_currentTimeMillis) &&
comp()->cg()->getSupportsMaxPrecisionMilliTime() &&
(methodSymbol->isJNI() || methodSymbol->isVMInternalNative() || methodSymbol->isJITInternalNative()))
{
node = convertCurrentTimeMillis(node, block);
}
else if ((methodSymbol->getRecognizedMethod() == TR::java_lang_System_nanoTime) &&
comp()->cg()->getSupportsCurrentTimeMaxPrecision() &&
(methodSymbol->isJNI() || methodSymbol->isVMInternalNative() || methodSymbol->isJITInternalNative()))
{
node = convertNanoTime(node, block);
}
}
else
{
TR::MethodSymbol * symbol = node->getSymbol()->castToMethodSymbol();
if (symbol &&
(symbol->getRecognizedMethod()==TR::java_lang_Math_abs_L))
{
node = foldAbs(node);
}
}
return node;
}
// Convert currentTimeMillis into currentTimeMaxPrecision from:
// <tree>
// lcall currentTimeMillis
//
// to:
//
// <new tree>
// lcall currentTimeMaxPrecision
// <tree>
// ldiv
// ==>lcall currentTimeMaxPrecision
// lconst <divisor value>
TR::Node *J9::Simplifier::convertCurrentTimeMillis(TR::Node * node, TR::Block * block)
{
if (performTransformation(comp(), "%sConvert currentTimeMillis to currentTimeMaxPrecision with divide of"
INT64_PRINTF_FORMAT " on node [%p]\n", optDetailString(), OMRPORT_TIME_HIRES_MILLITIME_DIVISOR, node))
{
TR::Node* lcallNode = TR::Node::createWithSymRef(node, TR::lcall, 0,
comp()->getSymRefTab()->findOrCreateCurrentTimeMaxPrecisionSymbol());
TR::TreeTop* callTreeTop = findTreeTop(node, block);
TR_ASSERT(callTreeTop != NULL, "should have been able to find this tree top");
if (node->getNumChildren() >= 1)
{
anchorNode(node->getFirstChild(), _curTree);
node->getFirstChild()->recursivelyDecReferenceCount();
}
TR::Node* divConstNode = TR::Node::create(node, TR::lconst);
divConstNode->setLongInt(OMRPORT_TIME_HIRES_MILLITIME_DIVISOR);
TR::Node::recreate(node, TR::ldiv);
callTreeTop->insertBefore(TR::TreeTop::create(comp(), TR::Node::create(node, TR::treetop, 1, lcallNode)));
node->setNumChildren(2);
node->setAndIncChild(0, lcallNode);
node->setAndIncChild(1, divConstNode);
TR::Node *callTreeNode = callTreeTop->getNode();
if (callTreeNode->getOpCode().isResolveCheck())
{
if (callTreeNode->getOpCodeValue() == TR::ResolveCHK)
TR::Node::recreate(callTreeNode, TR::treetop);
else
TR_ASSERT(0, "Null check not expected in call to static method in class System\n");
}
_alteredBlock = true;
}
return node;
}
// Convert java/lang/System.nanoTime() into currentTimeMaxPrecision
//
// Generate trees based on the following:
// const int64_t quotient = lcall / denominator;
// const int64_t remainder = lcall - quotient * denominator;
// const int64_t result = quotient * numerator + ((remainder * numerator) / denominator);
//
// The resulting tree:
// <tree>
// ladd
// lmul
// ldiv
// ==>lcall currentTimeMaxPrecision
// lconst <denominator>
// lconst <numerator>
// ldiv
// lmul
// lsub
// ==>lcall currentTimeMaxPrecision
// lmul
// ==>ldiv
// ==>lconst <denominator>
// ==>lconst <numerator>
// ==>lconst <denominator>
TR::Node *J9::Simplifier::convertNanoTime(TR::Node * node, TR::Block * block)
{
if (performTransformation(comp(), "%sConvert nanoTime to currentTimeMaxPrecision with multiply of %d/%d on node [%p]\n",
optDetailString(), OMRPORT_TIME_HIRES_NANOTIME_NUMERATOR, OMRPORT_TIME_HIRES_NANOTIME_DENOMINATOR, node))
{
TR::Node* lcallNode = TR::Node::createWithSymRef(node, TR::lcall, 0, comp()->getSymRefTab()->findOrCreateCurrentTimeMaxPrecisionSymbol());
TR::TreeTop* callTreeTop = findTreeTop(node, block);
TR_ASSERT(callTreeTop != NULL, "should have been able to find this tree top");
if (node->getNumChildren() >= 1)
{
anchorNode(node->getFirstChild(), _curTree);
node->getFirstChild()->recursivelyDecReferenceCount();
}
TR::Node* numeratorNode = TR::Node::lconst(node, OMRPORT_TIME_HIRES_NANOTIME_NUMERATOR);
TR::Node* denominatorNode = TR::Node::lconst(node, OMRPORT_TIME_HIRES_NANOTIME_DENOMINATOR);
// const int64_t quotient = lcall / denominator;
TR::Node* quotientNode = TR::Node::create(node, TR::ldiv, 2, lcallNode, denominatorNode);
// Calculating remainder {{
// const int64_t remainderMulNode = quotient * denominator;
TR::Node* remainderMulNode = TR::Node::create(node, TR::lmul, 2, quotientNode, denominatorNode);
//const int64_t remainder = lcall - remainderMulNode;
TR::Node* remainderNode = TR::Node::create(node, TR::lsub, 2, lcallNode, remainderMulNode);
// }}
// Calculating result {{
// const int64_t addendNode1 = quotient * numerator;
TR::Node* addendNode1 = TR::Node::create(node, TR::lmul, 2, quotientNode, numeratorNode);
// const int64_t addend2MulNode = (remainder * numerator);
TR::Node* addend2MulNode = TR::Node::create(node, TR::lmul, 2, remainderNode, numeratorNode);
// const int64_t addendNode1 = ((remainder * numerator) / denominator);
TR::Node* addendNode2 = TR::Node::create(node, TR::ldiv, 2, addend2MulNode, denominatorNode);
// const int64_t result = quotient * numerator + ((remainder * numerator) / denominator);
TR::Node::recreate(node, TR::ladd);
node->setNumChildren(2);
node->setAndIncChild(0, addendNode1);
node->setAndIncChild(1, addendNode2);
// }}
TR::Node *callTreeNode = callTreeTop->getNode();
if (callTreeNode->getOpCode().isResolveCheck())
{
if (callTreeNode->getOpCodeValue() == TR::ResolveCHK)
TR::Node::recreate(callTreeNode, TR::treetop);
else
TR_ASSERT(0, "Null check not expected in call to static method in class System\n");
}
_alteredBlock = true;
}
return node;
}
TR::Node *
J9::Simplifier::simplifyaCallMethods(TR::Node * node, TR::Block * block)
{
if ((node->getOpCode().isCallDirect()) && !node->getSymbolReference()->isUnresolved() &&
(node->getSymbol()->isResolvedMethod()))
{
bool isSimplifiableMethod = false;
// Will arg be NULLCHKed? If so, preserve it even if call is simplified away
bool requiresArgNULLCHK = false;
switch (node->getSymbol()->getResolvedMethodSymbol()->getRecognizedMethod())
{
case TR::java_math_BigDecimal_valueOf:
{
isSimplifiableMethod = true;
break;
}
case TR::java_math_BigDecimal_add:
case TR::java_math_BigDecimal_subtract:
case TR::java_math_BigDecimal_multiply:
case TR::java_math_BigInteger_add:
case TR::java_math_BigInteger_subtract:
case TR::java_math_BigInteger_multiply:
{
isSimplifiableMethod = true;
requiresArgNULLCHK = true;
break;
}
default:
{
isSimplifiableMethod = false;
break;
}
}
if (isSimplifiableMethod && (node->getReferenceCount() == 1) &&
performTransformation(comp(),
requiresArgNULLCHK ?
("%sReplaced dead BigDecimal/BigInteger call node [" POINTER_PRINTF_FORMAT "] with NULLCHK of argument\n") :
("%sRemoved dead BigDecimal/BigInteger call node [" POINTER_PRINTF_FORMAT "]\n"),
optDetailString(), node))
{
TR::Node *firstChild = node->getFirstChild();
anchorChildren(node, _curTree);
firstChild->incReferenceCount();
// Need to preserve NULLCHK for argument to
// Big{Integer|Decimal}.{add|multiply|subtract}
if (requiresArgNULLCHK)
{
TR::Node *secondChild = node->getSecondChild();
TR::SymbolReference * const nullCheckSR =
comp()->getSymRefTab()->findOrCreateNullCheckSymbolRef(
comp()->getMethodSymbol());
TR::TreeTop::create(comp(), _curTree,
TR::Node::createWithSymRef(node, TR::NULLCHK, 1,
TR::Node::create(node, TR::PassThrough, 1, secondChild),
nullCheckSR));
_alteredBlock = true;
}
for (int i = 0; i < node->getNumChildren(); i++)
node->getChild(i)->recursivelyDecReferenceCount();
TR::Node::recreate(node, TR::PassThrough);
node->setNumChildren(1);
}
}
return node;
}
bool
J9::Simplifier::isLegalToFold(TR::Node *node, TR::Node *firstChild)
{
if (!OMR::Simplifier::isLegalToFold(node, firstChild))
return false;
if (node->getOpCode().isConversionWithFraction() &&
firstChild->getOpCode().isConversionWithFraction() &&
node->getDecimalFraction() != firstChild->getDecimalFraction())
{
// illegal to fold a pattern like:
// pd2d frac=5
// f2pd frac=0
// f
// to just f2d because the extra digits that should be introduced by the frac=5 in the parent will be lost
return false;
}
if (node->getOpCode().isConversionWithFraction() &&
!firstChild->getOpCode().isConversionWithFraction() &&
node->getDecimalFraction() != 0)
{
// illegal to fold a pattern like:
// pd2f frac=5
// i2pd
// i
// to just i2f because the extra digits that should be introduced by the frac=5 in the parent will be lost
return false;
}
return true;
}
TR::Node *
J9::Simplifier::getUnsafeIorByteChild(TR::Node *child, TR::ILOpCodes b2iOpCode, int32_t mulConst)
{
if (child->getOpCodeValue() == TR::imul &&
child->getSecondChild()->getOpCodeValue() == TR::iconst &&
child->getSecondChild()->getInt() == mulConst &&
child->getFirstChild()->getOpCodeValue() == b2iOpCode &&
child->getFirstChild()->getReferenceCount() == 1 &&
child->getFirstChild()->getFirstChild()->getOpCodeValue() == TR::bloadi &&
child->getFirstChild()->getFirstChild()->getReferenceCount() == 1 &&
child->getFirstChild()->getFirstChild()->getSymbolReference() == getSymRefTab()->findOrCreateUnsafeSymbolRef(TR::Int8))
{
return child->getFirstChild()->getFirstChild()->getFirstChild();
}
return NULL;
}
static TR::Node *getUnsafeBaseAddr(TR::Node * node, int32_t isubConst)
{
if (node->getOpCodeValue() == TR::isub &&
node->getReferenceCount() == 1 &&
node->getSecondChild()->getOpCodeValue() == TR::iconst &&
node->getSecondChild()->getInt() == isubConst)
{
return node->getFirstChild();
}
return NULL;
}
TR::Node *
J9::Simplifier::getLastUnsafeIorByteChild(TR::Node *child)
{
if (child->getOpCodeValue() == TR::bu2i &&
child->getReferenceCount() == 1 &&
child->getFirstChild()->getOpCodeValue() == TR::bloadi &&
child->getFirstChild()->getReferenceCount() == 1 &&
child->getFirstChild()->getSymbolReference() == getSymRefTab()->findOrCreateUnsafeSymbolRef(TR::Int8))
{
return child->getFirstChild()->getFirstChild();
}
return NULL;
}
/** Check if we are reading an element from a byte array and multiplying by mulConst.
@param mulConst will be a power of 2 because the pattern we are matching involves shifting by 8/16/24 bits.
@return the child node (that is expected to be an aiadd or aladd) under the byte array element load.
*/
TR::Node * J9::Simplifier::getArrayByteChildWithMultiplier(TR::Node * child, TR::ILOpCodes b2iOpCode, int32_t mulConst)
{
// The refCount == 1 checks ensure that we only transform cases where there are no
// other uses of the four bytes, as the performance benefit would likely be
// negated if we have to re-read any of the individual byte values.
if (child->getOpCodeValue() == TR::imul &&
child->getSecondChild()->getOpCodeValue() == TR::iconst && child->getSecondChild()->getInt() == mulConst &&
child->getFirstChild()->getOpCodeValue() == b2iOpCode && child->getFirstChild()->getReferenceCount() == 1 &&
child->getFirstChild()->getFirstChild()->getOpCodeValue() == TR::bloadi &&
child->getFirstChild()->getFirstChild()->getReferenceCount() == 1 &&
child->getFirstChild()->getFirstChild()->getSymbolReference()->getSymbol()->isArrayShadowSymbol())
{
return child->getFirstChild()->getFirstChild()->getFirstChild();
}
return NULL;
}
/** Check if we are simply reading an element from a byte array.
This "last" byte in the pattern we are matching does not involve any shifting and we will read and OR
this byte into the 32-bit word where the other three bytes we read have been shifted by 8/16/24 bits.
@return the child node (that is expected to be an aiadd or aladd) under the byte array element load.
*/
TR::Node * J9::Simplifier::getLastArrayByteChild(TR::Node * child)
{
// The refCount == 1 checks ensure that we only transform cases where there are no
// other uses of the four bytes, as the performance benefit would likely be
// negated if we have to re-read any of the individual byte values.
if (child->getOpCodeValue() == TR::bu2i && child->getReferenceCount() == 1 &&
child->getFirstChild()->getOpCodeValue() == TR::bloadi &&
child->getFirstChild()->getReferenceCount() == 1 &&
child->getFirstChild()->getSymbolReference()->getSymbol()->isArrayShadowSymbol())
{
return child->getFirstChild()->getFirstChild();
}
return NULL;
}
/** Get the first child of an aiadd/aladd which will be a node representing the underlying array.
@return the first child or 0 if we do not pass in an aiadd/aladd node since that would break the pattern we are matching.
*/
TR::Node * J9::Simplifier::getArrayBaseAddr(TR::Node * node)
{
if (node->getOpCode().isArrayRef() && node->getReferenceCount() == 1)
{
return node->getFirstChild();
}
return NULL;
}
/** Check if we are reading a byte array element that is offset by a particular constant amount
off a base index value.
Since the pattern we are matching involves reading four successive byte array elements, we are
essentially looking for whatever base index value the first array element access used being offset
by +1/+2/+3 for the subsequent array accesses.
@return 0 if the constant offset value passed in did not match what is expected at the given byte
array load, otherwise return the base index value, e.g. array accesses a[i], a[i + 1], a[i + 2],
a[i + 3] will return i as the base index value.
*/
TR::Node * J9::Simplifier::getArrayOffset(TR::Node * node, int32_t isubConst)
{
// The refCount == 1 checks ensure that we only transform cases where there are no
// other uses of the node, as the performance benefit would likely be
// negated if we have to re-generate the node after transforming the sequence to
// an ibyteswap.
if (node->getOpCode().isArrayRef() && node->getReferenceCount() == 1 &&
node->getSecondChild()->getOpCode().isSub() &&
node->getSecondChild()->getReferenceCount() == 1 &&
((node->getSecondChild()->getSecondChild()->getOpCodeValue() == TR::iconst && node->getSecondChild()->getSecondChild()->getInt() == isubConst) ||
(node->getSecondChild()->getSecondChild()->getOpCodeValue() == TR::lconst && node->getSecondChild()->getSecondChild()->getLongInt() == (int64_t)isubConst))
)
{
return node->getSecondChild()->getFirstChild();
}
return NULL;
}
TR::Node *
J9::Simplifier::simplifyiOrPatterns(TR::Node *node)
{
TR::Node *firstChild = node->getFirstChild();
TR::Node *secondChild = node->getSecondChild();
TR::ILOpCodes firstChildOp = firstChild->getOpCodeValue();
TR::ILOpCodes secondChildOp = secondChild->getOpCodeValue();
// optimize java/nio/DirectByteBuffer.getInt(I)I
//
// Little Endian
// ior
// ior
// imul
// bu2i
// bloadi #245[0x107AF564] Shadow[<Unsafe shadow sym>]
// isub
// l2i
// lload #202[0x107A1050] Parm[<parm 1 J>]
// iconst -1
// iconst 256
// ior
// imul
// bu2i
// bloadi #245[0x107AF564] Shadow[<Unsafe shadow sym>]
// isub
// ==>l2i at [0x107AE8F4]
// iconst -3
// iconst 16777216
// imul
// bu2i
// bloadi #245[0x107AF564] Shadow[<Unsafe shadow sym>]
// isub
// ==>l2i at [0x107AE8F4]
// iconst -2
// iconst 65536
// bu2i
// bloadi #245[0x107AF564] Shadow[<Unsafe shadow sym>]
// ==>l2i at [0x107AE8F4]
//
TR::Node *byte1, *byte2, *byte3, *byte4, *temp, *addr;
if (firstChild->getReferenceCount() == 1 &&
firstChildOp == TR::ior &&
firstChild->getSecondChild()->getOpCodeValue() == TR::ior &&
(byte1 = getUnsafeIorByteChild(firstChild->getSecondChild()->getFirstChild(), TR::bu2i, 16777216)) &&
(byte2 = getUnsafeIorByteChild(firstChild->getSecondChild()->getSecondChild(), TR::bu2i, 65536)) &&
(byte3 = getUnsafeIorByteChild(firstChild->getFirstChild(), TR::bu2i, 256)) &&
(byte4 = getLastUnsafeIorByteChild(node->getSecondChild())))
{
if (comp()->target().cpu.isLittleEndian())
{
temp = byte1, byte1 = byte4, byte4 = temp;
temp = byte2, byte2 = byte3, byte3 = temp;
}
if ((addr = getUnsafeBaseAddr(byte2, -1)) && addr == byte1 &&
(addr = getUnsafeBaseAddr(byte3, -2)) && addr == byte1 &&
(addr = getUnsafeBaseAddr(byte4, -3)) && addr == byte1 &&
performTransformation(comp(), "%sconvert ior to iloadi node [" POINTER_PRINTF_FORMAT "]\n", optDetailString(), node))
{
TR::Node::recreate(node, TR::iloadi);
node->setNumChildren(1);
node->setSymbolReference(getSymRefTab()->findOrCreateUnsafeSymbolRef(TR::Int32));
node->setAndIncChild(0, byte1);
// now remove the two children
firstChild->recursivelyDecReferenceCount();
secondChild->recursivelyDecReferenceCount();
return node;
}
}
static char *disableIORByteSwap = feGetEnv("TR_DisableIORByteSwap");
firstChild = node->getFirstChild();
secondChild = node->getSecondChild();
if (!disableIORByteSwap &&
!comp()->target().cpu.isBigEndian() && // the bigEndian case needs more thought
secondChild->getOpCodeValue() == TR::ior)
{
/*
A common application pattern for reading from a byte array and performing endianness conversion
will perform four consecutive reads from a byte array, multiplying three of the bytes
by 256, 65536 and 16777216 and or'ing the results together. We can detect this pattern and
transform it to a single ibyteswap. Note that the ishl in the trees below have been converted
to imul by the time we reach here.
ior
ishl
b2i
bloadi <array-shadow>
aladd
==>aLoad
lsub
i2l
==>iLoad
lconst -11
iconst 24
ior
ishl
bu2i
bloadi <array-shadow>
aladd
==>aLoad
lsub
==>i2l
lconst -10
iconst 16
ior
ishl
bu2i
bloadi <array-shadow>
aladd
==>aLoad
lsub
==>i2l
lconst -9
iconst 8
bu2i
bloadi <array-shadow>
aladd
==>aLoad
lsub
==>i2l
lconst -8
*/
TR::Node * byte1, * byte2, * byte3, * byte4, * temp, * addr, *offset, *addr1, *offset1;
int32_t initialConstantOffset;
const int SHIFT8MUL=256;
const int SHIFT16MUL=65536;
const int SHIFT24MUL=16777216;
// Check for the expected pattern of four array accesses
if (secondChild->getSecondChild()->getOpCodeValue() == TR::ior && secondChild->getReferenceCount() == 1 &&
(byte1 = getArrayByteChildWithMultiplier(firstChild, TR::b2i, SHIFT24MUL)) &&
(byte2 = getArrayByteChildWithMultiplier(secondChild->getFirstChild(), TR::bu2i, SHIFT16MUL)) &&
(byte3 = getArrayByteChildWithMultiplier(secondChild->getSecondChild()->getFirstChild(), TR::bu2i, SHIFT8MUL)) &&
(byte4 = getLastArrayByteChild(secondChild->getSecondChild()->getSecondChild())))
{
// This caused incorrect behaviour, so I've disabled the entire code path for bigEndian (see above)
//if (comp()->target().cpu.isBigEndian())
// {
// temp = byte1, byte1 = byte4, byte4 = temp;
// temp = byte2, byte2 = byte3, byte3 = temp;
// }
// Get the address and offset from the first array access
// These will be compared with the subsequent array accesses to confirm that
// the code is reading four contiguous locations.
addr1 = NULL;
if (byte1->getOpCode().isArrayRef() && byte1->getReferenceCount() == 1)
addr1 = byte1->getFirstChild();
offset1 = NULL;
if (addr1 && byte1->getSecondChild()->getOpCode().isSub() && byte1->getSecondChild()->getReferenceCount() == 1)
offset1 = byte1->getSecondChild()->getFirstChild();
// Get the array offset from the first array access
bool expectedConstantOffset1 = false;
if (offset1)
{
if ((byte1->getSecondChild()->getSecondChild()->getOpCodeValue() == TR::lconst &&
byte1->getSecondChild()->getSecondChild()->getLongInt() <= ((int64_t) TR::getMaxSigned<TR::Int32>()) &&
byte1->getSecondChild()->getSecondChild()->getLongInt() >= ((int64_t) TR::getMinSigned<TR::Int32>())))
{
expectedConstantOffset1 = true;
initialConstantOffset = (int32_t) byte1->getSecondChild()->getSecondChild()->getLongInt();
}
else if (byte1->getSecondChild()->getSecondChild()->getOpCodeValue() == TR::iconst)
{
expectedConstantOffset1 = true;
initialConstantOffset = byte1->getSecondChild()->getSecondChild()->getInt();
}
}
// Confirm that all four array accesses deal with the same base address and offset, i.e. the code
// is reading four contiguous bytes.
if (expectedConstantOffset1 &&
(getArrayBaseAddr(byte2) == addr1) && (getArrayOffset(byte2, initialConstantOffset+1) == offset1) &&
(getArrayBaseAddr(byte3) == addr1) && (getArrayOffset(byte3, initialConstantOffset+2) == offset1) &&
(getArrayBaseAddr(byte4) == addr1) && (getArrayOffset(byte4, initialConstantOffset+3) == offset1) &&
performTransformation(comp(), "%sconvert ior to ibyteswap node [" POINTER_PRINTF_FORMAT "]\n", optDetailString(), node))
{
TR::Node::recreateWithSymRef(node, TR::iloadi, getSymRefTab()->findOrCreateUnsafeSymbolRef(TR::Int32));
node->setNumChildren(1);
node->setAndIncChild(0, byte4);
// now remove the two children
firstChild->recursivelyDecReferenceCount();
secondChild->recursivelyDecReferenceCount();
return node;
}
}
}
// recognize Long.signum(), which maps to TR::lcmp
// ior
// l2i <- fc
// lshr <- fgc
// load X <- loadVal
// iconst 63
// l2i <- sc
// lushr <- sgc
// lneg
// load X
// iconst 63
//
//
//
//
if (firstChild->getOpCodeValue() == TR::l2i &&
secondChild->getOpCodeValue() == TR::l2i &&
firstChild ->getFirstChild()->getOpCodeValue() == TR::lshr &&
secondChild->getFirstChild()->getOpCodeValue() == TR::lushr)
{
TR::Node *firstGrandChild = firstChild->getFirstChild();
TR::Node *secondGrandChild = secondChild->getFirstChild();
if (secondGrandChild->getFirstChild()->getOpCodeValue() == TR::lneg &&
firstGrandChild->getSecondChild()->getOpCodeValue() == TR::iconst &&
firstGrandChild->getSecondChild()->getInt() == 63 &&
secondGrandChild->getSecondChild()->getOpCodeValue() == TR::iconst &&
secondGrandChild->getSecondChild()->getInt() == 63)
{
TR::Node *loadVal = firstGrandChild->getFirstChild();
if (secondGrandChild->getFirstChild()->getFirstChild() == loadVal &&
(loadVal->getOpCode().isLoadVar() || loadVal->getOpCode().isLoadReg()) &&
performTransformation(comp(), "%sTransform ior to lcmp [" POINTER_PRINTF_FORMAT "]\n", optDetailString(), node))
{
TR::Node::recreate(node, TR::lcmp);
TR::Node * constZero = TR::Node::create(secondChild, TR::lconst, 0);
constZero->setLongInt(0);
node->setFirst(replaceNode(firstChild,loadVal, _curTree));
node->setSecond(replaceNode(secondChild, constZero, _curTree));
return node;
}
}
}
return NULL;
}
TR::Node *
J9::Simplifier::getOrOfTwoConsecutiveBytes(TR::Node * ior)
{
// Big Endian
// ior
// imul
// b2i
// bloadi #231[0x10D06670] Shadow[unknown field]
// address
// iconst 256
// bu2i
// bloadi #231[0x10D06670] Shadow[unknown field]
// isub
// ==>address
// iconst -1
//
// Little Endian
// ior
// imul