1 /*
2 * Copyright (c) 2014, 2015, 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/connode.hpp"
28 #include "opto/convertnode.hpp"
29 #include "opto/movenode.hpp"
30 #include "opto/phaseX.hpp"
31 #include "opto/subnode.hpp"
32
33 //=============================================================================
34 /*
35 The major change is for CMoveP and StrComp. They have related but slightly
36 different problems. They both take in TWO oops which are both null-checked
37 independently before the using Node. After CCP removes the CastPP's they need
38 to pick up the guarding test edge - in this case TWO control edges. I tried
39 various solutions, all have problems:
40
41 (1) Do nothing. This leads to a bug where we hoist a Load from a CMoveP or a
42 StrComp above a guarding null check. I've seen both cases in normal -Xcomp
43 testing.
44
45 (2) Plug the control edge from 1 of the 2 oops in. Apparent problem here is
46 to figure out which test post-dominates. The real problem is that it doesn't
47 matter which one you pick. After you pick up, the dominating-test elider in
48 IGVN can remove the test and allow you to hoist up to the dominating test on
49 the chosen oop bypassing the test on the not-chosen oop. Seen in testing.
50 Oops.
51
52 (3) Leave the CastPP's in. This makes the graph more accurate in some sense;
53 we get to keep around the knowledge that an oop is not-null after some test.
54 Alas, the CastPP's interfere with GVN (some values are the regular oop, some
55 are the CastPP of the oop, all merge at Phi's which cannot collapse, etc).
56 This cost us 10% on SpecJVM, even when I removed some of the more trivial
57 cases in the optimizer. Removing more useless Phi's started allowing Loads to
58 illegally float above null checks. I gave up on this approach.
59
60 (4) Add BOTH control edges to both tests. Alas, too much code knows that
61 control edges are in slot-zero ONLY. Many quick asserts fail; no way to do
62 this one. Note that I really want to allow the CMoveP to float and add both
63 control edges to the dependent Load op - meaning I can select early but I
64 cannot Load until I pass both tests.
65
66 (5) Do not hoist CMoveP and StrComp. To this end I added the v-call
67 depends_only_on_test(). No obvious performance loss on Spec, but we are
68 clearly conservative on CMoveP (also so on StrComp but that's unlikely to
69 matter ever).
70
71 */
72
73
74 //------------------------------Ideal------------------------------------------
75 // Return a node which is more "ideal" than the current node.
76 // Move constants to the right.
Ideal(PhaseGVN * phase,bool can_reshape)77 Node *CMoveNode::Ideal(PhaseGVN *phase, bool can_reshape) {
78 if( in(0) && remove_dead_region(phase, can_reshape) ) return this;
79 // Don't bother trying to transform a dead node
80 if( in(0) && in(0)->is_top() ) return NULL;
81 assert(in(Condition) != this &&
82 in(IfFalse) != this &&
83 in(IfTrue) != this, "dead loop in CMoveNode::Ideal" );
84 if( phase->type(in(Condition)) == Type::TOP )
85 return NULL; // return NULL when Condition is dead
86
87 if( in(IfFalse)->is_Con() && !in(IfTrue)->is_Con() ) {
88 if( in(Condition)->is_Bool() ) {
89 BoolNode* b = in(Condition)->as_Bool();
90 BoolNode* b2 = b->negate(phase);
91 return make(in(Control), phase->transform(b2), in(IfTrue), in(IfFalse), _type);
92 }
93 }
94 return NULL;
95 }
96
97 //------------------------------is_cmove_id------------------------------------
98 // Helper function to check for CMOVE identity. Shared with PhiNode::Identity
is_cmove_id(PhaseTransform * phase,Node * cmp,Node * t,Node * f,BoolNode * b)99 Node *CMoveNode::is_cmove_id( PhaseTransform *phase, Node *cmp, Node *t, Node *f, BoolNode *b ) {
100 // Check for Cmp'ing and CMove'ing same values
101 if ((cmp->in(1) == f && cmp->in(2) == t) ||
102 // Swapped Cmp is OK
103 (cmp->in(2) == f && cmp->in(1) == t)) {
104 // Give up this identity check for floating points because it may choose incorrect
105 // value around 0.0 and -0.0
106 if ( cmp->Opcode()==Op_CmpF || cmp->Opcode()==Op_CmpD )
107 return NULL;
108 // Check for "(t==f)?t:f;" and replace with "f"
109 if( b->_test._test == BoolTest::eq )
110 return f;
111 // Allow the inverted case as well
112 // Check for "(t!=f)?t:f;" and replace with "t"
113 if( b->_test._test == BoolTest::ne )
114 return t;
115 }
116 return NULL;
117 }
118
119 //------------------------------Identity---------------------------------------
120 // Conditional-move is an identity if both inputs are the same, or the test
121 // true or false.
Identity(PhaseGVN * phase)122 Node* CMoveNode::Identity(PhaseGVN* phase) {
123 // C-moving identical inputs?
124 if (in(IfFalse) == in(IfTrue)) {
125 return in(IfFalse); // Then it doesn't matter
126 }
127 if (phase->type(in(Condition)) == TypeInt::ZERO) {
128 return in(IfFalse); // Always pick left(false) input
129 }
130 if (phase->type(in(Condition)) == TypeInt::ONE) {
131 return in(IfTrue); // Always pick right(true) input
132 }
133
134 // Check for CMove'ing a constant after comparing against the constant.
135 // Happens all the time now, since if we compare equality vs a constant in
136 // the parser, we "know" the variable is constant on one path and we force
137 // it. Thus code like "if( x==0 ) {/*EMPTY*/}" ends up inserting a
138 // conditional move: "x = (x==0)?0:x;". Yucko. This fix is slightly more
139 // general in that we don't need constants.
140 if( in(Condition)->is_Bool() ) {
141 BoolNode *b = in(Condition)->as_Bool();
142 Node *cmp = b->in(1);
143 if( cmp->is_Cmp() ) {
144 Node *id = is_cmove_id( phase, cmp, in(IfTrue), in(IfFalse), b );
145 if( id ) return id;
146 }
147 }
148
149 return this;
150 }
151
152 //------------------------------Value------------------------------------------
153 // Result is the meet of inputs
Value(PhaseGVN * phase) const154 const Type* CMoveNode::Value(PhaseGVN* phase) const {
155 if (phase->type(in(Condition)) == Type::TOP) {
156 return Type::TOP;
157 }
158 const Type* t = phase->type(in(IfFalse))->meet_speculative(phase->type(in(IfTrue)));
159 return t->filter(_type);
160 }
161
162 //------------------------------make-------------------------------------------
163 // Make a correctly-flavored CMove. Since _type is directly determined
164 // from the inputs we do not need to specify it here.
make(Node * c,Node * bol,Node * left,Node * right,const Type * t)165 CMoveNode *CMoveNode::make(Node *c, Node *bol, Node *left, Node *right, const Type *t) {
166 switch( t->basic_type() ) {
167 case T_INT: return new CMoveINode( bol, left, right, t->is_int() );
168 case T_FLOAT: return new CMoveFNode( bol, left, right, t );
169 case T_DOUBLE: return new CMoveDNode( bol, left, right, t );
170 case T_LONG: return new CMoveLNode( bol, left, right, t->is_long() );
171 case T_OBJECT: return new CMovePNode( c, bol, left, right, t->is_oopptr() );
172 case T_ADDRESS: return new CMovePNode( c, bol, left, right, t->is_ptr() );
173 case T_NARROWOOP: return new CMoveNNode( c, bol, left, right, t );
174 default:
175 ShouldNotReachHere();
176 return NULL;
177 }
178 }
179
180 //=============================================================================
181 //------------------------------Ideal------------------------------------------
182 // Return a node which is more "ideal" than the current node.
183 // Check for conversions to boolean
Ideal(PhaseGVN * phase,bool can_reshape)184 Node *CMoveINode::Ideal(PhaseGVN *phase, bool can_reshape) {
185 // Try generic ideal's first
186 Node *x = CMoveNode::Ideal(phase, can_reshape);
187 if( x ) return x;
188
189 // If zero is on the left (false-case, no-move-case) it must mean another
190 // constant is on the right (otherwise the shared CMove::Ideal code would
191 // have moved the constant to the right). This situation is bad for Intel
192 // and a don't-care for Sparc. It's bad for Intel because the zero has to
193 // be manifested in a register with a XOR which kills flags, which are live
194 // on input to the CMoveI, leading to a situation which causes excessive
195 // spilling on Intel. For Sparc, if the zero in on the left the Sparc will
196 // zero a register via G0 and conditionally-move the other constant. If the
197 // zero is on the right, the Sparc will load the first constant with a
198 // 13-bit set-lo and conditionally move G0. See bug 4677505.
199 if( phase->type(in(IfFalse)) == TypeInt::ZERO && !(phase->type(in(IfTrue)) == TypeInt::ZERO) ) {
200 if( in(Condition)->is_Bool() ) {
201 BoolNode* b = in(Condition)->as_Bool();
202 BoolNode* b2 = b->negate(phase);
203 return make(in(Control), phase->transform(b2), in(IfTrue), in(IfFalse), _type);
204 }
205 }
206
207 // Now check for booleans
208 int flip = 0;
209
210 // Check for picking from zero/one
211 if( phase->type(in(IfFalse)) == TypeInt::ZERO && phase->type(in(IfTrue)) == TypeInt::ONE ) {
212 flip = 1 - flip;
213 } else if( phase->type(in(IfFalse)) == TypeInt::ONE && phase->type(in(IfTrue)) == TypeInt::ZERO ) {
214 } else return NULL;
215
216 // Check for eq/ne test
217 if( !in(1)->is_Bool() ) return NULL;
218 BoolNode *bol = in(1)->as_Bool();
219 if( bol->_test._test == BoolTest::eq ) {
220 } else if( bol->_test._test == BoolTest::ne ) {
221 flip = 1-flip;
222 } else return NULL;
223
224 // Check for vs 0 or 1
225 if( !bol->in(1)->is_Cmp() ) return NULL;
226 const CmpNode *cmp = bol->in(1)->as_Cmp();
227 if( phase->type(cmp->in(2)) == TypeInt::ZERO ) {
228 } else if( phase->type(cmp->in(2)) == TypeInt::ONE ) {
229 // Allow cmp-vs-1 if the other input is bounded by 0-1
230 if( phase->type(cmp->in(1)) != TypeInt::BOOL )
231 return NULL;
232 flip = 1 - flip;
233 } else return NULL;
234
235 // Convert to a bool (flipped)
236 // Build int->bool conversion
237 if (PrintOpto) { tty->print_cr("CMOV to I2B"); }
238 Node *n = new Conv2BNode( cmp->in(1) );
239 if( flip )
240 n = new XorINode( phase->transform(n), phase->intcon(1) );
241
242 return n;
243 }
244
245 //=============================================================================
246 //------------------------------Ideal------------------------------------------
247 // Return a node which is more "ideal" than the current node.
248 // Check for absolute value
Ideal(PhaseGVN * phase,bool can_reshape)249 Node *CMoveFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
250 // Try generic ideal's first
251 Node *x = CMoveNode::Ideal(phase, can_reshape);
252 if( x ) return x;
253
254 int cmp_zero_idx = 0; // Index of compare input where to look for zero
255 int phi_x_idx = 0; // Index of phi input where to find naked x
256
257 // Find the Bool
258 if( !in(1)->is_Bool() ) return NULL;
259 BoolNode *bol = in(1)->as_Bool();
260 // Check bool sense
261 switch( bol->_test._test ) {
262 case BoolTest::lt: cmp_zero_idx = 1; phi_x_idx = IfTrue; break;
263 case BoolTest::le: cmp_zero_idx = 2; phi_x_idx = IfFalse; break;
264 case BoolTest::gt: cmp_zero_idx = 2; phi_x_idx = IfTrue; break;
265 case BoolTest::ge: cmp_zero_idx = 1; phi_x_idx = IfFalse; break;
266 default: return NULL; break;
267 }
268
269 // Find zero input of CmpF; the other input is being abs'd
270 Node *cmpf = bol->in(1);
271 if( cmpf->Opcode() != Op_CmpF ) return NULL;
272 Node *X = NULL;
273 bool flip = false;
274 if( phase->type(cmpf->in(cmp_zero_idx)) == TypeF::ZERO ) {
275 X = cmpf->in(3 - cmp_zero_idx);
276 } else if (phase->type(cmpf->in(3 - cmp_zero_idx)) == TypeF::ZERO) {
277 // The test is inverted, we should invert the result...
278 X = cmpf->in(cmp_zero_idx);
279 flip = true;
280 } else {
281 return NULL;
282 }
283
284 // If X is found on the appropriate phi input, find the subtract on the other
285 if( X != in(phi_x_idx) ) return NULL;
286 int phi_sub_idx = phi_x_idx == IfTrue ? IfFalse : IfTrue;
287 Node *sub = in(phi_sub_idx);
288
289 // Allow only SubF(0,X) and fail out for all others; NegF is not OK
290 if( sub->Opcode() != Op_SubF ||
291 sub->in(2) != X ||
292 phase->type(sub->in(1)) != TypeF::ZERO ) return NULL;
293
294 Node *abs = new AbsFNode( X );
295 if( flip )
296 abs = new SubFNode(sub->in(1), phase->transform(abs));
297
298 return abs;
299 }
300
301 //=============================================================================
302 //------------------------------Ideal------------------------------------------
303 // Return a node which is more "ideal" than the current node.
304 // Check for absolute value
Ideal(PhaseGVN * phase,bool can_reshape)305 Node *CMoveDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
306 // Try generic ideal's first
307 Node *x = CMoveNode::Ideal(phase, can_reshape);
308 if( x ) return x;
309
310 int cmp_zero_idx = 0; // Index of compare input where to look for zero
311 int phi_x_idx = 0; // Index of phi input where to find naked x
312
313 // Find the Bool
314 if( !in(1)->is_Bool() ) return NULL;
315 BoolNode *bol = in(1)->as_Bool();
316 // Check bool sense
317 switch( bol->_test._test ) {
318 case BoolTest::lt: cmp_zero_idx = 1; phi_x_idx = IfTrue; break;
319 case BoolTest::le: cmp_zero_idx = 2; phi_x_idx = IfFalse; break;
320 case BoolTest::gt: cmp_zero_idx = 2; phi_x_idx = IfTrue; break;
321 case BoolTest::ge: cmp_zero_idx = 1; phi_x_idx = IfFalse; break;
322 default: return NULL; break;
323 }
324
325 // Find zero input of CmpD; the other input is being abs'd
326 Node *cmpd = bol->in(1);
327 if( cmpd->Opcode() != Op_CmpD ) return NULL;
328 Node *X = NULL;
329 bool flip = false;
330 if( phase->type(cmpd->in(cmp_zero_idx)) == TypeD::ZERO ) {
331 X = cmpd->in(3 - cmp_zero_idx);
332 } else if (phase->type(cmpd->in(3 - cmp_zero_idx)) == TypeD::ZERO) {
333 // The test is inverted, we should invert the result...
334 X = cmpd->in(cmp_zero_idx);
335 flip = true;
336 } else {
337 return NULL;
338 }
339
340 // If X is found on the appropriate phi input, find the subtract on the other
341 if( X != in(phi_x_idx) ) return NULL;
342 int phi_sub_idx = phi_x_idx == IfTrue ? IfFalse : IfTrue;
343 Node *sub = in(phi_sub_idx);
344
345 // Allow only SubD(0,X) and fail out for all others; NegD is not OK
346 if( sub->Opcode() != Op_SubD ||
347 sub->in(2) != X ||
348 phase->type(sub->in(1)) != TypeD::ZERO ) return NULL;
349
350 Node *abs = new AbsDNode( X );
351 if( flip )
352 abs = new SubDNode(sub->in(1), phase->transform(abs));
353
354 return abs;
355 }
356
357 //------------------------------MoveNode------------------------------------------
358
Ideal(PhaseGVN * phase,bool can_reshape)359 Node* MoveNode::Ideal(PhaseGVN* phase, bool can_reshape) {
360 if (can_reshape) {
361 // Fold reinterpret cast into memory operation:
362 // MoveX2Y (LoadX mem) => LoadY mem
363 LoadNode* ld = in(1)->isa_Load();
364 if (ld != NULL && (ld->outcnt() == 1)) { // replace only
365 const Type* rt = bottom_type();
366 if (ld->has_reinterpret_variant(rt)) {
367 if (phase->C->post_loop_opts_phase()) {
368 return ld->convert_to_reinterpret_load(*phase, rt);
369 } else {
370 phase->C->record_for_post_loop_opts_igvn(this); // attempt the transformation once loop opts are over
371 }
372 }
373 }
374 }
375 return NULL;
376 }
377
Identity(PhaseGVN * phase)378 Node* MoveNode::Identity(PhaseGVN* phase) {
379 if (in(1)->is_Move()) {
380 // Back-to-back moves: MoveX2Y (MoveY2X v) => v
381 assert(bottom_type() == in(1)->in(1)->bottom_type(), "sanity");
382 return in(1)->in(1);
383 }
384 return this;
385 }
386
387 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const388 const Type* MoveL2DNode::Value(PhaseGVN* phase) const {
389 const Type *t = phase->type( in(1) );
390 if( t == Type::TOP ) return Type::TOP;
391 const TypeLong *tl = t->is_long();
392 if( !tl->is_con() ) return bottom_type();
393 JavaValue v;
394 v.set_jlong(tl->get_con());
395 return TypeD::make( v.get_jdouble() );
396 }
397
398 //------------------------------Identity----------------------------------------
Identity(PhaseGVN * phase)399 Node* MoveL2DNode::Identity(PhaseGVN* phase) {
400 if (in(1)->Opcode() == Op_MoveD2L) {
401 return in(1)->in(1);
402 }
403 return this;
404 }
405
406 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const407 const Type* MoveI2FNode::Value(PhaseGVN* phase) const {
408 const Type *t = phase->type( in(1) );
409 if( t == Type::TOP ) return Type::TOP;
410 const TypeInt *ti = t->is_int();
411 if( !ti->is_con() ) return bottom_type();
412 JavaValue v;
413 v.set_jint(ti->get_con());
414 return TypeF::make( v.get_jfloat() );
415 }
416
417 //------------------------------Identity----------------------------------------
Identity(PhaseGVN * phase)418 Node* MoveI2FNode::Identity(PhaseGVN* phase) {
419 if (in(1)->Opcode() == Op_MoveF2I) {
420 return in(1)->in(1);
421 }
422 return this;
423 }
424
425 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const426 const Type* MoveF2INode::Value(PhaseGVN* phase) const {
427 const Type *t = phase->type( in(1) );
428 if( t == Type::TOP ) return Type::TOP;
429 if( t == Type::FLOAT ) return TypeInt::INT;
430 const TypeF *tf = t->is_float_constant();
431 JavaValue v;
432 v.set_jfloat(tf->getf());
433 return TypeInt::make( v.get_jint() );
434 }
435
436 //------------------------------Identity----------------------------------------
Identity(PhaseGVN * phase)437 Node* MoveF2INode::Identity(PhaseGVN* phase) {
438 if (in(1)->Opcode() == Op_MoveI2F) {
439 return in(1)->in(1);
440 }
441 return this;
442 }
443
444 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const445 const Type* MoveD2LNode::Value(PhaseGVN* phase) const {
446 const Type *t = phase->type( in(1) );
447 if( t == Type::TOP ) return Type::TOP;
448 if( t == Type::DOUBLE ) return TypeLong::LONG;
449 const TypeD *td = t->is_double_constant();
450 JavaValue v;
451 v.set_jdouble(td->getd());
452 return TypeLong::make( v.get_jlong() );
453 }
454
455 //------------------------------Identity----------------------------------------
Identity(PhaseGVN * phase)456 Node* MoveD2LNode::Identity(PhaseGVN* phase) {
457 if (in(1)->Opcode() == Op_MoveL2D) {
458 return in(1)->in(1);
459 }
460 return this;
461 }
462
463 #ifndef PRODUCT
464 //----------------------------BinaryNode---------------------------------------
465 // The set of related nodes for a BinaryNode is all data inputs and all outputs
466 // till level 2 (i.e., one beyond the associated CMoveNode). In compact mode,
467 // it's the inputs till level 1 and the outputs till level 2.
related(GrowableArray<Node * > * in_rel,GrowableArray<Node * > * out_rel,bool compact) const468 void BinaryNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {
469 if (compact) {
470 this->collect_nodes(in_rel, 1, false, true);
471 } else {
472 this->collect_nodes_in_all_data(in_rel, false);
473 }
474 this->collect_nodes(out_rel, -2, false, false);
475 }
476 #endif
477