/
OMRInstruction.cpp
2444 lines (2158 loc) · 82.3 KB
/
OMRInstruction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at http://eclipse.org/legal/epl-2.0
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
//On zOS XLC linker can't handle files with same name at link time
//This workaround with pragma is needed. What this does is essentially
//give a different name to the codesection (csect) for this file. So it
//doesn't conflict with another file with same name.
#pragma csect(CODE,"OMRZInstBase#C")
#pragma csect(STATIC,"OMRZInstBase#S")
#pragma csect(TEST,"OMRZInstBase#T")
#include "codegen/Instruction.hpp"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "codegen/CodeGenPhase.hpp"
#include "codegen/CodeGenerator.hpp"
#include "codegen/ConstantDataSnippet.hpp"
#include "codegen/FrontEnd.hpp"
#include "codegen/InstOpCode.hpp"
#include "codegen/Instruction.hpp"
#include "codegen/Linkage.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/RegisterPair.hpp"
#include "compile/Compilation.hpp"
#include "compile/Method.hpp"
#include "compile/SymbolReferenceTable.hpp"
#include "control/Options.hpp"
#include "control/Options_inlines.hpp"
#include "cs2/arrayof.h"
#include "cs2/hashtab.h"
#include "env/CompilerEnv.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/Node.hpp"
#include "il/Node_inlines.hpp"
#include "il/Symbol.hpp"
#include "il/SymbolReference.hpp"
#include "il/TreeTop.hpp"
#include "il/TreeTop_inlines.hpp"
#include "il/symbol/LabelSymbol.hpp"
#include "il/symbol/ResolvedMethodSymbol.hpp"
#include "il/symbol/StaticSymbol.hpp"
#include "infra/Assert.hpp"
#include "infra/deque.hpp"
#include "ras/Debug.hpp"
#include "z/codegen/S390Instruction.hpp"
#include "z/codegen/S390OutOfLineCodeSection.hpp"
#include "env/TRMemory.hpp"
#define CTOR_INITIALIZER_LIST _conditions(NULL), _operands(NULL), _useRegs(NULL), _defRegs(NULL), \
_sourceUsedInMemoryReference(NULL), _flags(0), _longDispSpillReg1(NULL), _longDispSpillReg2(NULL), _binFreeRegs(0), \
_targetRegSize(0), _sourceRegSize(0), _sourceMemSize(0), _targetMemSize(0), _sourceStart(-1), _targetStart(-1)
OMR::Z::Instruction::Instruction(TR::CodeGenerator* cg, TR::InstOpCode::Mnemonic op, TR::Node* node)
:
OMR::Instruction(cg, op, node),
CTOR_INITIALIZER_LIST
{
TR_ASSERT(cg->getS390ProcessorInfo()->supportsArch(_opcode.getMinimumALS()), "Processor detected does not support instruction %s\n", _opcode.getMnemonicName());
self()->initialize();
}
OMR::Z::Instruction::Instruction(TR::CodeGenerator*cg, TR::Instruction* precedingInstruction, TR::InstOpCode::Mnemonic op, TR::Node* node)
:
OMR::Instruction(cg, precedingInstruction, op, node),
CTOR_INITIALIZER_LIST
{
TR_ASSERT(cg->getS390ProcessorInfo()->supportsArch(_opcode.getMinimumALS()), "Processor detected does not support instruction %s\n", _opcode.getMnemonicName());
self()->initialize(precedingInstruction, true);
}
TR::RegisterDependencyConditions *
OMR::Z::Instruction::setDependencyConditionsNoBookKeeping(TR::RegisterDependencyConditions *cond)
{
// If register dependency conditions exist, merge with the argument conditions
if( _conditions )
{
TR::RegisterDependencyConditions * n = new (self()->cg()->trHeapMemory()) TR::RegisterDependencyConditions( _conditions, cond, self()->cg() );
_conditions = 0;
cond = n;
}
return _conditions = cond;
}
TR::RegisterDependencyConditions *
OMR::Z::Instruction::setDependencyConditions(TR::RegisterDependencyConditions *cond)
{
TR_ASSERT(cond, "NULL S390 register dependency conditions");
int32_t oldPreAddCursor = 0;
int32_t oldPostAddCursor = 0;
// If register dependency conditions exist, merge with the argument conditions
if (_conditions)
{
oldPreAddCursor = _conditions->getAddCursorForPre();
oldPostAddCursor = _conditions->getAddCursorForPost();
TR::RegisterDependencyConditions * n = new (self()->cg()->trHeapMemory()) TR::RegisterDependencyConditions( _conditions, cond, self()->cg() );
_conditions = 0;
cond = n;
}
// Perform book keeping only on argument conditions
cond->bookKeepingRegisterUses(self(), self()->cg(), oldPreAddCursor, oldPostAddCursor);
cond->setIsUsed();
if(cond->getPreConditions()) cond->getPreConditions()->incNumUses();
if(cond->getPostConditions()) cond->getPostConditions()->incNumUses();
return _conditions = cond;
}
TR::RegisterDependencyConditions *
OMR::Z::Instruction::updateDependencyConditions(TR::RegisterDependencyConditions *cond)
{
int32_t currentPreCursor = 0;
int32_t currentPostCursor = 0;
if (_conditions)
{
currentPreCursor = _conditions->getAddCursorForPre();
currentPostCursor = _conditions->getAddCursorForPost();
}
cond->bookKeepingRegisterUses(self(), self()->cg(), currentPreCursor, currentPostCursor);
cond->setIsUsed();
return _conditions = cond;
}
void
OMR::Z::Instruction::initialize(TR::Instruction * precedingInstruction, bool instFlag, TR::RegisterDependencyConditions * cond, bool condFlag)
{
//call other initializer first
self()->initialize(cond);
TR::CodeGenerator * cg = OMR::Instruction::cg();
self()->setBlockIndex(cg->getCurrentBlockIndex());
if (cond)
{
// Don't want to increment total use counts for ASSOCREGS instructions
// because their register references will confuse the code that tries
// to determine when the first use of a register takes place
if (condFlag || self()->getOpCodeValue() != TR::InstOpCode::ASSOCREGS)
{
cond->bookKeepingRegisterUses(self(), cg);
if(cond->getPreConditions()) cond->getPreConditions()->incNumUses();
if(cond->getPostConditions()) cond->getPostConditions()->incNumUses();
}
cond->setIsUsed();
}
if (cg->getCondCodeShouldBePreserved())
TR_ASSERT( (_opcode.setsOverflowFlag() || _opcode.setsZeroFlag() || _opcode.setsSignFlag() || _opcode.setsCarryFlag()) == false,
"We thought the instruction [0x%p] won't modify the CC, but it does.\n", self());
self()->readCCInfo(); // Must come before setCCInfo - Instruction reads CC before setting CC.
if (cg->comp()->getOption(TR_EnableEBBCCInfo))
{
((self()->isLabel() || self()->isCall()) || (instFlag && precedingInstruction == NULL)) ? self()->clearCCInfo() : self()->setCCInfo();
}
else
{
(!self()->isBranchOp() && (!instFlag || precedingInstruction == NULL)) ? self()->setCCInfo() : self()->clearCCInfo();
}
}
// Realistic offset estimate for the instruction.
// Applicable to frontends which does not do binary encoding (i.e., not Java or OMR).
// Therefore, we will reuse the field for the binary buffer pointer to store the binary
// offset. This way we dont use an extra 4 byte field in TR::Instruction
uint32_t
OMR::Z::Instruction::getEstimatedBinaryOffset()
{
union { uint8_t *p; uint32_t i; } _u;
_u.p = self()->getBinaryEncoding();
return _u.i;
}
void
OMR::Z::Instruction::setEstimatedBinaryOffset(uint32_t l)
{
union { uint8_t *p; uint32_t i; } _u;
_u.i = l;
self()->setBinaryEncoding(_u.p) ;
}
void
OMR::Z::Instruction::setCCInfo()
{
if (self()->getNode() == NULL)
{
// play it safe... perhaps this should be changed to an assert that the node should never be NULL?
self()->clearCCInfo();
return;
}
if (_opcode.setsCC())
{
if (self()->cg()->ccInstruction() != NULL)
{
// Check previous CC setting instruction... we can mark its usage as
// known, since the current instruction will override its CC value.
self()->cg()->ccInstruction()->setCCuseKnown();
}
if (!self()->cg()->comp()->getOption(TR_EnableEBBCCInfo))
{
if (_opcode.setsOverflowFlag() || _opcode.setsZeroFlag() || _opcode.setsSignFlag() || _opcode.setsCarryFlag())
{
// we have detailed information on the CC setting
self()->cg()->setHasCCInfo(true);
self()->cg()->setHasCCOverflow(_opcode.setsOverflowFlag() && !self()->getNode()->cannotOverflow());
self()->cg()->setHasCCZero(_opcode.setsZeroFlag());
self()->cg()->setHasCCSigned(_opcode.setsSignFlag() || _opcode.setsCompareFlag());
self()->cg()->setCCInstruction(self());
self()->cg()->setHasCCCarry(_opcode.setsCarryFlag());
}
else
{
// the CC setting is generic or
// there is no node associated with this instruction,
// play it safe and record no information
self()->cg()->setHasCCInfo(false);
}
self()->cg()->setCCInstruction(self());
}
else
{
if (_opcode.setsOverflowFlag() || _opcode.setsZeroFlag() || _opcode.setsSignFlag() || _opcode.setsCompareFlag() || _opcode.setsCarryFlag())
// track Ops that setsCompareFlag for compare ops as well, so that we may remove redudent compare ops;
// note this will not intruduce mismatching for compare immediate ops with CC from compare Ops,
// because compare immed ops looks for setsSignedFlag or setsZeroFlags - compare ops does not sets these flags
{
// we have detailed information on the CC setting
self()->cg()->setHasCCInfo(true);
self()->cg()->setHasCCOverflow(_opcode.setsOverflowFlag() && !self()->getNode()->cannotOverflow());
self()->cg()->setHasCCZero(_opcode.setsZeroFlag());
self()->cg()->setHasCCSigned(_opcode.setsSignFlag());
self()->cg()->setHasCCCompare(_opcode.setsCompareFlag());
self()->cg()->setHasCCCarry(_opcode.setsCarryFlag());
self()->cg()->setCCInstruction(self());
}
else
{
// the CC setting is generic, such as Load & Test Reg, Test Under Mask etc are not handled; or,
// there is no node associated with this instruction,
// play it safe and record no information
self()->cg()->setHasCCInfo(false);
}
self()->cg()->setCCInstruction(self());
} // if enableEBBCCInfo
}
}
void
OMR::Z::Instruction::readCCInfo()
{
if (_opcode.readsCC())
{
if (self()->cg()->ccInstruction() != NULL)
{
// Current instruction reads CC. Mark the last instruction that
// set the CC value as having its CC being used.
self()->cg()->ccInstruction()->setCCuseKnown();
self()->cg()->ccInstruction()->setCCused();
}
}
}
void
OMR::Z::Instruction::clearCCInfo()
{
self()->cg()->setHasCCInfo(false);
self()->cg()->setCCInstruction(NULL);
}
bool
OMR::Z::Instruction::matchesTargetRegister(TR::Register* reg)
{
if (reg->getRealRegister())
{
if (self()->implicitlySetsGPR0() &&
toRealRegister(reg)->getRegisterNumber() == TR::RealRegister::GPR0)
return true;
if (self()->implicitlySetsGPR1() &&
toRealRegister(reg)->getRegisterNumber() == TR::RealRegister::GPR1)
return true;
if (self()->implicitlySetsGPR2() &&
toRealRegister(reg)->getRegisterNumber() == TR::RealRegister::GPR2)
return true;
}
return false;
}
bool
OMR::Z::Instruction::matchesAnyRegister(TR::Register * reg, TR::Register * instReg)
{
if (!instReg)
return false;
TR::RegisterPair * regPair = instReg->getRegisterPair();
TR::RealRegister * realReg = NULL;
TR::RealRegister * realInstReg1 = NULL;
TR::RealRegister * realInstReg2 = NULL;
bool enableHighWordRA =
self()->cg()->supportsHighWordFacility() &&
(reg->getKind()!=TR_FPR) &&
(instReg->getKind()!=TR_FPR) &&
(reg->getKind()!=TR_VRF) &&
(instReg->getKind()!=TR_VRF);
if (enableHighWordRA && reg->getRealRegister())
{
realReg = (TR::RealRegister *)reg;
if (realReg->isHighWordRegister())
{
// Highword aliasing low word regs
realReg = realReg->getLowWordRegister();
}
}
if (regPair)
{
// if we are matching real regs
if (enableHighWordRA && regPair->getHighOrder()->getRealRegister())
{
// reg pairs do not use HPRs
realInstReg1 = (TR::RealRegister *)(regPair->getHighOrder());
realInstReg2 = toRealRegister(regPair->getLowOrder());
return realReg == realInstReg1 || realReg == realInstReg2;
}
// if we are matching virt regs
if ((reg == regPair->getHighOrder()) || reg == regPair->getLowOrder())
{
return true;
}
}
else
{
// if we are matching real regs
if (enableHighWordRA && instReg->getRealRegister())
{
realInstReg1 = (TR::RealRegister *)instReg;
return realReg == realInstReg1;
}
// if we are matching virt regs
if (reg == instReg)
{
return true;
}
}
return false;
}
bool
OMR::Z::Instruction::matchesAnyRegister(TR::Register * reg, TR::Register * instReg1, TR::Register * instReg2)
{
return self()->matchesAnyRegister(reg, instReg1) || self()->matchesAnyRegister(reg, instReg2);
}
TR::Register *
OMR::Z::Instruction::getRegForBinaryEncoding(TR::Register * reg)
{
return (reg->getRegisterPair()) ? reg->getRegisterPair()->getHighOrder() : reg;
}
void
OMR::Z::Instruction::useRegister(TR::Register * reg, bool isDummy)
{
TR_ASSERT( reg != NULL, "NULL Register encountered");
TR::RegisterPair * regPair = reg->getRegisterPair();
if (regPair)
{
TR::Register * firstRegister = regPair->getHighOrder();
TR::Register * lastRegister = regPair->getLowOrder();
TR_ASSERT( firstRegister != NULL, "NULL First Register encountered");
TR_ASSERT( lastRegister != NULL,"NULL Last Register encountered");
if (firstRegister->getRealRegister()==NULL)
{
TR_ASSERT(firstRegister->getSiblingRegister(),
"[%p] Register Pairs should be allocated using allocateConsecutivePair.\n", self());
}
if (lastRegister->getRealRegister()==NULL)
{
TR_ASSERT(lastRegister->getSiblingRegister(),
"[%p] Register Pairs should be allocated using allocateConsecutivePair.\n", self());
}
if (_opcode.is64bit() || _opcode.is32to64bit())
{
firstRegister->setIsUpperBitsAreDirty(true);
lastRegister->setIsUpperBitsAreDirty(true);
}
OMR::Instruction::useRegister(firstRegister);
OMR::Instruction::useRegister(lastRegister);
}
else
{
if (_opcode.is64bit() || _opcode.is32to64bit())
{
reg->setIsUpperBitsAreDirty(true);
}
OMR::Instruction::useRegister(reg);
// If an instruction uses a dummy register, that register should no longer be considered dummy.
// S390RegisterDependency also calls useRegister, in this case we do not want to reset the dummy status of these regs
//
if (!isDummy && reg->isPlaceholderReg())
{
reg->resetPlaceholderReg();
}
}
return;
}
bool
OMR::Z::Instruction::refsRegister(TR::Register * reg)
{
return self()->getDependencyConditions() ? self()->getDependencyConditions()->refsRegister(reg) : false;
}
bool
OMR::Z::Instruction::defsAnyRegister(TR::Register * reg)
{
TR::Register **_targetReg = self()->targetRegBase();
if (_targetReg)
{
for (int32_t i = 0; i < _targetRegSize; ++i)
{
if (_targetReg[i]->usesAnyRegister(reg))
return true;
}
}
return false;
}
bool
OMR::Z::Instruction::defsRegister(TR::Register * reg)
{
TR::Register **_targetReg = self()->targetRegBase();
if (_targetReg)
{
for (int32_t i = 0; i < _targetRegSize; ++i)
{
if (_targetReg[i]->usesRegister(reg))
return true;
}
}
return false;
}
// this may replace defsRegister
bool
OMR::Z::Instruction::isDefRegister(TR::Register * reg)
{
// Test if this is the implicitly targeted register
if (self()->matchesTargetRegister(reg))
return true;
//Test explicitly targeted register by the instruction
if (_defRegs)
{
for (int32_t i = 0; i < _defRegs->size(); ++i)
{
if ((* _defRegs) [i]->usesRegister(reg))
return true;
}
}
return false;
}
bool OMR::Z::Instruction::usesOnlyRegister(TR::Register *reg)
{
int32_t i;
TR::Register **_sourceReg = self()->sourceRegBase();
TR::MemoryReference **_sourceMem = self()->sourceMemBase();
TR::MemoryReference **_targetMem = self()->targetMemBase();
for (i = 0; i < _sourceRegSize; ++i)
{
if (_sourceReg[i]->usesRegister(reg))
return true;
}
for (i = 0; i < _sourceMemSize; ++i)
{
if (_sourceMem[i]->usesRegister(reg))
return true;
}
for (i = 0; i < _targetMemSize; ++i)
{
if (_targetMem[i]->usesRegister(reg))
return true;
}
// We might have an out of line EX instruction. If so, we
// need to check whether either the EX instruction OR the instruction
// it refers to uses the specified register.
TR::Instruction *outOfLineEXInstr = self()->getOutOfLineEXInstr();
if (outOfLineEXInstr && outOfLineEXInstr->usesOnlyRegister(reg))
return true;
return false;
}
bool
OMR::Z::Instruction::usesRegister(TR::Register * reg)
{
int32_t i;
if(self()->usesOnlyRegister(reg) || self()->defsRegister(reg))
return true;
// The only thing that is not covered by defsRegister is target registers found in EX
// We might have an out of line EX instruction. If so, we
// need to check whether either the EX instruction OR the instruction
// it refers to uses the specified register.
TR::Instruction *outOfLineEXInstr = self()->getOutOfLineEXInstr();
if (outOfLineEXInstr)
{
TR::Register **_targetReg = outOfLineEXInstr->targetRegBase();
if (_targetReg)
{
for (i = 0; i < outOfLineEXInstr->_targetRegSize; ++i)
{
if (_targetReg[i]->usesRegister(reg))
return true;
}
}
}
return false;
}
static bool isInternalControlFlowOneEntryOneExit(TR::Instruction *regionEnd, TR::Compilation *comp)
{
TR::deque<TR::LabelSymbol *> labels(comp->allocator());
TR::Instruction *curr=NULL;
TR::Instruction *regionStart=NULL;
bool done=false;
// Collect all the labels and start of region
for(curr=regionEnd;!done;curr=curr->getPrev())
{
if(curr->isLabel())
{
TR::LabelSymbol *labelSym=toS390LabelInstruction(curr)->getLabelSymbol();
labels.push_back(labelSym);
labelSym->isStartInternalControlFlow();
done=true;
regionStart=curr->getPrev();
}
}
// Now check if all branches jump to lables inside region
for(curr=regionEnd; curr != regionStart; curr=curr->getPrev())
{
if(curr->isBranchOp())
{
TR::S390LabeledInstruction *labeledInstr=(TR::S390LabeledInstruction *)curr;
TR::LabelSymbol *targetLabel=labeledInstr->getLabelSymbol();
bool found=false;
for(auto it = labels.begin(); it != labels.end(); ++it)
{
if(*it == targetLabel)
{
found = true;
break;
}
}
if(!found)
return false; // Found a branch to an outside label
}
}
return true;
}
TR::Instruction *
OMR::Z::Instruction::getOutOfLineEXInstr()
{
if (!self()->isOutOfLineEX())
return NULL;
switch(self()->getOpCodeValue())
{
case TR::InstOpCode::EX:
{
TR::MemoryReference * tempMR = ((TR::S390RXInstruction *)self())->getMemoryReference();
TR::S390ConstantInstructionSnippet * cis = (TR::S390ConstantInstructionSnippet *) tempMR->getConstantDataSnippet();
TR_ASSERT( cis != NULL, "Out of line EX instruction doesn't have a constantInstructionSnippet\n");
return cis->getInstruction();
}
case TR::InstOpCode::EXRL:
{
TR::S390ConstantInstructionSnippet * cis = (TR::S390ConstantInstructionSnippet *)
((TR::S390RILInstruction *)self())->getTargetSnippet();
TR_ASSERT( cis != NULL, "Out of line EXRL instruction doesn't have a constantInstructionSnippet\n");
return cis->getInstruction();
}
default:
return NULL;
}
}
// determine if this instruction will throw an implicit null pointer exception
// and set appropriate flags
void
OMR::Z::Instruction::setupThrowsImplicitNullPointerException(TR::Node *n, TR::MemoryReference *mr)
{
TR::Compilation *comp = self()->cg()->comp();
if(self()->cg()->getSupportsImplicitNullChecks())
{
// this instruction throws an implicit null check if:
// 1. The treetop node is a NULLCHK node
// 2. The memory reference of this instruction can cause a null pointer exception
// 3. The null check reference node must be a child of this node
// 4. This memory reference uses the same register as the null check reference
// 5. This is the first instruction in the evaluation of this null check node to have met all the conditions
// Test conditions 1, 2, and 5
if(n != NULL & mr != NULL &&
mr->getCausesImplicitNullPointerException() &&
self()->cg()->getCurrentEvaluationTreeTop()->getNode()->getOpCode().isNullCheck() &&
self()->cg()->getImplicitExceptionPoint() == NULL)
{
// determine what the NULLcheck reference node is
TR::Node * nullCheckReference;
TR::Node * firstChild = self()->cg()->getCurrentEvaluationTreeTop()->getNode()->getFirstChild();
if (comp->useCompressedPointers() &&
firstChild->getOpCodeValue() == TR::l2a)
{
TR::ILOpCodes loadOp = comp->il.opCodeForIndirectLoad(TR::Int32);
TR::ILOpCodes rdbarOp = comp->il.opCodeForIndirectReadBarrier(TR::Int32);
while (firstChild->getOpCodeValue() != loadOp && firstChild->getOpCodeValue() != rdbarOp)
firstChild = firstChild->getFirstChild();
nullCheckReference = firstChild->getFirstChild();
}
else
nullCheckReference = self()->cg()->getCurrentEvaluationTreeTop()->getNode()->getNullCheckReference();
// Test conditions 3 and 4
if ((self()->getNode()->getOpCode().hasSymbolReference() &&
self()->getNode()->getSymbolReference() == comp->getSymRefTab()->findVftSymbolRef()) ||
(n->hasChild(nullCheckReference) && mr->usesRegister(nullCheckReference->getRegister())))
{
traceMsg(comp,"Instruction %p throws an implicit NPE, node: %p NPE node: %p\n", self(), n, nullCheckReference);
self()->cg()->setImplicitExceptionPoint(self());
}
}
}
}
// The following safe virtual downcast method is only used in an assertion
// check within "toS390ImmInstruction"
#if defined(DEBUG) || defined(PROD_WITH_ASSUMES)
TR::S390ImmInstruction *
OMR::Z::Instruction::getS390ImmInstruction()
{
return NULL;
}
#endif
uint8_t *
OMR::Z::Instruction::generateBinaryEncoding()
{
uint8_t * instructionStart = self()->cg()->getBinaryBufferCursor();
uint8_t * cursor = instructionStart;
memset( (void*)cursor,0,self()->getEstimatedBinaryLength());
self()->getOpCode().copyBinaryToBuffer(instructionStart);
self()->setBinaryLength(self()->getOpCode().getInstructionLength());
self()->setBinaryEncoding(instructionStart);
self()->cg()->addAccumulatedInstructionLengthError(self()->getEstimatedBinaryLength() - self()->getBinaryLength());
return cursor;
}
int32_t
OMR::Z::Instruction::estimateBinaryLength(int32_t currentEstimate)
{
self()->setEstimatedBinaryLength(self()->getOpCode().getInstructionLength());
return currentEstimate + self()->getEstimatedBinaryLength();
}
bool
OMR::Z::Instruction::dependencyRefsRegister(TR::Register * reg)
{
return false;
}
void
OMR::Z::Instruction::assignRegisters(TR_RegisterKinds kindToBeAssigned)
{
TR::Compilation *comp = self()->cg()->comp();
if (self()->getOpCodeValue() != TR::InstOpCode::ASSOCREGS)
{
self()->assignRegistersAndDependencies(kindToBeAssigned);
}
else if (self()->cg()->enableRegisterAssociations())
{
//if (kindToBeAssigned == TR_GPR)
{
TR::Machine * machine = self()->cg()->machine();
int32_t first = TR::RealRegister::FirstGPR;
int32_t last = TR::RealRegister::LastAssignableVRF;
// Step 1 : First traverse the existing associations and remove them so that they don't interfere with the new ones
for (int32_t i = first; i <= last; ++i)
{
TR::Register * virtReg = machine->getVirtualAssociatedWithReal((TR::RealRegister::RegNum) (i));
if (virtReg)
{
virtReg->setAssociation(TR::RealRegister::NoReg);
}
}
if (self()->cg()->supportsHighWordFacility())
{
for (int32_t i = TR::RealRegister::FirstHPR; i <= TR::RealRegister::LastHPR; i++)
{
TR::Register * virtReg = machine->getVirtualAssociatedWithReal((TR::RealRegister::RegNum) (i));
if (virtReg)
{
virtReg->setAssociation(TR::RealRegister::NoReg);
}
}
}
// Step 2 : loop through and set up the new associations (both on the machine and by associating the virtual
// registers with their real dependencies)
TR_S390RegisterDependencyGroup * depGroup = self()->getDependencyConditions()->getPostConditions();
for (int32_t j = 0; j < last; ++j)
{
TR::Register * virtReg = depGroup->getRegisterDependency(j)->getRegister();
machine->setVirtualAssociatedWithReal((TR::RealRegister::RegNum) (j + 1), virtReg);
}
if(self()->cg()->supportsHighWordFacility())
{
for (int32_t j = 0; j < TR::RealRegister::LastHPR-TR::RealRegister::FirstHPR; ++j)
{
TR::Register * virtReg = depGroup->getRegisterDependency(j+last)->getRegister();
machine->setVirtualAssociatedWithReal((TR::RealRegister::RegNum) (j + TR::RealRegister::FirstHPR), virtReg);
}
}
machine->setRegisterWeightsFromAssociations();
}
}
// If we are register assigning an EX or EXRL instruction with the isOutOfLineEX flag set
// we must register assign the instruction object hanging off of the memory reference's constantInstructionSnippet
TR::Instruction *outOfLineEXInstr = self()->getOutOfLineEXInstr();
if (outOfLineEXInstr)
{
TR::Instruction *savePrev = outOfLineEXInstr->getPrev();
outOfLineEXInstr->setPrev(self()->getPrev()); // Temporarily set Prev() instruction of snippet to Prev() of EX just in case we insert LR_move on assignRegisters()
self()->cg()->tracePreRAInstruction(outOfLineEXInstr);
self()->cg()->setCurrentBlockIndex(outOfLineEXInstr->getBlockIndex());
outOfLineEXInstr->assignRegisters(kindToBeAssigned);
TR::RegisterDependencyConditions *deps = outOfLineEXInstr->getDependencyConditions();
if (deps) // merge the dependency into the EX deps
{
outOfLineEXInstr->resetDependencyConditions();
TR::RegisterDependencyConditions * exDeps = (self())->getDependencyConditions();
TR::RegisterDependencyConditions * newDeps = new (self()->cg()->trHeapMemory()) TR::RegisterDependencyConditions(deps, exDeps, self()->cg());
(self())->setDependencyConditionsNoBookKeeping(newDeps);
}
outOfLineEXInstr->setPrev(savePrev); // Restore Prev() of snippet
self()->cg()->tracePostRAInstruction(outOfLineEXInstr);
//Java and other languages probably need the value field to be set to the binary encoding of the instruction
//This can be done any time after register assignment
//If the value is set here, the snippet can be handled in an identical fashion to a normal constantdataSnippet
}
// trace assigned registers bit vector
TR_Debug * debugObj = self()->cg()->getDebug();
if (debugObj && comp->getOption(TR_TraceRegisterState))
{
// determine which GPRs are assigned and the total number of available GPRs
uint32_t freeRegs = self()->getBinLocalFreeRegs();
uint32_t numRegs = TR::RealRegister::LastAssignableGPR - TR::RealRegister::FirstGPR + 1;
// print freeRegs as a binary number
char * strFreeRegs = (char *)self()->cg()->trMemory()->allocateHeapMemory(numRegs+1);
for(uint32_t i = 0; i < numRegs; i++)
{
strFreeRegs[numRegs - i - 1] = (freeRegs & 0x1) ? '1' : '0';
freeRegs >>= 1;
}
strFreeRegs[numRegs] = '\0';
// format the comment nicely and append it as a comment to the instruction
char *commentTemplate = "RegState=[%s]";
char *comment = (char *)self()->cg()->trMemory()->allocateHeapMemory(strlen(commentTemplate)+strlen(strFreeRegs)-1);
if (comment != NULL)
{
sprintf(comment, commentTemplate, strFreeRegs);
debugObj->addInstructionComment(self(),comment);
}
}
// Modify TBEGIN/TBEGINC's General Register Save Mask (GRSM) to only include
// live registers.
if (self()->getOpCodeValue() == TR::InstOpCode::TBEGIN || self()->getOpCodeValue() == TR::InstOpCode::TBEGINC)
{
uint8_t linkageBasedSaveMask = 0;
for (int32_t i = TR::RealRegister::GPR0; i != TR::RealRegister::GPR15 + 1; i++)
{
if (0 != self()->cg()->getS390Linkage()->getPreserved((TR::RealRegister::RegNum)i))
{
//linkageBasedSaveMask is 8 bit mask where each bit represents consecutive even/odd regpair to save.
linkageBasedSaveMask |= (1 << (7 - ((i - 1) >> 1))); // bit 0 = GPR0/1, GPR0=1, GPR15=16. 'Or' with bit [(i-1)>>1]
}
}
// General Register Save Mask (GRSM)
uint8_t grsm = (self()->cg()->machine()->genBitVectOfLiveGPRPairs() | linkageBasedSaveMask);
// GRSM occupies the top 8 bits of the immediate field. Need to
// preserve the lower 8 bits, which has controls for AR, Floating
// Point and Program Interruption Filtering.
uint16_t originalGRSM = ((TR::S390SILInstruction*)self())->getSourceImmediate();
((TR::S390SILInstruction*)self())->setSourceImmediate((grsm << 8) | (originalGRSM & 0xFF));
}
}
///// registerSets utilities for generic register assignment
bool
OMR::Z::Instruction::isHPRUpgradable(uint16_t operandNumber)
{
if (_opcode.is64bit())
return false;
switch (self()->getOpCodeValue())
{
case TR::InstOpCode::AR:
case TR::InstOpCode::ALR:
//AHHHR, AHHLR,
//ALHHHR, ALHHLR
//Target Reg cannot be low word
if (operandNumber == 0) return true;
break;
case TR::InstOpCode::AHI:
//AIH
return true;
break;
case TR::InstOpCode::BRCT:
//BRCTH
return true;
break;
case TR::InstOpCode::CR:
case TR::InstOpCode::CLR:
//CHHR, CHLR
//CLHHR, CLHLR
if (operandNumber == 0)
return true;
break;
case TR::InstOpCode::C:
case TR::InstOpCode::CY:
case TR::InstOpCode::CL:
case TR::InstOpCode::CLY:
//CHF, CIH
if (operandNumber == 0) return true;
break;
case TR::InstOpCode::CFI:
case TR::InstOpCode::CLFI:
//CLHF, CLIH
return true;
break;
case TR::InstOpCode::LB:
case TR::InstOpCode::LH:
case TR::InstOpCode::LHY:
case TR::InstOpCode::L:
case TR::InstOpCode::LY:
case TR::InstOpCode::LLC:
case TR::InstOpCode::LLH:
//LBH LHH LFH LLCH LLHH
if (operandNumber == 0) return true;
break;
case TR::InstOpCode::LR:
case TR::InstOpCode::LLCR:
case TR::InstOpCode::LLHR:
//LHHR, LHLR, LLHFR
//LLCHHR, LLCHLR, LLCLHR
//LLHHR, LLHHLR, LLHLHR
return true;
break;
case TR::InstOpCode::LHI:
//IIHF need to manually sign-extend
return true;
break;
/* --- RNSBG etc are cracked, not worth it
case TR::InstOpCode::NR:
case TR::InstOpCode::OR:
case TR::InstOpCode::XR:
//NHHR, NHLR, NLHR
//OHHR, OHLR, OLHR
//XHHR, XHLR, XLHR
return true;
break;
*/
case TR::InstOpCode::ST:
case TR::InstOpCode::STY:
case TR::InstOpCode::STC:
case TR::InstOpCode::STCY:
case TR::InstOpCode::STH:
case TR::InstOpCode::STHY:
//STFH, STCH, STHH
if (operandNumber == 0) return true;
break;
case TR::InstOpCode::SR:
case TR::InstOpCode::SLR:
//SHHHR, SHHLR
//SLHHHR, SLHHR
if (operandNumber == 0) return true;
break;
case TR::InstOpCode::SLFI:
//ALSIH
return true;
break;
default:
return false;
}
return false;
}
uint32_t
OMR::Z::Instruction::useSourceRegister(TR::Register * reg)
{
TR::Compilation *comp = self()->cg()->comp();
if(_sourceStart < 0)
{
if(_targetStart < 0)
_sourceStart = 0;
else
_sourceStart = _targetRegSize;
}
self()->recordOperand((void*) reg, _sourceRegSize);