Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/opto/addnode.cpp
40930 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
return AddNode::Ideal(phase, can_reshape);
367
}
368
369
370
//------------------------------Identity---------------------------------------
371
// Fold (x-y)+y OR y+(x-y) into x
372
Node* AddINode::Identity(PhaseGVN* phase) {
373
if (in(1)->Opcode() == Op_SubI && in(1)->in(2) == in(2)) {
374
return in(1)->in(1);
375
} else if (in(2)->Opcode() == Op_SubI && in(2)->in(2) == in(1)) {
376
return in(2)->in(1);
377
}
378
return AddNode::Identity(phase);
379
}
380
381
382
//------------------------------add_ring---------------------------------------
383
// Supplied function returns the sum of the inputs. Guaranteed never
384
// to be passed a TOP or BOTTOM type, these are filtered out by
385
// pre-check.
386
const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const {
387
const TypeInt *r0 = t0->is_int(); // Handy access
388
const TypeInt *r1 = t1->is_int();
389
int lo = java_add(r0->_lo, r1->_lo);
390
int hi = java_add(r0->_hi, r1->_hi);
391
if( !(r0->is_con() && r1->is_con()) ) {
392
// Not both constants, compute approximate result
393
if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
394
lo = min_jint; hi = max_jint; // Underflow on the low side
395
}
396
if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
397
lo = min_jint; hi = max_jint; // Overflow on the high side
398
}
399
if( lo > hi ) { // Handle overflow
400
lo = min_jint; hi = max_jint;
401
}
402
} else {
403
// both constants, compute precise result using 'lo' and 'hi'
404
// Semantics define overflow and underflow for integer addition
405
// as expected. In particular: 0x80000000 + 0x80000000 --> 0x0
406
}
407
return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
408
}
409
410
411
//=============================================================================
412
//------------------------------Idealize---------------------------------------
413
Node *AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
414
Node* in1 = in(1);
415
Node* in2 = in(2);
416
int op1 = in1->Opcode();
417
int op2 = in2->Opcode();
418
// Fold (con1-x)+con2 into (con1+con2)-x
419
if ( op1 == Op_AddL && op2 == Op_SubL ) {
420
// Swap edges to try optimizations below
421
in1 = in2;
422
in2 = in(1);
423
op1 = op2;
424
op2 = in2->Opcode();
425
}
426
// Fold (con1-x)+con2 into (con1+con2)-x
427
if( op1 == Op_SubL ) {
428
const Type *t_sub1 = phase->type( in1->in(1) );
429
const Type *t_2 = phase->type( in2 );
430
if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
431
return new SubLNode(phase->makecon( add_ring( t_sub1, t_2 ) ), in1->in(2) );
432
// Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
433
if( op2 == Op_SubL ) {
434
// Check for dead cycle: d = (a-b)+(c-d)
435
assert( in1->in(2) != this && in2->in(2) != this,
436
"dead loop in AddLNode::Ideal" );
437
Node *sub = new SubLNode(NULL, NULL);
438
sub->init_req(1, phase->transform(new AddLNode(in1->in(1), in2->in(1) ) ));
439
sub->init_req(2, phase->transform(new AddLNode(in1->in(2), in2->in(2) ) ));
440
return sub;
441
}
442
// Convert "(a-b)+(b+c)" into "(a+c)"
443
if( op2 == Op_AddL && in1->in(2) == in2->in(1) ) {
444
assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");
445
return new AddLNode(in1->in(1), in2->in(2));
446
}
447
// Convert "(a-b)+(c+b)" into "(a+c)"
448
if( op2 == Op_AddL && in1->in(2) == in2->in(2) ) {
449
assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");
450
return new AddLNode(in1->in(1), in2->in(1));
451
}
452
// Convert "(a-b)+(b-c)" into "(a-c)"
453
if( op2 == Op_SubL && in1->in(2) == in2->in(1) ) {
454
assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");
455
return new SubLNode(in1->in(1), in2->in(2));
456
}
457
// Convert "(a-b)+(c-a)" into "(c-b)"
458
if( op2 == Op_SubL && in1->in(1) == in2->in(2) ) {
459
assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");
460
return new SubLNode(in2->in(1), in1->in(2));
461
}
462
}
463
464
// Convert "x+(0-y)" into "(x-y)"
465
if( op2 == Op_SubL && phase->type(in2->in(1)) == TypeLong::ZERO )
466
return new SubLNode( in1, in2->in(2) );
467
468
// Convert "(0-y)+x" into "(x-y)"
469
if( op1 == Op_SubL && phase->type(in1->in(1)) == TypeLong::ZERO )
470
return new SubLNode( in2, in1->in(2) );
471
472
// Convert (x >>> rshift) + (x << lshift) into RotateRight(x, rshift)
473
if (Matcher::match_rule_supported(Op_RotateRight) &&
474
((op1 == Op_URShiftL && op2 == Op_LShiftL) || (op1 == Op_LShiftL && op2 == Op_URShiftL)) &&
475
in1->in(1) != NULL && in1->in(1) == in2->in(1)) {
476
Node* rshift = op1 == Op_URShiftL ? in1->in(2) : in2->in(2);
477
Node* lshift = op1 == Op_URShiftL ? in2->in(2) : in1->in(2);
478
if (rshift != NULL && lshift != NULL) {
479
const TypeInt* rshift_t = phase->type(rshift)->isa_int();
480
const TypeInt* lshift_t = phase->type(lshift)->isa_int();
481
if (lshift_t != NULL && lshift_t->is_con() &&
482
rshift_t != NULL && rshift_t->is_con() &&
483
((lshift_t->get_con() & 0x3F) == (64 - (rshift_t->get_con() & 0x3F)))) {
484
return new RotateRightNode(in1->in(1), phase->intcon(rshift_t->get_con() & 0x3F), TypeLong::LONG);
485
}
486
}
487
}
488
489
490
return AddNode::Ideal(phase, can_reshape);
491
}
492
493
494
//------------------------------Identity---------------------------------------
495
// Fold (x-y)+y OR y+(x-y) into x
496
Node* AddLNode::Identity(PhaseGVN* phase) {
497
if (in(1)->Opcode() == Op_SubL && in(1)->in(2) == in(2)) {
498
return in(1)->in(1);
499
} else if (in(2)->Opcode() == Op_SubL && in(2)->in(2) == in(1)) {
500
return in(2)->in(1);
501
}
502
return AddNode::Identity(phase);
503
}
504
505
506
//------------------------------add_ring---------------------------------------
507
// Supplied function returns the sum of the inputs. Guaranteed never
508
// to be passed a TOP or BOTTOM type, these are filtered out by
509
// pre-check.
510
const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const {
511
const TypeLong *r0 = t0->is_long(); // Handy access
512
const TypeLong *r1 = t1->is_long();
513
jlong lo = java_add(r0->_lo, r1->_lo);
514
jlong hi = java_add(r0->_hi, r1->_hi);
515
if( !(r0->is_con() && r1->is_con()) ) {
516
// Not both constants, compute approximate result
517
if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
518
lo =min_jlong; hi = max_jlong; // Underflow on the low side
519
}
520
if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
521
lo = min_jlong; hi = max_jlong; // Overflow on the high side
522
}
523
if( lo > hi ) { // Handle overflow
524
lo = min_jlong; hi = max_jlong;
525
}
526
} else {
527
// both constants, compute precise result using 'lo' and 'hi'
528
// Semantics define overflow and underflow for integer addition
529
// as expected. In particular: 0x80000000 + 0x80000000 --> 0x0
530
}
531
return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
532
}
533
534
535
//=============================================================================
536
//------------------------------add_of_identity--------------------------------
537
// Check for addition of the identity
538
const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {
539
// x ADD 0 should return x unless 'x' is a -zero
540
//
541
// const Type *zero = add_id(); // The additive identity
542
// jfloat f1 = t1->getf();
543
// jfloat f2 = t2->getf();
544
//
545
// if( t1->higher_equal( zero ) ) return t2;
546
// if( t2->higher_equal( zero ) ) return t1;
547
548
return NULL;
549
}
550
551
//------------------------------add_ring---------------------------------------
552
// Supplied function returns the sum of the inputs.
553
// This also type-checks the inputs for sanity. Guaranteed never to
554
// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
555
const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {
556
// We must be adding 2 float constants.
557
return TypeF::make( t0->getf() + t1->getf() );
558
}
559
560
//------------------------------Ideal------------------------------------------
561
Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
562
// Floating point additions are not associative because of boundary conditions (infinity)
563
return commute(phase, this) ? this : NULL;
564
}
565
566
567
//=============================================================================
568
//------------------------------add_of_identity--------------------------------
569
// Check for addition of the identity
570
const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const {
571
// x ADD 0 should return x unless 'x' is a -zero
572
//
573
// const Type *zero = add_id(); // The additive identity
574
// jfloat f1 = t1->getf();
575
// jfloat f2 = t2->getf();
576
//
577
// if( t1->higher_equal( zero ) ) return t2;
578
// if( t2->higher_equal( zero ) ) return t1;
579
580
return NULL;
581
}
582
//------------------------------add_ring---------------------------------------
583
// Supplied function returns the sum of the inputs.
584
// This also type-checks the inputs for sanity. Guaranteed never to
585
// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
586
const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {
587
// We must be adding 2 double constants.
588
return TypeD::make( t0->getd() + t1->getd() );
589
}
590
591
//------------------------------Ideal------------------------------------------
592
Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
593
// Floating point additions are not associative because of boundary conditions (infinity)
594
return commute(phase, this) ? this : NULL;
595
}
596
597
598
//=============================================================================
599
//------------------------------Identity---------------------------------------
600
// If one input is a constant 0, return the other input.
601
Node* AddPNode::Identity(PhaseGVN* phase) {
602
return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this;
603
}
604
605
//------------------------------Idealize---------------------------------------
606
Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
607
// Bail out if dead inputs
608
if( phase->type( in(Address) ) == Type::TOP ) return NULL;
609
610
// If the left input is an add of a constant, flatten the expression tree.
611
const Node *n = in(Address);
612
if (n->is_AddP() && n->in(Base) == in(Base)) {
613
const AddPNode *addp = n->as_AddP(); // Left input is an AddP
614
assert( !addp->in(Address)->is_AddP() ||
615
addp->in(Address)->as_AddP() != addp,
616
"dead loop in AddPNode::Ideal" );
617
// Type of left input's right input
618
const Type *t = phase->type( addp->in(Offset) );
619
if( t == Type::TOP ) return NULL;
620
const TypeX *t12 = t->is_intptr_t();
621
if( t12->is_con() ) { // Left input is an add of a constant?
622
// If the right input is a constant, combine constants
623
const Type *temp_t2 = phase->type( in(Offset) );
624
if( temp_t2 == Type::TOP ) return NULL;
625
const TypeX *t2 = temp_t2->is_intptr_t();
626
Node* address;
627
Node* offset;
628
if( t2->is_con() ) {
629
// The Add of the flattened expression
630
address = addp->in(Address);
631
offset = phase->MakeConX(t2->get_con() + t12->get_con());
632
} else {
633
// Else move the constant to the right. ((A+con)+B) into ((A+B)+con)
634
address = phase->transform(new AddPNode(in(Base),addp->in(Address),in(Offset)));
635
offset = addp->in(Offset);
636
}
637
set_req_X(Address, address, phase);
638
set_req_X(Offset, offset, phase);
639
return this;
640
}
641
}
642
643
// Raw pointers?
644
if( in(Base)->bottom_type() == Type::TOP ) {
645
// If this is a NULL+long form (from unsafe accesses), switch to a rawptr.
646
if (phase->type(in(Address)) == TypePtr::NULL_PTR) {
647
Node* offset = in(Offset);
648
return new CastX2PNode(offset);
649
}
650
}
651
652
// If the right is an add of a constant, push the offset down.
653
// Convert: (ptr + (offset+con)) into (ptr+offset)+con.
654
// The idea is to merge array_base+scaled_index groups together,
655
// and only have different constant offsets from the same base.
656
const Node *add = in(Offset);
657
if( add->Opcode() == Op_AddX && add->in(1) != add ) {
658
const Type *t22 = phase->type( add->in(2) );
659
if( t22->singleton() && (t22 != Type::TOP) ) { // Right input is an add of a constant?
660
set_req(Address, phase->transform(new AddPNode(in(Base),in(Address),add->in(1))));
661
set_req(Offset, add->in(2));
662
PhaseIterGVN* igvn = phase->is_IterGVN();
663
if (add->outcnt() == 0 && igvn) {
664
// add disconnected.
665
igvn->_worklist.push((Node*)add);
666
}
667
return this; // Made progress
668
}
669
}
670
671
return NULL; // No progress
672
}
673
674
//------------------------------bottom_type------------------------------------
675
// Bottom-type is the pointer-type with unknown offset.
676
const Type *AddPNode::bottom_type() const {
677
if (in(Address) == NULL) return TypePtr::BOTTOM;
678
const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();
679
if( !tp ) return Type::TOP; // TOP input means TOP output
680
assert( in(Offset)->Opcode() != Op_ConP, "" );
681
const Type *t = in(Offset)->bottom_type();
682
if( t == Type::TOP )
683
return tp->add_offset(Type::OffsetTop);
684
const TypeX *tx = t->is_intptr_t();
685
intptr_t txoffset = Type::OffsetBot;
686
if (tx->is_con()) { // Left input is an add of a constant?
687
txoffset = tx->get_con();
688
}
689
return tp->add_offset(txoffset);
690
}
691
692
//------------------------------Value------------------------------------------
693
const Type* AddPNode::Value(PhaseGVN* phase) const {
694
// Either input is TOP ==> the result is TOP
695
const Type *t1 = phase->type( in(Address) );
696
const Type *t2 = phase->type( in(Offset) );
697
if( t1 == Type::TOP ) return Type::TOP;
698
if( t2 == Type::TOP ) return Type::TOP;
699
700
// Left input is a pointer
701
const TypePtr *p1 = t1->isa_ptr();
702
// Right input is an int
703
const TypeX *p2 = t2->is_intptr_t();
704
// Add 'em
705
intptr_t p2offset = Type::OffsetBot;
706
if (p2->is_con()) { // Left input is an add of a constant?
707
p2offset = p2->get_con();
708
}
709
return p1->add_offset(p2offset);
710
}
711
712
//------------------------Ideal_base_and_offset--------------------------------
713
// Split an oop pointer into a base and offset.
714
// (The offset might be Type::OffsetBot in the case of an array.)
715
// Return the base, or NULL if failure.
716
Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseTransform* phase,
717
// second return value:
718
intptr_t& offset) {
719
if (ptr->is_AddP()) {
720
Node* base = ptr->in(AddPNode::Base);
721
Node* addr = ptr->in(AddPNode::Address);
722
Node* offs = ptr->in(AddPNode::Offset);
723
if (base == addr || base->is_top()) {
724
offset = phase->find_intptr_t_con(offs, Type::OffsetBot);
725
if (offset != Type::OffsetBot) {
726
return addr;
727
}
728
}
729
}
730
offset = Type::OffsetBot;
731
return NULL;
732
}
733
734
//------------------------------unpack_offsets----------------------------------
735
// Collect the AddP offset values into the elements array, giving up
736
// if there are more than length.
737
int AddPNode::unpack_offsets(Node* elements[], int length) {
738
int count = 0;
739
Node* addr = this;
740
Node* base = addr->in(AddPNode::Base);
741
while (addr->is_AddP()) {
742
if (addr->in(AddPNode::Base) != base) {
743
// give up
744
return -1;
745
}
746
elements[count++] = addr->in(AddPNode::Offset);
747
if (count == length) {
748
// give up
749
return -1;
750
}
751
addr = addr->in(AddPNode::Address);
752
}
753
if (addr != base) {
754
return -1;
755
}
756
return count;
757
}
758
759
//------------------------------match_edge-------------------------------------
760
// Do we Match on this edge index or not? Do not match base pointer edge
761
uint AddPNode::match_edge(uint idx) const {
762
return idx > Base;
763
}
764
765
//=============================================================================
766
//------------------------------Identity---------------------------------------
767
Node* OrINode::Identity(PhaseGVN* phase) {
768
// x | x => x
769
if (in(1) == in(2)) {
770
return in(1);
771
}
772
773
return AddNode::Identity(phase);
774
}
775
776
// Find shift value for Integer or Long OR.
777
Node* rotate_shift(PhaseGVN* phase, Node* lshift, Node* rshift, int mask) {
778
// val << norm_con_shift | val >> ({32|64} - norm_con_shift) => rotate_left val, norm_con_shift
779
const TypeInt* lshift_t = phase->type(lshift)->isa_int();
780
const TypeInt* rshift_t = phase->type(rshift)->isa_int();
781
if (lshift_t != NULL && lshift_t->is_con() &&
782
rshift_t != NULL && rshift_t->is_con() &&
783
((lshift_t->get_con() & mask) == ((mask + 1) - (rshift_t->get_con() & mask)))) {
784
return phase->intcon(lshift_t->get_con() & mask);
785
}
786
// val << var_shift | val >> ({0|32|64} - var_shift) => rotate_left val, var_shift
787
if (rshift->Opcode() == Op_SubI && rshift->in(2) == lshift && rshift->in(1)->is_Con()){
788
const TypeInt* shift_t = phase->type(rshift->in(1))->isa_int();
789
if (shift_t != NULL && shift_t->is_con() &&
790
(shift_t->get_con() == 0 || shift_t->get_con() == (mask + 1))) {
791
return lshift;
792
}
793
}
794
return NULL;
795
}
796
797
Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) {
798
int lopcode = in(1)->Opcode();
799
int ropcode = in(2)->Opcode();
800
if (Matcher::match_rule_supported(Op_RotateLeft) &&
801
lopcode == Op_LShiftI && ropcode == Op_URShiftI && in(1)->in(1) == in(2)->in(1)) {
802
Node* lshift = in(1)->in(2);
803
Node* rshift = in(2)->in(2);
804
Node* shift = rotate_shift(phase, lshift, rshift, 0x1F);
805
if (shift != NULL) {
806
return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT);
807
}
808
return NULL;
809
}
810
if (Matcher::match_rule_supported(Op_RotateRight) &&
811
lopcode == Op_URShiftI && ropcode == Op_LShiftI && in(1)->in(1) == in(2)->in(1)) {
812
Node* rshift = in(1)->in(2);
813
Node* lshift = in(2)->in(2);
814
Node* shift = rotate_shift(phase, rshift, lshift, 0x1F);
815
if (shift != NULL) {
816
return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT);
817
}
818
}
819
return NULL;
820
}
821
822
//------------------------------add_ring---------------------------------------
823
// Supplied function returns the sum of the inputs IN THE CURRENT RING. For
824
// the logical operations the ring's ADD is really a logical OR function.
825
// This also type-checks the inputs for sanity. Guaranteed never to
826
// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
827
const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {
828
const TypeInt *r0 = t0->is_int(); // Handy access
829
const TypeInt *r1 = t1->is_int();
830
831
// If both args are bool, can figure out better types
832
if ( r0 == TypeInt::BOOL ) {
833
if ( r1 == TypeInt::ONE) {
834
return TypeInt::ONE;
835
} else if ( r1 == TypeInt::BOOL ) {
836
return TypeInt::BOOL;
837
}
838
} else if ( r0 == TypeInt::ONE ) {
839
if ( r1 == TypeInt::BOOL ) {
840
return TypeInt::ONE;
841
}
842
}
843
844
// If either input is not a constant, just return all integers.
845
if( !r0->is_con() || !r1->is_con() )
846
return TypeInt::INT; // Any integer, but still no symbols.
847
848
// Otherwise just OR them bits.
849
return TypeInt::make( r0->get_con() | r1->get_con() );
850
}
851
852
//=============================================================================
853
//------------------------------Identity---------------------------------------
854
Node* OrLNode::Identity(PhaseGVN* phase) {
855
// x | x => x
856
if (in(1) == in(2)) {
857
return in(1);
858
}
859
860
return AddNode::Identity(phase);
861
}
862
863
Node* OrLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
864
int lopcode = in(1)->Opcode();
865
int ropcode = in(2)->Opcode();
866
if (Matcher::match_rule_supported(Op_RotateLeft) &&
867
lopcode == Op_LShiftL && ropcode == Op_URShiftL && in(1)->in(1) == in(2)->in(1)) {
868
Node* lshift = in(1)->in(2);
869
Node* rshift = in(2)->in(2);
870
Node* shift = rotate_shift(phase, lshift, rshift, 0x3F);
871
if (shift != NULL) {
872
return new RotateLeftNode(in(1)->in(1), shift, TypeLong::LONG);
873
}
874
return NULL;
875
}
876
if (Matcher::match_rule_supported(Op_RotateRight) &&
877
lopcode == Op_URShiftL && ropcode == Op_LShiftL && in(1)->in(1) == in(2)->in(1)) {
878
Node* rshift = in(1)->in(2);
879
Node* lshift = in(2)->in(2);
880
Node* shift = rotate_shift(phase, rshift, lshift, 0x3F);
881
if (shift != NULL) {
882
return new RotateRightNode(in(1)->in(1), shift, TypeLong::LONG);
883
}
884
}
885
return NULL;
886
}
887
888
//------------------------------add_ring---------------------------------------
889
const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
890
const TypeLong *r0 = t0->is_long(); // Handy access
891
const TypeLong *r1 = t1->is_long();
892
893
// If either input is not a constant, just return all integers.
894
if( !r0->is_con() || !r1->is_con() )
895
return TypeLong::LONG; // Any integer, but still no symbols.
896
897
// Otherwise just OR them bits.
898
return TypeLong::make( r0->get_con() | r1->get_con() );
899
}
900
901
//=============================================================================
902
903
const Type* XorINode::Value(PhaseGVN* phase) const {
904
Node* in1 = in(1);
905
Node* in2 = in(2);
906
const Type* t1 = phase->type(in1);
907
const Type* t2 = phase->type(in2);
908
if (t1 == Type::TOP || t2 == Type::TOP) {
909
return Type::TOP;
910
}
911
// x ^ x ==> 0
912
if (in1->eqv_uncast(in2)) {
913
return add_id();
914
}
915
// result of xor can only have bits sets where any of the
916
// inputs have bits set. lo can always become 0.
917
const TypeInt* t1i = t1->is_int();
918
const TypeInt* t2i = t2->is_int();
919
if ((t1i->_lo >= 0) &&
920
(t1i->_hi > 0) &&
921
(t2i->_lo >= 0) &&
922
(t2i->_hi > 0)) {
923
// hi - set all bits below the highest bit. Using round_down to avoid overflow.
924
const TypeInt* t1x = TypeInt::make(0, round_down_power_of_2(t1i->_hi) + (round_down_power_of_2(t1i->_hi) - 1), t1i->_widen);
925
const TypeInt* t2x = TypeInt::make(0, round_down_power_of_2(t2i->_hi) + (round_down_power_of_2(t2i->_hi) - 1), t2i->_widen);
926
return t1x->meet(t2x);
927
}
928
return AddNode::Value(phase);
929
}
930
931
932
//------------------------------add_ring---------------------------------------
933
// Supplied function returns the sum of the inputs IN THE CURRENT RING. For
934
// the logical operations the ring's ADD is really a logical OR function.
935
// This also type-checks the inputs for sanity. Guaranteed never to
936
// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
937
const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {
938
const TypeInt *r0 = t0->is_int(); // Handy access
939
const TypeInt *r1 = t1->is_int();
940
941
// Complementing a boolean?
942
if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE
943
|| r1 == TypeInt::BOOL))
944
return TypeInt::BOOL;
945
946
if( !r0->is_con() || !r1->is_con() ) // Not constants
947
return TypeInt::INT; // Any integer, but still no symbols.
948
949
// Otherwise just XOR them bits.
950
return TypeInt::make( r0->get_con() ^ r1->get_con() );
951
}
952
953
//=============================================================================
954
//------------------------------add_ring---------------------------------------
955
const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
956
const TypeLong *r0 = t0->is_long(); // Handy access
957
const TypeLong *r1 = t1->is_long();
958
959
// If either input is not a constant, just return all integers.
960
if( !r0->is_con() || !r1->is_con() )
961
return TypeLong::LONG; // Any integer, but still no symbols.
962
963
// Otherwise just OR them bits.
964
return TypeLong::make( r0->get_con() ^ r1->get_con() );
965
}
966
967
const Type* XorLNode::Value(PhaseGVN* phase) const {
968
Node* in1 = in(1);
969
Node* in2 = in(2);
970
const Type* t1 = phase->type(in1);
971
const Type* t2 = phase->type(in2);
972
if (t1 == Type::TOP || t2 == Type::TOP) {
973
return Type::TOP;
974
}
975
// x ^ x ==> 0
976
if (in1->eqv_uncast(in2)) {
977
return add_id();
978
}
979
// result of xor can only have bits sets where any of the
980
// inputs have bits set. lo can always become 0.
981
const TypeLong* t1l = t1->is_long();
982
const TypeLong* t2l = t2->is_long();
983
if ((t1l->_lo >= 0) &&
984
(t1l->_hi > 0) &&
985
(t2l->_lo >= 0) &&
986
(t2l->_hi > 0)) {
987
// hi - set all bits below the highest bit. Using round_down to avoid overflow.
988
const TypeLong* t1x = TypeLong::make(0, round_down_power_of_2(t1l->_hi) + (round_down_power_of_2(t1l->_hi) - 1), t1l->_widen);
989
const TypeLong* t2x = TypeLong::make(0, round_down_power_of_2(t2l->_hi) + (round_down_power_of_2(t2l->_hi) - 1), t2l->_widen);
990
return t1x->meet(t2x);
991
}
992
return AddNode::Value(phase);
993
}
994
995
Node* MaxNode::build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn) {
996
bool is_int = gvn.type(a)->isa_int();
997
assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
998
assert(is_int == (gvn.type(b)->isa_int() != NULL), "inconsistent inputs");
999
Node* hook = NULL;
1000
if (gvn.is_IterGVN()) {
1001
// Make sure a and b are not destroyed
1002
hook = new Node(2);
1003
hook->init_req(0, a);
1004
hook->init_req(1, b);
1005
}
1006
Node* res = NULL;
1007
if (!is_unsigned) {
1008
if (is_max) {
1009
if (is_int) {
1010
res = gvn.transform(new MaxINode(a, b));
1011
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");
1012
} else {
1013
Node* cmp = gvn.transform(new CmpLNode(a, b));
1014
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1015
res = gvn.transform(new CMoveLNode(bol, a, b, t->is_long()));
1016
}
1017
} else {
1018
if (is_int) {
1019
Node* res = gvn.transform(new MinINode(a, b));
1020
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");
1021
} else {
1022
Node* cmp = gvn.transform(new CmpLNode(b, a));
1023
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1024
res = gvn.transform(new CMoveLNode(bol, a, b, t->is_long()));
1025
}
1026
}
1027
} else {
1028
if (is_max) {
1029
if (is_int) {
1030
Node* cmp = gvn.transform(new CmpUNode(a, b));
1031
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1032
res = gvn.transform(new CMoveINode(bol, a, b, t->is_int()));
1033
} else {
1034
Node* cmp = gvn.transform(new CmpULNode(a, b));
1035
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1036
res = gvn.transform(new CMoveLNode(bol, a, b, t->is_long()));
1037
}
1038
} else {
1039
if (is_int) {
1040
Node* cmp = gvn.transform(new CmpUNode(b, a));
1041
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1042
res = gvn.transform(new CMoveINode(bol, a, b, t->is_int()));
1043
} else {
1044
Node* cmp = gvn.transform(new CmpULNode(b, a));
1045
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1046
res = gvn.transform(new CMoveLNode(bol, a, b, t->is_long()));
1047
}
1048
}
1049
}
1050
if (hook != NULL) {
1051
hook->destruct(&gvn);
1052
}
1053
return res;
1054
}
1055
1056
Node* MaxNode::build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn) {
1057
bool is_int = gvn.type(a)->isa_int();
1058
assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1059
assert(is_int == (gvn.type(b)->isa_int() != NULL), "inconsistent inputs");
1060
Node* zero = NULL;
1061
if (is_int) {
1062
zero = gvn.intcon(0);
1063
} else {
1064
zero = gvn.longcon(0);
1065
}
1066
Node* hook = NULL;
1067
if (gvn.is_IterGVN()) {
1068
// Make sure a and b are not destroyed
1069
hook = new Node(2);
1070
hook->init_req(0, a);
1071
hook->init_req(1, b);
1072
}
1073
Node* res = NULL;
1074
if (is_max) {
1075
if (is_int) {
1076
Node* cmp = gvn.transform(new CmpINode(a, b));
1077
Node* sub = gvn.transform(new SubINode(a, b));
1078
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1079
res = gvn.transform(new CMoveINode(bol, sub, zero, t->is_int()));
1080
} else {
1081
Node* cmp = gvn.transform(new CmpLNode(a, b));
1082
Node* sub = gvn.transform(new SubLNode(a, b));
1083
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1084
res = gvn.transform(new CMoveLNode(bol, sub, zero, t->is_long()));
1085
}
1086
} else {
1087
if (is_int) {
1088
Node* cmp = gvn.transform(new CmpINode(b, a));
1089
Node* sub = gvn.transform(new SubINode(a, b));
1090
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1091
res = gvn.transform(new CMoveINode(bol, sub, zero, t->is_int()));
1092
} else {
1093
Node* cmp = gvn.transform(new CmpLNode(b, a));
1094
Node* sub = gvn.transform(new SubLNode(a, b));
1095
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1096
res = gvn.transform(new CMoveLNode(bol, sub, zero, t->is_long()));
1097
}
1098
}
1099
if (hook != NULL) {
1100
hook->destruct(&gvn);
1101
}
1102
return res;
1103
}
1104
1105
//=============================================================================
1106
//------------------------------add_ring---------------------------------------
1107
// Supplied function returns the sum of the inputs.
1108
const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {
1109
const TypeInt *r0 = t0->is_int(); // Handy access
1110
const TypeInt *r1 = t1->is_int();
1111
1112
// Otherwise just MAX them bits.
1113
return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1114
}
1115
1116
// Check if addition of an integer with type 't' and a constant 'c' can overflow
1117
static bool can_overflow(const TypeInt* t, jint c) {
1118
jint t_lo = t->_lo;
1119
jint t_hi = t->_hi;
1120
return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1121
(c > 0 && (java_add(t_hi, c) < t_hi)));
1122
}
1123
1124
//=============================================================================
1125
//------------------------------Idealize---------------------------------------
1126
// MINs show up in range-check loop limit calculations. Look for
1127
// "MIN2(x+c0,MIN2(y,x+c1))". Pick the smaller constant: "MIN2(x+c0,y)"
1128
Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) {
1129
Node *progress = NULL;
1130
// Force a right-spline graph
1131
Node *l = in(1);
1132
Node *r = in(2);
1133
// Transform MinI1( MinI2(a,b), c) into MinI1( a, MinI2(b,c) )
1134
// to force a right-spline graph for the rest of MinINode::Ideal().
1135
if( l->Opcode() == Op_MinI ) {
1136
assert( l != l->in(1), "dead loop in MinINode::Ideal" );
1137
r = phase->transform(new MinINode(l->in(2),r));
1138
l = l->in(1);
1139
set_req_X(1, l, phase);
1140
set_req_X(2, r, phase);
1141
return this;
1142
}
1143
1144
// Get left input & constant
1145
Node *x = l;
1146
jint x_off = 0;
1147
if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant
1148
x->in(2)->is_Con() ) {
1149
const Type *t = x->in(2)->bottom_type();
1150
if( t == Type::TOP ) return NULL; // No progress
1151
x_off = t->is_int()->get_con();
1152
x = x->in(1);
1153
}
1154
1155
// Scan a right-spline-tree for MINs
1156
Node *y = r;
1157
jint y_off = 0;
1158
// Check final part of MIN tree
1159
if( y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant
1160
y->in(2)->is_Con() ) {
1161
const Type *t = y->in(2)->bottom_type();
1162
if( t == Type::TOP ) return NULL; // No progress
1163
y_off = t->is_int()->get_con();
1164
y = y->in(1);
1165
}
1166
if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) {
1167
swap_edges(1, 2);
1168
return this;
1169
}
1170
1171
const TypeInt* tx = phase->type(x)->isa_int();
1172
1173
if( r->Opcode() == Op_MinI ) {
1174
assert( r != r->in(2), "dead loop in MinINode::Ideal" );
1175
y = r->in(1);
1176
// Check final part of MIN tree
1177
if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant
1178
y->in(2)->is_Con() ) {
1179
const Type *t = y->in(2)->bottom_type();
1180
if( t == Type::TOP ) return NULL; // No progress
1181
y_off = t->is_int()->get_con();
1182
y = y->in(1);
1183
}
1184
1185
if( x->_idx > y->_idx )
1186
return new MinINode(r->in(1),phase->transform(new MinINode(l,r->in(2))));
1187
1188
// Transform MIN2(x + c0, MIN2(x + c1, z)) into MIN2(x + MIN2(c0, c1), z)
1189
// if x == y and the additions can't overflow.
1190
if (x == y && tx != NULL &&
1191
!can_overflow(tx, x_off) &&
1192
!can_overflow(tx, y_off)) {
1193
return new MinINode(phase->transform(new AddINode(x, phase->intcon(MIN2(x_off, y_off)))), r->in(2));
1194
}
1195
} else {
1196
// Transform MIN2(x + c0, y + c1) into x + MIN2(c0, c1)
1197
// if x == y and the additions can't overflow.
1198
if (x == y && tx != NULL &&
1199
!can_overflow(tx, x_off) &&
1200
!can_overflow(tx, y_off)) {
1201
return new AddINode(x,phase->intcon(MIN2(x_off,y_off)));
1202
}
1203
}
1204
return NULL;
1205
}
1206
1207
//------------------------------add_ring---------------------------------------
1208
// Supplied function returns the sum of the inputs.
1209
const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
1210
const TypeInt *r0 = t0->is_int(); // Handy access
1211
const TypeInt *r1 = t1->is_int();
1212
1213
// Otherwise just MIN them bits.
1214
return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1215
}
1216
1217
//------------------------------add_ring---------------------------------------
1218
const Type *MinFNode::add_ring( const Type *t0, const Type *t1 ) const {
1219
const TypeF *r0 = t0->is_float_constant();
1220
const TypeF *r1 = t1->is_float_constant();
1221
1222
if (r0->is_nan()) {
1223
return r0;
1224
}
1225
if (r1->is_nan()) {
1226
return r1;
1227
}
1228
1229
float f0 = r0->getf();
1230
float f1 = r1->getf();
1231
if (f0 != 0.0f || f1 != 0.0f) {
1232
return f0 < f1 ? r0 : r1;
1233
}
1234
1235
// handle min of 0.0, -0.0 case.
1236
return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1;
1237
}
1238
1239
//------------------------------add_ring---------------------------------------
1240
const Type *MinDNode::add_ring( const Type *t0, const Type *t1 ) const {
1241
const TypeD *r0 = t0->is_double_constant();
1242
const TypeD *r1 = t1->is_double_constant();
1243
1244
if (r0->is_nan()) {
1245
return r0;
1246
}
1247
if (r1->is_nan()) {
1248
return r1;
1249
}
1250
1251
double d0 = r0->getd();
1252
double d1 = r1->getd();
1253
if (d0 != 0.0 || d1 != 0.0) {
1254
return d0 < d1 ? r0 : r1;
1255
}
1256
1257
// handle min of 0.0, -0.0 case.
1258
return (jlong_cast(d0) < jlong_cast(d1)) ? r0 : r1;
1259
}
1260
1261
//------------------------------add_ring---------------------------------------
1262
const Type *MaxFNode::add_ring( const Type *t0, const Type *t1 ) const {
1263
const TypeF *r0 = t0->is_float_constant();
1264
const TypeF *r1 = t1->is_float_constant();
1265
1266
if (r0->is_nan()) {
1267
return r0;
1268
}
1269
if (r1->is_nan()) {
1270
return r1;
1271
}
1272
1273
float f0 = r0->getf();
1274
float f1 = r1->getf();
1275
if (f0 != 0.0f || f1 != 0.0f) {
1276
return f0 > f1 ? r0 : r1;
1277
}
1278
1279
// handle max of 0.0,-0.0 case.
1280
return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1;
1281
}
1282
1283
//------------------------------add_ring---------------------------------------
1284
const Type *MaxDNode::add_ring( const Type *t0, const Type *t1 ) const {
1285
const TypeD *r0 = t0->is_double_constant();
1286
const TypeD *r1 = t1->is_double_constant();
1287
1288
if (r0->is_nan()) {
1289
return r0;
1290
}
1291
if (r1->is_nan()) {
1292
return r1;
1293
}
1294
1295
double d0 = r0->getd();
1296
double d1 = r1->getd();
1297
if (d0 != 0.0 || d1 != 0.0) {
1298
return d0 > d1 ? r0 : r1;
1299
}
1300
1301
// handle max of 0.0, -0.0 case.
1302
return (jlong_cast(d0) > jlong_cast(d1)) ? r0 : r1;
1303
}
1304
1305