-
Notifications
You must be signed in to change notification settings - Fork 392
/
ControlFlowEvaluator.cpp
2353 lines (2098 loc) · 91 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/CodeGenerator.hpp"
#include "codegen/CodeGenerator_inlines.hpp"
#include "codegen/ConstantDataSnippet.hpp"
#include "env/FrontEnd.hpp"
#include "codegen/Instruction.hpp"
#include "codegen/Linkage.hpp"
#include "codegen/Linkage_inlines.hpp"
#include "codegen/LinkageConventionsEnum.hpp"
#include "codegen/LiveRegister.hpp"
#include "codegen/Machine.hpp"
#include "codegen/MemoryReference.hpp"
#include "codegen/RealRegister.hpp"
#include "codegen/RecognizedMethods.hpp"
#include "codegen/Register.hpp"
#include "codegen/RegisterConstants.hpp"
#include "codegen/RegisterDependency.hpp"
#include "codegen/RegisterDependencyStruct.hpp"
#include "codegen/RegisterPair.hpp"
#include "codegen/Relocation.hpp"
#include "codegen/TreeEvaluator.hpp"
#include "compile/Compilation.hpp"
#include "compile/ResolvedMethod.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/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/MethodSymbol.hpp"
#include "il/Node.hpp"
#include "il/Node_inlines.hpp"
#include "il/ResolvedMethodSymbol.hpp"
#include "il/Symbol.hpp"
#include "il/TreeTop.hpp"
#include "il/TreeTop_inlines.hpp"
#include "infra/Assert.hpp"
#include "infra/Bit.hpp"
#include "infra/List.hpp"
#include "ras/Debug.hpp"
#include "runtime/Runtime.hpp"
#include "x/codegen/BinaryCommutativeAnalyser.hpp"
#include "x/codegen/CompareAnalyser.hpp"
#include "x/codegen/FPTreeEvaluator.hpp"
#include "x/codegen/X86Instruction.hpp"
#include "codegen/InstOpCode.hpp"
class TR_OpaqueClassBlock;
class TR_OpaqueMethodBlock;
static bool virtualGuardHelper(TR::Node *node, TR::CodeGenerator *cg);
// The following functions are simple enough to inline, and are called often
// enough that we want to take advantage of inlining. However, it is used
// across multiple translation units, preventing us from just using 'inline'
// TODO: Put in own header file shared by the various TreeEvaluator.cpp
// (but not TreeEvaluator.hpp, too many files include this).
// Right now, this function is duplicated in TreeEval, ControlFlowEval, Binary &UnaryEval.
inline bool getNodeIs64Bit(TR::Node *node, TR::CodeGenerator *cg)
{
return cg->comp()->target().is64Bit() && node->getSize() > 4;
}
// Right now, this is duplicated in ControlFlowEval, BinaryEval and TreeEval.
inline intptr_t integerConstNodeValue(TR::Node *node, TR::CodeGenerator *cg)
{
if (getNodeIs64Bit(node, cg))
{
return node->getLongInt();
}
else
{
TR_ASSERT(node->getSize() <= 4, "For efficiency on IA32, only call integerConstNodeValue for 32-bit constants");
return node->getInt();
}
}
// Right now, this is duplicated in ControlFlowEval, BinaryEval and TreeEval.
inline bool constNodeValueIs32BitSigned(TR::Node *node, intptr_t *value, TR::CodeGenerator *cg)
{
*value = integerConstNodeValue(node, cg);
if (cg->comp()->target().is64Bit())
{
return IS_32BIT_SIGNED(*value);
}
else
{
TR_ASSERT(IS_32BIT_SIGNED(*value), "assertion failure");
return true;
}
}
static inline TR::Instruction *generateWiderCompare(TR::Node *node, TR::Register *targetReg,
intptr_t value,
TR::CodeGenerator *cg)
{
// using 16bit immediate in a 32-bit compare instruction (0x66 length change prefix)
// results in a decode penalty.
// The instruction length decoder of the Intel Core processors cannot decode
// the length of an LCP instruction in one cycle, therefore it initiates slow decoding,
// which takes five extra cycles to complete.
//
// the recommended fix is to use a compare register with a 32-bit value
// instead
//
generateRegRegInstruction(MOVSXReg4Reg2, node, targetReg, targetReg, cg);
return generateRegImmInstruction(CMP4RegImm4, node, targetReg, value, cg);
}
bool isConditionCodeSetForCompareToZero(TR::Node *node, bool justTestZeroFlag)
{
TR::Compilation *comp = TR::comp();
// Disable. Need to re-think how we handle overflow cases.
//
static char *disableNoTestEFlags = feGetEnv("TR_disableNoTestEFlags");
if (disableNoTestEFlags)
return false;
// See if there is a previous instruction that has set the condition flags
// properly for this node's register
//
if (!node->getRegister())
return false;
// This is neccessary because the branch instruction may rely on OF
// e.g jl tests OF != SF
// Since cmp r1,0 always unsets OF, the prevInstr cannot set OF
//
// Example:
// add r1, 1 (r1=0x7fffffff, OF set)
// cmp r1, 0 (OF unset, and this instr cannot be ommitted)
// jl address
//
// This check can be taken out if the branch instruction generated by
// the caller won't test OF (e.g. js )
//
if (!node->cannotOverflow())
return false;
// Find the last instruction that either
// 1) sets the appropriate condition flags, or
// 2) modifies the register to be tested
// (and that hopefully does both)
//
TR::Instruction *prevInstr;
TR::X86RegInstruction *prevRegInstr;
for (prevInstr = comp->cg()->getAppendInstruction();
prevInstr;
prevInstr = prevInstr->getPrev())
{
prevRegInstr = prevInstr->getX86RegInstruction();
// The register must be equal and the node size must be equal in order to
// insure the instruction is setting the condition code based on the
// correct set of bits
if (prevRegInstr && prevRegInstr->getTargetRegister() == node->getRegister() &&
prevRegInstr->getNode() && prevRegInstr->getNode()->getSize() == node->getSize())
{
if (prevRegInstr->getOpCode().modifiesTarget())
{
// This instruction sets the register.
//
if (justTestZeroFlag)
{
if (!prevInstr->getOpCode().setsCCForTest())
return false;
else if (prevInstr->getOpCode().isShiftOp())
{
// Only constant shift instructions should be considered. For those instructions,
// the flags will only be set as required if the count != 0.
//
uint32_t count = 0;
if (prevInstr->getOpCode().hasByteImmediate())
{
count = ((TR::X86RegImmInstruction *)prevRegInstr)->getSourceImmediate();
}
if (count == 0)
{
return false;
}
}
}
else
{
if (!prevInstr->getOpCode().setsCCForCompare())
return false;
}
return true;
}
}
if (prevInstr->getOpCodeValue() == LABEL)
{
// This instruction is a possible branch target.
return false;
}
if (prevInstr->getOpCode().modifiesSomeArithmeticFlags())
{
// This instruction overwrites the condition flags.
return false;
}
}
return false;
}
static uint32_t sumOf2ConsecutivePowersOf2(uint32_t numberOfCases)
{
for (uint32_t i = 3; i < 0xc0000000; i <<= 1)
{
if (i == numberOfCases)
{
return ((i & (i-1)) >> 1) + 1;
}
}
return 0;
}
static void binarySearchCaseSpace(TR::Register *selectorReg,
TR::Node *lookupNode,
uint32_t lowChild,
uint32_t highChild,
bool &evaluateDefaultGlRegDeps,
TR::CodeGenerator *cg)
{
uint32_t numCases = highChild - lowChild + 1;
TR::InstOpCode::Mnemonic opCode;
uint32_t pivot;
if ((pivot = sumOf2ConsecutivePowersOf2(numCases)) == 0)
{
pivot = lowChild + (numCases / 2) - 1;
}
else
{
pivot += lowChild - 1;
}
if (pivot >= lowChild)
{
int32_t pivotValue = lookupNode->getChild(pivot)->getCaseConstant();
// while this is a signed comparison, it's valid for unsigned too
// (if -128 <= (signed) pivot <= 0; then (unsigned) pivot >= 0xFFFF FF80
// so sign extension is valid)
if (pivotValue >= -128 && pivotValue <= 127)
{
opCode = CMP4RegImms;
}
else
{
opCode = CMP4RegImm4;
}
generateRegImmInstruction(opCode, lookupNode, selectorReg, pivotValue, cg);
TR::LabelSymbol *startLabel = generateLabelSymbol(cg);
TR::LabelSymbol *pivotLabel = generateLabelSymbol(cg);
startLabel->setStartInternalControlFlow();
pivotLabel->setEndInternalControlFlow();
generateLabelInstruction(LABEL, lookupNode, startLabel, cg);
// We are guaranteed that the case children are sorted.
// Image the Z32 number lines
// [------------------------|--------------------------) signed
// TR::getMinSigned<TR::Int32>() 0 TR::getMaxSigned<TR::Int32>() TR::getMaxUnsigned<TR::Int32>()
// [--------------------------|------------------------------------) unsigned
// If all the cases of the lookup are on the same 'half' of the number lines, it doesn't matter
// if we use JA or JG, so lets use JG.
// For the signed case, we better use JG if the numbers straddle the 0 boundary; we already do.
// For the unsigned case, we better use JA if the numbers straddle the TR::getMaxSigned<TR::Int32>() boundary. If that's
// the case, the highVal will be less than (in the signed sense) than lowVal.
//
bool isUnsigned;
int32_t lowVal = lookupNode->getChild(lowChild)->getCaseConstant();
int32_t highVal = lookupNode->getChild(highChild)->getCaseConstant();
if (highVal < lowVal)
isUnsigned = true;
else
isUnsigned = false;
generateLabelInstruction(isUnsigned ? JA4 : JG4, lookupNode, pivotLabel, cg);
if (lowChild == pivot)
{
generateJumpInstruction(JE4, lookupNode->getChild(lowChild), cg);
generateJumpInstruction(JMP4, lookupNode->getChild(1), cg, false, evaluateDefaultGlRegDeps);
evaluateDefaultGlRegDeps = false;
}
else
{
binarySearchCaseSpace(selectorReg, lookupNode, lowChild, pivot, evaluateDefaultGlRegDeps, cg);
}
generateLabelInstruction(LABEL, lookupNode, pivotLabel, cg);
}
else
TR_ASSERT(pivot == lowChild - 1 && lowChild == highChild, "unexpected pivot value in binarySearchCaseSpace");
if (highChild == pivot + 1)
{
int32_t highValue = lookupNode->getChild(highChild)->getCaseConstant();
// while this is a signed comparison, it's valid for unsigned too
// (if -128 <= (signed) pivot <= 0; then (unsigned) pivot >= 0xFFFF FF80
// so sign extension is valid)
if (highValue >= -128 && highValue <= 127)
{
opCode = CMP4RegImms;
}
else
{
opCode = CMP4RegImm4;
}
generateRegImmInstruction(opCode, lookupNode, selectorReg, highValue, cg);
generateJumpInstruction(JE4, lookupNode->getChild(highChild), cg);
generateJumpInstruction(JMP4, lookupNode->getChild(1), cg, false, evaluateDefaultGlRegDeps);
evaluateDefaultGlRegDeps = false;
}
else
{
binarySearchCaseSpace(selectorReg, lookupNode, pivot + 1, highChild, evaluateDefaultGlRegDeps, cg);
}
}
TR::Register *OMR::X86::TreeEvaluator::lookupEvaluator(TR::Node *node, TR::CodeGenerator *cg)
{
TR::Register *selectorReg = cg->evaluate(node->getFirstChild());
bool evaluateDefaultGlRegDeps = true;
TR::RealRegister::RegNum depsRegisterIndex = TR::RealRegister::NoReg;
bool selectorRegInGlRegDeps = false;
TR::LabelSymbol *startLabel = generateLabelSymbol(cg);
TR::LabelSymbol *endLabel = generateLabelSymbol(cg);
TR::RegisterDependencyConditions *deps = generateRegisterDependencyConditions((uint8_t) 0, TR::RealRegister::MaxAssignableRegisters, cg);
startLabel->setStartInternalControlFlow();
endLabel->setEndInternalControlFlow();
generateLabelInstruction(LABEL, node, startLabel, cg);
for (int i=1; i< node->getNumChildren(); i++)
{
TR::Node *caseChild = node->getChild(i);
if (caseChild->getNumChildren() > 0)
{
TR::Node *firstCaseChild = caseChild->getFirstChild();
if (firstCaseChild->getOpCodeValue() == TR::GlRegDeps)
{
for (int j = firstCaseChild->getNumChildren()-1; j >= 0; j--)
{
TR::Node *child = firstCaseChild->getChild(j);
TR::Register *globalReg = NULL;
if (child->getOpCodeValue() == TR::PassThrough)
globalReg = child->getFirstChild()->getRegister();
else
globalReg = child->getRegister();
TR_GlobalRegisterNumber globalRegNum = child->getGlobalRegisterNumber();
TR_GlobalRegisterNumber highGlobalRegNum = child->getHighGlobalRegisterNumber();
if (globalReg->getKind() == TR_GPR && highGlobalRegNum < 0 &&
(globalReg == selectorReg))
{
depsRegisterIndex = (TR::RealRegister::RegNum) cg->getGlobalRegister(globalRegNum);
selectorRegInGlRegDeps = true;
}
else if (globalReg->getKind() == TR_GPR || globalReg->getKind() == TR_FPR || globalReg->getKind() == TR_VRF)
{
TR::RegisterPair *globalRegPair = globalReg->getRegisterPair();
TR::RealRegister::RegNum registerIndex = (TR::RealRegister::RegNum) cg->getGlobalRegister(globalRegNum);
if (!deps->findPostCondition(registerIndex))
deps->addPostCondition(globalRegPair ? globalRegPair->getLowOrder() : globalReg , registerIndex, cg);
if (highGlobalRegNum >=0)
{
TR::RealRegister::RegNum highRegisterIndex = (TR::RealRegister::RegNum) cg->getGlobalRegister(highGlobalRegNum);
if (!deps->findPostCondition(highRegisterIndex))
deps->addPostCondition(globalRegPair->getHighOrder(), highRegisterIndex, cg);
}
}
}
}
}
}
binarySearchCaseSpace(selectorReg, node, 2, node->getNumChildren() - 1, evaluateDefaultGlRegDeps, cg);
cg->decReferenceCount(node->getFirstChild());
deps->addPostCondition(selectorReg, depsRegisterIndex, cg);
deps->stopAddingConditions();
generateLabelInstruction(LABEL, node, endLabel, deps, cg);
return NULL;
}
TR::Register *OMR::X86::TreeEvaluator::tableEvaluator(TR::Node *node, TR::CodeGenerator *cg)
{
int32_t i;
uint32_t numBranchTableEntries = node->getNumChildren() - 2;
intptr_t *branchTable =
(intptr_t*)cg->allocateCodeMemory(numBranchTableEntries * sizeof(branchTable[0]), cg->getCurrentEvaluationBlock()->isCold());
TR::Register *selectorReg = cg->evaluate(node->getFirstChild());
TR::InstOpCode::Mnemonic opCode;
bool canSkipBoundTest = node->isSafeToSkipTableBoundCheck();
TR::Node *secondChild = node->getSecondChild();
if (!canSkipBoundTest)
{
if (numBranchTableEntries <= 127)
{
opCode = CMP4RegImms;
}
else
{
opCode = CMP4RegImm4;
}
generateRegImmInstruction(opCode, node, selectorReg, numBranchTableEntries, cg);
// The glRegDep is hung off the default case statement.
//
generateJumpInstruction(JAE4, secondChild, cg, true);
}
else
{
if (secondChild->getNumChildren() >= 1)
cg->evaluate(secondChild->getFirstChild()); // evaluate the glRegDeps
}
TR::MemoryReference *jumpMR = NULL;
TR::Register *branchTableReg = NULL;
if (cg->comp()->target().is64Bit() && cg->comp()->compileRelocatableCode())
{
// Generate position-independent code so that no (external) relocation is
// necessary:
//
// lea rBranchTable, [rip + OFFSET_TO_JUMP_TABLE]
// jmp qword ptr [rBranchTable + 8*rIndex]
//
TR::LabelSymbol *label = generateLabelSymbol(cg);
label->setCodeLocation(reinterpret_cast<uint8_t*>(branchTable));
TR::MemoryReference *branchTableLeaMR = generateX86MemoryReference(label, cg);
branchTableReg = cg->allocateRegister();
generateRegMemInstruction(LEA8RegMem, node, branchTableReg, branchTableLeaMR, cg);
jumpMR = generateX86MemoryReference(branchTableReg, selectorReg, 3, cg);
}
else
{
jumpMR = generateX86MemoryReference(
(TR::Register *)NULL,
selectorReg,
(uint8_t)(cg->comp()->target().is64Bit()? 3 : 2),
(intptr_t)branchTable, cg);
jumpMR->setNeedsCodeAbsoluteExternalRelocation();
}
TR::X86MemTableInstruction *jmpTableInstruction = NULL;
TR::RegisterDependencyConditions *deps = NULL;
// Add GlRegDep dependencies to indirect jump
//
if (secondChild->getNumChildren() > 0)
{
deps = generateRegisterDependencyConditions(secondChild->getFirstChild(), cg, 0, NULL);
deps->stopAddingConditions();
}
if (cg->getLinkage()->getProperties().getMethodMetaDataRegister() != TR::RealRegister::NoReg)
{
jmpTableInstruction = generateMemTableInstruction(JMPMem, node, jumpMR, numBranchTableEntries, deps, cg);
}
else
{
generateMemInstruction(JMPMem, node, jumpMR, deps, cg);
}
for (i = 2; i < node->getNumChildren(); ++i)
{
TR::Node * caseNode = node->getChild(i);
uint8_t * target = (uint8_t *)&branchTable[i-2];
cg->addMetaDataForBranchTableAddress(target, caseNode, jmpTableInstruction);
}
for (i = 0; i < node->getNumChildren(); ++i)
{
cg->decReferenceCount(node->getChild(i));
}
if (branchTableReg != NULL)
cg->stopUsingRegister(branchTableReg);
return NULL;
}
TR::Register *OMR::X86::TreeEvaluator::minmaxEvaluator(TR::Node *node, TR::CodeGenerator *cg)
{
TR::InstOpCode::Mnemonic CMP = BADIA32Op;
TR::InstOpCode::Mnemonic MOV = BADIA32Op;
TR::InstOpCode::Mnemonic CMOV = BADIA32Op;
switch (node->getOpCodeValue())
{
case TR::imin:
CMP = CMP4RegReg;
MOV = MOV4RegReg;
CMOV = CMOVG4RegReg;
break;
case TR::imax:
CMP = CMP4RegReg;
MOV = MOV4RegReg;
CMOV = CMOVL4RegReg;
break;
case TR::lmin:
CMP = CMP8RegReg;
MOV = MOV8RegReg;
CMOV = CMOVG8RegReg;
break;
case TR::lmax:
CMP = CMP8RegReg;
MOV = MOV8RegReg;
CMOV = CMOVL8RegReg;
break;
default:
TR_ASSERT(false, "INCORRECT IL OPCODE.");
break;
}
auto operand0 = cg->evaluate(node->getChild(0));
auto operand1 = cg->evaluate(node->getChild(1));
auto result = cg->allocateRegister();
generateRegRegInstruction(CMP, node, operand0, operand1, cg);
generateRegRegInstruction(MOV, node, result, operand0, cg);
generateRegRegInstruction(CMOV, node, result, operand1, cg);
node->setRegister(result);
cg->decReferenceCount(node->getChild(0));
cg->decReferenceCount(node->getChild(1));
return result;
}
void
OMR::X86::TreeEvaluator::setupProfiledGuardRelocation(TR::X86RegImmInstruction *cmpInstruction,
TR::Node *node,
TR_ExternalRelocationTargetKind reloKind)
{
#ifdef J9_PROJECT_SPECIFIC
// The following makes sure that the TR_ProfiledInlinedMethod relocation for this inlined site will be created
// later in TR::CodeGenerator::processRelocations()
TR::Compilation *comp = TR::comp();
TR_VirtualGuard *virtualGuard = comp->findVirtualGuardInfo(node);
TR_AOTGuardSite *site = comp->addAOTNOPSite();
site->setLocation(NULL);
site->setType(TR_ProfiledGuard);
site->setGuard(virtualGuard);
site->setNode(node);
site->setAconstNode(node->getSecondChild());
// If we've generated an instruction, then make sure it is marked to get the right kind of relocation
if (cmpInstruction)
{
cmpInstruction->setReloKind(reloKind);
cmpInstruction->setNode(node->getSecondChild());
}
traceMsg(comp, "setupProfiledGuardRelocation: site %p type %d node %p\n", site, site->getType(), node);
#endif
}
void OMR::X86::TreeEvaluator::compareIntegersForEquality(TR::Node *node, TR::CodeGenerator *cg)
{
TR::Compilation *comp = cg->comp();
TR::Node *secondChild = node->getSecondChild();
// The opcode size of the compare node doesn't tell us whether we need a
// 64-bit compare. We need to check a child.
bool is64Bit = TR::TreeEvaluator::getNodeIs64Bit(secondChild, cg);
if (cg->profiledPointersRequireRelocation() &&
secondChild->getOpCodeValue() == TR::aconst &&
(secondChild->isMethodPointerConstant() || secondChild->isClassPointerConstant()))
{
TR_ASSERT(!(node->isNopableInlineGuard()),"Should not evaluate class or method pointer constants underneath NOPable guards as they are runtime assumptions handled by virtualGuardHelper");
cg->evaluate(secondChild);
}
intptr_t constValue;
if (secondChild->getOpCode().isLoadConst() &&
secondChild->getRegister() == NULL &&
(((secondChild->getSize() <= 2) && (!secondChild->isUnsigned())) ||
(TR::TreeEvaluator::constNodeValueIs32BitSigned(secondChild, &constValue, cg) && !cg->constantAddressesCanChangeSize(secondChild))))
{
if(secondChild->getSize() <= 2)
constValue = (intptr_t)secondChild->get64bitIntegralValue();
// compare (node)
// ? (firstChild)
// const (secondChild and constValue)
//
TR::Node *firstChild = node->getFirstChild();
// Extra goodies for branches
//
if (node->getOpCode().isIf() && constValue == 0)
{
TR::ILOpCodes op = node->getOpCodeValue();
if (op == TR::ifacmpeq)
{
node->getFirstChild()->setIsNonNull(true);
}
else if (op == TR::ificmpeq || op == TR::iflcmpeq)
{
node->getFirstChild()->setIsNonZero(true);
}
}
if (constValue >= -128 && constValue <= 127)
{
if (constValue == 0)
// compare (node)
// ? (firstChild)
// 0
//
{
if (firstChild->getOpCode().isAnd() &&
(firstChild->getRegister() == NULL) &&
(firstChild->getReferenceCount() == 1))
{
// compare (node)
// and (firstChild)
// ? (andFirstChild)
// ? (andSecondChild)
// 0
//
bool conversionSkipped = false;
TR::Node *andFirstChild = firstChild->getFirstChild();
TR::Node *andSecondChild = firstChild->getSecondChild();
//This code path cheats a bit and uses a 4-byte instruction to compare a 2-byte
//value with zero. Ordinarily, this wouldn't work because the top 2 bytes of
//the value are garbage. To make this work, we must zero-extend the mask immediate
//to 4 bytes, thereby ensuring the top two bytes of the masked value will be
//zero and won't affect the subsequent compare with zero.
//Therefore, counterintuitively, zero-entending the mask is always the correct thing
//to do, regardless of whether the compare node claims to be a signed operation!
uint64_t mask;
if (andSecondChild->getOpCode().isLoadConst() &&
andSecondChild->getRegister() == NULL &&
((mask = andSecondChild->get64bitIntegralValueAsUnsigned()) >> 31) == 0) // this needs to be an unsigned value otherwise
// <4 byte mask values will be sign extended
// and may be used in a 4 byte compare
{
// compare (node)
// and (firstChild)
// ? (andFirstChild)
// const (andSecondChild)
// 0
//
if (andFirstChild->getRegister() == NULL &&
andFirstChild->getReferenceCount() == 1 &&
andFirstChild->getOpCode().isLoadVar())
{
// memory case
TR::MemoryReference *tempMR = generateX86MemoryReference(andFirstChild, cg);
if(((mask >> 8) == 0) || (andSecondChild->getSize() == 1))
generateMemImmInstruction(TEST1MemImm1, node, tempMR, mask, cg);
else if(andSecondChild->getSize() == 2)
{
TR::Register *tempReg = cg->allocateRegister();
TR::TreeEvaluator::loadConstant(node, mask, TR_RematerializableShort, cg, tempReg);
generateMemRegInstruction(TEST2MemReg, node, tempMR, tempReg, cg);
cg->stopUsingRegister(tempReg);
}
else
generateMemImmInstruction(TESTMemImm4(is64Bit), node, tempMR, mask, cg);
tempMR->decNodeReferenceCounts(cg);
}
else
{
//register case
// before evaluating the andFirstChild we check to see if it is an integral
// conversion that is redundant because of the constant value in the compare
TR::Register *tempReg = cg->evaluate(andFirstChild);
if (andFirstChild->getOpCode().isConversion()
&& andFirstChild->getType().isIntegral()
&& andFirstChild->getFirstChild()->getType().isIntegral()
&& !andFirstChild->getRegister()
&& andFirstChild->getSize() > andFirstChild->getFirstChild()->getSize()
&& ((mask >> (8 * andFirstChild->getFirstChild()->getSize())) == 0))
{
tempReg = cg->evaluate(andFirstChild->getFirstChild());
conversionSkipped = true;
}
else
{
tempReg = cg->evaluate(andFirstChild);
}
TR::InstOpCode::Mnemonic testInstr;
if(((mask >> 8) == 0 && !andFirstChild->isInvalid8BitGlobalRegister()) ||
(andSecondChild->getSize() == 1))
testInstr = TEST1RegImm1;
else
testInstr = TESTRegImm4(is64Bit);
generateRegImmInstruction(testInstr, node, tempReg, mask, cg);
}
if (conversionSkipped)
{
cg->recursivelyDecReferenceCount(andFirstChild);
}
else
{
cg->decReferenceCount(andFirstChild);
}
cg->decReferenceCount(andSecondChild);
}
else
{
TR_X86BinaryCommutativeAnalyser temp(cg);
TR::InstOpCode::Mnemonic testRRInstr, testMRInstr, movRRInstr;
uint32_t size = firstChild->getSize();
if(size == 1)
{
testRRInstr = TEST1RegReg;
testMRInstr = TEST1MemReg;
movRRInstr = MOV1RegReg;
}
else if(size == 2)
{
testRRInstr = TEST2RegReg;
testMRInstr = TEST2MemReg;
movRRInstr = MOV2RegReg;
}
else
{
testRRInstr = TESTRegReg(is64Bit);
testMRInstr = TESTMemReg(is64Bit);
movRRInstr = MOVRegReg(is64Bit);
}
temp.genericAnalyser(firstChild, testRRInstr, testMRInstr, movRRInstr, true);
}
}
else
{
if ((firstChild->getRegister() == NULL) &&
(firstChild->getOpCode().isAnd() || firstChild->getOpCode().isOr() || firstChild->getOpCode().isXor()))
{
// child must be evaluated to a register but we can still make
// use of the conditions codes that it produces
cg->evaluate(firstChild);
}
if (isConditionCodeSetForCompareToZero(firstChild, true))
{
// Nothing to do because previous instruction already set the
// condition code for the first child's register
}
else
{
int compareSize = secondChild->getSize();
if (compareSize > 1 &&
(firstChild->getOpCodeValue() == TR::b2i || firstChild->getOpCodeValue() == TR::bu2i ||
firstChild->getOpCodeValue() == TR::b2s || firstChild->getOpCodeValue() == TR::bu2s) &&
firstChild->getRegister() == NULL &&
firstChild->getReferenceCount() == 1)
{
compareSize = 1;
cg->decReferenceCount(firstChild);
firstChild = firstChild->getFirstChild();
}
if (compareSize > 2 &&
(firstChild->getOpCodeValue() == TR::su2i || firstChild->getOpCodeValue() == TR::s2i) &&
firstChild->getRegister() == NULL &&
firstChild->getReferenceCount() == 1)
{
compareSize = 2;
cg->decReferenceCount(firstChild);
firstChild = firstChild->getFirstChild();
}
if (firstChild->getOpCode().isMemoryReference() &&
firstChild->getRegister() == NULL &&
firstChild->getReferenceCount() == 1)
{
TR::MemoryReference *tempMR = generateX86MemoryReference(firstChild, cg);
if (compareSize == 1)
generateMemImmInstruction(CMP1MemImm1, node, tempMR, 0, cg);
else if (compareSize == 2)
{
//shouldn't use Imm2 instructions
TR::Register *tempReg = cg->allocateRegister();
TR::TreeEvaluator::loadConstant(node, 0, TR_RematerializableShort, cg, tempReg);
generateMemRegInstruction(CMP2MemReg, node, tempMR, tempReg, cg);
cg->stopUsingRegister(tempReg);
}
else
TR::TreeEvaluator::compareGPMemoryToImmediate(node, tempMR, 0, cg);
tempMR->decNodeReferenceCounts(cg);
}
else
{
TR::Register *firstChildReg = cg->evaluate(firstChild);
if (isConditionCodeSetForCompareToZero(firstChild, true))
{
// Nothing to do because the evaluation of firstChild set the
// condition codes for us
}
else
{
if (compareSize == 1)
generateRegRegInstruction(TEST1RegReg, node, firstChildReg, firstChildReg, cg);
else if (compareSize == 2)
generateRegRegInstruction(TEST2RegReg, node, firstChildReg, firstChildReg, cg);
else
TR::TreeEvaluator::compareGPRegisterToImmediateForEquality(node, firstChildReg, 0, cg);
}
}
}
}
}
else
{
int compareSize = secondChild->getSize();
if (compareSize > 1 &&
(firstChild->getOpCodeValue() == TR::b2i || firstChild->getOpCodeValue() == TR::bu2i ||
firstChild->getOpCodeValue() == TR::b2s || firstChild->getOpCodeValue() == TR::bu2s) &&
firstChild->getRegister() == NULL &&
firstChild->getReferenceCount() == 1)
{
compareSize = 1;
cg->decReferenceCount(firstChild);
firstChild = firstChild->getFirstChild();
}
if (compareSize > 2 &&
(firstChild->getOpCodeValue() == TR::su2i || firstChild->getOpCodeValue() == TR::s2i) &&
firstChild->getRegister() == NULL &&
firstChild->getReferenceCount() == 1)
{
compareSize = 2;
cg->decReferenceCount(firstChild);
firstChild = firstChild->getFirstChild();
}
if (firstChild->getOpCode().isMemoryReference() &&
firstChild->getRegister() == NULL &&
firstChild->getReferenceCount() == 1)
{
TR::MemoryReference *tempMR = generateX86MemoryReference(firstChild, cg);
if (compareSize == 1)
generateMemImmInstruction(CMP1MemImm1, node, tempMR, constValue, cg);
else if (compareSize == 2)
{
//shouldn't use Imm2 instructions
TR::Register *tempReg = cg->allocateRegister();
TR::TreeEvaluator::loadConstant(node, constValue, TR_RematerializableShort, cg, tempReg);
generateMemRegInstruction(CMP2MemReg, node, tempMR, tempReg, cg);
cg->stopUsingRegister(tempReg);
}
else
TR::TreeEvaluator::compareGPMemoryToImmediate(node, tempMR, constValue, cg);
tempMR->decNodeReferenceCounts(cg);
}
else
{
TR::Register *firstChildReg = cg->evaluate(firstChild);
if (compareSize == 1)
generateRegImmInstruction(CMP1RegImm1, node, firstChildReg, constValue, cg);
else if (compareSize == 2)
{
///generateRegImmInstruction(CMP2RegImm2, node, firstChildReg, constValue, cg);
generateWiderCompare(node, firstChildReg, constValue, cg);
}
else
TR::TreeEvaluator::compareGPRegisterToImmediateForEquality(node, firstChildReg, constValue, cg);
}
if (secondChild->getOpCodeValue() == TR::aconst)
{
TR_ASSERT(!secondChild->isClassPointerConstant(), "Class pointer constant unexpected here\n");
TR_ASSERT(!secondChild->isMethodPointerConstant(), "Method pointer constant unexpected here\n");
}
}
}
else
{
TR::Instruction *cmpInstruction = NULL;
uint32_t size = secondChild->getSize();
TR::Register *firstChildReg = cg->evaluate(firstChild);
if(size == 1)
cmpInstruction = generateRegImmInstruction(CMP1RegImm1, node, firstChildReg, constValue, cg);
else if (size == 2)
{
///cmpInstruction = generateRegImmInstruction(CMP2RegImm2, node, firstChildReg, constValue, cg);
cmpInstruction = generateWiderCompare(node, firstChildReg, constValue, cg);
}
else
{
cmpInstruction = generateRegImmInstruction(CMPRegImm4(is64Bit), node, firstChildReg, constValue, cg);
}
TR::Symbol *symbol = NULL;
if (node && secondChild->getOpCode().hasSymbolReference())
symbol = secondChild->getSymbol();
bool isPICCandidate = symbol ? symbol->isStatic() && symbol->isClassObject() : false;
if (isPICCandidate && cg->wantToPatchClassPointer((TR_OpaqueClassBlock*)constValue, secondChild))
comp->getStaticHCRPICSites()->push_front(cmpInstruction);
if (secondChild->getOpCodeValue() == TR::aconst)
{
if (secondChild->isClassPointerConstant())
{
if (cg->profiledPointersRequireRelocation())
TR::TreeEvaluator::setupProfiledGuardRelocation((TR::X86RegImmInstruction *)cmpInstruction, node, TR_ClassPointer);
if (cg->fe()->isUnloadAssumptionRequired((TR_OpaqueClassBlock *) secondChild->getAddress(), comp->getCurrentMethod()) || cg->profiledPointersRequireRelocation())
comp->getStaticPICSites()->push_front(cmpInstruction);
}
if (secondChild->isMethodPointerConstant())
{
if (cg->profiledPointersRequireRelocation())
TR::TreeEvaluator::setupProfiledGuardRelocation((TR::X86RegImmInstruction *)cmpInstruction, node, TR_MethodPointer);
if (cg->fe()->isUnloadAssumptionRequired(cg->fe()->createResolvedMethod(cg->trMemory(), (TR_OpaqueMethodBlock *) secondChild->getAddress(), comp->getCurrentMethod())->classOfMethod(), comp->getCurrentMethod()) || cg->profiledPointersRequireRelocation())
comp->getStaticMethodPICSites()->push_front(cmpInstruction);
}
}
}
cg->decReferenceCount(firstChild);
cg->decReferenceCount(secondChild);
}
else
{
TR_X86CompareAnalyser temp(cg);
// If we are comparing class addresses, check whether the header is compressed on 64-bit
// to do the right size of compare.
//
TR::Node *firstChild = node->getFirstChild();
if (cg->comp()->target().is64Bit() && TR::Compiler->om.generateCompressedObjectHeaders())
{
if ( (firstChild->getOpCode().isLoadIndirect()
&& firstChild->getSymbolReference() == comp->getSymRefTab()->findVftSymbolRef())
|| (secondChild->getOpCode().isLoadIndirect()
&& secondChild->getSymbolReference() == comp->getSymRefTab()->findVftSymbolRef()))
{
is64Bit = false;
}
}
uint32_t size = firstChild->getSize();
TR::InstOpCode::Mnemonic cmpRRInstr, cmpRMInstr, cmpMRInstr;
if(size == 1)
{
cmpRRInstr = CMP1RegReg;
cmpRMInstr = CMP1RegMem;
cmpMRInstr = CMP1MemReg;
}
else if(size == 2)
{
cmpRRInstr = CMP2RegReg;
cmpRMInstr = CMP2RegMem;
cmpMRInstr = CMP2MemReg;
}
else
{
cmpRRInstr = CMPRegReg(is64Bit);
cmpRMInstr = CMPRegMem(is64Bit);
cmpMRInstr = CMPMemReg(is64Bit);