2
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
25
#include "precompiled.hpp"
26
#include "opto/addnode.hpp"
27
#include "opto/callnode.hpp"
28
#include "opto/castnode.hpp"
29
#include "opto/connode.hpp"
30
#include "opto/matcher.hpp"
31
#include "opto/phaseX.hpp"
32
#include "opto/subnode.hpp"
33
#include "opto/type.hpp"
34
#include "castnode.hpp"
35
#include "utilities/checkedCast.hpp"
37
//=============================================================================
38
// If input is already higher or equal to cast type, then this is an identity.
39
Node* ConstraintCastNode::Identity(PhaseGVN* phase) {
40
if (_dependency == UnconditionalDependency) {
43
Node* dom = dominating_cast(phase, phase);
47
return higher_equal_types(phase, in(1)) ? in(1) : this;
50
//------------------------------Value------------------------------------------
51
// Take 'join' of input and cast-up type
52
const Type* ConstraintCastNode::Value(PhaseGVN* phase) const {
53
if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP;
55
const Type* in_type = phase->type(in(1));
56
const Type* ft = in_type->filter_speculative(_type);
58
// Check if both _type and in_type had a speculative type, but for the just
59
// computed ft the speculative type was dropped.
60
if (ft->speculative() == nullptr &&
61
_type->speculative() != nullptr &&
62
in_type->speculative() != nullptr) {
63
// Speculative type may have disagreed between cast and input, and was
64
// dropped in filtering. Recompute so that ft can take speculative type
65
// of in_type. If we did not do it now, a subsequent ::Value call would
66
// do it, and violate idempotence of ::Value.
67
ft = in_type->filter_speculative(ft);
71
// Previous versions of this function had some special case logic,
72
// which is no longer necessary. Make sure of the required effects.
76
if (in_type == Type::TOP) {
77
assert(ft == Type::TOP, "special case #1");
79
const Type* rt = in_type->join_speculative(_type);
81
assert(ft == Type::TOP, "special case #2");
86
if (in_type == TypePtr::NULL_PTR &&
87
_type->isa_ptr() && _type->is_ptr()->_ptr == TypePtr::NotNull) {
88
assert(ft == Type::TOP, "special case #3");
97
//------------------------------Ideal------------------------------------------
98
// Return a node which is more "ideal" than the current node. Strip out
100
Node *ConstraintCastNode::Ideal(PhaseGVN *phase, bool can_reshape) {
101
return (in(0) && remove_dead_region(phase, can_reshape)) ? this : nullptr;
104
uint ConstraintCastNode::hash() const {
105
return TypeNode::hash() + (int)_dependency + (_extra_types != nullptr ? _extra_types->hash() : 0);
108
bool ConstraintCastNode::cmp(const Node &n) const {
109
if (!TypeNode::cmp(n)) {
112
ConstraintCastNode& cast = (ConstraintCastNode&) n;
113
if (cast._dependency != _dependency) {
116
if (_extra_types == nullptr || cast._extra_types == nullptr) {
117
return _extra_types == cast._extra_types;
119
return _extra_types->eq(cast._extra_types);
122
uint ConstraintCastNode::size_of() const {
123
return sizeof(*this);
126
Node* ConstraintCastNode::make_cast_for_basic_type(Node* c, Node* n, const Type* t, DependencyType dependency, BasicType bt) {
129
return new CastIINode(c, n, t, dependency);
131
return new CastLLNode(c, n, t, dependency);
133
fatal("Bad basic type %s", type2name(bt));
138
TypeNode* ConstraintCastNode::dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const {
139
if (_dependency == UnconditionalDependency) {
145
if (ctl == nullptr) {
148
// Range check CastIIs may all end up under a single range check and
149
// in that case only the narrower CastII would be kept by the code
150
// below which would be incorrect.
151
if (is_CastII() && as_CastII()->has_range_check()) {
154
if (type()->isa_rawptr() && (gvn->type_or_null(val) == nullptr || gvn->type(val)->isa_oopptr())) {
157
for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {
158
Node* u = val->fast_out(i);
161
u->Opcode() == opc &&
162
u->in(0) != nullptr &&
163
higher_equal_types(gvn, u)) {
164
if (pt->is_dominator(u->in(0), ctl)) {
167
if (is_CheckCastPP() && u->in(1)->is_Proj() && u->in(1)->in(0)->is_Allocate() &&
168
u->in(0)->is_Proj() && u->in(0)->in(0)->is_Initialize() &&
169
u->in(1)->in(0)->as_Allocate()->initialization() == u->in(0)->in(0)) {
170
// CheckCastPP following an allocation always dominates all
171
// use of the allocation result
179
bool ConstraintCastNode::higher_equal_types(PhaseGVN* phase, const Node* other) const {
180
const Type* t = phase->type(other);
181
if (!t->higher_equal_speculative(type())) {
184
if (_extra_types != nullptr) {
185
for (uint i = 0; i < _extra_types->cnt(); ++i) {
186
if (!t->higher_equal_speculative(_extra_types->field_at(i))) {
195
void ConstraintCastNode::dump_spec(outputStream *st) const {
196
TypeNode::dump_spec(st);
197
if (_extra_types != nullptr) {
198
st->print(" extra types: ");
199
_extra_types->dump_on(st);
201
if (_dependency != RegularDependency) {
202
st->print(" %s dependency", _dependency == StrongDependency ? "strong" : "unconditional");
207
const Type* CastIINode::Value(PhaseGVN* phase) const {
208
const Type *res = ConstraintCastNode::Value(phase);
209
if (res == Type::TOP) {
212
assert(res->isa_int(), "res must be int");
214
// Similar to ConvI2LNode::Value() for the same reasons
215
// see if we can remove type assertion after loop opts
216
// But here we have to pay extra attention:
217
// Do not narrow the type of range check dependent CastIINodes to
218
// avoid corruption of the graph if a CastII is replaced by TOP but
219
// the corresponding range check is not removed.
220
if (!_range_check_dependency) {
221
res = widen_type(phase, res, T_INT);
227
static Node* find_or_make_integer_cast(PhaseIterGVN* igvn, Node* parent, Node* control, const TypeInteger* type, ConstraintCastNode::DependencyType dependency, BasicType bt) {
228
Node* n = ConstraintCastNode::make_cast_for_basic_type(control, parent, type, dependency, bt);
229
Node* existing = igvn->hash_find_insert(n);
230
if (existing != nullptr) {
234
return igvn->register_new_node_with_optimizer(n);
237
Node *CastIINode::Ideal(PhaseGVN *phase, bool can_reshape) {
238
Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
239
if (progress != nullptr) {
242
if (can_reshape && !_range_check_dependency && !phase->C->post_loop_opts_phase()) {
243
// makes sure we run ::Value to potentially remove type assertion after loop opts
244
phase->C->record_for_post_loop_opts_igvn(this);
246
if (!_range_check_dependency) {
247
return optimize_integer_cast(phase, T_INT);
252
Node* CastIINode::Identity(PhaseGVN* phase) {
253
Node* progress = ConstraintCastNode::Identity(phase);
254
if (progress != this) {
257
if (_range_check_dependency) {
258
if (phase->C->post_loop_opts_phase()) {
261
phase->C->record_for_post_loop_opts_igvn(this);
267
bool CastIINode::cmp(const Node &n) const {
268
return ConstraintCastNode::cmp(n) && ((CastIINode&)n)._range_check_dependency == _range_check_dependency;
271
uint CastIINode::size_of() const {
272
return sizeof(*this);
276
void CastIINode::dump_spec(outputStream* st) const {
277
ConstraintCastNode::dump_spec(st);
278
if (_range_check_dependency) {
279
st->print(" range check dependency");
284
CastIINode* CastIINode::pin_array_access_node() const {
285
assert(_dependency == RegularDependency, "already pinned");
286
if (has_range_check()) {
287
return new CastIINode(in(0), in(1), bottom_type(), StrongDependency, has_range_check());
293
const Type* CastLLNode::Value(PhaseGVN* phase) const {
294
const Type* res = ConstraintCastNode::Value(phase);
295
if (res == Type::TOP) {
298
assert(res->isa_long(), "res must be long");
300
return widen_type(phase, res, T_LONG);
303
Node* CastLLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
304
Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
305
if (progress != nullptr) {
308
if (!phase->C->post_loop_opts_phase()) {
309
// makes sure we run ::Value to potentially remove type assertion after loop opts
310
phase->C->record_for_post_loop_opts_igvn(this);
312
// transform (CastLL (ConvI2L ..)) into (ConvI2L (CastII ..)) if the type of the CastLL is narrower than the type of
315
if (in1 != nullptr && in1->Opcode() == Op_ConvI2L) {
316
const Type* t = Value(phase);
317
const Type* t_in = phase->type(in1);
318
if (t != Type::TOP && t_in != Type::TOP) {
319
const TypeLong* tl = t->is_long();
320
const TypeLong* t_in_l = t_in->is_long();
321
assert(tl->_lo >= t_in_l->_lo && tl->_hi <= t_in_l->_hi, "CastLL type should be narrower than or equal to the type of its input");
322
assert((tl != t_in_l) == (tl->_lo > t_in_l->_lo || tl->_hi < t_in_l->_hi), "if type differs then this nodes's type must be narrower");
324
const TypeInt* ti = TypeInt::make(checked_cast<jint>(tl->_lo), checked_cast<jint>(tl->_hi), tl->_widen);
325
Node* castii = phase->transform(new CastIINode(in(0), in1->in(1), ti));
326
Node* convi2l = in1->clone();
327
convi2l->set_req(1, castii);
332
return optimize_integer_cast(phase, T_LONG);
335
//------------------------------Value------------------------------------------
336
// Take 'join' of input and cast-up type, unless working with an Interface
337
const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {
338
if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;
340
const Type *inn = phase->type(in(1));
341
if( inn == Type::TOP ) return Type::TOP; // No information yet
343
if (inn->isa_oopptr() && _type->isa_oopptr()) {
344
return ConstraintCastNode::Value(phase);
347
const TypePtr *in_type = inn->isa_ptr();
348
const TypePtr *my_type = _type->isa_ptr();
349
const Type *result = _type;
350
if (in_type != nullptr && my_type != nullptr) {
351
TypePtr::PTR in_ptr = in_type->ptr();
352
if (in_ptr == TypePtr::Null) {
354
} else if (in_ptr != TypePtr::Constant) {
355
result = my_type->cast_to_ptr_type(my_type->join_ptr(in_ptr));
362
//=============================================================================
363
//------------------------------Value------------------------------------------
364
const Type* CastX2PNode::Value(PhaseGVN* phase) const {
365
const Type* t = phase->type(in(1));
366
if (t == Type::TOP) return Type::TOP;
367
if (t->base() == Type_X && t->singleton()) {
368
uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();
369
if (bits == 0) return TypePtr::NULL_PTR;
370
return TypeRawPtr::make((address) bits);
372
return CastX2PNode::bottom_type();
375
//------------------------------Idealize---------------------------------------
376
static inline bool fits_in_int(const Type* t, bool but_not_min_int = false) {
377
if (t == Type::TOP) return false;
378
const TypeX* tl = t->is_intptr_t();
381
if (but_not_min_int) ++lo; // caller wants to negate the value w/o overflow
382
return (tl->_lo >= lo) && (tl->_hi <= hi);
385
static inline Node* addP_of_X2P(PhaseGVN *phase,
388
bool negate = false) {
390
dispX = phase->transform(new SubXNode(phase->MakeConX(0), dispX));
392
return new AddPNode(phase->C->top(),
393
phase->transform(new CastX2PNode(base)),
397
Node *CastX2PNode::Ideal(PhaseGVN *phase, bool can_reshape) {
398
// convert CastX2P(AddX(x, y)) to AddP(CastX2P(x), y) if y fits in an int
399
int op = in(1)->Opcode();
405
// Avoid ideal transformations ping-pong between this and AddP for raw pointers.
406
if (phase->find_intptr_t_con(x, -1) == 0)
409
if (fits_in_int(phase->type(y), true)) {
410
return addP_of_X2P(phase, x, y, true);
416
if (fits_in_int(phase->type(y))) {
417
return addP_of_X2P(phase, x, y);
419
if (fits_in_int(phase->type(x))) {
420
return addP_of_X2P(phase, y, x);
427
//------------------------------Identity---------------------------------------
428
Node* CastX2PNode::Identity(PhaseGVN* phase) {
429
if (in(1)->Opcode() == Op_CastP2X) return in(1)->in(1);
433
//=============================================================================
434
//------------------------------Value------------------------------------------
435
const Type* CastP2XNode::Value(PhaseGVN* phase) const {
436
const Type* t = phase->type(in(1));
437
if (t == Type::TOP) return Type::TOP;
438
if (t->base() == Type::RawPtr && t->singleton()) {
439
uintptr_t bits = (uintptr_t) t->is_rawptr()->get_con();
440
return TypeX::make(bits);
442
return CastP2XNode::bottom_type();
445
Node *CastP2XNode::Ideal(PhaseGVN *phase, bool can_reshape) {
446
return (in(0) && remove_dead_region(phase, can_reshape)) ? this : nullptr;
449
//------------------------------Identity---------------------------------------
450
Node* CastP2XNode::Identity(PhaseGVN* phase) {
451
if (in(1)->Opcode() == Op_CastX2P) return in(1)->in(1);
455
Node* ConstraintCastNode::make_cast_for_type(Node* c, Node* in, const Type* type, DependencyType dependency,
456
const TypeTuple* types) {
457
if (type->isa_int()) {
458
return new CastIINode(c, in, type, dependency, false, types);
459
} else if (type->isa_long()) {
460
return new CastLLNode(c, in, type, dependency, types);
461
} else if (type->isa_float()) {
462
return new CastFFNode(c, in, type, dependency, types);
463
} else if (type->isa_double()) {
464
return new CastDDNode(c, in, type, dependency, types);
465
} else if (type->isa_vect()) {
466
return new CastVVNode(c, in, type, dependency, types);
467
} else if (type->isa_ptr()) {
468
return new CastPPNode(c, in, type, dependency, types);
470
fatal("unreachable. Invalid cast type.");
474
Node* ConstraintCastNode::optimize_integer_cast(PhaseGVN* phase, BasicType bt) {
475
PhaseIterGVN *igvn = phase->is_IterGVN();
476
const TypeInteger* this_type = this->type()->is_integer(bt);
478
const TypeInteger* rx = nullptr;
479
const TypeInteger* ry = nullptr;
480
// Similar to ConvI2LNode::Ideal() for the same reasons
481
if (Compile::push_thru_add(phase, z, this_type, rx, ry, bt, bt)) {
482
if (igvn == nullptr) {
483
// Postpone this optimization to iterative GVN, where we can handle deep
484
// AddI chains without an exponential number of recursive Ideal() calls.
485
phase->record_for_igvn(this);
488
int op = z->Opcode();
492
Node* cx = find_or_make_integer_cast(igvn, x, in(0), rx, _dependency, bt);
493
Node* cy = find_or_make_integer_cast(igvn, y, in(0), ry, _dependency, bt);
494
if (op == Op_Add(bt)) {
495
return AddNode::make(cx, cy, bt);
497
assert(op == Op_Sub(bt), "");
498
return SubNode::make(cx, cy, bt);
505
const Type* ConstraintCastNode::widen_type(const PhaseGVN* phase, const Type* res, BasicType bt) const {
506
if (!phase->C->post_loop_opts_phase()) {
509
const TypeInteger* this_type = res->is_integer(bt);
510
const TypeInteger* in_type = phase->type(in(1))->isa_integer(bt);
511
if (in_type != nullptr &&
512
(in_type->lo_as_long() != this_type->lo_as_long() ||
513
in_type->hi_as_long() != this_type->hi_as_long())) {
514
jlong lo1 = this_type->lo_as_long();
515
jlong hi1 = this_type->hi_as_long();
516
int w1 = this_type->_widen;
518
// Keep a range assertion of >=0.
519
lo1 = 0; hi1 = max_signed_integer(bt);
520
} else if (hi1 < 0) {
521
// Keep a range assertion of <0.
522
lo1 = min_signed_integer(bt); hi1 = -1;
524
lo1 = min_signed_integer(bt); hi1 = max_signed_integer(bt);
526
return TypeInteger::make(MAX2(in_type->lo_as_long(), lo1),
527
MIN2(in_type->hi_as_long(), hi1),
528
MAX2((int)in_type->_widen, w1), bt);