1 /*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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.
8 *
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).
14 *
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.
18 *
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
21 * questions.
22 *
23 */
24
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
35 //=============================================================================
36 // If input is already higher or equal to cast type, then this is an identity.
Identity(PhaseGVN * phase)37 Node* ConstraintCastNode::Identity(PhaseGVN* phase) {
38 Node* dom = dominating_cast(phase, phase);
39 if (dom != NULL) {
40 return dom;
41 }
42 if (_carry_dependency) {
43 return this;
44 }
45 return phase->type(in(1))->higher_equal_speculative(_type) ? in(1) : this;
46 }
47
48 //------------------------------Value------------------------------------------
49 // Take 'join' of input and cast-up type
Value(PhaseGVN * phase) const50 const Type* ConstraintCastNode::Value(PhaseGVN* phase) const {
51 if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP;
52 const Type* ft = phase->type(in(1))->filter_speculative(_type);
53
54 #ifdef ASSERT
55 // Previous versions of this function had some special case logic,
56 // which is no longer necessary. Make sure of the required effects.
57 switch (Opcode()) {
58 case Op_CastII:
59 {
60 const Type* t1 = phase->type(in(1));
61 if( t1 == Type::TOP ) assert(ft == Type::TOP, "special case #1");
62 const Type* rt = t1->join_speculative(_type);
63 if (rt->empty()) assert(ft == Type::TOP, "special case #2");
64 break;
65 }
66 case Op_CastPP:
67 if (phase->type(in(1)) == TypePtr::NULL_PTR &&
68 _type->isa_ptr() && _type->is_ptr()->_ptr == TypePtr::NotNull)
69 assert(ft == Type::TOP, "special case #3");
70 break;
71 }
72 #endif //ASSERT
73
74 return ft;
75 }
76
77 //------------------------------Ideal------------------------------------------
78 // Return a node which is more "ideal" than the current node. Strip out
79 // control copies
Ideal(PhaseGVN * phase,bool can_reshape)80 Node *ConstraintCastNode::Ideal(PhaseGVN *phase, bool can_reshape) {
81 return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL;
82 }
83
cmp(const Node & n) const84 bool ConstraintCastNode::cmp(const Node &n) const {
85 return TypeNode::cmp(n) && ((ConstraintCastNode&)n)._carry_dependency == _carry_dependency;
86 }
87
size_of() const88 uint ConstraintCastNode::size_of() const {
89 return sizeof(*this);
90 }
91
make_cast(int opcode,Node * c,Node * n,const Type * t,bool carry_dependency)92 Node* ConstraintCastNode::make_cast(int opcode, Node* c, Node *n, const Type *t, bool carry_dependency) {
93 switch(opcode) {
94 case Op_CastII: {
95 Node* cast = new CastIINode(n, t, carry_dependency);
96 cast->set_req(0, c);
97 return cast;
98 }
99 case Op_CastLL: {
100 Node* cast = new CastLLNode(n, t, carry_dependency);
101 cast->set_req(0, c);
102 return cast;
103 }
104 case Op_CastPP: {
105 Node* cast = new CastPPNode(n, t, carry_dependency);
106 cast->set_req(0, c);
107 return cast;
108 }
109 case Op_CheckCastPP: return new CheckCastPPNode(c, n, t, carry_dependency);
110 default:
111 fatal("Bad opcode %d", opcode);
112 }
113 return NULL;
114 }
115
make(Node * c,Node * n,const Type * t,BasicType bt)116 Node* ConstraintCastNode::make(Node* c, Node *n, const Type *t, BasicType bt) {
117 switch(bt) {
118 case T_INT: {
119 return make_cast(Op_CastII, c, n, t, false);
120 }
121 case T_LONG: {
122 return make_cast(Op_CastLL, c, n, t, false);
123 }
124 default:
125 fatal("Bad basic type %s", type2name(bt));
126 }
127 return NULL;
128 }
129
dominating_cast(PhaseGVN * gvn,PhaseTransform * pt) const130 TypeNode* ConstraintCastNode::dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const {
131 Node* val = in(1);
132 Node* ctl = in(0);
133 int opc = Opcode();
134 if (ctl == NULL) {
135 return NULL;
136 }
137 // Range check CastIIs may all end up under a single range check and
138 // in that case only the narrower CastII would be kept by the code
139 // below which would be incorrect.
140 if (is_CastII() && as_CastII()->has_range_check()) {
141 return NULL;
142 }
143 if (type()->isa_rawptr() && (gvn->type_or_null(val) == NULL || gvn->type(val)->isa_oopptr())) {
144 return NULL;
145 }
146 for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {
147 Node* u = val->fast_out(i);
148 if (u != this &&
149 u->outcnt() > 0 &&
150 u->Opcode() == opc &&
151 u->in(0) != NULL &&
152 u->bottom_type()->higher_equal(type())) {
153 if (pt->is_dominator(u->in(0), ctl)) {
154 return u->as_Type();
155 }
156 if (is_CheckCastPP() && u->in(1)->is_Proj() && u->in(1)->in(0)->is_Allocate() &&
157 u->in(0)->is_Proj() && u->in(0)->in(0)->is_Initialize() &&
158 u->in(1)->in(0)->as_Allocate()->initialization() == u->in(0)->in(0)) {
159 // CheckCastPP following an allocation always dominates all
160 // use of the allocation result
161 return u->as_Type();
162 }
163 }
164 }
165 return NULL;
166 }
167
168 #ifndef PRODUCT
dump_spec(outputStream * st) const169 void ConstraintCastNode::dump_spec(outputStream *st) const {
170 TypeNode::dump_spec(st);
171 if (_carry_dependency) {
172 st->print(" carry dependency");
173 }
174 }
175 #endif
176
Value(PhaseGVN * phase) const177 const Type* CastIINode::Value(PhaseGVN* phase) const {
178 const Type *res = ConstraintCastNode::Value(phase);
179
180 // Try to improve the type of the CastII if we recognize a CmpI/If
181 // pattern.
182 if (_carry_dependency) {
183 if (in(0) != NULL && in(0)->in(0) != NULL && in(0)->in(0)->is_If()) {
184 assert(in(0)->is_IfFalse() || in(0)->is_IfTrue(), "should be If proj");
185 Node* proj = in(0);
186 if (proj->in(0)->in(1)->is_Bool()) {
187 Node* b = proj->in(0)->in(1);
188 if (b->in(1)->Opcode() == Op_CmpI) {
189 Node* cmp = b->in(1);
190 if (cmp->in(1) == in(1) && phase->type(cmp->in(2))->isa_int()) {
191 const TypeInt* in2_t = phase->type(cmp->in(2))->is_int();
192 const Type* t = TypeInt::INT;
193 BoolTest test = b->as_Bool()->_test;
194 if (proj->is_IfFalse()) {
195 test = test.negate();
196 }
197 BoolTest::mask m = test._test;
198 jlong lo_long = min_jint;
199 jlong hi_long = max_jint;
200 if (m == BoolTest::le || m == BoolTest::lt) {
201 hi_long = in2_t->_hi;
202 if (m == BoolTest::lt) {
203 hi_long -= 1;
204 }
205 } else if (m == BoolTest::ge || m == BoolTest::gt) {
206 lo_long = in2_t->_lo;
207 if (m == BoolTest::gt) {
208 lo_long += 1;
209 }
210 } else if (m == BoolTest::eq) {
211 lo_long = in2_t->_lo;
212 hi_long = in2_t->_hi;
213 } else if (m == BoolTest::ne) {
214 // can't do any better
215 } else {
216 stringStream ss;
217 test.dump_on(&ss);
218 fatal("unexpected comparison %s", ss.as_string());
219 }
220 int lo_int = (int)lo_long;
221 int hi_int = (int)hi_long;
222
223 if (lo_long != (jlong)lo_int) {
224 lo_int = min_jint;
225 }
226 if (hi_long != (jlong)hi_int) {
227 hi_int = max_jint;
228 }
229
230 t = TypeInt::make(lo_int, hi_int, Type::WidenMax);
231
232 res = res->filter_speculative(t);
233
234 return res;
235 }
236 }
237 }
238 }
239 }
240 return res;
241 }
242
find_or_make_CastII(PhaseIterGVN * igvn,Node * parent,Node * control,const TypeInt * type)243 static Node* find_or_make_CastII(PhaseIterGVN* igvn, Node* parent, Node* control,
244 const TypeInt* type) {
245 Node* n = new CastIINode(parent, type);
246 n->set_req(0, control);
247 Node* existing = igvn->hash_find_insert(n);
248 if (existing != NULL) {
249 n->destruct(igvn);
250 return existing;
251 }
252 return igvn->register_new_node_with_optimizer(n);
253 }
254
Ideal(PhaseGVN * phase,bool can_reshape)255 Node *CastIINode::Ideal(PhaseGVN *phase, bool can_reshape) {
256 Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
257 if (progress != NULL) {
258 return progress;
259 }
260
261 PhaseIterGVN *igvn = phase->is_IterGVN();
262 const TypeInt* this_type = this->type()->is_int();
263 Node* z = in(1);
264 const TypeInteger* rx = NULL;
265 const TypeInteger* ry = NULL;
266 // Similar to ConvI2LNode::Ideal() for the same reasons
267 if (!_range_check_dependency && Compile::push_thru_add(phase, z, this_type, rx, ry, T_INT)) {
268 if (igvn == NULL) {
269 // Postpone this optimization to iterative GVN, where we can handle deep
270 // AddI chains without an exponential number of recursive Ideal() calls.
271 phase->record_for_igvn(this);
272 return NULL;
273 }
274 int op = z->Opcode();
275 Node* x = z->in(1);
276 Node* y = z->in(2);
277
278 Node* cx = find_or_make_CastII(igvn, x, in(0), rx->is_int());
279 Node* cy = find_or_make_CastII(igvn, y, in(0), ry->is_int());
280 switch (op) {
281 case Op_AddI: return new AddINode(cx, cy);
282 case Op_SubI: return new SubINode(cx, cy);
283 default: ShouldNotReachHere();
284 }
285 }
286
287 // Similar to ConvI2LNode::Ideal() for the same reasons
288 // Do not narrow the type of range check dependent CastIINodes to
289 // avoid corruption of the graph if a CastII is replaced by TOP but
290 // the corresponding range check is not removed.
291 if (can_reshape && !_range_check_dependency) {
292 if (phase->C->post_loop_opts_phase()) {
293 const TypeInt* this_type = this->type()->is_int();
294 const TypeInt* in_type = phase->type(in(1))->isa_int();
295 if (in_type != NULL && this_type != NULL &&
296 (in_type->_lo != this_type->_lo ||
297 in_type->_hi != this_type->_hi)) {
298 jint lo1 = this_type->_lo;
299 jint hi1 = this_type->_hi;
300 int w1 = this_type->_widen;
301
302 if (lo1 >= 0) {
303 // Keep a range assertion of >=0.
304 lo1 = 0; hi1 = max_jint;
305 } else if (hi1 < 0) {
306 // Keep a range assertion of <0.
307 lo1 = min_jint; hi1 = -1;
308 } else {
309 lo1 = min_jint; hi1 = max_jint;
310 }
311 const TypeInt* wtype = TypeInt::make(MAX2(in_type->_lo, lo1),
312 MIN2(in_type->_hi, hi1),
313 MAX2((int)in_type->_widen, w1));
314 if (wtype != type()) {
315 set_type(wtype);
316 return this;
317 }
318 }
319 } else {
320 phase->C->record_for_post_loop_opts_igvn(this);
321 }
322 }
323 return NULL;
324 }
325
Identity(PhaseGVN * phase)326 Node* CastIINode::Identity(PhaseGVN* phase) {
327 Node* progress = ConstraintCastNode::Identity(phase);
328 if (progress != this) {
329 return progress;
330 }
331 if (_range_check_dependency) {
332 if (phase->C->post_loop_opts_phase()) {
333 return this->in(1);
334 } else {
335 phase->C->record_for_post_loop_opts_igvn(this);
336 }
337 }
338 return this;
339 }
340
cmp(const Node & n) const341 bool CastIINode::cmp(const Node &n) const {
342 return ConstraintCastNode::cmp(n) && ((CastIINode&)n)._range_check_dependency == _range_check_dependency;
343 }
344
size_of() const345 uint CastIINode::size_of() const {
346 return sizeof(*this);
347 }
348
349 #ifndef PRODUCT
dump_spec(outputStream * st) const350 void CastIINode::dump_spec(outputStream* st) const {
351 ConstraintCastNode::dump_spec(st);
352 if (_range_check_dependency) {
353 st->print(" range check dependency");
354 }
355 }
356 #endif
357
358 //=============================================================================
359 //------------------------------Identity---------------------------------------
360 // If input is already higher or equal to cast type, then this is an identity.
Identity(PhaseGVN * phase)361 Node* CheckCastPPNode::Identity(PhaseGVN* phase) {
362 Node* dom = dominating_cast(phase, phase);
363 if (dom != NULL) {
364 return dom;
365 }
366 if (_carry_dependency) {
367 return this;
368 }
369 const Type* t = phase->type(in(1));
370 if (EnableVectorReboxing && in(1)->Opcode() == Op_VectorBox) {
371 if (t->higher_equal_speculative(phase->type(this))) {
372 return in(1);
373 }
374 } else if (t == phase->type(this)) {
375 // Toned down to rescue meeting at a Phi 3 different oops all implementing
376 // the same interface.
377 return in(1);
378 }
379 return this;
380 }
381
382 //------------------------------Value------------------------------------------
383 // Take 'join' of input and cast-up type, unless working with an Interface
Value(PhaseGVN * phase) const384 const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {
385 if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;
386
387 const Type *inn = phase->type(in(1));
388 if( inn == Type::TOP ) return Type::TOP; // No information yet
389
390 const TypePtr *in_type = inn->isa_ptr();
391 const TypePtr *my_type = _type->isa_ptr();
392 const Type *result = _type;
393 if( in_type != NULL && my_type != NULL ) {
394 TypePtr::PTR in_ptr = in_type->ptr();
395 if (in_ptr == TypePtr::Null) {
396 result = in_type;
397 } else if (in_ptr == TypePtr::Constant) {
398 if (my_type->isa_rawptr()) {
399 result = my_type;
400 } else {
401 const TypeOopPtr *jptr = my_type->isa_oopptr();
402 assert(jptr, "");
403 result = !in_type->higher_equal(_type)
404 ? my_type->cast_to_ptr_type(TypePtr::NotNull)
405 : in_type;
406 }
407 } else {
408 result = my_type->cast_to_ptr_type( my_type->join_ptr(in_ptr) );
409 }
410 }
411
412 // This is the code from TypePtr::xmeet() that prevents us from
413 // having 2 ways to represent the same type. We have to replicate it
414 // here because we don't go through meet/join.
415 if (result->remove_speculative() == result->speculative()) {
416 result = result->remove_speculative();
417 }
418
419 // Same as above: because we don't go through meet/join, remove the
420 // speculative type if we know we won't use it.
421 return result->cleanup_speculative();
422
423 // JOIN NOT DONE HERE BECAUSE OF INTERFACE ISSUES.
424 // FIX THIS (DO THE JOIN) WHEN UNION TYPES APPEAR!
425
426 //
427 // Remove this code after overnight run indicates no performance
428 // loss from not performing JOIN at CheckCastPPNode
429 //
430 // const TypeInstPtr *in_oop = in->isa_instptr();
431 // const TypeInstPtr *my_oop = _type->isa_instptr();
432 // // If either input is an 'interface', return destination type
433 // assert (in_oop == NULL || in_oop->klass() != NULL, "");
434 // assert (my_oop == NULL || my_oop->klass() != NULL, "");
435 // if( (in_oop && in_oop->klass()->is_interface())
436 // ||(my_oop && my_oop->klass()->is_interface()) ) {
437 // TypePtr::PTR in_ptr = in->isa_ptr() ? in->is_ptr()->_ptr : TypePtr::BotPTR;
438 // // Preserve cast away nullness for interfaces
439 // if( in_ptr == TypePtr::NotNull && my_oop && my_oop->_ptr == TypePtr::BotPTR ) {
440 // return my_oop->cast_to_ptr_type(TypePtr::NotNull);
441 // }
442 // return _type;
443 // }
444 //
445 // // Neither the input nor the destination type is an interface,
446 //
447 // // history: JOIN used to cause weird corner case bugs
448 // // return (in == TypeOopPtr::NULL_PTR) ? in : _type;
449 // // JOIN picks up NotNull in common instance-of/check-cast idioms, both oops.
450 // // JOIN does not preserve NotNull in other cases, e.g. RawPtr vs InstPtr
451 // const Type *join = in->join(_type);
452 // // Check if join preserved NotNull'ness for pointers
453 // if( join->isa_ptr() && _type->isa_ptr() ) {
454 // TypePtr::PTR join_ptr = join->is_ptr()->_ptr;
455 // TypePtr::PTR type_ptr = _type->is_ptr()->_ptr;
456 // // If there isn't any NotNull'ness to preserve
457 // // OR if join preserved NotNull'ness then return it
458 // if( type_ptr == TypePtr::BotPTR || type_ptr == TypePtr::Null ||
459 // join_ptr == TypePtr::NotNull || join_ptr == TypePtr::Constant ) {
460 // return join;
461 // }
462 // // ELSE return same old type as before
463 // return _type;
464 // }
465 // // Not joining two pointers
466 // return join;
467 }
468
469 //=============================================================================
470 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const471 const Type* CastX2PNode::Value(PhaseGVN* phase) const {
472 const Type* t = phase->type(in(1));
473 if (t == Type::TOP) return Type::TOP;
474 if (t->base() == Type_X && t->singleton()) {
475 uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();
476 if (bits == 0) return TypePtr::NULL_PTR;
477 return TypeRawPtr::make((address) bits);
478 }
479 return CastX2PNode::bottom_type();
480 }
481
482 //------------------------------Idealize---------------------------------------
fits_in_int(const Type * t,bool but_not_min_int=false)483 static inline bool fits_in_int(const Type* t, bool but_not_min_int = false) {
484 if (t == Type::TOP) return false;
485 const TypeX* tl = t->is_intptr_t();
486 jint lo = min_jint;
487 jint hi = max_jint;
488 if (but_not_min_int) ++lo; // caller wants to negate the value w/o overflow
489 return (tl->_lo >= lo) && (tl->_hi <= hi);
490 }
491
addP_of_X2P(PhaseGVN * phase,Node * base,Node * dispX,bool negate=false)492 static inline Node* addP_of_X2P(PhaseGVN *phase,
493 Node* base,
494 Node* dispX,
495 bool negate = false) {
496 if (negate) {
497 dispX = phase->transform(new SubXNode(phase->MakeConX(0), dispX));
498 }
499 return new AddPNode(phase->C->top(),
500 phase->transform(new CastX2PNode(base)),
501 dispX);
502 }
503
Ideal(PhaseGVN * phase,bool can_reshape)504 Node *CastX2PNode::Ideal(PhaseGVN *phase, bool can_reshape) {
505 // convert CastX2P(AddX(x, y)) to AddP(CastX2P(x), y) if y fits in an int
506 int op = in(1)->Opcode();
507 Node* x;
508 Node* y;
509 switch (op) {
510 case Op_SubX:
511 x = in(1)->in(1);
512 // Avoid ideal transformations ping-pong between this and AddP for raw pointers.
513 if (phase->find_intptr_t_con(x, -1) == 0)
514 break;
515 y = in(1)->in(2);
516 if (fits_in_int(phase->type(y), true)) {
517 return addP_of_X2P(phase, x, y, true);
518 }
519 break;
520 case Op_AddX:
521 x = in(1)->in(1);
522 y = in(1)->in(2);
523 if (fits_in_int(phase->type(y))) {
524 return addP_of_X2P(phase, x, y);
525 }
526 if (fits_in_int(phase->type(x))) {
527 return addP_of_X2P(phase, y, x);
528 }
529 break;
530 }
531 return NULL;
532 }
533
534 //------------------------------Identity---------------------------------------
Identity(PhaseGVN * phase)535 Node* CastX2PNode::Identity(PhaseGVN* phase) {
536 if (in(1)->Opcode() == Op_CastP2X) return in(1)->in(1);
537 return this;
538 }
539
540 //=============================================================================
541 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const542 const Type* CastP2XNode::Value(PhaseGVN* phase) const {
543 const Type* t = phase->type(in(1));
544 if (t == Type::TOP) return Type::TOP;
545 if (t->base() == Type::RawPtr && t->singleton()) {
546 uintptr_t bits = (uintptr_t) t->is_rawptr()->get_con();
547 return TypeX::make(bits);
548 }
549 return CastP2XNode::bottom_type();
550 }
551
Ideal(PhaseGVN * phase,bool can_reshape)552 Node *CastP2XNode::Ideal(PhaseGVN *phase, bool can_reshape) {
553 return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL;
554 }
555
556 //------------------------------Identity---------------------------------------
Identity(PhaseGVN * phase)557 Node* CastP2XNode::Identity(PhaseGVN* phase) {
558 if (in(1)->Opcode() == Op_CastX2P) return in(1)->in(1);
559 return this;
560 }
561