Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/hotspot/share/opto/addnode.cpp
64440 views
1
/*
2
* Copyright (c) 1997, 2021, 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 "memory/allocation.inline.hpp"
27
#include "opto/addnode.hpp"
28
#include "opto/castnode.hpp"
29
#include "opto/cfgnode.hpp"
30
#include "opto/connode.hpp"
31
#include "opto/machnode.hpp"
32
#include "opto/movenode.hpp"
33
#include "opto/mulnode.hpp"
34
#include "opto/phaseX.hpp"
35
#include "opto/subnode.hpp"
36
37
// Portions of code courtesy of Clifford Click
38
39
// Classic Add functionality. This covers all the usual 'add' behaviors for
40
// an algebraic ring. Add-integer, add-float, add-double, and binary-or are
41
// all inherited from this class. The various identity values are supplied
42
// by virtual functions.
43
44
45
//=============================================================================
46
//------------------------------hash-------------------------------------------
47
// Hash function over AddNodes. Needs to be commutative; i.e., I swap
48
// (commute) inputs to AddNodes willy-nilly so the hash function must return
49
// the same value in the presence of edge swapping.
50
uint AddNode::hash() const {
51
return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();
52
}
53
54
//------------------------------Identity---------------------------------------
55
// If either input is a constant 0, return the other input.
56
Node* AddNode::Identity(PhaseGVN* phase) {
57
const Type *zero = add_id(); // The additive identity
58
if( phase->type( in(1) )->higher_equal( zero ) ) return in(2);
59
if( phase->type( in(2) )->higher_equal( zero ) ) return in(1);
60
return this;
61
}
62
63
//------------------------------commute----------------------------------------
64
// Commute operands to move loads and constants to the right.
65
static bool commute(PhaseGVN* phase, Node* add) {
66
Node *in1 = add->in(1);
67
Node *in2 = add->in(2);
68
69
// convert "max(a,b) + min(a,b)" into "a+b".
70
if ((in1->Opcode() == add->as_Add()->max_opcode() && in2->Opcode() == add->as_Add()->min_opcode())
71
|| (in1->Opcode() == add->as_Add()->min_opcode() && in2->Opcode() == add->as_Add()->max_opcode())) {
72
Node *in11 = in1->in(1);
73
Node *in12 = in1->in(2);
74
75
Node *in21 = in2->in(1);
76
Node *in22 = in2->in(2);
77
78
if ((in11 == in21 && in12 == in22) ||
79
(in11 == in22 && in12 == in21)) {
80
add->set_req(1, in11);
81
add->set_req(2, in12);
82
PhaseIterGVN* igvn = phase->is_IterGVN();
83
if (igvn) {
84
igvn->_worklist.push(in1);
85
igvn->_worklist.push(in2);
86
}
87
return true;
88
}
89
}
90
91
bool con_left = phase->type(in1)->singleton();
92
bool con_right = phase->type(in2)->singleton();
93
94
// Convert "1+x" into "x+1".
95
// Right is a constant; leave it
96
if( con_right ) return false;
97
// Left is a constant; move it right.
98
if( con_left ) {
99
add->swap_edges(1, 2);
100
return true;
101
}
102
103
// Convert "Load+x" into "x+Load".
104
// Now check for loads
105
if (in2->is_Load()) {
106
if (!in1->is_Load()) {
107
// already x+Load to return
108
return false;
109
}
110
// both are loads, so fall through to sort inputs by idx
111
} else if( in1->is_Load() ) {
112
// Left is a Load and Right is not; move it right.
113
add->swap_edges(1, 2);
114
return true;
115
}
116
117
PhiNode *phi;
118
// Check for tight loop increments: Loop-phi of Add of loop-phi
119
if (in1->is_Phi() && (phi = in1->as_Phi()) && phi->region()->is_Loop() && phi->in(2) == add)
120
return false;
121
if (in2->is_Phi() && (phi = in2->as_Phi()) && phi->region()->is_Loop() && phi->in(2) == add) {
122
add->swap_edges(1, 2);
123
return true;
124
}
125
126
// Otherwise, sort inputs (commutativity) to help value numbering.
127
if( in1->_idx > in2->_idx ) {
128
add->swap_edges(1, 2);
129
return true;
130
}
131
return false;
132
}
133
134
//------------------------------Idealize---------------------------------------
135
// If we get here, we assume we are associative!
136
Node *AddNode::Ideal(PhaseGVN *phase, bool can_reshape) {
137
const Type *t1 = phase->type(in(1));
138
const Type *t2 = phase->type(in(2));
139
bool con_left = t1->singleton();
140
bool con_right = t2->singleton();
141
142
// Check for commutative operation desired
143
if (commute(phase, this)) return this;
144
145
AddNode *progress = NULL; // Progress flag
146
147
// Convert "(x+1)+2" into "x+(1+2)". If the right input is a
148
// constant, and the left input is an add of a constant, flatten the
149
// expression tree.
150
Node *add1 = in(1);
151
Node *add2 = in(2);
152
int add1_op = add1->Opcode();
153
int this_op = Opcode();
154
if (con_right && t2 != Type::TOP && // Right input is a constant?
155
add1_op == this_op) { // Left input is an Add?
156
157
// Type of left _in right input
158
const Type *t12 = phase->type(add1->in(2));
159
if (t12->singleton() && t12 != Type::TOP) { // Left input is an add of a constant?
160
// Check for rare case of closed data cycle which can happen inside
161
// unreachable loops. In these cases the computation is undefined.
162
#ifdef ASSERT
163
Node *add11 = add1->in(1);
164
int add11_op = add11->Opcode();
165
if ((add1 == add1->in(1))
166
|| (add11_op == this_op && add11->in(1) == add1)) {
167
assert(false, "dead loop in AddNode::Ideal");
168
}
169
#endif
170
// The Add of the flattened expression
171
Node *x1 = add1->in(1);
172
Node *x2 = phase->makecon(add1->as_Add()->add_ring(t2, t12));
173
set_req_X(2, x2, phase);
174
set_req_X(1, x1, phase);
175
progress = this; // Made progress
176
add1 = in(1);
177
add1_op = add1->Opcode();
178
}
179
}
180
181
// Convert "(x+1)+y" into "(x+y)+1". Push constants down the expression tree.
182
if (add1_op == this_op && !con_right) {
183
Node *a12 = add1->in(2);
184
const Type *t12 = phase->type( a12 );
185
if (t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) &&
186
!(add1->in(1)->is_Phi() && (add1->in(1)->as_Phi()->is_tripcount(T_INT) || add1->in(1)->as_Phi()->is_tripcount(T_LONG)))) {
187
assert(add1->in(1) != this, "dead loop in AddNode::Ideal");
188
add2 = add1->clone();
189
add2->set_req(2, in(2));
190
add2 = phase->transform(add2);
191
set_req_X(1, add2, phase);
192
set_req_X(2, a12, phase);
193
progress = this;
194
add2 = a12;
195
}
196
}
197
198
// Convert "x+(y+1)" into "(x+y)+1". Push constants down the expression tree.
199
int add2_op = add2->Opcode();
200
if (add2_op == this_op && !con_left) {
201
Node *a22 = add2->in(2);
202
const Type *t22 = phase->type( a22 );
203
if (t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) &&
204
!(add2->in(1)->is_Phi() && (add2->in(1)->as_Phi()->is_tripcount(T_INT) || add2->in(1)->as_Phi()->is_tripcount(T_LONG)))) {
205
assert(add2->in(1) != this, "dead loop in AddNode::Ideal");
206
Node *addx = add2->clone();
207
addx->set_req(1, in(1));
208
addx->set_req(2, add2->in(1));
209
addx = phase->transform(addx);
210
set_req_X(1, addx, phase);
211
set_req_X(2, a22, phase);
212
progress = this;
213
}
214
}
215
216
return progress;
217
}
218
219
//------------------------------Value-----------------------------------------
220
// An add node sums it's two _in. If one input is an RSD, we must mixin
221
// the other input's symbols.
222
const Type* AddNode::Value(PhaseGVN* phase) const {
223
// Either input is TOP ==> the result is TOP
224
const Type *t1 = phase->type( in(1) );
225
const Type *t2 = phase->type( in(2) );
226
if( t1 == Type::TOP ) return Type::TOP;
227
if( t2 == Type::TOP ) return Type::TOP;
228
229
// Either input is BOTTOM ==> the result is the local BOTTOM
230
const Type *bot = bottom_type();
231
if( (t1 == bot) || (t2 == bot) ||
232
(t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
233
return bot;
234
235
// Check for an addition involving the additive identity
236
const Type *tadd = add_of_identity( t1, t2 );
237
if( tadd ) return tadd;
238
239
return add_ring(t1,t2); // Local flavor of type addition
240
}
241
242
//------------------------------add_identity-----------------------------------
243
// Check for addition of the identity
244
const Type *AddNode::add_of_identity( const Type *t1, const Type *t2 ) const {
245
const Type *zero = add_id(); // The additive identity
246
if( t1->higher_equal( zero ) ) return t2;
247
if( t2->higher_equal( zero ) ) return t1;
248
249
return NULL;
250
}
251
252
AddNode* AddNode::make(Node* in1, Node* in2, BasicType bt) {
253
switch (bt) {
254
case T_INT:
255
return new AddINode(in1, in2);
256
case T_LONG:
257
return new AddLNode(in1, in2);
258
default:
259
fatal("Not implemented for %s", type2name(bt));
260
}
261
return NULL;
262
}
263
264
//=============================================================================
265
//------------------------------Idealize---------------------------------------
266
Node *AddINode::Ideal(PhaseGVN *phase, bool can_reshape) {
267
Node* in1 = in(1);
268
Node* in2 = in(2);
269
int op1 = in1->Opcode();
270
int op2 = in2->Opcode();
271
// Fold (con1-x)+con2 into (con1+con2)-x
272
if ( op1 == Op_AddI && op2 == Op_SubI ) {
273
// Swap edges to try optimizations below
274
in1 = in2;
275
in2 = in(1);
276
op1 = op2;
277
op2 = in2->Opcode();
278
}
279
if( op1 == Op_SubI ) {
280
const Type *t_sub1 = phase->type( in1->in(1) );
281
const Type *t_2 = phase->type( in2 );
282
if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
283
return new SubINode(phase->makecon( add_ring( t_sub1, t_2 ) ), in1->in(2) );
284
// Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
285
if( op2 == Op_SubI ) {
286
// Check for dead cycle: d = (a-b)+(c-d)
287
assert( in1->in(2) != this && in2->in(2) != this,
288
"dead loop in AddINode::Ideal" );
289
Node *sub = new SubINode(NULL, NULL);
290
sub->init_req(1, phase->transform(new AddINode(in1->in(1), in2->in(1) ) ));
291
sub->init_req(2, phase->transform(new AddINode(in1->in(2), in2->in(2) ) ));
292
return sub;
293
}
294
// Convert "(a-b)+(b+c)" into "(a+c)"
295
if( op2 == Op_AddI && in1->in(2) == in2->in(1) ) {
296
assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal");
297
return new AddINode(in1->in(1), in2->in(2));
298
}
299
// Convert "(a-b)+(c+b)" into "(a+c)"
300
if( op2 == Op_AddI && in1->in(2) == in2->in(2) ) {
301
assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal");
302
return new AddINode(in1->in(1), in2->in(1));
303
}
304
// Convert "(a-b)+(b-c)" into "(a-c)"
305
if( op2 == Op_SubI && in1->in(2) == in2->in(1) ) {
306
assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal");
307
return new SubINode(in1->in(1), in2->in(2));
308
}
309
// Convert "(a-b)+(c-a)" into "(c-b)"
310
if( op2 == Op_SubI && in1->in(1) == in2->in(2) ) {
311
assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddINode::Ideal");
312
return new SubINode(in2->in(1), in1->in(2));
313
}
314
}
315
316
// Convert "x+(0-y)" into "(x-y)"
317
if( op2 == Op_SubI && phase->type(in2->in(1)) == TypeInt::ZERO )
318
return new SubINode(in1, in2->in(2) );
319
320
// Convert "(0-y)+x" into "(x-y)"
321
if( op1 == Op_SubI && phase->type(in1->in(1)) == TypeInt::ZERO )
322
return new SubINode( in2, in1->in(2) );
323
324
// Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.
325
// Helps with array allocation math constant folding
326
// See 4790063:
327
// Unrestricted transformation is unsafe for some runtime values of 'x'
328
// ( x == 0, z == 1, y == -1 ) fails
329
// ( x == -5, z == 1, y == 1 ) fails
330
// Transform works for small z and small negative y when the addition
331
// (x + (y << z)) does not cross zero.
332
// Implement support for negative y and (x >= -(y << z))
333
// Have not observed cases where type information exists to support
334
// positive y and (x <= -(y << z))
335
if( op1 == Op_URShiftI && op2 == Op_ConI &&
336
in1->in(2)->Opcode() == Op_ConI ) {
337
jint z = phase->type( in1->in(2) )->is_int()->get_con() & 0x1f; // only least significant 5 bits matter
338
jint y = phase->type( in2 )->is_int()->get_con();
339
340
if( z < 5 && -5 < y && y < 0 ) {
341
const Type *t_in11 = phase->type(in1->in(1));
342
if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z)) ) {
343
Node *a = phase->transform( new AddINode( in1->in(1), phase->intcon(y<<z) ) );
344
return new URShiftINode( a, in1->in(2) );
345
}
346
}
347
}
348
349
// Convert (x >>> rshift) + (x << lshift) into RotateRight(x, rshift)
350
if (Matcher::match_rule_supported(Op_RotateRight) &&
351
((op1 == Op_URShiftI && op2 == Op_LShiftI) || (op1 == Op_LShiftI && op2 == Op_URShiftI)) &&
352
in1->in(1) != NULL && in1->in(1) == in2->in(1)) {
353
Node* rshift = op1 == Op_URShiftI ? in1->in(2) : in2->in(2);
354
Node* lshift = op1 == Op_URShiftI ? in2->in(2) : in1->in(2);
355
if (rshift != NULL && lshift != NULL) {
356
const TypeInt* rshift_t = phase->type(rshift)->isa_int();
357
const TypeInt* lshift_t = phase->type(lshift)->isa_int();
358
if (lshift_t != NULL && lshift_t->is_con() &&
359
rshift_t != NULL && rshift_t->is_con() &&
360
((lshift_t->get_con() & 0x1F) == (32 - (rshift_t->get_con() & 0x1F)))) {
361
return new RotateRightNode(in1->in(1), phase->intcon(rshift_t->get_con() & 0x1F), TypeInt::INT);
362
}
363
}
364
}
365
366
// Convert (~x+1) into -x. Note there isn't a bitwise not bytecode,
367
// "~x" would typically represented as "x^(-1)", so (~x+1) will
368
// be (x^(-1))+1.
369
if (op1 == Op_XorI && phase->type(in2) == TypeInt::ONE &&
370
phase->type(in1->in(2)) == TypeInt::MINUS_1) {
371
return new SubINode(phase->makecon(TypeInt::ZERO), in1->in(1));
372
}
373
return AddNode::Ideal(phase, can_reshape);
374
}
375
376
377
//------------------------------Identity---------------------------------------
378
// Fold (x-y)+y OR y+(x-y) into x
379
Node* AddINode::Identity(PhaseGVN* phase) {
380
if (in(1)->Opcode() == Op_SubI && in(1)->in(2) == in(2)) {
381
return in(1)->in(1);
382
} else if (in(2)->Opcode() == Op_SubI && in(2)->in(2) == in(1)) {
383
return in(2)->in(1);
384
}
385
return AddNode::Identity(phase);
386
}
387
388
389
//------------------------------add_ring---------------------------------------
390
// Supplied function returns the sum of the inputs. Guaranteed never
391
// to be passed a TOP or BOTTOM type, these are filtered out by
392
// pre-check.
393
const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const {
394
const TypeInt *r0 = t0->is_int(); // Handy access
395
const TypeInt *r1 = t1->is_int();
396
int lo = java_add(r0->_lo, r1->_lo);
397
int hi = java_add(r0->_hi, r1->_hi);
398
if( !(r0->is_con() && r1->is_con()) ) {
399
// Not both constants, compute approximate result
400
if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
401
lo = min_jint; hi = max_jint; // Underflow on the low side
402
}
403
if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
404
lo = min_jint; hi = max_jint; // Overflow on the high side
405
}
406
if( lo > hi ) { // Handle overflow
407
lo = min_jint; hi = max_jint;
408
}
409
} else {
410
// both constants, compute precise result using 'lo' and 'hi'
411
// Semantics define overflow and underflow for integer addition
412
// as expected. In particular: 0x80000000 + 0x80000000 --> 0x0
413
}
414
return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
415
}
416
417
418
//=============================================================================
419
//------------------------------Idealize---------------------------------------
420
Node *AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
421
Node* in1 = in(1);
422
Node* in2 = in(2);
423
int op1 = in1->Opcode();
424
int op2 = in2->Opcode();
425
// Fold (con1-x)+con2 into (con1+con2)-x
426
if ( op1 == Op_AddL && op2 == Op_SubL ) {
427
// Swap edges to try optimizations below
428
in1 = in2;
429
in2 = in(1);
430
op1 = op2;
431
op2 = in2->Opcode();
432
}
433
// Fold (con1-x)+con2 into (con1+con2)-x
434
if( op1 == Op_SubL ) {
435
const Type *t_sub1 = phase->type( in1->in(1) );
436
const Type *t_2 = phase->type( in2 );
437
if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
438
return new SubLNode(phase->makecon( add_ring( t_sub1, t_2 ) ), in1->in(2) );
439
// Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
440
if( op2 == Op_SubL ) {
441
// Check for dead cycle: d = (a-b)+(c-d)
442
assert( in1->in(2) != this && in2->in(2) != this,
443
"dead loop in AddLNode::Ideal" );
444
Node *sub = new SubLNode(NULL, NULL);
445
sub->init_req(1, phase->transform(new AddLNode(in1->in(1), in2->in(1) ) ));
446
sub->init_req(2, phase->transform(new AddLNode(in1->in(2), in2->in(2) ) ));
447
return sub;
448
}
449
// Convert "(a-b)+(b+c)" into "(a+c)"
450
if( op2 == Op_AddL && in1->in(2) == in2->in(1) ) {
451
assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");
452
return new AddLNode(in1->in(1), in2->in(2));
453
}
454
// Convert "(a-b)+(c+b)" into "(a+c)"
455
if( op2 == Op_AddL && in1->in(2) == in2->in(2) ) {
456
assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");
457
return new AddLNode(in1->in(1), in2->in(1));
458
}
459
// Convert "(a-b)+(b-c)" into "(a-c)"
460
if( op2 == Op_SubL && in1->in(2) == in2->in(1) ) {
461
assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");
462
return new SubLNode(in1->in(1), in2->in(2));
463
}
464
// Convert "(a-b)+(c-a)" into "(c-b)"
465
if( op2 == Op_SubL && in1->in(1) == in2->in(2) ) {
466
assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");
467
return new SubLNode(in2->in(1), in1->in(2));
468
}
469
}
470
471
// Convert "x+(0-y)" into "(x-y)"
472
if( op2 == Op_SubL && phase->type(in2->in(1)) == TypeLong::ZERO )
473
return new SubLNode( in1, in2->in(2) );
474
475
// Convert "(0-y)+x" into "(x-y)"
476
if( op1 == Op_SubL && phase->type(in1->in(1)) == TypeLong::ZERO )
477
return new SubLNode( in2, in1->in(2) );
478
479
// Convert (x >>> rshift) + (x << lshift) into RotateRight(x, rshift)
480
if (Matcher::match_rule_supported(Op_RotateRight) &&
481
((op1 == Op_URShiftL && op2 == Op_LShiftL) || (op1 == Op_LShiftL && op2 == Op_URShiftL)) &&
482
in1->in(1) != NULL && in1->in(1) == in2->in(1)) {
483
Node* rshift = op1 == Op_URShiftL ? in1->in(2) : in2->in(2);
484
Node* lshift = op1 == Op_URShiftL ? in2->in(2) : in1->in(2);
485
if (rshift != NULL && lshift != NULL) {
486
const TypeInt* rshift_t = phase->type(rshift)->isa_int();
487
const TypeInt* lshift_t = phase->type(lshift)->isa_int();
488
if (lshift_t != NULL && lshift_t->is_con() &&
489
rshift_t != NULL && rshift_t->is_con() &&
490
((lshift_t->get_con() & 0x3F) == (64 - (rshift_t->get_con() & 0x3F)))) {
491
return new RotateRightNode(in1->in(1), phase->intcon(rshift_t->get_con() & 0x3F), TypeLong::LONG);
492
}
493
}
494
}
495
496
// Convert (~x+1) into -x. Note there isn't a bitwise not bytecode,
497
// "~x" would typically represented as "x^(-1)", so (~x+1) will
498
// be (x^(-1))+1
499
if (op1 == Op_XorL && phase->type(in2) == TypeLong::ONE &&
500
phase->type(in1->in(2)) == TypeLong::MINUS_1) {
501
return new SubLNode(phase->makecon(TypeLong::ZERO), in1->in(1));
502
}
503
return AddNode::Ideal(phase, can_reshape);
504
}
505
506
507
//------------------------------Identity---------------------------------------
508
// Fold (x-y)+y OR y+(x-y) into x
509
Node* AddLNode::Identity(PhaseGVN* phase) {
510
if (in(1)->Opcode() == Op_SubL && in(1)->in(2) == in(2)) {
511
return in(1)->in(1);
512
} else if (in(2)->Opcode() == Op_SubL && in(2)->in(2) == in(1)) {
513
return in(2)->in(1);
514
}
515
return AddNode::Identity(phase);
516
}
517
518
519
//------------------------------add_ring---------------------------------------
520
// Supplied function returns the sum of the inputs. Guaranteed never
521
// to be passed a TOP or BOTTOM type, these are filtered out by
522
// pre-check.
523
const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const {
524
const TypeLong *r0 = t0->is_long(); // Handy access
525
const TypeLong *r1 = t1->is_long();
526
jlong lo = java_add(r0->_lo, r1->_lo);
527
jlong hi = java_add(r0->_hi, r1->_hi);
528
if( !(r0->is_con() && r1->is_con()) ) {
529
// Not both constants, compute approximate result
530
if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
531
lo =min_jlong; hi = max_jlong; // Underflow on the low side
532
}
533
if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
534
lo = min_jlong; hi = max_jlong; // Overflow on the high side
535
}
536
if( lo > hi ) { // Handle overflow
537
lo = min_jlong; hi = max_jlong;
538
}
539
} else {
540
// both constants, compute precise result using 'lo' and 'hi'
541
// Semantics define overflow and underflow for integer addition
542
// as expected. In particular: 0x80000000 + 0x80000000 --> 0x0
543
}
544
return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
545
}
546
547
548
//=============================================================================
549
//------------------------------add_of_identity--------------------------------
550
// Check for addition of the identity
551
const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {
552
// x ADD 0 should return x unless 'x' is a -zero
553
//
554
// const Type *zero = add_id(); // The additive identity
555
// jfloat f1 = t1->getf();
556
// jfloat f2 = t2->getf();
557
//
558
// if( t1->higher_equal( zero ) ) return t2;
559
// if( t2->higher_equal( zero ) ) return t1;
560
561
return NULL;
562
}
563
564
//------------------------------add_ring---------------------------------------
565
// Supplied function returns the sum of the inputs.
566
// This also type-checks the inputs for sanity. Guaranteed never to
567
// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
568
const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {
569
// We must be adding 2 float constants.
570
return TypeF::make( t0->getf() + t1->getf() );
571
}
572
573
//------------------------------Ideal------------------------------------------
574
Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
575
// Floating point additions are not associative because of boundary conditions (infinity)
576
return commute(phase, this) ? this : NULL;
577
}
578
579
580
//=============================================================================
581
//------------------------------add_of_identity--------------------------------
582
// Check for addition of the identity
583
const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const {
584
// x ADD 0 should return x unless 'x' is a -zero
585
//
586
// const Type *zero = add_id(); // The additive identity
587
// jfloat f1 = t1->getf();
588
// jfloat f2 = t2->getf();
589
//
590
// if( t1->higher_equal( zero ) ) return t2;
591
// if( t2->higher_equal( zero ) ) return t1;
592
593
return NULL;
594
}
595
//------------------------------add_ring---------------------------------------
596
// Supplied function returns the sum of the inputs.
597
// This also type-checks the inputs for sanity. Guaranteed never to
598
// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
599
const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {
600
// We must be adding 2 double constants.
601
return TypeD::make( t0->getd() + t1->getd() );
602
}
603
604
//------------------------------Ideal------------------------------------------
605
Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
606
// Floating point additions are not associative because of boundary conditions (infinity)
607
return commute(phase, this) ? this : NULL;
608
}
609
610
611
//=============================================================================
612
//------------------------------Identity---------------------------------------
613
// If one input is a constant 0, return the other input.
614
Node* AddPNode::Identity(PhaseGVN* phase) {
615
return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this;
616
}
617
618
//------------------------------Idealize---------------------------------------
619
Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
620
// Bail out if dead inputs
621
if( phase->type( in(Address) ) == Type::TOP ) return NULL;
622
623
// If the left input is an add of a constant, flatten the expression tree.
624
const Node *n = in(Address);
625
if (n->is_AddP() && n->in(Base) == in(Base)) {
626
const AddPNode *addp = n->as_AddP(); // Left input is an AddP
627
assert( !addp->in(Address)->is_AddP() ||
628
addp->in(Address)->as_AddP() != addp,
629
"dead loop in AddPNode::Ideal" );
630
// Type of left input's right input
631
const Type *t = phase->type( addp->in(Offset) );
632
if( t == Type::TOP ) return NULL;
633
const TypeX *t12 = t->is_intptr_t();
634
if( t12->is_con() ) { // Left input is an add of a constant?
635
// If the right input is a constant, combine constants
636
const Type *temp_t2 = phase->type( in(Offset) );
637
if( temp_t2 == Type::TOP ) return NULL;
638
const TypeX *t2 = temp_t2->is_intptr_t();
639
Node* address;
640
Node* offset;
641
if( t2->is_con() ) {
642
// The Add of the flattened expression
643
address = addp->in(Address);
644
offset = phase->MakeConX(t2->get_con() + t12->get_con());
645
} else {
646
// Else move the constant to the right. ((A+con)+B) into ((A+B)+con)
647
address = phase->transform(new AddPNode(in(Base),addp->in(Address),in(Offset)));
648
offset = addp->in(Offset);
649
}
650
set_req_X(Address, address, phase);
651
set_req_X(Offset, offset, phase);
652
return this;
653
}
654
}
655
656
// Raw pointers?
657
if( in(Base)->bottom_type() == Type::TOP ) {
658
// If this is a NULL+long form (from unsafe accesses), switch to a rawptr.
659
if (phase->type(in(Address)) == TypePtr::NULL_PTR) {
660
Node* offset = in(Offset);
661
return new CastX2PNode(offset);
662
}
663
}
664
665
// If the right is an add of a constant, push the offset down.
666
// Convert: (ptr + (offset+con)) into (ptr+offset)+con.
667
// The idea is to merge array_base+scaled_index groups together,
668
// and only have different constant offsets from the same base.
669
const Node *add = in(Offset);
670
if( add->Opcode() == Op_AddX && add->in(1) != add ) {
671
const Type *t22 = phase->type( add->in(2) );
672
if( t22->singleton() && (t22 != Type::TOP) ) { // Right input is an add of a constant?
673
set_req(Address, phase->transform(new AddPNode(in(Base),in(Address),add->in(1))));
674
set_req(Offset, add->in(2));
675
PhaseIterGVN* igvn = phase->is_IterGVN();
676
if (add->outcnt() == 0 && igvn) {
677
// add disconnected.
678
igvn->_worklist.push((Node*)add);
679
}
680
return this; // Made progress
681
}
682
}
683
684
return NULL; // No progress
685
}
686
687
//------------------------------bottom_type------------------------------------
688
// Bottom-type is the pointer-type with unknown offset.
689
const Type *AddPNode::bottom_type() const {
690
if (in(Address) == NULL) return TypePtr::BOTTOM;
691
const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();
692
if( !tp ) return Type::TOP; // TOP input means TOP output
693
assert( in(Offset)->Opcode() != Op_ConP, "" );
694
const Type *t = in(Offset)->bottom_type();
695
if( t == Type::TOP )
696
return tp->add_offset(Type::OffsetTop);
697
const TypeX *tx = t->is_intptr_t();
698
intptr_t txoffset = Type::OffsetBot;
699
if (tx->is_con()) { // Left input is an add of a constant?
700
txoffset = tx->get_con();
701
}
702
return tp->add_offset(txoffset);
703
}
704
705
//------------------------------Value------------------------------------------
706
const Type* AddPNode::Value(PhaseGVN* phase) const {
707
// Either input is TOP ==> the result is TOP
708
const Type *t1 = phase->type( in(Address) );
709
const Type *t2 = phase->type( in(Offset) );
710
if( t1 == Type::TOP ) return Type::TOP;
711
if( t2 == Type::TOP ) return Type::TOP;
712
713
// Left input is a pointer
714
const TypePtr *p1 = t1->isa_ptr();
715
// Right input is an int
716
const TypeX *p2 = t2->is_intptr_t();
717
// Add 'em
718
intptr_t p2offset = Type::OffsetBot;
719
if (p2->is_con()) { // Left input is an add of a constant?
720
p2offset = p2->get_con();
721
}
722
return p1->add_offset(p2offset);
723
}
724
725
//------------------------Ideal_base_and_offset--------------------------------
726
// Split an oop pointer into a base and offset.
727
// (The offset might be Type::OffsetBot in the case of an array.)
728
// Return the base, or NULL if failure.
729
Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseTransform* phase,
730
// second return value:
731
intptr_t& offset) {
732
if (ptr->is_AddP()) {
733
Node* base = ptr->in(AddPNode::Base);
734
Node* addr = ptr->in(AddPNode::Address);
735
Node* offs = ptr->in(AddPNode::Offset);
736
if (base == addr || base->is_top()) {
737
offset = phase->find_intptr_t_con(offs, Type::OffsetBot);
738
if (offset != Type::OffsetBot) {
739
return addr;
740
}
741
}
742
}
743
offset = Type::OffsetBot;
744
return NULL;
745
}
746
747
//------------------------------unpack_offsets----------------------------------
748
// Collect the AddP offset values into the elements array, giving up
749
// if there are more than length.
750
int AddPNode::unpack_offsets(Node* elements[], int length) {
751
int count = 0;
752
Node* addr = this;
753
Node* base = addr->in(AddPNode::Base);
754
while (addr->is_AddP()) {
755
if (addr->in(AddPNode::Base) != base) {
756
// give up
757
return -1;
758
}
759
elements[count++] = addr->in(AddPNode::Offset);
760
if (count == length) {
761
// give up
762
return -1;
763
}
764
addr = addr->in(AddPNode::Address);
765
}
766
if (addr != base) {
767
return -1;
768
}
769
return count;
770
}
771
772
//------------------------------match_edge-------------------------------------
773
// Do we Match on this edge index or not? Do not match base pointer edge
774
uint AddPNode::match_edge(uint idx) const {
775
return idx > Base;
776
}
777
778
//=============================================================================
779
//------------------------------Identity---------------------------------------
780
Node* OrINode::Identity(PhaseGVN* phase) {
781
// x | x => x
782
if (in(1) == in(2)) {
783
return in(1);
784
}
785
786
return AddNode::Identity(phase);
787
}
788
789
// Find shift value for Integer or Long OR.
790
Node* rotate_shift(PhaseGVN* phase, Node* lshift, Node* rshift, int mask) {
791
// val << norm_con_shift | val >> ({32|64} - norm_con_shift) => rotate_left val, norm_con_shift
792
const TypeInt* lshift_t = phase->type(lshift)->isa_int();
793
const TypeInt* rshift_t = phase->type(rshift)->isa_int();
794
if (lshift_t != NULL && lshift_t->is_con() &&
795
rshift_t != NULL && rshift_t->is_con() &&
796
((lshift_t->get_con() & mask) == ((mask + 1) - (rshift_t->get_con() & mask)))) {
797
return phase->intcon(lshift_t->get_con() & mask);
798
}
799
// val << var_shift | val >> ({0|32|64} - var_shift) => rotate_left val, var_shift
800
if (rshift->Opcode() == Op_SubI && rshift->in(2) == lshift && rshift->in(1)->is_Con()){
801
const TypeInt* shift_t = phase->type(rshift->in(1))->isa_int();
802
if (shift_t != NULL && shift_t->is_con() &&
803
(shift_t->get_con() == 0 || shift_t->get_con() == (mask + 1))) {
804
return lshift;
805
}
806
}
807
return NULL;
808
}
809
810
Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) {
811
int lopcode = in(1)->Opcode();
812
int ropcode = in(2)->Opcode();
813
if (Matcher::match_rule_supported(Op_RotateLeft) &&
814
lopcode == Op_LShiftI && ropcode == Op_URShiftI && in(1)->in(1) == in(2)->in(1)) {
815
Node* lshift = in(1)->in(2);
816
Node* rshift = in(2)->in(2);
817
Node* shift = rotate_shift(phase, lshift, rshift, 0x1F);
818
if (shift != NULL) {
819
return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT);
820
}
821
return NULL;
822
}
823
if (Matcher::match_rule_supported(Op_RotateRight) &&
824
lopcode == Op_URShiftI && ropcode == Op_LShiftI && in(1)->in(1) == in(2)->in(1)) {
825
Node* rshift = in(1)->in(2);
826
Node* lshift = in(2)->in(2);
827
Node* shift = rotate_shift(phase, rshift, lshift, 0x1F);
828
if (shift != NULL) {
829
return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT);
830
}
831
}
832
return NULL;
833
}
834
835
//------------------------------add_ring---------------------------------------
836
// Supplied function returns the sum of the inputs IN THE CURRENT RING. For
837
// the logical operations the ring's ADD is really a logical OR function.
838
// This also type-checks the inputs for sanity. Guaranteed never to
839
// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
840
const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {
841
const TypeInt *r0 = t0->is_int(); // Handy access
842
const TypeInt *r1 = t1->is_int();
843
844
// If both args are bool, can figure out better types
845
if ( r0 == TypeInt::BOOL ) {
846
if ( r1 == TypeInt::ONE) {
847
return TypeInt::ONE;
848
} else if ( r1 == TypeInt::BOOL ) {
849
return TypeInt::BOOL;
850
}
851
} else if ( r0 == TypeInt::ONE ) {
852
if ( r1 == TypeInt::BOOL ) {
853
return TypeInt::ONE;
854
}
855
}
856
857
// If either input is not a constant, just return all integers.
858
if( !r0->is_con() || !r1->is_con() )
859
return TypeInt::INT; // Any integer, but still no symbols.
860
861
// Otherwise just OR them bits.
862
return TypeInt::make( r0->get_con() | r1->get_con() );
863
}
864
865
//=============================================================================
866
//------------------------------Identity---------------------------------------
867
Node* OrLNode::Identity(PhaseGVN* phase) {
868
// x | x => x
869
if (in(1) == in(2)) {
870
return in(1);
871
}
872
873
return AddNode::Identity(phase);
874
}
875
876
Node* OrLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
877
int lopcode = in(1)->Opcode();
878
int ropcode = in(2)->Opcode();
879
if (Matcher::match_rule_supported(Op_RotateLeft) &&
880
lopcode == Op_LShiftL && ropcode == Op_URShiftL && in(1)->in(1) == in(2)->in(1)) {
881
Node* lshift = in(1)->in(2);
882
Node* rshift = in(2)->in(2);
883
Node* shift = rotate_shift(phase, lshift, rshift, 0x3F);
884
if (shift != NULL) {
885
return new RotateLeftNode(in(1)->in(1), shift, TypeLong::LONG);
886
}
887
return NULL;
888
}
889
if (Matcher::match_rule_supported(Op_RotateRight) &&
890
lopcode == Op_URShiftL && ropcode == Op_LShiftL && in(1)->in(1) == in(2)->in(1)) {
891
Node* rshift = in(1)->in(2);
892
Node* lshift = in(2)->in(2);
893
Node* shift = rotate_shift(phase, rshift, lshift, 0x3F);
894
if (shift != NULL) {
895
return new RotateRightNode(in(1)->in(1), shift, TypeLong::LONG);
896
}
897
}
898
return NULL;
899
}
900
901
//------------------------------add_ring---------------------------------------
902
const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
903
const TypeLong *r0 = t0->is_long(); // Handy access
904
const TypeLong *r1 = t1->is_long();
905
906
// If either input is not a constant, just return all integers.
907
if( !r0->is_con() || !r1->is_con() )
908
return TypeLong::LONG; // Any integer, but still no symbols.
909
910
// Otherwise just OR them bits.
911
return TypeLong::make( r0->get_con() | r1->get_con() );
912
}
913
914
//=============================================================================
915
//------------------------------Idealize---------------------------------------
916
Node* XorINode::Ideal(PhaseGVN* phase, bool can_reshape) {
917
Node* in1 = in(1);
918
Node* in2 = in(2);
919
int op1 = in1->Opcode();
920
// Convert ~(x-1) into -x. Note there isn't a bitwise not bytecode,
921
// "~x" would typically represented as "x^(-1)", and "x-c0" would
922
// convert into "x+ -c0" in SubXNode::Ideal. So ~(x-1) will eventually
923
// be (x+(-1))^-1.
924
if (op1 == Op_AddI && phase->type(in2) == TypeInt::MINUS_1 &&
925
phase->type(in1->in(2)) == TypeInt::MINUS_1) {
926
return new SubINode(phase->makecon(TypeInt::ZERO), in1->in(1));
927
}
928
return AddNode::Ideal(phase, can_reshape);
929
}
930
931
const Type* XorINode::Value(PhaseGVN* phase) const {
932
Node* in1 = in(1);
933
Node* in2 = in(2);
934
const Type* t1 = phase->type(in1);
935
const Type* t2 = phase->type(in2);
936
if (t1 == Type::TOP || t2 == Type::TOP) {
937
return Type::TOP;
938
}
939
// x ^ x ==> 0
940
if (in1->eqv_uncast(in2)) {
941
return add_id();
942
}
943
// result of xor can only have bits sets where any of the
944
// inputs have bits set. lo can always become 0.
945
const TypeInt* t1i = t1->is_int();
946
const TypeInt* t2i = t2->is_int();
947
if ((t1i->_lo >= 0) &&
948
(t1i->_hi > 0) &&
949
(t2i->_lo >= 0) &&
950
(t2i->_hi > 0)) {
951
// hi - set all bits below the highest bit. Using round_down to avoid overflow.
952
const TypeInt* t1x = TypeInt::make(0, round_down_power_of_2(t1i->_hi) + (round_down_power_of_2(t1i->_hi) - 1), t1i->_widen);
953
const TypeInt* t2x = TypeInt::make(0, round_down_power_of_2(t2i->_hi) + (round_down_power_of_2(t2i->_hi) - 1), t2i->_widen);
954
return t1x->meet(t2x);
955
}
956
return AddNode::Value(phase);
957
}
958
959
960
//------------------------------add_ring---------------------------------------
961
// Supplied function returns the sum of the inputs IN THE CURRENT RING. For
962
// the logical operations the ring's ADD is really a logical OR function.
963
// This also type-checks the inputs for sanity. Guaranteed never to
964
// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
965
const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {
966
const TypeInt *r0 = t0->is_int(); // Handy access
967
const TypeInt *r1 = t1->is_int();
968
969
// Complementing a boolean?
970
if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE
971
|| r1 == TypeInt::BOOL))
972
return TypeInt::BOOL;
973
974
if( !r0->is_con() || !r1->is_con() ) // Not constants
975
return TypeInt::INT; // Any integer, but still no symbols.
976
977
// Otherwise just XOR them bits.
978
return TypeInt::make( r0->get_con() ^ r1->get_con() );
979
}
980
981
//=============================================================================
982
//------------------------------add_ring---------------------------------------
983
const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
984
const TypeLong *r0 = t0->is_long(); // Handy access
985
const TypeLong *r1 = t1->is_long();
986
987
// If either input is not a constant, just return all integers.
988
if( !r0->is_con() || !r1->is_con() )
989
return TypeLong::LONG; // Any integer, but still no symbols.
990
991
// Otherwise just OR them bits.
992
return TypeLong::make( r0->get_con() ^ r1->get_con() );
993
}
994
995
Node* XorLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
996
Node* in1 = in(1);
997
Node* in2 = in(2);
998
int op1 = in1->Opcode();
999
// Convert ~(x-1) into -x. Note there isn't a bitwise not bytecode,
1000
// "~x" would typically represented as "x^(-1)", and "x-c0" would
1001
// convert into "x+ -c0" in SubXNode::Ideal. So ~(x-1) will eventually
1002
// be (x+(-1))^-1.
1003
if (op1 == Op_AddL && phase->type(in2) == TypeLong::MINUS_1 &&
1004
phase->type(in1->in(2)) == TypeLong::MINUS_1) {
1005
return new SubLNode(phase->makecon(TypeLong::ZERO), in1->in(1));
1006
}
1007
return AddNode::Ideal(phase, can_reshape);
1008
}
1009
1010
const Type* XorLNode::Value(PhaseGVN* phase) const {
1011
Node* in1 = in(1);
1012
Node* in2 = in(2);
1013
const Type* t1 = phase->type(in1);
1014
const Type* t2 = phase->type(in2);
1015
if (t1 == Type::TOP || t2 == Type::TOP) {
1016
return Type::TOP;
1017
}
1018
// x ^ x ==> 0
1019
if (in1->eqv_uncast(in2)) {
1020
return add_id();
1021
}
1022
// result of xor can only have bits sets where any of the
1023
// inputs have bits set. lo can always become 0.
1024
const TypeLong* t1l = t1->is_long();
1025
const TypeLong* t2l = t2->is_long();
1026
if ((t1l->_lo >= 0) &&
1027
(t1l->_hi > 0) &&
1028
(t2l->_lo >= 0) &&
1029
(t2l->_hi > 0)) {
1030
// hi - set all bits below the highest bit. Using round_down to avoid overflow.
1031
const TypeLong* t1x = TypeLong::make(0, round_down_power_of_2(t1l->_hi) + (round_down_power_of_2(t1l->_hi) - 1), t1l->_widen);
1032
const TypeLong* t2x = TypeLong::make(0, round_down_power_of_2(t2l->_hi) + (round_down_power_of_2(t2l->_hi) - 1), t2l->_widen);
1033
return t1x->meet(t2x);
1034
}
1035
return AddNode::Value(phase);
1036
}
1037
1038
Node* MaxNode::build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn) {
1039
bool is_int = gvn.type(a)->isa_int();
1040
assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1041
assert(is_int == (gvn.type(b)->isa_int() != NULL), "inconsistent inputs");
1042
Node* hook = NULL;
1043
if (gvn.is_IterGVN()) {
1044
// Make sure a and b are not destroyed
1045
hook = new Node(2);
1046
hook->init_req(0, a);
1047
hook->init_req(1, b);
1048
}
1049
Node* res = NULL;
1050
if (!is_unsigned) {
1051
if (is_max) {
1052
if (is_int) {
1053
res = gvn.transform(new MaxINode(a, b));
1054
assert(gvn.type(res)->is_int()->_lo >= t->is_int()->_lo && gvn.type(res)->is_int()->_hi <= t->is_int()->_hi, "type doesn't match");
1055
} else {
1056
Node* cmp = gvn.transform(new CmpLNode(a, b));
1057
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1058
res = gvn.transform(new CMoveLNode(bol, a, b, t->is_long()));
1059
}
1060
} else {
1061
if (is_int) {
1062
Node* res = gvn.transform(new MinINode(a, b));
1063
assert(gvn.type(res)->is_int()->_lo >= t->is_int()->_lo && gvn.type(res)->is_int()->_hi <= t->is_int()->_hi, "type doesn't match");
1064
} else {
1065
Node* cmp = gvn.transform(new CmpLNode(b, a));
1066
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1067
res = gvn.transform(new CMoveLNode(bol, a, b, t->is_long()));
1068
}
1069
}
1070
} else {
1071
if (is_max) {
1072
if (is_int) {
1073
Node* cmp = gvn.transform(new CmpUNode(a, b));
1074
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1075
res = gvn.transform(new CMoveINode(bol, a, b, t->is_int()));
1076
} else {
1077
Node* cmp = gvn.transform(new CmpULNode(a, b));
1078
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1079
res = gvn.transform(new CMoveLNode(bol, a, b, t->is_long()));
1080
}
1081
} else {
1082
if (is_int) {
1083
Node* cmp = gvn.transform(new CmpUNode(b, a));
1084
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1085
res = gvn.transform(new CMoveINode(bol, a, b, t->is_int()));
1086
} else {
1087
Node* cmp = gvn.transform(new CmpULNode(b, a));
1088
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1089
res = gvn.transform(new CMoveLNode(bol, a, b, t->is_long()));
1090
}
1091
}
1092
}
1093
if (hook != NULL) {
1094
hook->destruct(&gvn);
1095
}
1096
return res;
1097
}
1098
1099
Node* MaxNode::build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn) {
1100
bool is_int = gvn.type(a)->isa_int();
1101
assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1102
assert(is_int == (gvn.type(b)->isa_int() != NULL), "inconsistent inputs");
1103
Node* zero = NULL;
1104
if (is_int) {
1105
zero = gvn.intcon(0);
1106
} else {
1107
zero = gvn.longcon(0);
1108
}
1109
Node* hook = NULL;
1110
if (gvn.is_IterGVN()) {
1111
// Make sure a and b are not destroyed
1112
hook = new Node(2);
1113
hook->init_req(0, a);
1114
hook->init_req(1, b);
1115
}
1116
Node* res = NULL;
1117
if (is_max) {
1118
if (is_int) {
1119
Node* cmp = gvn.transform(new CmpINode(a, b));
1120
Node* sub = gvn.transform(new SubINode(a, b));
1121
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1122
res = gvn.transform(new CMoveINode(bol, sub, zero, t->is_int()));
1123
} else {
1124
Node* cmp = gvn.transform(new CmpLNode(a, b));
1125
Node* sub = gvn.transform(new SubLNode(a, b));
1126
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1127
res = gvn.transform(new CMoveLNode(bol, sub, zero, t->is_long()));
1128
}
1129
} else {
1130
if (is_int) {
1131
Node* cmp = gvn.transform(new CmpINode(b, a));
1132
Node* sub = gvn.transform(new SubINode(a, b));
1133
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1134
res = gvn.transform(new CMoveINode(bol, sub, zero, t->is_int()));
1135
} else {
1136
Node* cmp = gvn.transform(new CmpLNode(b, a));
1137
Node* sub = gvn.transform(new SubLNode(a, b));
1138
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1139
res = gvn.transform(new CMoveLNode(bol, sub, zero, t->is_long()));
1140
}
1141
}
1142
if (hook != NULL) {
1143
hook->destruct(&gvn);
1144
}
1145
return res;
1146
}
1147
1148
//=============================================================================
1149
//------------------------------add_ring---------------------------------------
1150
// Supplied function returns the sum of the inputs.
1151
const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {
1152
const TypeInt *r0 = t0->is_int(); // Handy access
1153
const TypeInt *r1 = t1->is_int();
1154
1155
// Otherwise just MAX them bits.
1156
return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1157
}
1158
1159
// Check if addition of an integer with type 't' and a constant 'c' can overflow
1160
static bool can_overflow(const TypeInt* t, jint c) {
1161
jint t_lo = t->_lo;
1162
jint t_hi = t->_hi;
1163
return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1164
(c > 0 && (java_add(t_hi, c) < t_hi)));
1165
}
1166
1167
//=============================================================================
1168
//------------------------------Idealize---------------------------------------
1169
// MINs show up in range-check loop limit calculations. Look for
1170
// "MIN2(x+c0,MIN2(y,x+c1))". Pick the smaller constant: "MIN2(x+c0,y)"
1171
Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) {
1172
Node *progress = NULL;
1173
// Force a right-spline graph
1174
Node *l = in(1);
1175
Node *r = in(2);
1176
// Transform MinI1( MinI2(a,b), c) into MinI1( a, MinI2(b,c) )
1177
// to force a right-spline graph for the rest of MinINode::Ideal().
1178
if( l->Opcode() == Op_MinI ) {
1179
assert( l != l->in(1), "dead loop in MinINode::Ideal" );
1180
r = phase->transform(new MinINode(l->in(2),r));
1181
l = l->in(1);
1182
set_req_X(1, l, phase);
1183
set_req_X(2, r, phase);
1184
return this;
1185
}
1186
1187
// Get left input & constant
1188
Node *x = l;
1189
jint x_off = 0;
1190
if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant
1191
x->in(2)->is_Con() ) {
1192
const Type *t = x->in(2)->bottom_type();
1193
if( t == Type::TOP ) return NULL; // No progress
1194
x_off = t->is_int()->get_con();
1195
x = x->in(1);
1196
}
1197
1198
// Scan a right-spline-tree for MINs
1199
Node *y = r;
1200
jint y_off = 0;
1201
// Check final part of MIN tree
1202
if( y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant
1203
y->in(2)->is_Con() ) {
1204
const Type *t = y->in(2)->bottom_type();
1205
if( t == Type::TOP ) return NULL; // No progress
1206
y_off = t->is_int()->get_con();
1207
y = y->in(1);
1208
}
1209
if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) {
1210
swap_edges(1, 2);
1211
return this;
1212
}
1213
1214
const TypeInt* tx = phase->type(x)->isa_int();
1215
1216
if( r->Opcode() == Op_MinI ) {
1217
assert( r != r->in(2), "dead loop in MinINode::Ideal" );
1218
y = r->in(1);
1219
// Check final part of MIN tree
1220
if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant
1221
y->in(2)->is_Con() ) {
1222
const Type *t = y->in(2)->bottom_type();
1223
if( t == Type::TOP ) return NULL; // No progress
1224
y_off = t->is_int()->get_con();
1225
y = y->in(1);
1226
}
1227
1228
if( x->_idx > y->_idx )
1229
return new MinINode(r->in(1),phase->transform(new MinINode(l,r->in(2))));
1230
1231
// Transform MIN2(x + c0, MIN2(x + c1, z)) into MIN2(x + MIN2(c0, c1), z)
1232
// if x == y and the additions can't overflow.
1233
if (x == y && tx != NULL &&
1234
!can_overflow(tx, x_off) &&
1235
!can_overflow(tx, y_off)) {
1236
return new MinINode(phase->transform(new AddINode(x, phase->intcon(MIN2(x_off, y_off)))), r->in(2));
1237
}
1238
} else {
1239
// Transform MIN2(x + c0, y + c1) into x + MIN2(c0, c1)
1240
// if x == y and the additions can't overflow.
1241
if (x == y && tx != NULL &&
1242
!can_overflow(tx, x_off) &&
1243
!can_overflow(tx, y_off)) {
1244
return new AddINode(x,phase->intcon(MIN2(x_off,y_off)));
1245
}
1246
}
1247
return NULL;
1248
}
1249
1250
//------------------------------add_ring---------------------------------------
1251
// Supplied function returns the sum of the inputs.
1252
const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
1253
const TypeInt *r0 = t0->is_int(); // Handy access
1254
const TypeInt *r1 = t1->is_int();
1255
1256
// Otherwise just MIN them bits.
1257
return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1258
}
1259
1260
//------------------------------add_ring---------------------------------------
1261
const Type *MinFNode::add_ring( const Type *t0, const Type *t1 ) const {
1262
const TypeF *r0 = t0->is_float_constant();
1263
const TypeF *r1 = t1->is_float_constant();
1264
1265
if (r0->is_nan()) {
1266
return r0;
1267
}
1268
if (r1->is_nan()) {
1269
return r1;
1270
}
1271
1272
float f0 = r0->getf();
1273
float f1 = r1->getf();
1274
if (f0 != 0.0f || f1 != 0.0f) {
1275
return f0 < f1 ? r0 : r1;
1276
}
1277
1278
// handle min of 0.0, -0.0 case.
1279
return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1;
1280
}
1281
1282
//------------------------------add_ring---------------------------------------
1283
const Type *MinDNode::add_ring( const Type *t0, const Type *t1 ) const {
1284
const TypeD *r0 = t0->is_double_constant();
1285
const TypeD *r1 = t1->is_double_constant();
1286
1287
if (r0->is_nan()) {
1288
return r0;
1289
}
1290
if (r1->is_nan()) {
1291
return r1;
1292
}
1293
1294
double d0 = r0->getd();
1295
double d1 = r1->getd();
1296
if (d0 != 0.0 || d1 != 0.0) {
1297
return d0 < d1 ? r0 : r1;
1298
}
1299
1300
// handle min of 0.0, -0.0 case.
1301
return (jlong_cast(d0) < jlong_cast(d1)) ? r0 : r1;
1302
}
1303
1304
//------------------------------add_ring---------------------------------------
1305
const Type *MaxFNode::add_ring( const Type *t0, const Type *t1 ) const {
1306
const TypeF *r0 = t0->is_float_constant();
1307
const TypeF *r1 = t1->is_float_constant();
1308
1309
if (r0->is_nan()) {
1310
return r0;
1311
}
1312
if (r1->is_nan()) {
1313
return r1;
1314
}
1315
1316
float f0 = r0->getf();
1317
float f1 = r1->getf();
1318
if (f0 != 0.0f || f1 != 0.0f) {
1319
return f0 > f1 ? r0 : r1;
1320
}
1321
1322
// handle max of 0.0,-0.0 case.
1323
return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1;
1324
}
1325
1326
//------------------------------add_ring---------------------------------------
1327
const Type *MaxDNode::add_ring( const Type *t0, const Type *t1 ) const {
1328
const TypeD *r0 = t0->is_double_constant();
1329
const TypeD *r1 = t1->is_double_constant();
1330
1331
if (r0->is_nan()) {
1332
return r0;
1333
}
1334
if (r1->is_nan()) {
1335
return r1;
1336
}
1337
1338
double d0 = r0->getd();
1339
double d1 = r1->getd();
1340
if (d0 != 0.0 || d1 != 0.0) {
1341
return d0 > d1 ? r0 : r1;
1342
}
1343
1344
// handle max of 0.0, -0.0 case.
1345
return (jlong_cast(d0) > jlong_cast(d1)) ? r0 : r1;
1346
}
1347
1348