-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathControlFlowEvaluator.cpp
4852 lines (4298 loc) · 187 KB
/
ControlFlowEvaluator.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, 2020 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 <stddef.h>
#include <stdint.h>
#include "codegen/AheadOfTimeCompile.hpp"
#include "codegen/CodeGenerator.hpp"
#include "codegen/CodeGeneratorUtils.hpp"
#include "env/FrontEnd.hpp"
#include "codegen/InstOpCode.hpp"
#include "codegen/Instruction.hpp"
#include "codegen/Linkage.hpp"
#include "codegen/Linkage_inlines.hpp"
#include "codegen/Machine.hpp"
#include "codegen/MemoryReference.hpp"
#include "codegen/RealRegister.hpp"
#include "codegen/Register.hpp"
#include "codegen/RegisterConstants.hpp"
#include "codegen/RegisterDependency.hpp"
#include "codegen/RegisterDependencyStruct.hpp"
#include "codegen/RegisterPair.hpp"
#include "codegen/Snippet.hpp"
#include "codegen/TreeEvaluator.hpp"
#include "compile/Compilation.hpp"
#include "compile/SymbolReferenceTable.hpp"
#include "compile/VirtualGuard.hpp"
#include "control/Options.hpp"
#include "control/Options_inlines.hpp"
#include "env/CompilerEnv.hpp"
#ifdef J9_PROJECT_SPECIFIC
#include "env/CHTable.hpp"
#endif
#include "env/ObjectModel.hpp"
#include "env/Processors.hpp"
#include "env/TRMemory.hpp"
#include "env/jittypes.h"
#include "il/Block.hpp"
#include "il/DataTypes.hpp"
#include "il/ILOpCodes.hpp"
#include "il/ILOps.hpp"
#include "il/LabelSymbol.hpp"
#include "il/Node.hpp"
#include "il/Node_inlines.hpp"
#include "il/SymbolReference.hpp"
#include "il/TreeTop.hpp"
#include "il/TreeTop_inlines.hpp"
#include "infra/Assert.hpp"
#include "infra/BitVector.hpp"
#include "infra/List.hpp"
#include "p/codegen/GenerateInstructions.hpp"
#include "p/codegen/PPCAOTRelocation.hpp"
#include "p/codegen/PPCHelperCallSnippet.hpp"
#include "p/codegen/PPCInstruction.hpp"
#include "p/codegen/PPCOpsDefines.hpp"
#include "p/codegen/PPCOutOfLineCodeSection.hpp"
#include "p/codegen/PPCTableOfConstants.hpp"
#include "runtime/Runtime.hpp"
static bool virtualGuardHelper(TR::Node *node, TR::CodeGenerator *cg);
static void switchDispatch(TR::Node *node, bool fromTableEval, TR::CodeGenerator *cg);
static bool isGlDepsUnBalanced(TR::Node *node, TR::CodeGenerator *cg);
static void lookupScheme1(TR::Node *node, bool unbalanced, bool fromTableEval, TR::CodeGenerator *cg);
static void lookupScheme2(TR::Node *node, bool unbalanced, bool fromTableEval, TR::CodeGenerator *cg);
static void lookupScheme3(TR::Node *node, bool unbalanced, TR::CodeGenerator *cg);
static void lookupScheme4(TR::Node *node, TR::CodeGenerator *cg);
/**
* @brief Represents an integer comparison condition.
*/
enum class CompareCondition
{
eq,
ne,
lt,
ge,
gt,
le
};
/**
* \brief
* Represents a condition as a (potentially reversed) bit in a condition register field.
*/
struct CRCompareCondition
{
TR::RealRegister::CRCC crcc; //< Which bit in the CR field the result will be placed in.
bool isReversed; //< Whether the bit in the CR field is the complement of the comparison result.
CRCompareCondition(TR::RealRegister::CRCC crcc, bool isReversed) : crcc(crcc), isReversed(isReversed) {}
};
/**
* \brief
* Gets the placement of a condition in a CR field after a compare instruction is run.
*
* \param cond
* The condition which should be checked for.
*
* \return
* The placement of the provided condition in a CR field.
*/
CRCompareCondition compareConditionInCR(CompareCondition cond)
{
switch (cond)
{
case CompareCondition::eq:
return CRCompareCondition(TR::RealRegister::CRCC_EQ, false);
case CompareCondition::ne:
return CRCompareCondition(TR::RealRegister::CRCC_EQ, true);
case CompareCondition::lt:
return CRCompareCondition(TR::RealRegister::CRCC_LT, false);
case CompareCondition::ge:
return CRCompareCondition(TR::RealRegister::CRCC_LT, true);
case CompareCondition::gt:
return CRCompareCondition(TR::RealRegister::CRCC_GT, false);
case CompareCondition::le:
return CRCompareCondition(TR::RealRegister::CRCC_GT, true);
default:
TR_ASSERT_FATAL(false, "Invalid CompareCondition %d", static_cast<int>(cond));
}
}
/**
* \brief
* Returns a condition representing the logical complement of the provided condition.
*
* \param cond
* The condition to be reversed.
*
* \return
* The logical complement of cond.
*/
CompareCondition reverseCondition(CompareCondition cond)
{
switch (cond)
{
case CompareCondition::eq:
return CompareCondition::ne;
case CompareCondition::ne:
return CompareCondition::eq;
case CompareCondition::lt:
return CompareCondition::ge;
case CompareCondition::ge:
return CompareCondition::lt;
case CompareCondition::gt:
return CompareCondition::le;
case CompareCondition::le:
return CompareCondition::gt;
default:
TR_ASSERT_FATAL(false, "Invalid CompareCondition %d", static_cast<int>(cond));
}
}
/**
* \brief
* Returns a condition that should be used if the order of operands to the compare instruction
* is flipped.
*
* \param cond
* The original condition.
*
* \return
* The condition that should be used if the compare operands are flipped.
*/
CompareCondition flipConditionOrder(CompareCondition cond)
{
switch (cond)
{
case CompareCondition::eq:
return CompareCondition::eq;
case CompareCondition::ne:
return CompareCondition::ne;
case CompareCondition::lt:
return CompareCondition::gt;
case CompareCondition::ge:
return CompareCondition::le;
case CompareCondition::gt:
return CompareCondition::lt;
case CompareCondition::le:
return CompareCondition::ge;
default:
TR_ASSERT_FATAL(false, "Invalid CompareCondition %d", static_cast<int>(cond));
}
}
/**
* \brief
* Gets the opcode of a conditional branch instruction that branches when the provided condition
* is true.
*
* \param cond
* The condition upon which the instruction should branch.
*
* \return
* An extended mnemonic for the bc instruction that branches when the provided condition is true.
*/
TR::InstOpCode::Mnemonic compareConditionToBranch(CompareCondition cond)
{
switch (cond)
{
case CompareCondition::eq:
return TR::InstOpCode::beq;
case CompareCondition::ne:
return TR::InstOpCode::bne;
case CompareCondition::lt:
return TR::InstOpCode::blt;
case CompareCondition::ge:
return TR::InstOpCode::bge;
case CompareCondition::gt:
return TR::InstOpCode::bgt;
case CompareCondition::le:
return TR::InstOpCode::ble;
default:
TR_ASSERT_FATAL(false, "Invalid CompareCondition %d", static_cast<int>(cond));
}
}
/**
* \brief
* Gets the opcode of an isel instruction that selects based on the given condition code bit.
*
* \param cond
* The condition code bit to select based on.
*
* \return
* An extended mnemonic for the isel instruction that selects based on the given condition code
* bit.
*/
TR::InstOpCode::Mnemonic compareConditionToISel(TR::RealRegister::CRCC crcc)
{
switch (crcc)
{
case TR::RealRegister::CRCC_EQ:
return TR::InstOpCode::iseleq;
case TR::RealRegister::CRCC_LT:
return TR::InstOpCode::isellt;
case TR::RealRegister::CRCC_GT:
return TR::InstOpCode::iselgt;
case TR::RealRegister::CRCC_FU:
return TR::InstOpCode::iselun;
default:
TR_ASSERT_FATAL(false, "Invalid CRCC %d in compareConditionToISel", static_cast<int>(crcc));
}
}
/**
* \brief
* Represents information about a comparison to be performed.
*/
struct CompareInfo
{
CompareCondition cond; //< The condition that is being evaluated for this comparison.
TR::DataTypes type; //< The types of the operands being compared.
bool isUnsigned; //< Whether an unsigned integral comparison should be performed.
bool isUnorderedTrue; //< Whether unordered floating-point operands should evaluate as true.
CompareInfo(CompareCondition cond, TR::DataTypes type, bool isUnsigned, bool isUnorderedTrue)
: cond(cond), type(type), isUnsigned(isUnsigned), isUnorderedTrue(isUnorderedTrue) {}
};
/**
* \brief
* Determines whether the provided value is valid for a 16-bit signed SI field in an
* instruction.
*
* \param value
* The value to be checked.
*
* \return
* true if the value provided can be encoded in a 16-bit SI field; false otherwise.
*/
bool is16BitSignedImmediate(int64_t value)
{
return value >= -0x8000 && value < 0x8000;
}
/**
* \brief
* Determines whether the provided value is valid for a 16-bit unsigned UI field in an
* instruction.
*
* \param value
* The value to be checked.
*
* \return
* true if the value provided can be encoded in a 16-bit UI field; false otherwise.
*/
bool is16BitUnsignedImmediate(uint64_t value)
{
return value < 0x10000;
}
/**
* \brief
* Evaluates a node, sign-extending it to fill a register if its type is less than the size of
* a register.
*
* The returned register may or may not be the same as the value of \c node->getRegister() so it
* is necessary to call stopUsingExtendedRegister when done using the returned register.
*
* \param node
* The node to evaluate.
*
* \param extendInt32
* This flag determines whether 32-bit integers need to be sign-extended when running on 64-bit.
* If only 32-bit instructions will be used on the register, then this flag does not need to be
* set.
*
* \param cg
* The code generator.
*
* \return
* A register containing the evaluated value of the provided node, sign-extended to the desired
* length.
*/
static TR::Register *evaluateAndSignExtend(TR::Node *node, bool extendInt32, TR::CodeGenerator *cg)
{
TR::Register *srcReg = cg->evaluate(node);
switch (node->getDataType().getDataType())
{
case TR::Int8:
{
TR::Register *trgReg = cg->allocateRegister();
generateTrg1Src1Instruction(cg, TR::InstOpCode::extsb, node, trgReg, srcReg);
return trgReg;
}
case TR::Int16:
{
TR::Register *trgReg = cg->allocateRegister();
generateTrg1Src1Instruction(cg, TR::InstOpCode::extsh, node, trgReg, srcReg);
return trgReg;
}
case TR::Int32:
if (extendInt32 && cg->comp()->target().is64Bit())
{
TR::Register *trgReg = cg->allocateRegister();
generateTrg1Src1Instruction(cg, TR::InstOpCode::extsw, node, trgReg, srcReg);
return trgReg;
}
else
{
return srcReg;
}
default:
return srcReg;
}
}
/**
* \brief
* Evaluates a node, zero-extending it to fill a register if its type is less than the size of
* a register.
*
* The returned register may or may not be the same as the value of \c node->getRegister() so it
* is necessary to call stopUsingExtendedRegister when done using the returned register.
*
* \param node
* The node to evaluate.
*
* \param extendInt32
* This flag determines whether 32-bit integers need to be zero-extended when running on 64-bit.
* If only 32-bit instructions will be used on the register, then this flag does not need to be
* set.
*
* \param cg
* The code generator.
*
* \return
* A register containing the evaluated value of the provided node, zero-extended to the desired
* length.
*/
static TR::Register *evaluateAndZeroExtend(TR::Node *node, bool extendInt32, TR::CodeGenerator *cg)
{
TR::Register *srcReg = cg->evaluate(node);
switch (node->getDataType().getDataType())
{
case TR::Int8:
{
TR::Register *trgReg = cg->allocateRegister();
generateTrg1Src1Imm2Instruction(cg, TR::InstOpCode::rlwinm, node, trgReg, srcReg, 0, 0xffu);
return trgReg;
}
case TR::Int16:
{
TR::Register *trgReg = cg->allocateRegister();
generateTrg1Src1Imm2Instruction(cg, TR::InstOpCode::rlwinm, node, trgReg, srcReg, 0, 0xffffu);
return trgReg;
}
case TR::Int32:
if (extendInt32 && cg->comp()->target().is64Bit())
{
TR::Register *trgReg = cg->allocateRegister();
generateTrg1Src1Imm2Instruction(cg, TR::InstOpCode::rlwinm, node, trgReg, srcReg, 0, 0xffffffffu);
return trgReg;
}
else
{
return srcReg;
}
default:
return srcReg;
}
}
/**
* \brief
* Evaluates a node, sign- or zero-extending it to fill a register if its type is less than the
* size of a register.
*
* The returned register may or may not be the same as the value of \c node->getRegister() so it
* is necessary to call stopUsingExtendedRegister when done using the returned register.
*
* \param node
* The node to evaluate.
*
* \param isUnsigned
* If true, the register will be zero-extended; otherwise, it will be sign-extended.
*
* \param extendInt32
* This flag determines whether 32-bit integers need to be extended when running on 64-bit. If
* only 32-bit instructions will be used on the register, then this flag does not need to be
* set.
*
* \param cg
* The code generator.
*
* \return
* A register containing the evaluated value of the provided node, sign- or zero-extended to the
* desired length.
*/
static TR::Register *evaluateAndExtend(TR::Node *node, bool isUnsigned, bool extendInt32, TR::CodeGenerator *cg)
{
return isUnsigned ? evaluateAndZeroExtend(node, extendInt32, cg) : evaluateAndSignExtend(node, extendInt32, cg);
}
/**
* \brief
* Stops using a register returned from the evaluateAndExtend, evaluateAndZeroExtend, or
* evaluateAndSignExtend helpers.
*
* \param reg
* The sign- or zero-extended register to be freed.
*
* \param node
* The node from which the provided register was evaluated.
*
* \param cg
* The code generator.
*/
static void stopUsingExtendedRegister(TR::Register *reg, TR::Node *node, TR::CodeGenerator *cg)
{
if (reg != node->getRegister())
cg->stopUsingRegister(reg);
}
/**
* \brief
* Evaluates a 64-bit comparison on 32-bit machines using register pairs for the operands.
*
* \param condReg
* The condition register in which the result of the comparison should be placed.
*
* \param node
* The node for which this comparison is being generated.
*
* \param firstChild
* The node corresponding to the left-hand side operand of the comparison.
*
* \param secondChild
* The node corresponding to the right-hand side operand of the comparison.
*
* \param compareInfo
* The CompareInfo struct containing information about the comparison.
*
* \param cg
* The code generator.
*
* \return
* The condition code corresponding to the CR field bit into which the result of the comparison
* was placed.
*/
CompareCondition evaluateDualIntCompareToConditionRegister(
TR::Register *condReg,
TR::Node *node,
TR::Node *firstChild,
TR::Node *secondChild,
const CompareInfo& compareInfo,
TR::CodeGenerator *cg)
{
TR::Register *condReg2 = cg->allocateRegister(TR_CCR);
TR::Register *firstReg = cg->evaluate(firstChild);
if (secondChild->getOpCode().isLoadConst() && !secondChild->getRegister() && secondChild->getReferenceCount() == 1)
{
int32_t secondHi = secondChild->getLongIntHigh();
int32_t secondLo = secondChild->getLongIntLow();
if (compareInfo.isUnsigned && is16BitUnsignedImmediate(secondHi))
{
generateTrg1Src1ImmInstruction(cg, TR::InstOpCode::cmpli4, node, condReg, firstReg->getHighOrder(), secondHi);
}
else if (!compareInfo.isUnsigned && is16BitSignedImmediate(secondHi))
{
generateTrg1Src1ImmInstruction(cg, TR::InstOpCode::cmpi4, node, condReg, firstReg->getHighOrder(), secondHi);
}
else
{
TR::Register *secondHiReg = cg->allocateRegister();
loadConstant(cg, node, secondHi, secondHiReg);
generateTrg1Src2Instruction(cg, compareInfo.isUnsigned ? TR::InstOpCode::cmpl4 : TR::InstOpCode::cmp4, node, condReg, firstReg->getHighOrder(), secondHiReg);
cg->stopUsingRegister(secondHiReg);
}
if (is16BitUnsignedImmediate(secondLo))
{
generateTrg1Src1ImmInstruction(cg, TR::InstOpCode::cmpli4, node, condReg2, firstReg->getLowOrder(), secondLo);
}
else
{
TR::Register *secondLoReg = cg->allocateRegister();
loadConstant(cg, node, secondLo, secondLoReg);
generateTrg1Src2Instruction(cg, TR::InstOpCode::cmpl4, node, condReg2, firstReg->getHighOrder(), secondLoReg);
cg->stopUsingRegister(secondLoReg);
}
}
else
{
TR::Register *secondReg = cg->evaluate(secondChild);
generateTrg1Src2Instruction(cg, compareInfo.isUnsigned ? TR::InstOpCode::cmpl4 : TR::InstOpCode::cmp4, node, condReg, firstReg->getHighOrder(), secondReg->getHighOrder());
generateTrg1Src2Instruction(cg, TR::InstOpCode::cmpl4, node, condReg2, firstReg->getLowOrder(), secondReg->getLowOrder());
}
switch (compareInfo.cond)
{
case CompareCondition::eq:
// x_h == y_h && x_l == y_l
generateTrg1Src2ImmInstruction(cg, TR::InstOpCode::crand, node, condReg, condReg2, condReg,
(TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RT) | (TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RA) | (TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RB));
break;
case CompareCondition::ne:
// x_h != y_h || x_l != y_l
generateTrg1Src2ImmInstruction(cg, TR::InstOpCode::crnand, node, condReg, condReg2, condReg,
(TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RT) | (TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RA) | (TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RB));
break;
case CompareCondition::lt:
// x_h < y_h || (x_h == y_h && x_l < y_l)
generateTrg1Src2ImmInstruction(cg, TR::InstOpCode::crand, node, condReg2, condReg, condReg2,
(TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RT) | (TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RA) | (TR::RealRegister::CRCC_LT << TR::RealRegister::pos_RB));
generateTrg1Src2ImmInstruction(cg, TR::InstOpCode::cror, node, condReg, condReg2, condReg,
(TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RT) | (TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RA) | (TR::RealRegister::CRCC_LT << TR::RealRegister::pos_RB));
break;
case CompareCondition::ge:
// x_h > y_h || (x_h == y_h && x_l >= y_l)
generateTrg1Src2ImmInstruction(cg, TR::InstOpCode::crandc, node, condReg2, condReg, condReg2,
(TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RT) | (TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RA) | (TR::RealRegister::CRCC_LT << TR::RealRegister::pos_RB));
generateTrg1Src2ImmInstruction(cg, TR::InstOpCode::cror, node, condReg, condReg2, condReg,
(TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RT) | (TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RA) | (TR::RealRegister::CRCC_GT << TR::RealRegister::pos_RB));
break;
case CompareCondition::gt:
// x_h > y_h || (x_h == y_h && x_l > y_l)
generateTrg1Src2ImmInstruction(cg, TR::InstOpCode::crand, node, condReg2, condReg, condReg2,
(TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RT) | (TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RA) | (TR::RealRegister::CRCC_GT << TR::RealRegister::pos_RB));
generateTrg1Src2ImmInstruction(cg, TR::InstOpCode::cror, node, condReg, condReg2, condReg,
(TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RT) | (TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RA) | (TR::RealRegister::CRCC_GT << TR::RealRegister::pos_RB));
break;
case CompareCondition::le:
// x_h < y_h || (x_h == y_h && x_l <= y_l)
generateTrg1Src2ImmInstruction(cg, TR::InstOpCode::crandc, node, condReg2, condReg, condReg2,
(TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RT) | (TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RA) | (TR::RealRegister::CRCC_GT << TR::RealRegister::pos_RB));
generateTrg1Src2ImmInstruction(cg, TR::InstOpCode::cror, node, condReg, condReg2, condReg,
(TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RT) | (TR::RealRegister::CRCC_EQ << TR::RealRegister::pos_RA) | (TR::RealRegister::CRCC_LT << TR::RealRegister::pos_RB));
break;
default:
TR_ASSERT_FATAL(false, "Invalid CompareCondition %d", static_cast<int>(compareInfo.cond));
}
cg->stopUsingRegister(condReg2);
return CompareCondition::eq;
}
static bool registerRecentlyWritten(TR::Register *reg, uint32_t windowSize, TR::CodeGenerator *cg)
{
uint32_t i = 0;
TR::Instruction *cursor = cg->getAppendInstruction();
while (cursor && i < windowSize)
{
if (cursor->getOpCode().getMaxBinaryLength() != 0)
{
if (cursor->getTargetRegister(0) == reg)
return true;
i++;
}
cursor = cursor->getPrev();
}
return false;
}
/**
* \brief
* Evaluates an integral comparison to all bits of a CR field using a cmp or cmpi instruction.
*
* Under certain conditions, this function may actually evaluate the comparison by reversing its
* operands, in which case this routine will return true to indicate that this has been done.
*
* This function is not capable of handling 64-bit integral comparisons on 32-bit machines, as
* such comparisons cannot be performed in a single instruction and would require many extra CR
* logical instructions, rendering it too inefficient for general use.
*
* \param condReg
* The condition register in which the result of the comparison should be placed.
*
* \param node
* The node for which this comparison is being generated.
*
* \param firstChild
* The node corresponding to the left-hand side operand of the comparison.
*
* \param secondChild
* The node corresponding to the right-hand side operand of the comparison.
*
* \param compareInfo
* The CompareInfo struct containing information about the comparison. Only the fields relating
* to the types of values being compared (i.e. type and signedness) will be used.
*
* \param cg
* The code generator.
*
* \return
* true if the order of operands to the compare instruction was reversed; false otherwise.
*/
bool evaluateThreeWayIntCompareToConditionRegister(
TR::Register *condReg,
TR::Node *node,
TR::Node *firstChild,
TR::Node *secondChild,
const CompareInfo& compareInfo,
TR::CodeGenerator *cg)
{
TR::InstOpCode::Mnemonic cmpOp;
TR::InstOpCode::Mnemonic cmpiOp;
bool is64Bit;
switch (compareInfo.type)
{
case TR::Int8:
case TR::Int16:
case TR::Int32:
is64Bit = false;
break;
case TR::Int64:
is64Bit = true;
break;
case TR::Address:
is64Bit = cg->comp()->target().is64Bit();
break;
default:
TR_ASSERT_FATAL_WITH_NODE(node, false, "Cannot call evaluateThreeWayIntCompareToConditionRegister with data type %s", TR::DataType::getName(compareInfo.type));
}
TR_ASSERT_FATAL(!is64Bit || cg->comp()->target().is64Bit(), "Cannot call evaluateThreeWayIntCompareToConditionRegister for 64-bit values on 32-bit");
if (is64Bit)
{
if (compareInfo.isUnsigned)
{
cmpOp = TR::InstOpCode::cmpl8;
cmpiOp = TR::InstOpCode::cmpli8;
}
else
{
cmpOp = TR::InstOpCode::cmp8;
cmpiOp = TR::InstOpCode::cmpi8;
}
}
else
{
if (compareInfo.isUnsigned)
{
cmpOp = TR::InstOpCode::cmpl4;
cmpiOp = TR::InstOpCode::cmpli4;
}
else
{
cmpOp = TR::InstOpCode::cmp4;
cmpiOp = TR::InstOpCode::cmpi4;
}
}
TR::Register *firstReg = evaluateAndExtend(firstChild, compareInfo.isUnsigned, false, cg);
bool canUseCmpi = secondChild->getOpCode().isLoadConst() &&
(compareInfo.isUnsigned
? is16BitUnsignedImmediate(secondChild->get64bitIntegralValueAsUnsigned())
: is16BitSignedImmediate(secondChild->get64bitIntegralValue()));
static bool disableFlipCompare = feGetEnv("TR_DisableFlipCompare") != NULL;
CompareCondition cond = compareInfo.cond;
bool wasFlipped = false;
if (canUseCmpi)
{
generateTrg1Src1ImmInstruction(cg, cmpiOp, node, condReg, firstReg, secondChild->get64bitIntegralValue());
}
else if (
(firstReg->containsInternalPointer() || registerRecentlyWritten(firstReg, 4, cg)) &&
performTransformation(
cg->comp(),
"O^O evaluateIntCompareToConditionRegister: flipping order of compare operands (n%dn, n%dn) while evaluating n%dn to avoid P6 FXU reject",
firstChild->getGlobalIndex(),
secondChild->getGlobalIndex(),
node->getGlobalIndex()
)
)
{
TR::Register *secondReg = evaluateAndExtend(secondChild, compareInfo.isUnsigned, false, cg);
generateTrg1Src2Instruction(cg, cmpOp, node, condReg, secondReg, firstReg);
stopUsingExtendedRegister(secondReg, secondChild, cg);
wasFlipped = true;
}
else
{
TR::Register *secondReg = evaluateAndExtend(secondChild, compareInfo.isUnsigned, false, cg);
generateTrg1Src2Instruction(cg, cmpOp, node, condReg, firstReg, secondReg);
stopUsingExtendedRegister(secondReg, secondChild, cg);
}
stopUsingExtendedRegister(firstReg, firstChild, cg);
return wasFlipped;
}
/**
* \brief
* Evaluates an integral comparison to a single bit of a CR field.
*
* \param condReg
* The condition register in which the result of the comparison should be placed.
*
* \param node
* The node for which this comparison is being generated.
*
* \param firstChild
* The node corresponding to the left-hand side operand of the comparison.
*
* \param secondChild
* The node corresponding to the right-hand side operand of the comparison.
*
* \param compareInfo
* The CompareInfo struct containing information about the comparison.
*
* \param cg
* The code generator.
*
* \return
* The condition code corresponding to the CR field bit into which the result of the comparison
* was placed.
*/
CompareCondition evaluateIntCompareToConditionRegister(
TR::Register *condReg,
TR::Node *node,
TR::Node *firstChild,
TR::Node *secondChild,
const CompareInfo& compareInfo,
TR::CodeGenerator *cg
)
{
if (compareInfo.type == TR::Int64 && !cg->comp()->target().is64Bit())
return evaluateDualIntCompareToConditionRegister(condReg, node, firstChild, secondChild, compareInfo, cg);
if (evaluateThreeWayIntCompareToConditionRegister(condReg, node, firstChild, secondChild, compareInfo, cg))
return flipConditionOrder(compareInfo.cond);
else
return compareInfo.cond;
}
/**
* \brief
* Evaluates a floating-point comparison to a single bit of a CR field.
*
* \param condReg
* The condition register in which the result of the comparison should be placed.
*
* \param node
* The node for which this comparison is being generated.
*
* \param firstChild
* The node corresponding to the left-hand side operand of the comparison.
*
* \param secondChild
* The node corresponding to the right-hand side operand of the comparison.
*
* \param compareInfo
* The CompareInfo struct containing information about the comparison.
*
* \param cg
* The code generator.
*
* \return
* The condition code corresponding to the CR field bit into which the result of the comparison
* was placed.
*/
CompareCondition evaluateFloatCompareToConditionRegister(
TR::Register *condReg,
TR::Node *node,
TR::Node *firstChild,
TR::Node *secondChild,
const CompareInfo& compareInfo,
TR::CodeGenerator *cg)
{
CRCompareCondition crCond = compareConditionInCR(compareInfo.cond);
TR::Register *firstReg = cg->evaluate(firstChild);
TR::Register *secondReg = cg->evaluate(secondChild);
generateTrg1Src2Instruction(cg, TR::InstOpCode::fcmpu, node, condReg, firstReg, secondReg);
// When we're using the negation of a CR bit (e.g. x >= y is checked as x < y), we must take
// into account the possibility that the two operands were unordered for a floating-point
// comparison. The isUnorderedTrue flag tells us whether two unordered operands should return
// true, so if that is different from whether the CR bit is negated, we must flip that CR bit
// if the operands were unordered. Since both CR bits can never be set simultaneously, this can
// be done with either cror or crxor, as both are equivalent.
if (crCond.isReversed != compareInfo.isUnorderedTrue)
generateTrg1Src2ImmInstruction(
cg,
TR::InstOpCode::crxor,
node,
condReg,
condReg,
condReg,
(crCond.crcc << TR::RealRegister::pos_RT) | (crCond.crcc << TR::RealRegister::pos_RA) | (TR::RealRegister::CRCC_FU << TR::RealRegister::pos_RB)
);
return compareInfo.cond;
}
/**
* \brief
* Evaluates an arbitrary comparison to a single bit of a CR field.
*
* \param condReg
* The condition register in which the result of the comparison should be placed.
*
* \param node
* The node for which this comparison is being generated.
*
* \param firstChild
* The node corresponding to the left-hand side operand of the comparison.
*
* \param secondChild
* The node corresponding to the right-hand side operand of the comparison.
*
* \param compareInfo
* The CompareInfo struct containing information about the comparison.
*
* \param cg
* The code generator.
*
* \return
* The condition code corresponding to the CR field bit into which the result of the comparison
* was placed.
*/
CompareCondition evaluateCompareToConditionRegister(
TR::Register *condReg,
TR::Node *node,
TR::Node *firstChild,
TR::Node *secondChild,
const CompareInfo& compareInfo,
TR::CodeGenerator *cg)
{
switch (compareInfo.type)
{
case TR::Int8:
case TR::Int16:
case TR::Int32:
case TR::Int64:
case TR::Address:
return evaluateIntCompareToConditionRegister(condReg, node, firstChild, secondChild, compareInfo, cg);
case TR::Float:
case TR::Double:
return evaluateFloatCompareToConditionRegister(condReg, node, firstChild, secondChild, compareInfo, cg);
default:
TR_ASSERT_FATAL_WITH_NODE(node, false, "Unrecognized comparison type %s", TR::DataType::getName(compareInfo.type));
}
}
CompareInfo getCompareInfo(TR::ILOpCode op)
{
TR::DataTypes type = op.expectedChildType(0);
switch (op.getOpCodeValue())
{
case TR::bcmpeq:
case TR::scmpeq:
case TR::icmpeq:
case TR::lcmpeq:
case TR::acmpeq:
case TR::fcmpeq:
case TR::dcmpeq:
return CompareInfo(CompareCondition::eq, type, false, false);
case TR::fcmpequ:
case TR::dcmpequ:
return CompareInfo(CompareCondition::eq, type, false, true);
case TR::bcmpne:
case TR::scmpne:
case TR::icmpne:
case TR::lcmpne:
case TR::acmpne:
case TR::fcmpne:
case TR::dcmpne:
return CompareInfo(CompareCondition::ne, type, false, false);
case TR::fcmpneu:
case TR::dcmpneu:
return CompareInfo(CompareCondition::ne, type, false, true);
case TR::bcmplt:
case TR::scmplt:
case TR::icmplt:
case TR::lcmplt:
case TR::fcmplt:
case TR::dcmplt:
return CompareInfo(CompareCondition::lt, type, false, false);
case TR::bucmplt:
case TR::sucmplt:
case TR::iucmplt:
case TR::lucmplt:
case TR::acmplt:
return CompareInfo(CompareCondition::lt, type, true, false);
case TR::fcmpltu:
case TR::dcmpltu:
return CompareInfo(CompareCondition::lt, type, false, true);
case TR::bcmpge:
case TR::scmpge:
case TR::icmpge:
case TR::lcmpge:
case TR::fcmpge:
case TR::dcmpge:
return CompareInfo(CompareCondition::ge, type, false, false);
case TR::bucmpge:
case TR::sucmpge:
case TR::iucmpge:
case TR::lucmpge:
case TR::acmpge:
return CompareInfo(CompareCondition::ge, type, true, false);
case TR::fcmpgeu:
case TR::dcmpgeu:
return CompareInfo(CompareCondition::ge, type, false, true);
case TR::bcmpgt:
case TR::scmpgt:
case TR::icmpgt: