This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
/
c1_LIRGenerator.cpp
3675 lines (3150 loc) · 127 KB
/
c1_LIRGenerator.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) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "c1/c1_Compilation.hpp"
#include "c1/c1_Defs.hpp"
#include "c1/c1_FrameMap.hpp"
#include "c1/c1_Instruction.hpp"
#include "c1/c1_LIRAssembler.hpp"
#include "c1/c1_LIRGenerator.hpp"
#include "c1/c1_ValueStack.hpp"
#include "ci/ciArrayKlass.hpp"
#include "ci/ciInstance.hpp"
#include "ci/ciObjArray.hpp"
#include "ci/ciUtilities.hpp"
#include "gc/shared/barrierSet.hpp"
#include "gc/shared/c1/barrierSetC1.hpp"
#include "oops/klass.inline.hpp"
#include "runtime/arguments.hpp"
#include "runtime/sharedRuntime.hpp"
#include "runtime/stubRoutines.hpp"
#include "runtime/vm_version.hpp"
#include "utilities/bitMap.inline.hpp"
#include "utilities/macros.hpp"
#ifdef ASSERT
#define __ gen()->lir(__FILE__, __LINE__)->
#else
#define __ gen()->lir()->
#endif
#ifndef PATCHED_ADDR
#define PATCHED_ADDR (max_jint)
#endif
void PhiResolverState::reset() {
_virtual_operands.clear();
_other_operands.clear();
_vreg_table.clear();
}
//--------------------------------------------------------------
// PhiResolver
// Resolves cycles:
//
// r1 := r2 becomes temp := r1
// r2 := r1 r1 := r2
// r2 := temp
// and orders moves:
//
// r2 := r3 becomes r1 := r2
// r1 := r2 r2 := r3
PhiResolver::PhiResolver(LIRGenerator* gen)
: _gen(gen)
, _state(gen->resolver_state())
, _temp(LIR_OprFact::illegalOpr)
{
// reinitialize the shared state arrays
_state.reset();
}
void PhiResolver::emit_move(LIR_Opr src, LIR_Opr dest) {
assert(src->is_valid(), "");
assert(dest->is_valid(), "");
__ move(src, dest);
}
void PhiResolver::move_temp_to(LIR_Opr dest) {
assert(_temp->is_valid(), "");
emit_move(_temp, dest);
NOT_PRODUCT(_temp = LIR_OprFact::illegalOpr);
}
void PhiResolver::move_to_temp(LIR_Opr src) {
assert(_temp->is_illegal(), "");
_temp = _gen->new_register(src->type());
emit_move(src, _temp);
}
// Traverse assignment graph in depth first order and generate moves in post order
// ie. two assignments: b := c, a := b start with node c:
// Call graph: move(NULL, c) -> move(c, b) -> move(b, a)
// Generates moves in this order: move b to a and move c to b
// ie. cycle a := b, b := a start with node a
// Call graph: move(NULL, a) -> move(a, b) -> move(b, a)
// Generates moves in this order: move b to temp, move a to b, move temp to a
void PhiResolver::move(ResolveNode* src, ResolveNode* dest) {
if (!dest->visited()) {
dest->set_visited();
for (int i = dest->no_of_destinations()-1; i >= 0; i --) {
move(dest, dest->destination_at(i));
}
} else if (!dest->start_node()) {
// cylce in graph detected
assert(_loop == NULL, "only one loop valid!");
_loop = dest;
move_to_temp(src->operand());
return;
} // else dest is a start node
if (!dest->assigned()) {
if (_loop == dest) {
move_temp_to(dest->operand());
dest->set_assigned();
} else if (src != NULL) {
emit_move(src->operand(), dest->operand());
dest->set_assigned();
}
}
}
PhiResolver::~PhiResolver() {
int i;
// resolve any cycles in moves from and to virtual registers
for (i = virtual_operands().length() - 1; i >= 0; i --) {
ResolveNode* node = virtual_operands().at(i);
if (!node->visited()) {
_loop = NULL;
move(NULL, node);
node->set_start_node();
assert(_temp->is_illegal(), "move_temp_to() call missing");
}
}
// generate move for move from non virtual register to abitrary destination
for (i = other_operands().length() - 1; i >= 0; i --) {
ResolveNode* node = other_operands().at(i);
for (int j = node->no_of_destinations() - 1; j >= 0; j --) {
emit_move(node->operand(), node->destination_at(j)->operand());
}
}
}
ResolveNode* PhiResolver::create_node(LIR_Opr opr, bool source) {
ResolveNode* node;
if (opr->is_virtual()) {
int vreg_num = opr->vreg_number();
node = vreg_table().at_grow(vreg_num, NULL);
assert(node == NULL || node->operand() == opr, "");
if (node == NULL) {
node = new ResolveNode(opr);
vreg_table().at_put(vreg_num, node);
}
// Make sure that all virtual operands show up in the list when
// they are used as the source of a move.
if (source && !virtual_operands().contains(node)) {
virtual_operands().append(node);
}
} else {
assert(source, "");
node = new ResolveNode(opr);
other_operands().append(node);
}
return node;
}
void PhiResolver::move(LIR_Opr src, LIR_Opr dest) {
assert(dest->is_virtual(), "");
// tty->print("move "); src->print(); tty->print(" to "); dest->print(); tty->cr();
assert(src->is_valid(), "");
assert(dest->is_valid(), "");
ResolveNode* source = source_node(src);
source->append(destination_node(dest));
}
//--------------------------------------------------------------
// LIRItem
void LIRItem::set_result(LIR_Opr opr) {
assert(value()->operand()->is_illegal() || value()->operand()->is_constant(), "operand should never change");
value()->set_operand(opr);
if (opr->is_virtual()) {
_gen->_instruction_for_operand.at_put_grow(opr->vreg_number(), value(), NULL);
}
_result = opr;
}
void LIRItem::load_item() {
if (result()->is_illegal()) {
// update the items result
_result = value()->operand();
}
if (!result()->is_register()) {
LIR_Opr reg = _gen->new_register(value()->type());
__ move(result(), reg);
if (result()->is_constant()) {
_result = reg;
} else {
set_result(reg);
}
}
}
void LIRItem::load_for_store(BasicType type) {
if (_gen->can_store_as_constant(value(), type)) {
_result = value()->operand();
if (!_result->is_constant()) {
_result = LIR_OprFact::value_type(value()->type());
}
} else if (type == T_BYTE || type == T_BOOLEAN) {
load_byte_item();
} else {
load_item();
}
}
void LIRItem::load_item_force(LIR_Opr reg) {
LIR_Opr r = result();
if (r != reg) {
#if !defined(ARM) && !defined(E500V2)
if (r->type() != reg->type()) {
// moves between different types need an intervening spill slot
r = _gen->force_to_spill(r, reg->type());
}
#endif
__ move(r, reg);
_result = reg;
}
}
ciObject* LIRItem::get_jobject_constant() const {
ObjectType* oc = type()->as_ObjectType();
if (oc) {
return oc->constant_value();
}
return NULL;
}
jint LIRItem::get_jint_constant() const {
assert(is_constant() && value() != NULL, "");
assert(type()->as_IntConstant() != NULL, "type check");
return type()->as_IntConstant()->value();
}
jint LIRItem::get_address_constant() const {
assert(is_constant() && value() != NULL, "");
assert(type()->as_AddressConstant() != NULL, "type check");
return type()->as_AddressConstant()->value();
}
jfloat LIRItem::get_jfloat_constant() const {
assert(is_constant() && value() != NULL, "");
assert(type()->as_FloatConstant() != NULL, "type check");
return type()->as_FloatConstant()->value();
}
jdouble LIRItem::get_jdouble_constant() const {
assert(is_constant() && value() != NULL, "");
assert(type()->as_DoubleConstant() != NULL, "type check");
return type()->as_DoubleConstant()->value();
}
jlong LIRItem::get_jlong_constant() const {
assert(is_constant() && value() != NULL, "");
assert(type()->as_LongConstant() != NULL, "type check");
return type()->as_LongConstant()->value();
}
//--------------------------------------------------------------
void LIRGenerator::block_do_prolog(BlockBegin* block) {
#ifndef PRODUCT
if (PrintIRWithLIR) {
block->print();
}
#endif
// set up the list of LIR instructions
assert(block->lir() == NULL, "LIR list already computed for this block");
_lir = new LIR_List(compilation(), block);
block->set_lir(_lir);
__ branch_destination(block->label());
if (LIRTraceExecution &&
Compilation::current()->hir()->start()->block_id() != block->block_id() &&
!block->is_set(BlockBegin::exception_entry_flag)) {
assert(block->lir()->instructions_list()->length() == 1, "should come right after br_dst");
trace_block_entry(block);
}
}
void LIRGenerator::block_do_epilog(BlockBegin* block) {
#ifndef PRODUCT
if (PrintIRWithLIR) {
tty->cr();
}
#endif
// LIR_Opr for unpinned constants shouldn't be referenced by other
// blocks so clear them out after processing the block.
for (int i = 0; i < _unpinned_constants.length(); i++) {
_unpinned_constants.at(i)->clear_operand();
}
_unpinned_constants.trunc_to(0);
// clear our any registers for other local constants
_constants.trunc_to(0);
_reg_for_constants.trunc_to(0);
}
void LIRGenerator::block_do(BlockBegin* block) {
CHECK_BAILOUT();
block_do_prolog(block);
set_block(block);
for (Instruction* instr = block; instr != NULL; instr = instr->next()) {
if (instr->is_pinned()) do_root(instr);
}
set_block(NULL);
block_do_epilog(block);
}
//-------------------------LIRGenerator-----------------------------
// This is where the tree-walk starts; instr must be root;
void LIRGenerator::do_root(Value instr) {
CHECK_BAILOUT();
InstructionMark im(compilation(), instr);
assert(instr->is_pinned(), "use only with roots");
assert(instr->subst() == instr, "shouldn't have missed substitution");
instr->visit(this);
assert(!instr->has_uses() || instr->operand()->is_valid() ||
instr->as_Constant() != NULL || bailed_out(), "invalid item set");
}
// This is called for each node in tree; the walk stops if a root is reached
void LIRGenerator::walk(Value instr) {
InstructionMark im(compilation(), instr);
//stop walk when encounter a root
if ((instr->is_pinned() && instr->as_Phi() == NULL) || instr->operand()->is_valid()) {
assert(instr->operand() != LIR_OprFact::illegalOpr || instr->as_Constant() != NULL, "this root has not yet been visited");
} else {
assert(instr->subst() == instr, "shouldn't have missed substitution");
instr->visit(this);
// assert(instr->use_count() > 0 || instr->as_Phi() != NULL, "leaf instruction must have a use");
}
}
CodeEmitInfo* LIRGenerator::state_for(Instruction* x, ValueStack* state, bool ignore_xhandler) {
assert(state != NULL, "state must be defined");
#ifndef PRODUCT
state->verify();
#endif
ValueStack* s = state;
for_each_state(s) {
if (s->kind() == ValueStack::EmptyExceptionState) {
assert(s->stack_size() == 0 && s->locals_size() == 0 && (s->locks_size() == 0 || s->locks_size() == 1), "state must be empty");
continue;
}
int index;
Value value;
for_each_stack_value(s, index, value) {
assert(value->subst() == value, "missed substitution");
if (!value->is_pinned() && value->as_Constant() == NULL && value->as_Local() == NULL) {
walk(value);
assert(value->operand()->is_valid(), "must be evaluated now");
}
}
int bci = s->bci();
IRScope* scope = s->scope();
ciMethod* method = scope->method();
MethodLivenessResult liveness = method->liveness_at_bci(bci);
if (bci == SynchronizationEntryBCI) {
if (x->as_ExceptionObject() || x->as_Throw()) {
// all locals are dead on exit from the synthetic unlocker
liveness.clear();
} else {
assert(x->as_MonitorEnter() || x->as_ProfileInvoke(), "only other cases are MonitorEnter and ProfileInvoke");
}
}
if (!liveness.is_valid()) {
// Degenerate or breakpointed method.
bailout("Degenerate or breakpointed method");
} else {
assert((int)liveness.size() == s->locals_size(), "error in use of liveness");
for_each_local_value(s, index, value) {
assert(value->subst() == value, "missed substition");
if (liveness.at(index) && !value->type()->is_illegal()) {
if (!value->is_pinned() && value->as_Constant() == NULL && value->as_Local() == NULL) {
walk(value);
assert(value->operand()->is_valid(), "must be evaluated now");
}
} else {
// NULL out this local so that linear scan can assume that all non-NULL values are live.
s->invalidate_local(index);
}
}
}
}
return new CodeEmitInfo(state, ignore_xhandler ? NULL : x->exception_handlers(), x->check_flag(Instruction::DeoptimizeOnException));
}
CodeEmitInfo* LIRGenerator::state_for(Instruction* x) {
return state_for(x, x->exception_state());
}
void LIRGenerator::klass2reg_with_patching(LIR_Opr r, ciMetadata* obj, CodeEmitInfo* info, bool need_resolve) {
/* C2 relies on constant pool entries being resolved (ciTypeFlow), so if TieredCompilation
* is active and the class hasn't yet been resolved we need to emit a patch that resolves
* the class. */
if ((TieredCompilation && need_resolve) || !obj->is_loaded() || PatchALot) {
assert(info != NULL, "info must be set if class is not loaded");
__ klass2reg_patch(NULL, r, info);
} else {
// no patching needed
__ metadata2reg(obj->constant_encoding(), r);
}
}
void LIRGenerator::array_range_check(LIR_Opr array, LIR_Opr index,
CodeEmitInfo* null_check_info, CodeEmitInfo* range_check_info) {
CodeStub* stub = new RangeCheckStub(range_check_info, index, array);
if (index->is_constant()) {
cmp_mem_int(lir_cond_belowEqual, array, arrayOopDesc::length_offset_in_bytes(),
index->as_jint(), null_check_info);
__ branch(lir_cond_belowEqual, T_INT, stub); // forward branch
} else {
cmp_reg_mem(lir_cond_aboveEqual, index, array,
arrayOopDesc::length_offset_in_bytes(), T_INT, null_check_info);
__ branch(lir_cond_aboveEqual, T_INT, stub); // forward branch
}
}
void LIRGenerator::nio_range_check(LIR_Opr buffer, LIR_Opr index, LIR_Opr result, CodeEmitInfo* info) {
CodeStub* stub = new RangeCheckStub(info, index);
if (index->is_constant()) {
cmp_mem_int(lir_cond_belowEqual, buffer, java_nio_Buffer::limit_offset(), index->as_jint(), info);
__ branch(lir_cond_belowEqual, T_INT, stub); // forward branch
} else {
cmp_reg_mem(lir_cond_aboveEqual, index, buffer,
java_nio_Buffer::limit_offset(), T_INT, info);
__ branch(lir_cond_aboveEqual, T_INT, stub); // forward branch
}
__ move(index, result);
}
void LIRGenerator::arithmetic_op(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, bool is_strictfp, LIR_Opr tmp_op, CodeEmitInfo* info) {
LIR_Opr result_op = result;
LIR_Opr left_op = left;
LIR_Opr right_op = right;
if (TwoOperandLIRForm && left_op != result_op) {
assert(right_op != result_op, "malformed");
__ move(left_op, result_op);
left_op = result_op;
}
switch(code) {
case Bytecodes::_dadd:
case Bytecodes::_fadd:
case Bytecodes::_ladd:
case Bytecodes::_iadd: __ add(left_op, right_op, result_op); break;
case Bytecodes::_fmul:
case Bytecodes::_lmul: __ mul(left_op, right_op, result_op); break;
case Bytecodes::_dmul:
{
if (is_strictfp) {
__ mul_strictfp(left_op, right_op, result_op, tmp_op); break;
} else {
__ mul(left_op, right_op, result_op); break;
}
}
break;
case Bytecodes::_imul:
{
bool did_strength_reduce = false;
if (right->is_constant()) {
jint c = right->as_jint();
if (c > 0 && is_power_of_2(c)) {
// do not need tmp here
__ shift_left(left_op, exact_log2(c), result_op);
did_strength_reduce = true;
} else {
did_strength_reduce = strength_reduce_multiply(left_op, c, result_op, tmp_op);
}
}
// we couldn't strength reduce so just emit the multiply
if (!did_strength_reduce) {
__ mul(left_op, right_op, result_op);
}
}
break;
case Bytecodes::_dsub:
case Bytecodes::_fsub:
case Bytecodes::_lsub:
case Bytecodes::_isub: __ sub(left_op, right_op, result_op); break;
case Bytecodes::_fdiv: __ div (left_op, right_op, result_op); break;
// ldiv and lrem are implemented with a direct runtime call
case Bytecodes::_ddiv:
{
if (is_strictfp) {
__ div_strictfp (left_op, right_op, result_op, tmp_op); break;
} else {
__ div (left_op, right_op, result_op); break;
}
}
break;
case Bytecodes::_drem:
case Bytecodes::_frem: __ rem (left_op, right_op, result_op); break;
default: ShouldNotReachHere();
}
}
void LIRGenerator::arithmetic_op_int(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, LIR_Opr tmp) {
arithmetic_op(code, result, left, right, false, tmp);
}
void LIRGenerator::arithmetic_op_long(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, CodeEmitInfo* info) {
arithmetic_op(code, result, left, right, false, LIR_OprFact::illegalOpr, info);
}
void LIRGenerator::arithmetic_op_fpu(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, bool is_strictfp, LIR_Opr tmp) {
arithmetic_op(code, result, left, right, is_strictfp, tmp);
}
void LIRGenerator::shift_op(Bytecodes::Code code, LIR_Opr result_op, LIR_Opr value, LIR_Opr count, LIR_Opr tmp) {
if (TwoOperandLIRForm && value != result_op
// Only 32bit right shifts require two operand form on S390.
S390_ONLY(&& (code == Bytecodes::_ishr || code == Bytecodes::_iushr))) {
assert(count != result_op, "malformed");
__ move(value, result_op);
value = result_op;
}
assert(count->is_constant() || count->is_register(), "must be");
switch(code) {
case Bytecodes::_ishl:
case Bytecodes::_lshl: __ shift_left(value, count, result_op, tmp); break;
case Bytecodes::_ishr:
case Bytecodes::_lshr: __ shift_right(value, count, result_op, tmp); break;
case Bytecodes::_iushr:
case Bytecodes::_lushr: __ unsigned_shift_right(value, count, result_op, tmp); break;
default: ShouldNotReachHere();
}
}
void LIRGenerator::logic_op (Bytecodes::Code code, LIR_Opr result_op, LIR_Opr left_op, LIR_Opr right_op) {
if (TwoOperandLIRForm && left_op != result_op) {
assert(right_op != result_op, "malformed");
__ move(left_op, result_op);
left_op = result_op;
}
switch(code) {
case Bytecodes::_iand:
case Bytecodes::_land: __ logical_and(left_op, right_op, result_op); break;
case Bytecodes::_ior:
case Bytecodes::_lor: __ logical_or(left_op, right_op, result_op); break;
case Bytecodes::_ixor:
case Bytecodes::_lxor: __ logical_xor(left_op, right_op, result_op); break;
default: ShouldNotReachHere();
}
}
void LIRGenerator::monitor_enter(LIR_Opr object, LIR_Opr lock, LIR_Opr hdr, LIR_Opr scratch, int monitor_no, CodeEmitInfo* info_for_exception, CodeEmitInfo* info) {
if (!GenerateSynchronizationCode) return;
// for slow path, use debug info for state after successful locking
CodeStub* slow_path = new MonitorEnterStub(object, lock, info);
__ load_stack_address_monitor(monitor_no, lock);
// for handling NullPointerException, use debug info representing just the lock stack before this monitorenter
__ lock_object(hdr, object, lock, scratch, slow_path, info_for_exception);
}
void LIRGenerator::monitor_exit(LIR_Opr object, LIR_Opr lock, LIR_Opr new_hdr, LIR_Opr scratch, int monitor_no) {
if (!GenerateSynchronizationCode) return;
// setup registers
LIR_Opr hdr = lock;
lock = new_hdr;
CodeStub* slow_path = new MonitorExitStub(lock, UseFastLocking, monitor_no);
__ load_stack_address_monitor(monitor_no, lock);
__ unlock_object(hdr, object, lock, scratch, slow_path);
}
#ifndef PRODUCT
void LIRGenerator::print_if_not_loaded(const NewInstance* new_instance) {
if (PrintNotLoaded && !new_instance->klass()->is_loaded()) {
tty->print_cr(" ###class not loaded at new bci %d", new_instance->printable_bci());
} else if (PrintNotLoaded && (TieredCompilation && new_instance->is_unresolved())) {
tty->print_cr(" ###class not resolved at new bci %d", new_instance->printable_bci());
}
}
#endif
void LIRGenerator::new_instance(LIR_Opr dst, ciInstanceKlass* klass, bool is_unresolved, LIR_Opr scratch1, LIR_Opr scratch2, LIR_Opr scratch3, LIR_Opr scratch4, LIR_Opr klass_reg, CodeEmitInfo* info) {
klass2reg_with_patching(klass_reg, klass, info, is_unresolved);
// If klass is not loaded we do not know if the klass has finalizers:
if (UseFastNewInstance && klass->is_loaded()
&& !Klass::layout_helper_needs_slow_path(klass->layout_helper())) {
Runtime1::StubID stub_id = klass->is_initialized() ? Runtime1::fast_new_instance_id : Runtime1::fast_new_instance_init_check_id;
CodeStub* slow_path = new NewInstanceStub(klass_reg, dst, klass, info, stub_id);
assert(klass->is_loaded(), "must be loaded");
// allocate space for instance
assert(klass->size_helper() >= 0, "illegal instance size");
const int instance_size = align_object_size(klass->size_helper());
__ allocate_object(dst, scratch1, scratch2, scratch3, scratch4,
oopDesc::header_size(), instance_size, klass_reg, !klass->is_initialized(), slow_path);
} else {
CodeStub* slow_path = new NewInstanceStub(klass_reg, dst, klass, info, Runtime1::new_instance_id);
__ branch(lir_cond_always, T_ILLEGAL, slow_path);
__ branch_destination(slow_path->continuation());
}
}
static bool is_constant_zero(Instruction* inst) {
IntConstant* c = inst->type()->as_IntConstant();
if (c) {
return (c->value() == 0);
}
return false;
}
static bool positive_constant(Instruction* inst) {
IntConstant* c = inst->type()->as_IntConstant();
if (c) {
return (c->value() >= 0);
}
return false;
}
static ciArrayKlass* as_array_klass(ciType* type) {
if (type != NULL && type->is_array_klass() && type->is_loaded()) {
return (ciArrayKlass*)type;
} else {
return NULL;
}
}
static ciType* phi_declared_type(Phi* phi) {
ciType* t = phi->operand_at(0)->declared_type();
if (t == NULL) {
return NULL;
}
for(int i = 1; i < phi->operand_count(); i++) {
if (t != phi->operand_at(i)->declared_type()) {
return NULL;
}
}
return t;
}
void LIRGenerator::arraycopy_helper(Intrinsic* x, int* flagsp, ciArrayKlass** expected_typep) {
Instruction* src = x->argument_at(0);
Instruction* src_pos = x->argument_at(1);
Instruction* dst = x->argument_at(2);
Instruction* dst_pos = x->argument_at(3);
Instruction* length = x->argument_at(4);
// first try to identify the likely type of the arrays involved
ciArrayKlass* expected_type = NULL;
bool is_exact = false, src_objarray = false, dst_objarray = false;
{
ciArrayKlass* src_exact_type = as_array_klass(src->exact_type());
ciArrayKlass* src_declared_type = as_array_klass(src->declared_type());
Phi* phi;
if (src_declared_type == NULL && (phi = src->as_Phi()) != NULL) {
src_declared_type = as_array_klass(phi_declared_type(phi));
}
ciArrayKlass* dst_exact_type = as_array_klass(dst->exact_type());
ciArrayKlass* dst_declared_type = as_array_klass(dst->declared_type());
if (dst_declared_type == NULL && (phi = dst->as_Phi()) != NULL) {
dst_declared_type = as_array_klass(phi_declared_type(phi));
}
if (src_exact_type != NULL && src_exact_type == dst_exact_type) {
// the types exactly match so the type is fully known
is_exact = true;
expected_type = src_exact_type;
} else if (dst_exact_type != NULL && dst_exact_type->is_obj_array_klass()) {
ciArrayKlass* dst_type = (ciArrayKlass*) dst_exact_type;
ciArrayKlass* src_type = NULL;
if (src_exact_type != NULL && src_exact_type->is_obj_array_klass()) {
src_type = (ciArrayKlass*) src_exact_type;
} else if (src_declared_type != NULL && src_declared_type->is_obj_array_klass()) {
src_type = (ciArrayKlass*) src_declared_type;
}
if (src_type != NULL) {
if (src_type->element_type()->is_subtype_of(dst_type->element_type())) {
is_exact = true;
expected_type = dst_type;
}
}
}
// at least pass along a good guess
if (expected_type == NULL) expected_type = dst_exact_type;
if (expected_type == NULL) expected_type = src_declared_type;
if (expected_type == NULL) expected_type = dst_declared_type;
src_objarray = (src_exact_type && src_exact_type->is_obj_array_klass()) || (src_declared_type && src_declared_type->is_obj_array_klass());
dst_objarray = (dst_exact_type && dst_exact_type->is_obj_array_klass()) || (dst_declared_type && dst_declared_type->is_obj_array_klass());
}
// if a probable array type has been identified, figure out if any
// of the required checks for a fast case can be elided.
int flags = LIR_OpArrayCopy::all_flags;
if (!src_objarray)
flags &= ~LIR_OpArrayCopy::src_objarray;
if (!dst_objarray)
flags &= ~LIR_OpArrayCopy::dst_objarray;
if (!x->arg_needs_null_check(0))
flags &= ~LIR_OpArrayCopy::src_null_check;
if (!x->arg_needs_null_check(2))
flags &= ~LIR_OpArrayCopy::dst_null_check;
if (expected_type != NULL) {
Value length_limit = NULL;
IfOp* ifop = length->as_IfOp();
if (ifop != NULL) {
// look for expressions like min(v, a.length) which ends up as
// x > y ? y : x or x >= y ? y : x
if ((ifop->cond() == If::gtr || ifop->cond() == If::geq) &&
ifop->x() == ifop->fval() &&
ifop->y() == ifop->tval()) {
length_limit = ifop->y();
}
}
// try to skip null checks and range checks
NewArray* src_array = src->as_NewArray();
if (src_array != NULL) {
flags &= ~LIR_OpArrayCopy::src_null_check;
if (length_limit != NULL &&
src_array->length() == length_limit &&
is_constant_zero(src_pos)) {
flags &= ~LIR_OpArrayCopy::src_range_check;
}
}
NewArray* dst_array = dst->as_NewArray();
if (dst_array != NULL) {
flags &= ~LIR_OpArrayCopy::dst_null_check;
if (length_limit != NULL &&
dst_array->length() == length_limit &&
is_constant_zero(dst_pos)) {
flags &= ~LIR_OpArrayCopy::dst_range_check;
}
}
// check from incoming constant values
if (positive_constant(src_pos))
flags &= ~LIR_OpArrayCopy::src_pos_positive_check;
if (positive_constant(dst_pos))
flags &= ~LIR_OpArrayCopy::dst_pos_positive_check;
if (positive_constant(length))
flags &= ~LIR_OpArrayCopy::length_positive_check;
// see if the range check can be elided, which might also imply
// that src or dst is non-null.
ArrayLength* al = length->as_ArrayLength();
if (al != NULL) {
if (al->array() == src) {
// it's the length of the source array
flags &= ~LIR_OpArrayCopy::length_positive_check;
flags &= ~LIR_OpArrayCopy::src_null_check;
if (is_constant_zero(src_pos))
flags &= ~LIR_OpArrayCopy::src_range_check;
}
if (al->array() == dst) {
// it's the length of the destination array
flags &= ~LIR_OpArrayCopy::length_positive_check;
flags &= ~LIR_OpArrayCopy::dst_null_check;
if (is_constant_zero(dst_pos))
flags &= ~LIR_OpArrayCopy::dst_range_check;
}
}
if (is_exact) {
flags &= ~LIR_OpArrayCopy::type_check;
}
}
IntConstant* src_int = src_pos->type()->as_IntConstant();
IntConstant* dst_int = dst_pos->type()->as_IntConstant();
if (src_int && dst_int) {
int s_offs = src_int->value();
int d_offs = dst_int->value();
if (src_int->value() >= dst_int->value()) {
flags &= ~LIR_OpArrayCopy::overlapping;
}
if (expected_type != NULL) {
BasicType t = expected_type->element_type()->basic_type();
int element_size = type2aelembytes(t);
if (((arrayOopDesc::base_offset_in_bytes(t) + s_offs * element_size) % HeapWordSize == 0) &&
((arrayOopDesc::base_offset_in_bytes(t) + d_offs * element_size) % HeapWordSize == 0)) {
flags &= ~LIR_OpArrayCopy::unaligned;
}
}
} else if (src_pos == dst_pos || is_constant_zero(dst_pos)) {
// src and dest positions are the same, or dst is zero so assume
// nonoverlapping copy.
flags &= ~LIR_OpArrayCopy::overlapping;
}
if (src == dst) {
// moving within a single array so no type checks are needed
if (flags & LIR_OpArrayCopy::type_check) {
flags &= ~LIR_OpArrayCopy::type_check;
}
}
*flagsp = flags;
*expected_typep = (ciArrayKlass*)expected_type;
}
LIR_Opr LIRGenerator::round_item(LIR_Opr opr) {
assert(opr->is_register(), "why spill if item is not register?");
if (strict_fp_requires_explicit_rounding) {
#ifdef IA32
if (UseSSE < 1 && opr->is_single_fpu()) {
LIR_Opr result = new_register(T_FLOAT);
set_vreg_flag(result, must_start_in_memory);
assert(opr->is_register(), "only a register can be spilled");
assert(opr->value_type()->is_float(), "rounding only for floats available");
__ roundfp(opr, LIR_OprFact::illegalOpr, result);
return result;
}
#else
Unimplemented();
#endif // IA32
}
return opr;
}
LIR_Opr LIRGenerator::force_to_spill(LIR_Opr value, BasicType t) {
assert(type2size[t] == type2size[value->type()],
"size mismatch: t=%s, value->type()=%s", type2name(t), type2name(value->type()));
if (!value->is_register()) {
// force into a register
LIR_Opr r = new_register(value->type());
__ move(value, r);
value = r;
}
// create a spill location
LIR_Opr tmp = new_register(t);
set_vreg_flag(tmp, LIRGenerator::must_start_in_memory);
// move from register to spill
__ move(value, tmp);
return tmp;
}
void LIRGenerator::profile_branch(If* if_instr, If::Condition cond) {
if (if_instr->should_profile()) {
ciMethod* method = if_instr->profiled_method();
assert(method != NULL, "method should be set if branch is profiled");
ciMethodData* md = method->method_data_or_null();
assert(md != NULL, "Sanity");
ciProfileData* data = md->bci_to_data(if_instr->profiled_bci());
assert(data != NULL, "must have profiling data");
assert(data->is_BranchData(), "need BranchData for two-way branches");
int taken_count_offset = md->byte_offset_of_slot(data, BranchData::taken_offset());
int not_taken_count_offset = md->byte_offset_of_slot(data, BranchData::not_taken_offset());
if (if_instr->is_swapped()) {
int t = taken_count_offset;
taken_count_offset = not_taken_count_offset;
not_taken_count_offset = t;
}
LIR_Opr md_reg = new_register(T_METADATA);
__ metadata2reg(md->constant_encoding(), md_reg);
LIR_Opr data_offset_reg = new_pointer_register();
__ cmove(lir_cond(cond),
LIR_OprFact::intptrConst(taken_count_offset),
LIR_OprFact::intptrConst(not_taken_count_offset),
data_offset_reg, as_BasicType(if_instr->x()->type()));
// MDO cells are intptr_t, so the data_reg width is arch-dependent.
LIR_Opr data_reg = new_pointer_register();
LIR_Address* data_addr = new LIR_Address(md_reg, data_offset_reg, data_reg->type());
__ move(data_addr, data_reg);
// Use leal instead of add to avoid destroying condition codes on x86
LIR_Address* fake_incr_value = new LIR_Address(data_reg, DataLayout::counter_increment, T_INT);
__ leal(LIR_OprFact::address(fake_incr_value), data_reg);
__ move(data_reg, data_addr);
}
}
// Phi technique:
// This is about passing live values from one basic block to the other.
// In code generated with Java it is rather rare that more than one
// value is on the stack from one basic block to the other.
// We optimize our technique for efficient passing of one value
// (of type long, int, double..) but it can be extended.
// When entering or leaving a basic block, all registers and all spill
// slots are release and empty. We use the released registers
// and spill slots to pass the live values from one block
// to the other. The topmost value, i.e., the value on TOS of expression
// stack is passed in registers. All other values are stored in spilling
// area. Every Phi has an index which designates its spill slot
// At exit of a basic block, we fill the register(s) and spill slots.
// At entry of a basic block, the block_prolog sets up the content of phi nodes
// and locks necessary registers and spilling slots.
// move current value to referenced phi function
void LIRGenerator::move_to_phi(PhiResolver* resolver, Value cur_val, Value sux_val) {
Phi* phi = sux_val->as_Phi();
// cur_val can be null without phi being null in conjunction with inlining
if (phi != NULL && cur_val != NULL && cur_val != phi && !phi->is_illegal()) {
Phi* cur_phi = cur_val->as_Phi();
if (cur_phi != NULL && cur_phi->is_illegal()) {
// Phi and local would need to get invalidated