-
Notifications
You must be signed in to change notification settings - Fork 747
/
Copy pathJ9SimplifierHandlers.cpp
3171 lines (2845 loc) · 142 KB
/
J9SimplifierHandlers.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 "optimizer/OMRSimplifierHelpers.hpp"
#include "optimizer/OMRSimplifierHandlers.hpp"
#include "optimizer/J9SimplifierHelpers.hpp"
#include "optimizer/J9SimplifierHandlers.hpp"
#include "optimizer/Simplifier.hpp"
#include <algorithm>
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "codegen/CodeGenerator.hpp"
#include "env/FrontEnd.hpp"
#include "codegen/RecognizedMethods.hpp"
#include "codegen/TreeEvaluator.hpp"
#include "compile/Compilation.hpp"
#include "compile/SymbolReferenceTable.hpp"
#include "control/Options.hpp"
#include "control/Options_inlines.hpp"
#include "env/IO.hpp"
#include "env/ObjectModel.hpp"
#include "env/TRMemory.hpp"
#include "env/VMJ9.h"
#include "il/AliasSetInterface.hpp"
#include "il/Block.hpp"
#include "il/DataTypes.hpp"
#include "il/ILOpCodes.hpp"
#include "il/ILOps.hpp"
#include "il/MethodSymbol.hpp"
#include "il/Node.hpp"
#include "il/Node_inlines.hpp"
#include "il/ResolvedMethodSymbol.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/Bit.hpp"
#include "infra/Cfg.hpp"
#include "infra/HashTab.hpp"
#include "infra/List.hpp"
#include "infra/TRCfgEdge.hpp"
#include "optimizer/Optimization.hpp"
#include "optimizer/Optimization_inlines.hpp"
#include "optimizer/OptimizationManager.hpp"
#include "optimizer/Optimizations.hpp"
#include "optimizer/Optimizer.hpp"
#include "optimizer/Structure.hpp"
#include "optimizer/Simplifier.hpp"
#include "optimizer/TransformUtil.hpp"
enum TR_PackedCompareCondition
{
TR_PACKED_COMPARE_EQUAL = 0, // negative 0 is considered equal to positive zero
TR_PACKED_COMPARE_LESS = 1,
TR_PACKED_COMPARE_GREATER = 2,
TR_PACKED_COMPARE_UNKNOWN = 3,
TR_PACKED_COMPARE_TYPES = 4
};
// Forward declarations.
static TR::Node *foldSetSignIntoNode(TR::Node *setSign, bool setSignIsTheChild, TR::Node *other, bool removeSetSign, TR::Block *block, TR::Simplifier *s);
static TR::Node *foldAndReplaceDominatedSetSign(TR::Node *setSign, bool setSignIsTheChild, TR::Node *other, TR::Block * block, TR::Simplifier * s);
static TR::Node *createSetSignForKnownSignChild(TR::Node *node, TR::Block * block, TR::Simplifier * s);
static void propagateNonNegativeFlagForArithmetic(TR::Node *node, TR::Simplifier *s)
{
if (!node->isNonNegative() &&
node->getFirstChild()->isNonNegative() && node->getSecondChild()->isNonNegative() &&
performTransformation(s->comp(),"%sSet X>=0 flag on %s [" POINTER_PRINTF_FORMAT "] due to both children X>=0\n",s->optDetailString(),node->getOpCode().getName(),node))
{
node->setIsNonNegative(true);
}
}
static TR::Node *foldSetSignFromGrandChild(TR::Node *node, TR::Block *block, TR::Simplifier *s)
{
// set sign operations are typically cheaper then the corresponding general sign moving operation (eg. zdshrSetSign vs zdshr).
TR::Node *child = node->getFirstChild();
TR::ILOpCodes newSetSignOp = TR::ILOpCode::setSignVersionOfOpCode(node->getOpCodeValue());
if (node->getReferenceCount() == 1 &&
newSetSignOp != TR::BadILOp &&
child->getReferenceCount() == 1 &&
(child->getOpCodeValue() == TR::zd2pd || child->getOpCodeValue() == TR::pd2zd) &&
child->getFirstChild()->getReferenceCount() == 1 &&
child->getFirstChild()->getOpCode().isSetSign())
{
TR::Node *grandChild = child->getFirstChild();
int32_t grandChildSetSignIndex = TR::ILOpCode::getSetSignValueIndex(grandChild->getOpCodeValue());
TR::Node *grandChildSetSign = child->getFirstChild()->getChild(grandChildSetSignIndex);
int32_t convertedSetSign = TR::DataType::getInvalidSignCode();
if (grandChildSetSign->getOpCode().isLoadConst())
{
int32_t originalSetSign = grandChildSetSign->get32bitIntegralValue();
convertedSetSign = TR::DataType::convertSignEncoding(grandChild->getDataType(), node->getDataType(), originalSetSign);
TR_ASSERT(convertedSetSign != TR::DataType::getInvalidSignCode(),"could not convert sign encoding of 0x%x from type %s to type %s\n",originalSetSign,grandChild->getDataType().toString(),node->getDataType().toString());
}
bool removeSetSignGrandChild = grandChild->getOpCodeValue() == TR::pdSetSign;
if (convertedSetSign != TR::DataType::getInvalidSignCode() &&
performTransformation(s->comp(), "%sFold%s %s [" POINTER_PRINTF_FORMAT "] above parent pd2zd [" POINTER_PRINTF_FORMAT "] and into grandparent %s [" POINTER_PRINTF_FORMAT "] and create new ",
s->optDetailString(),removeSetSignGrandChild?" and remove":"",child->getFirstChild()->getOpCode().getName(),child->getFirstChild(),child,node->getOpCode().getName(),node))
{
int32_t newSetSignindex = TR::ILOpCode::getSetSignValueIndex(newSetSignOp);
TR::Node *newNode = NULL;
TR::Node *newSetSign = TR::Node::iconst(node, convertedSetSign);
if (newSetSignindex == 1)
{
// Transform:
// zd2zdsls
// pd2zd
// pdshlSetSign - src
// x
// shift
// sign
// to:
// zd2zdslsSetSign
// pd2zd
// pdshlSetSign - src
// x
// shift
// newSign 0xf
// sign
newNode = TR::Node::create(newSetSignOp, 2,
child, // pd2zd
newSetSign);
child->decReferenceCount();
}
else
{
TR_ASSERT(false,"setSign index of %d not supported in foldSetSignFromGrandChild\n",newSetSignindex);
}
if (newNode)
{
dumpOptDetails(s->comp(), "%s node [" POINTER_PRINTF_FORMAT "]\n", newNode->getOpCode().getName(),newNode);
newNode->incReferenceCount();
newNode->setDecimalPrecision(node->getDecimalPrecision());
stopUsingSingleNode(node, true, s); // removePadding=true
child->setVisitCount(0); // revisit this node
grandChildSetSign->recursivelyDecReferenceCount();
if (removeSetSignGrandChild)
{
TR_ASSERT(grandChild->getNumChildren() == 2,"can only remove setSign grandChild node %p with two children and not %d children\n",grandChild,grandChild->getNumChildren());
child->setChild(0, grandChild->getFirstChild());
stopUsingSingleNode(grandChild, true, s); // removePadding=true
}
else
{
TR::Node *modifiedSetSign = TR::Node::iconst(grandChild, TR::DataType::getIgnoredSignCode());
grandChild->setAndIncChild(grandChildSetSignIndex, modifiedSetSign);
grandChild->resetSignState();
}
return newNode;
}
}
}
return node;
}
static TR::Node *removeShiftTruncationForConversionParent(TR::Node *conversion, TR::Block * block, TR::Simplifier * s)
{
//if (!conversion->alwaysGeneratesAKnownCleanSign()) return child; // Allow for pd2udsx nodes too even though they could generate a negative unicode zero?
// Look for a truncating child or grandchild shift under a pd2x. The truncation will likely be cheaper to perform as part of
// the pd2x so increase the precision of the shift (and an intermediate clean if present) so it no longer truncates.
TR::Node *child = conversion->getFirstChild();
if (child->getReferenceCount() == 1)
{
TR::Node *pdclean = NULL;
TR::Node *shift = NULL;
if (child->getOpCodeValue() == TR::pdclean &&
child->getFirstChild()->getReferenceCount() == 1 &&
child->getFirstChild()->getOpCode().isPackedShift())
{
// conversion
// pdclean
// pdshx
pdclean = child;
shift = child->getFirstChild();
}
else if (child->getOpCode().isPackedShift())
{
// conversion
// pdshx
shift = child;
}
if (shift &&
conversion->getDecimalPrecision() == shift->getDecimalPrecision() && // final precision matches truncated pdshl precision
(!pdclean || pdclean->getDecimalPrecision() >= shift->getDecimalPrecision())) // and there is no intermediate truncation by the pdclean (if there is a pdclean)
{
TR::Node *shiftSource = shift->getFirstChild();
int32_t shiftedPrecision = shiftSource->getDecimalPrecision() + shift->getDecimalAdjust();
if (shiftedPrecision <= TR::DataType::getMaxPackedDecimalPrecision() && // do not create large pdcleans or pdshrs that may not be supported by the code generator
shiftedPrecision > conversion->getDecimalPrecision() &&
performTransformation(s->comp(), "%sDelaying truncation until %s [" POINTER_PRINTF_FORMAT "] by increasing %s [" POINTER_PRINTF_FORMAT "] precision %d->%d",
s->optDetailString(), conversion->getOpCode().getName(), conversion, shift->getOpCode().getName(), shift, shift->getDecimalPrecision(), shiftedPrecision))
{
if (pdclean)
{
dumpOptDetails(s->comp(), " and intermediate pdclean [" POINTER_PRINTF_FORMAT "] precision %d->%d", pdclean,pdclean->getDecimalPrecision(),shiftedPrecision);
pdclean->setDecimalPrecision(shiftedPrecision);
pdclean->setVisitCount(0);
}
dumpOptDetails(s->comp(),"\n");
shift->setDecimalPrecision(shiftedPrecision);
shift->setVisitCount(0);
child->setVisitCount(0);
return s->simplify(child, block); // try to propagate the shrunken precision down the tree
}
}
}
return child;
}
static TR::Node *flipBinaryChildAndUnaryParent(TR::Node *parent, TR::Node *child, TR::ILOpCodes newParentOp, TR::Block *block, TR::Simplifier * s)
{
// Flip the parent/child order and change the new parent's (the old child's) opcode to newParentOp. Return the new parent node.
// Transform:
// parent
// child
// x
// y
// To:
// newParent (with newParentOp opcodeValue)
// parent
// x
// y
TR_ASSERT(parent->getNumChildren() == 1 || parent->getNumChildren() == 2,"parent node %p must have one or two children and not %d children\n",parent,parent->getNumChildren());
TR_ASSERT(child->getNumChildren() == 2,"child node must have exactly two children and not %d child(ren)\n",child->getNumChildren());
int32_t parentPrec = TR::DataType::getInvalidDecimalPrecision();
if (parent->getType().isBCD())
parentPrec = parent->getDecimalPrecision();
TR::Node *newChildren[2];
newChildren[0] = TR::Node::create(parent->getOpCodeValue(), 1, child->getFirstChild());
newChildren[1] = child->getSecondChild();
child->incReferenceCount(); // Otherwise prepareToReplaceNode may wrongly dec refcounts of child's children
s->prepareToReplaceNode(parent, newParentOp);
parent->addChildren(newChildren, 2);
if (parent->getType().isBCD())
{
TR_ASSERT(parentPrec != TR::DataType::getInvalidDecimalPrecision(),"expecting original parent node to be a BCD type\n"); // otherwise how do we get the precision value?
parent->setDecimalPrecision(parentPrec);
if (child->getType().isBCD())
parent->getFirstChild()->setDecimalPrecision(parentPrec);
}
parent->setVisitCount(0); // revisit this node
// the parent may not handle truncation so insert a modifyPrecision op if needed
TR::Node *newChild = child->getFirstChild();
if (parent->getDecimalPrecision() < newChild->getDecimalPrecision())
{
// zdSetSign - newParent
// pd2zd p=3 - parent
// pdX p=5 - origNewChild=newChild
// sign
// To:
// zdSetSign - newParent
// pd2zd p=3 - parent
// pdModifyPrecision p=3 - newChild = new mod prec child
// pdX p=5 - origNewChild
// sign
TR::Node *origNewChild = newChild;
TR::ILOpCodes modifyPrecisionOp = TR::ILOpCode::modifyPrecisionOpCode(origNewChild->getDataType());
TR_ASSERT(modifyPrecisionOp != TR::BadILOp,"no bcd modify precision opcode found\n");
newChild = TR::Node::create(origNewChild, modifyPrecisionOp, 1);
newChild->setChild(0, origNewChild);
newChild->setDecimalPrecision(parent->getDecimalPrecision());
newChild->setReferenceCount(1);
dumpOptDetails(s->comp(),"%sparent %s [" POINTER_PRINTF_FORMAT "] and newChild %s [" POINTER_PRINTF_FORMAT "] precision mismatch (%d < %d) so create a new %s [" POINTER_PRINTF_FORMAT "]\n",
s->optDetailString(),parent->getOpCode().getName(),parent,origNewChild->getOpCode().getName(),origNewChild,parent->getDecimalPrecision(),origNewChild->getDecimalPrecision(),newChild->getOpCode().getName(),newChild);
parent->getFirstChild()->setChild(0, newChild);
}
if (child->getReferenceCount() == 1)
{
stopUsingSingleNode(child, true, s); // removePadding=true
child->getFirstChild()->decReferenceCount();
child->getSecondChild()->decReferenceCount();
}
else
{
child->decReferenceCount();
}
parent->setChild(0, s->simplify(parent->getFirstChild(), block));
return s->simplify(parent, block);
}
static TR::Node *flipCleanAndShift(TR::Node *node, TR::Block * block, TR::Simplifier * s)
{
if ((node->getOpCodeValue() == TR::pdclean) &&
(node->getFirstChild()->getOpCodeValue() == TR::pdshl) &&
(!node->getFirstChild()->getFirstChild()->getOpCode().isConversion()) && // a conversion will likely have to initialize on its own so no benefit to trying to get the clean to do it
(node->getFirstChild()->getSecondChild()->getOpCode().isLoadConst()))
{
TR::Node *pdclean = node;
TR::Node *pdshl = node->getFirstChild();
int32_t shiftAmount = pdshl->getSecondChild()->get32bitIntegralValue();
int32_t maxShiftedPrecision = shiftAmount+pdshl->getFirstChild()->getDecimalPrecision();
if (((shiftAmount&0x1) == 0) && (pdshl->getDecimalPrecision() >= maxShiftedPrecision) &&
performTransformation(s->comp(), "%sCreate a new parent pdshl on pdclean [" POINTER_PRINTF_FORMAT "] and remove grandchild pdshl [" POINTER_PRINTF_FORMAT "]\n",
s->optDetailString(), pdclean, pdshl))
{
// A pdclean can likely initialize and clean in one step so move it lower in the tree to increase the chances that it will perform the initialization
// Note that this transformation is not done in the pdcleanSimplifier as we do not want to move the pdclean lower when the parent is pdstore because in this
// case the clean and store can be done in one step.
// Transform:
// pd2zd
// pdclean
// pdshl
// x
// shift
// To:
// pd2zd
// pdshl
// pdclean
// x
// shift
return flipBinaryChildAndUnaryParent(pdclean, pdshl, pdshl->getOpCodeValue(), block, s);
}
}
return node;
}
TR::Node *zdloadSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
s->simplifyChildren(node, block);
return node;
}
TR::Node *zdsleStoreSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
s->simplifyChildren(node, block);
// It is not good to remove all widenings for zdsle children because then this store node will have to move the sign code from the
// the narrowed child and clear (0xF0) the intermediate bytes.
// It is better to just let the child do the widening so the sign code does not have to be moved.
//
// TR::Node *valueChild = node->setValueChild(removeOperandWidening(node->getValueChild(), node, block, s));
TR::Node *valueChild = node->getValueChild();
if (valueChild->isSimpleWidening())
{
valueChild = node->setValueChild(s->replaceNodeWithChild(valueChild, valueChild->getFirstChild(), s->_curTree, block, false)); // correctBCDPrecision=false because node is a widening
}
return node;
}
// Handles izdsts, zdsts
TR::Node *zdstsStoreSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
s->simplifyChildren(node, block);
TR::Node *valueChild = node->setValueChild(removeOperandWidening(node->getValueChild(), node, block, s));
return node;
}
//---------------------------------------------------------------------
// Zoned decimal to zoned decimal sign leading embedded
//
TR::Node *zd2zdsleSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
s->simplifyChildren(node, block);
node->setChild(0,removeOperandWidening(node->getFirstChild(), node, block, s));
propagateSignStateUnaryConversion(node, block, s);
TR::Node *child = node->getFirstChild();
if (child->getOpCode().isSetSign())
{
TR::Node *newNode = foldSetSignIntoNode(child, true /* setSignIsTheChild */, node, true /* removeSetSign */, block, s);
if (newNode != node)
return newNode;
}
child = node->getFirstChild();
TR::Node * result = NULL;
if ((node->getDecimalPrecision() >= child->getDecimalPrecision()) &&
(result = s->unaryCancelOutWithChild(node, child, s->_curTree, TR::zdsle2zd)))
{
return result;
}
return node;
}
TR::Node *pd2udSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
s->simplifyChildren(node, block);
node->setChild(0, removeShiftTruncationForConversionParent(node, block, s));
TR::Node *child = node->getFirstChild();
if (node->getDecimalPrecision() == child->getDecimalPrecision() &&
child->isSimpleTruncation() &&
performTransformation(s->comp(), "%sRemove simple truncating %s [" POINTER_PRINTF_FORMAT "] under pd2ud node %s [" POINTER_PRINTF_FORMAT "]\n",
s->optDetailString(), child->getOpCode().getName(), child, node->getOpCode().getName(),node))
{
child = node->setChild(0, s->replaceNodeWithChild(child, child->getFirstChild(), s->_curTree, block, false)); // correctBCDPrecision=false because nodePrec==childPrec
}
TR::Node * result = s->unaryCancelOutWithChild(node, node->getFirstChild(), s->_curTree, TR::ud2pd);
if (result)
return result;
child = node->setChild(0,removeOperandWidening(child, node, block, s));
if (node->getFirstChild()->getOpCodeValue() == TR::pdclean || node->getFirstChild()->getOpCodeValue() == TR::pdSetSign)
{
node->setChild(0,s->replaceNodeWithChild(node->getFirstChild(), node->getFirstChild()->getFirstChild(), s->_curTree, block));
return s->simplify(node, block);
}
child = node->getFirstChild();
if (child->getReferenceCount() == 1 &&
(child->getOpCode().isSetSign() || TR::ILOpCode::setSignVersionOfOpCode(child->getOpCodeValue()) != TR::BadILOp))
{
TR::Node *newSetSignValue = TR::Node::iconst(child, TR::DataType::getIgnoredSignCode());
if (child->getOpCode().isSetSign())
{
int32_t childSetSignIndex = TR::ILOpCode::getSetSignValueIndex(child->getOpCodeValue());
TR::Node *oldSetSignValue = child->getChild(childSetSignIndex);
if (oldSetSignValue->getOpCode().isLoadConst() &&
performTransformation(s->comp(), "%sReplace %s [" POINTER_PRINTF_FORMAT "] dominated %s [" POINTER_PRINTF_FORMAT "] with setSignValueNode [" POINTER_PRINTF_FORMAT "] and value (0x%x) with new ",
s->optDetailString(),node->getOpCode().getName(),node,child->getOpCode().getName(),child,oldSetSignValue,oldSetSignValue->get32bitIntegralValue()))
{
newSetSignValue->incReferenceCount();
oldSetSignValue->recursivelyDecReferenceCount();
child->setChild(childSetSignIndex, newSetSignValue);
child->resetSignState();
dumpOptDetails(s->comp(), "setSignValueNode [" POINTER_PRINTF_FORMAT "] and ignored value (%d)\n", newSetSignValue, TR::DataType::getIgnoredSignCode());
}
}
else if (performTransformation(s->comp(), "%sReplace %s [" POINTER_PRINTF_FORMAT "] dominated %s [" POINTER_PRINTF_FORMAT "] with new ",
s->optDetailString(),node->getOpCode().getName(),node,child->getOpCode().getName(),child))
{
TR::Node *newNode = NULL;
TR::ILOpCodes setSignOp = TR::ILOpCode::setSignVersionOfOpCode(child->getOpCodeValue());
int32_t newSetSignIndex = TR::ILOpCode::getSetSignValueIndex(setSignOp);
if (newSetSignIndex == 1) // child 2
{
if (child->getOpCode().isConversion() &&
child->getFirstChild()->getType().isUnicodeSeparateSign() &&
child->getSecondChild()->getDataType() == TR::Address)
{
// Transform:
//
// pd2ud - node
// udsx2pd - child
// x
// signAddrForCompareAndSet (could be '-' or '+')
//
// to:
//
// p2dud
// udsx2pdSetSign - newNode
// x
// iconst TR::DataType::getIgnoredSignCode()
//
child->getSecondChild()->recursivelyDecReferenceCount();
newNode = TR::Node::create(setSignOp, 2,
child->getFirstChild(),
newSetSignValue);
}
else if (child->getNumChildren() == 1)
{
// Transform:
//
// pd2ud - node
// zdsts2pd - child
// x
//
// to:
//
// pd2ud
// zdsts2pdSetSign - newNode
// x
// iconst TR::DataType::getIgnoredSignCode()
//
newNode = TR::Node::create(setSignOp, 2,
child->getFirstChild(),
newSetSignValue);
}
else
{
TR_ASSERT(false,"unexpected parent/child combination in pd2udSimplifier for newSetSignIndex == 1\n");
}
}
else if (newSetSignIndex == 2) // child 3
{
// Transform:
//
// pd2ud - node
// pdshl - child
// x
// iconst shift
// to:
//
// pd2ud - node
// pdshlSetSign - newNode
// x
// iconst shift
// iconst TR::DataType::getIgnoredSignCode()
//
TR_ASSERT(child->getNumChildren() == 2,"expecting two children on the child node (%p) and not %d children\n",child,child->getNumChildren());
newNode = TR::Node::create(setSignOp, 3,
child->getFirstChild(),
child->getSecondChild(), // shiftAmount
newSetSignValue);
}
else if (newSetSignIndex == 3) // child 4
{
// Transform:
//
// pd2ud - node
// pdshr - child
// x
// iconst shift
// iconst round=0
// to:
//
// pd2ud - node
// pdshrSetSign - newNode
// x
// iconst shift
// iconst round=0
// iconst TR::DataType::getIgnoredSignCode()
//
TR_ASSERT(child->getNumChildren() == 3,"expecting three children on the child node (%p) and not %d children\n",child,child->getNumChildren());
newNode = TR::Node::create(setSignOp, 4,
child->getFirstChild(),
child->getSecondChild(), // shiftAmount
child->getThirdChild(), // roundAmount
newSetSignValue);
}
else
{
TR_ASSERT(false,"unexpected newSetSignIndex of %d\n",newSetSignIndex);
}
if (newNode)
{
//printf("changing %p pdshx %p to new setsign %p\n",node,child,newNode);
dumpOptDetails(s->comp(), " %s [" POINTER_PRINTF_FORMAT "] to ignored value (%d) [" POINTER_PRINTF_FORMAT "]\n", newNode->getOpCode().getName(),newNode,TR::DataType::getIgnoredSignCode());
newNode->incReferenceCount();
newNode->setDecimalPrecision(child->getDecimalPrecision());
// All the children up to but not including the setSignValue on newNode were existing nodes and were therefore incremented one extra time each when creating newNode above
for (int32_t i=0; i < newNode->getNumChildren()-1; i++)
newNode->getChild(i)->decReferenceCount();
stopUsingSingleNode(child, true, s); // removePadding=true
node->setChild(0, s->simplify(newNode,block));
child = newNode;
}
}
}
return node;
}
TR::Node *ud2pdSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
s->simplifyChildren(node, block);
if (!node->hasKnownOrAssumedSignCode())
{
TR_RawBCDSignCode sign = s->comp()->cg()->alwaysGeneratedSign(node);
if (sign != raw_bcd_sign_unknown &&
performTransformation(s->comp(), "%sSetting known sign 0x%x on %s [" POINTER_PRINTF_FORMAT "] from alwaysGeneratedSign\n",
s->optDetailString(),TR::DataType::getValue(sign),node->getOpCode().getName(),node))
{
node->setKnownSignCode(sign);
}
}
TR::Node * result = s->unaryCancelOutWithChild(node, node->getFirstChild(), s->_curTree, TR::pd2ud);
if (result)
return result;
return node;
}
// Handles udst2pd, udsl2pd
TR::Node *udsx2pdSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
s->simplifyChildren(node, block);
propagateSignStateUnaryConversion(node, block, s);
TR::DataType sourceDataType = TR::NoType;
TR::DataType targetDataType = TR::NoType;
if (decodeConversionOpcode(node->getOpCode(), node->getDataType(), sourceDataType, targetDataType))
{
TR::ILOpCodes inverseOp = TR::ILOpCode::getProperConversion(targetDataType, sourceDataType, false /* !wantZeroExtension */); // inverse conversion to what we are given
TR::Node * result = s->unaryCancelOutWithChild(node, node->getFirstChild(), s->_curTree, inverseOp);
if (result)
return result;
}
return node;
}
// Also handles pd2udst
TR::Node *pd2udslSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
s->simplifyChildren(node, block);
node->setChild(0,removeShiftTruncationForConversionParent(node, block, s));
node->setChild(0,removeOperandWidening(node->getFirstChild(), node, block, s));
if (node->getFirstChild()->getOpCodeValue() == TR::pdSetSign)
{
TR::Node *newNode = foldSetSignIntoNode(node->getFirstChild(), true /* setSignIsTheChild */, node, true /* removeSetSign */, block, s);
if (newNode != node)
return newNode;
}
if (node->getFirstChild()->getOpCode().isSetSign())
{
TR::Node *newNode = foldAndReplaceDominatedSetSign(node->getFirstChild(), true /* setSignIsTheChild */, node, block, s);
if (newNode != node)
return newNode;
}
TR::Node *newNode = createSetSignForKnownSignChild(node, block, s);
if (newNode != node)
return newNode;
newNode = foldSetSignFromGrandChild(node, block, s);
if (newNode != node)
return newNode;
return node;
}
TR::Node *zdsle2pdSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
s->simplifyChildren(node, block);
node->setChild(0,removeShiftTruncationForConversionParent(node, block, s));
node->setChild(0,removeOperandWidening(node->getFirstChild(), node, block, s));
return node;
}
// Also handles udst2ud
TR::Node *udsl2udSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
s->simplifyChildren(node, block);
node->setChild(0,removeShiftTruncationForConversionParent(node, block, s));
node->setChild(0,removeOperandWidening(node->getFirstChild(), node, block, s));
return node;
}
// Also handles zdsts2zd
TR::Node *zdsls2zdSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
s->simplifyChildren(node, block);
node->setChild(0,removeShiftTruncationForConversionParent(node, block, s));
node->setChild(0,removeOperandWidening(node->getFirstChild(), node, block, s));
return node;
}
//---------------------------------------------------------------------
// Packed decimal to zoned decimal sign leading separate
//
// also handles pd2zdsts
TR::Node *pd2zdslsSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
TR_ASSERT(node->getOpCodeValue() == TR::pd2zdsls || node->getOpCodeValue() == TR::pd2zdsts,"pd2zdslsSimplifier only valid for TR::pd2zdsls/TR::pd2zdsts nodes\n");
s->simplifyChildren(node, block);
propagateSignStateUnaryConversion(node, block, s);
TR::Node *child = node->setChild(0,removeOperandWidening(node->getFirstChild(), node, block, s));
TR::DataType sourceDataType = TR::NoType;
TR::DataType targetDataType = TR::NoType;
if (decodeConversionOpcode(node->getOpCode(), node->getDataType(), sourceDataType, targetDataType))
{
TR::ILOpCodes inverseOp = TR::ILOpCode::getProperConversion(targetDataType, sourceDataType, false /* !wantZeroExtension */); // inverse conversion to what we are given
TR::Node * result = NULL;
if ((node->getDecimalPrecision() >= child->getDecimalPrecision()) &&
(result = s->unaryCancelOutWithChild(node, child, s->_curTree, inverseOp)))
return result;
}
if (node->getFirstChild()->getOpCodeValue() == TR::pdSetSign)
{
TR::Node *newNode = foldSetSignIntoNode(node->getFirstChild(), true /* setSignIsTheChild */, node, true /* removeSetSign */, block, s);
if (newNode != node)
return newNode;
}
if (node->getFirstChild()->getOpCode().isSetSign())
{
TR::Node *newNode = foldAndReplaceDominatedSetSign(node->getFirstChild(), true /* setSignIsTheChild */, node, block, s);
if (newNode != node)
return newNode;
}
if (node->getFirstChild()->getOpCodeValue() == TR::zd2pd)
{
TR::Node *grandChild = s->unaryCancelOutWithChild(node, node->getFirstChild(), s->_curTree, TR::zd2pd); // unaryCancelOutWithChild takes care of the details of removing the pd2zdsls/zd2pd
if (grandChild)
{
TR::Node *newNode = TR::Node::create(node->getDataType() == TR::ZonedDecimalSignTrailingSeparate ? TR::zd2zdsts : TR::zd2zdsls, 1, grandChild);
grandChild->decReferenceCount();
newNode->incReferenceCount();
newNode->setDecimalPrecision(node->getDecimalPrecision());
dumpOptDetails(s->comp(), "%screated new %s [" POINTER_PRINTF_FORMAT "]\n", s->optDetailString(), newNode->getOpCode().getName(), newNode);
return newNode;
}
}
child = node->setChild(0, flipCleanAndShift(node->getFirstChild(), block, s));
child = node->setChild(0,removeOperandWidening(node->getFirstChild(), node, block, s));
return node;
}
// Also handles pd2zdstsSetSign
TR::Node *pd2zdslsSetSignSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
return node;
}
//---------------------------------------------------------------------
// Zoned decimal to zoned decimal sign leading separate
//
// Also handles zd2zdsts
TR::Node *zd2zdslsSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
TR_ASSERT(node->getOpCodeValue() == TR::zd2zdsls || node->getOpCodeValue() == TR::zd2zdsts,"zd2zdslsSimplifier only valid for TR::zd2zdsls/TR::zd2zdsts nodes\n");
s->simplifyChildren(node, block);
propagateSignStateUnaryConversion(node, block, s);
TR::Node *child = node->getFirstChild();
// do not remove widening on the leading sign case to avoid having to set the sign first on the
// narrower zd2zdsls conversion and then possibly move it again on a later widening.
if (node->getType().isTrailingSeparateSign())
child = node->setChild(0,removeOperandWidening(child, node, block, s));
TR::Node *newNode = foldSetSignFromGrandChild(node, block, s);
if (newNode != node)
return newNode;
TR::DataType sourceDataType = TR::NoType;
TR::DataType targetDataType = TR::NoType;
if (decodeConversionOpcode(node->getOpCode(), node->getDataType(), sourceDataType, targetDataType))
{
TR::ILOpCodes inverseOp = TR::ILOpCode::getProperConversion(targetDataType, sourceDataType, false /* !wantZeroExtension */); // inverse conversion to what we are given
TR::Node * result = NULL;
if ((node->getDecimalPrecision() == child->getDecimalPrecision()) &&
(result = s->unaryCancelOutWithChild(node, child, s->_curTree, inverseOp)))
return result;
}
return node;
}
//---------------------------------------------------------------------
// Zoned decimal sign leading separate to packed decimal
//
// Also handles zdsts2pd
TR::Node *zdsls2pdSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
s->simplifyChildren(node, block);
propagateSignStateUnaryConversion(node, block, s);
return node;
}
//---------------------------------------------------------------------
// Zoned decimal sign leading embedded to zoned decimal
//
TR::Node *zdsle2zdSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
s->simplifyChildren(node, block);
propagateSignStateUnaryConversion(node, block, s);
TR::Node *child = node->getFirstChild();
TR::Node * result = NULL;
if ((node->getDecimalPrecision() == child->getDecimalPrecision()) &&
(result = s->unaryCancelOutWithChild(node, child, s->_curTree, TR::zd2zdsle)))
return result;
return node;
}
//---------------------------------------------------------------------
// Zoned decimal to packed decimal
//
TR::Node *zd2pdSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
s->simplifyChildren(node, block);
TR::Node * firstChild = node->getFirstChild();
propagateSignStateUnaryConversion(node, block, s);
TR::Node * result = s->unaryCancelOutWithChild(node, firstChild, s->_curTree, TR::pd2zd);
if (result)
return result;
if (firstChild->getOpCodeValue() == TR::zdsle2zd &&
firstChild->getReferenceCount() == 1 &&
node->getDecimalPrecision() < firstChild->getDecimalPrecision() &&
performTransformation(s->comp(), "%sReduce zdsle2zd child [" POINTER_PRINTF_FORMAT "] precision to %d due to truncating zd2pd [" POINTER_PRINTF_FORMAT "]\n",s->optDetailString(),firstChild,node->getDecimalPrecision(),node))
{
firstChild->setDecimalPrecision(node->getDecimalPrecision());
}
firstChild = node->setChild(0,removeOperandWidening(node->getFirstChild(), node, block, s));
return node;
}
TR::Node *pd2zdSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
s->simplifyChildren(node, block);
TR::Node * firstChild = node->getFirstChild();
if (node->getDecimalPrecision() == firstChild->getDecimalPrecision() &&
firstChild->isSimpleTruncation() &&
performTransformation(s->comp(), "%sRemove simple truncating %s [" POINTER_PRINTF_FORMAT "] under pd2zd node %s [" POINTER_PRINTF_FORMAT "]\n",
s->optDetailString(), firstChild->getOpCode().getName(), firstChild, node->getOpCode().getName(),node))
{
firstChild = node->setChild(0, s->replaceNodeWithChild(firstChild, firstChild->getFirstChild(), s->_curTree, block, false)); // correctBCDPrecision=false because nodePrec==childPrec
}
propagateSignStateUnaryConversion(node, block, s);
TR::Node * result = s->unaryCancelOutWithChild(node, firstChild, s->_curTree, TR::zd2pd);
if (result)
return result;
TR::Node *child = node->setChild(0, flipCleanAndShift(node->getFirstChild(), block, s));
if ((child->getReferenceCount() == 1 && node->getReferenceCount() == 1) &&
(child->getOpCodeValue() == TR::zdsle2pd ||
child->getOpCodeValue() == TR::zdsls2pd ||
child->getOpCodeValue() == TR::zdsts2pd))
{
if ((node->getDecimalPrecision() == child->getDecimalPrecision()) &&
performTransformation(s->comp(), "%sFold %s [" POINTER_PRINTF_FORMAT "] into child %s [" POINTER_PRINTF_FORMAT "] and create new\n",
s->optDetailString(), node->getOpCode().getName(), node, child->getOpCode().getName(), child))
{
// Transform:
// pd2zd
// zdsls2pdSetSign
// x
// sign
// To:
// zdsls2zdSetSign
// x
// sign
bool childIsSetSign = child->getOpCode().isSetSign();
TR::ILOpCodes childOpValueForLookup = childIsSetSign ? TR::ILOpCode::reverseSetSignOpCode(child->getOpCodeValue()) : child->getOpCodeValue();
TR::ILOpCode childOpForLookup;
childOpForLookup.setOpCodeValue(childOpValueForLookup);
TR::DataType sourceDataType = TR::NoType;
TR::DataType targetDataType = TR::NoType;
if (childOpValueForLookup != TR::BadILOp && decodeConversionOpcode(childOpForLookup, child->getDataType(), sourceDataType, targetDataType))
{
TR::ILOpCodes newConvOp = TR::ILOpCode::getProperConversion(sourceDataType, TR::ZonedDecimal, false /* !wantZeroExtension */);
TR::Node *newNode = NULL;
if (newConvOp != TR::BadILOp)
{
TR::ILOpCodes newSetSignOp = TR::ILOpCode::setSignVersionOfOpCode(newConvOp);
if (childIsSetSign)
{
if (newSetSignOp != TR::BadILOp)
{
newNode = TR::Node::create(newSetSignOp, 2,
child->getFirstChild(),
child->getSecondChild());
child->getSecondChild()->decReferenceCount();
}
}
else
{
newNode = TR::Node::create(newConvOp, 1, child->getFirstChild());
}
}
if (newNode)
{
child->getFirstChild()->decReferenceCount();
dumpOptDetails(s->comp(), "%s [" POINTER_PRINTF_FORMAT "]\n", newNode->getOpCode().getName(), newNode);
newNode->incReferenceCount();
newNode->setDecimalPrecision(node->getDecimalPrecision());
stopUsingSingleNode(node, true, s); // removePadding=true
stopUsingSingleNode(child, true, s); // removePadding=true
return newNode;
}
}
else
{
TR_ASSERT(false,"could not find conversion in pd2zdSimplifier\n");
}
}
}
child = node->setChild(0,removeOperandWidening(node->getFirstChild(), node, block, s));
return node;
}
static TR::Node *reduceShiftRightOverShiftLeft(TR::Node * node, TR::Block * block, TR::Simplifier * s)
{
if (node->getOpCode().isPackedRightShift() &&
node->getFirstChild()->getOpCode().isPackedLeftShift() &&
!node->hasIntermediateTruncation())
{
TR::Node *child = node->getFirstChild();
// overflow not supported
if (child->getOpCodeValue() == TR::pdshlOverflow)
return node;
int32_t parentAdjust = node->getDecimalAdjust();
int32_t childAdjust = child->getDecimalAdjust();
int32_t combinedAdjust = parentAdjust + childAdjust;
if (performTransformation(s->comp(), "%sFold rightShift-over-leftShift : %s by %d [0x%p] over %s by %d [0x%p] by setting parent adjust to %d and removing child\n",
s->optDetailString(),node->getOpCode().getName(),parentAdjust,node,
child->getOpCode().getName(),childAdjust,child,combinedAdjust))
{
bool parentIsSetSign = node->getOpCode().isSetSign();
bool childIsSetSign = child->getOpCode().isSetSign();
bool resultIsSetSign = parentIsSetSign || childIsSetSign;
bool hasAdjust = combinedAdjust != 0;
TR::Node *setSignNodeToUse = NULL;
TR::Node *setSignValueNode = NULL;
int32_t setSignValueIndex = -1;
TR::ILOpCodes newOp = TR::BadILOp;
uint32_t numChildren = 0;
if (!hasAdjust)
{
newOp = resultIsSetSign ?
TR::ILOpCode::setSignOpCode(node->getDataType()) :
TR::ILOpCode::modifyPrecisionOpCode(node->getDataType());
numChildren = resultIsSetSign ? 2 : 1;
}
else if (combinedAdjust < 0) // right shift
{
newOp = resultIsSetSign ? TR::pdshrSetSign : TR::pdshr;
numChildren = resultIsSetSign ? 4 : 3;
}
else // combinedAdjust > 0 --> left shift
{
newOp = resultIsSetSign ? TR::pdshlSetSign : TR::pdshl;
numChildren = resultIsSetSign ? 3 : 2;
}
TR_ASSERT(newOp != TR::BadILOp, "Folding result operation not found");
bool shouldSkipReplacingSignChild = false;
if (resultIsSetSign)
{
setSignNodeToUse = parentIsSetSign ? node : child;
setSignValueNode = setSignNodeToUse->getSetSignValueNode();
setSignValueIndex = TR::ILOpCode::getSetSignValueIndex(newOp);
shouldSkipReplacingSignChild = setSignValueIndex >= node->getNumChildren();
}