288 igvn->register_new_node_with_optimizer( region_c );
289 igvn->register_new_node_with_optimizer( region_x );
290 // Prevent the untimely death of phi_x. Currently he has no uses. He is
291 // about to get one. If this only use goes away, then phi_x will look dead.
292 // However, he will be picking up some more uses down below.
293 Node *hook = new Node(4);
294 hook->init_req(0, phi_x);
295 hook->init_req(1, phi_c);
296 phi_x = phase->transform( phi_x );
297
298 // Make the compare
299 Node *cmp_c = phase->makecon(t);
300 Node *cmp_x = cmp->clone();
301 cmp_x->set_req(1,phi_x);
302 cmp_x->set_req(2,con2);
303 cmp_x = phase->transform(cmp_x);
304 // Make the bool
305 Node *b_c = phase->transform(new BoolNode(cmp_c,b->_test._test));
306 Node *b_x = phase->transform(new BoolNode(cmp_x,b->_test._test));
307 // Make the IfNode
308 IfNode *iff_c = new IfNode(region_c,b_c,iff->_prob,iff->_fcnt);
309 igvn->set_type_bottom(iff_c);
310 igvn->_worklist.push(iff_c);
311 hook->init_req(2, iff_c);
312
313 IfNode *iff_x = new IfNode(region_x,b_x,iff->_prob, iff->_fcnt);
314 igvn->set_type_bottom(iff_x);
315 igvn->_worklist.push(iff_x);
316 hook->init_req(3, iff_x);
317
318 // Make the true/false arms
319 Node *iff_c_t = phase->transform(new IfTrueNode (iff_c));
320 Node *iff_c_f = phase->transform(new IfFalseNode(iff_c));
321 if (predicate_c != NULL) {
322 assert(predicate_x == NULL, "only one predicate entry expected");
323 // Clone loop predicates to each path
324 iff_c_t = igvn->clone_loop_predicates(predicate_c, iff_c_t, !counted_loop);
325 iff_c_f = igvn->clone_loop_predicates(predicate_c, iff_c_f, !counted_loop);
326 }
327 Node *iff_x_t = phase->transform(new IfTrueNode (iff_x));
328 Node *iff_x_f = phase->transform(new IfFalseNode(iff_x));
329 if (predicate_x != NULL) {
330 assert(predicate_c == NULL, "only one predicate entry expected");
331 // Clone loop predicates to each path
332 iff_x_t = igvn->clone_loop_predicates(predicate_x, iff_x_t, !counted_loop);
333 iff_x_f = igvn->clone_loop_predicates(predicate_x, iff_x_f, !counted_loop);
478 flip_test = 2;
479 } else if (bn->_test._test != BoolTest::lt) {
480 return NULL;
481 }
482 if (l->is_top()) return NULL; // Top input means dead test
483 if (r->Opcode() != Op_LoadRange) return NULL;
484
485 // We have recognized one of these forms:
486 // Flip 1: If (Bool[<] CmpU(l, LoadRange)) ...
487 // Flip 2: If (Bool[<=] CmpU(LoadRange, l)) ...
488
489 ProjNode* iftrap = proj_out(flip_test == 2 ? true : false);
490 return iftrap;
491 }
492
493
494 //------------------------------is_range_check---------------------------------
495 // Return 0 if not a range check. Return 1 if a range check and set index and
496 // offset. Return 2 if we had to negate the test. Index is NULL if the check
497 // is versus a constant.
498 int IfNode::is_range_check(Node* &range, Node* &index, jint &offset) {
499 int flip_test = 0;
500 Node* l = NULL;
501 Node* r = NULL;
502 ProjNode* iftrap = range_check_trap_proj(flip_test, l, r);
503
504 if (iftrap == NULL) {
505 return 0;
506 }
507
508 // Make sure it's a real range check by requiring an uncommon trap
509 // along the OOB path. Otherwise, it's possible that the user wrote
510 // something which optimized to look like a range check but behaves
511 // in some other way.
512 if (iftrap->is_uncommon_trap_proj(Deoptimization::Reason_range_check) == NULL) {
513 return 0;
514 }
515
516 // Look for index+offset form
517 Node* ind = l;
518 jint off = 0;
706 // / unc
707 //
708
709 // Is the comparison for this If suitable for folding?
710 bool IfNode::cmpi_folds(PhaseIterGVN* igvn) {
711 return in(1) != NULL &&
712 in(1)->is_Bool() &&
713 in(1)->in(1) != NULL &&
714 in(1)->in(1)->Opcode() == Op_CmpI &&
715 in(1)->in(1)->in(2) != NULL &&
716 in(1)->in(1)->in(2) != igvn->C->top() &&
717 (in(1)->as_Bool()->_test.is_less() ||
718 in(1)->as_Bool()->_test.is_greater());
719 }
720
721 // Is a dominating control suitable for folding with this if?
722 bool IfNode::is_ctrl_folds(Node* ctrl, PhaseIterGVN* igvn) {
723 return ctrl != NULL &&
724 ctrl->is_Proj() &&
725 ctrl->in(0) != NULL &&
726 ctrl->in(0)->is_If() &&
727 ctrl->in(0)->outcnt() == 2 &&
728 ctrl->in(0)->as_If()->cmpi_folds(igvn) &&
729 // Must compare same value
730 ctrl->in(0)->in(1)->in(1)->in(1) != NULL &&
731 ctrl->in(0)->in(1)->in(1)->in(1) == in(1)->in(1)->in(1);
732 }
733
734 // Do this If and the dominating If share a region?
735 bool IfNode::has_shared_region(ProjNode* proj, ProjNode*& success, ProjNode*& fail) {
736 ProjNode* otherproj = proj->other_if_proj();
737 Node* otherproj_ctrl_use = otherproj->unique_ctrl_out();
738 RegionNode* region = (otherproj_ctrl_use != NULL && otherproj_ctrl_use->is_Region()) ? otherproj_ctrl_use->as_Region() : NULL;
739 success = NULL;
740 fail = NULL;
741
742 if (otherproj->outcnt() == 1 && region != NULL && !region->has_phi()) {
743 for (int i = 0; i < 2; i++) {
744 ProjNode* proj = proj_out(i);
745 if (success == NULL && proj->outcnt() == 1 && proj->unique_out() == region) {
746 success = proj;
924 } else if (lo_test == BoolTest::le) {
925 if (hi_test == BoolTest::lt || hi_test == BoolTest::ge) {
926 lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
927 cond = BoolTest::ge;
928 } else {
929 assert(hi_test == BoolTest::le || hi_test == BoolTest::gt, "bad test");
930 adjusted_lim = igvn->transform(new SubINode(hi, lo));
931 lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
932 cond = BoolTest::ge;
933 }
934 }
935 } else {
936 const TypeInt* failtype = filtered_int_type(igvn, n, proj);
937 if (failtype != NULL) {
938 const TypeInt* type2 = filtered_int_type(igvn, n, fail);
939 if (type2 != NULL) {
940 failtype = failtype->join(type2)->is_int();
941 if (failtype->_lo > failtype->_hi) {
942 // previous if determines the result of this if so
943 // replace Bool with constant
944 igvn->hash_delete(this);
945 set_req(1, igvn->intcon(success->_con));
946 return true;
947 }
948 }
949 }
950 lo = NULL;
951 hi = NULL;
952 }
953
954 if (lo && hi) {
955 // Merge the two compares into a single unsigned compare by building (CmpU (n - lo) (hi - lo))
956 Node* adjusted_val = igvn->transform(new SubINode(n, lo));
957 if (adjusted_lim == NULL) {
958 adjusted_lim = igvn->transform(new SubINode(hi, lo));
959 }
960 Node* newcmp = igvn->transform(new CmpUNode(adjusted_val, adjusted_lim));
961 Node* newbool = igvn->transform(new BoolNode(newcmp, cond));
962
963 igvn->replace_input_of(dom_iff, 1, igvn->intcon(proj->_con));
964 set_req(1, newbool);
965
966 return true;
967 }
968 return false;
969 }
970
971 // Merge the branches that trap for this If and the dominating If into
972 // a single region that branches to the uncommon trap for the
973 // dominating If
974 void IfNode::merge_uncommon_traps(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) {
975 ProjNode* otherproj = proj->other_if_proj();
976
977 CallStaticJavaNode* unc = success->is_uncommon_trap_proj(Deoptimization::Reason_none);
978 CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(Deoptimization::Reason_none);
979
980 if (unc != dom_unc) {
981 Node* r = new RegionNode(3);
982
983 r->set_req(1, otherproj);
984 r->set_req(2, success);
985 r = igvn->transform(r);
986 assert(r->is_Region(), "can't go away");
987
988 // Make both If trap at the state of the first If: once the CmpI
989 // nodes are merged, if we trap we don't know which of the CmpI
990 // nodes would have caused the trap so we have to restart
991 // execution at the first one
992 igvn->replace_input_of(dom_unc, 0, r);
993 igvn->replace_input_of(unc, 0, igvn->C->top());
994 }
995 int trap_request = dom_unc->uncommon_trap_request();
996 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
997 Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request);
998
999 int flip_test = 0;
1000 Node* l = NULL;
1001 Node* r = NULL;
1002
1003 if (success->in(0)->as_If()->range_check_trap_proj(flip_test, l, r) != NULL) {
1004 // If this looks like a range check, change the trap to
1005 // Reason_range_check so the compiler recognizes it as a range
1006 // check and applies the corresponding optimizations
1007 trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_range_check, action);
1008
1009 improve_address_types(l, r, fail, igvn);
1010 } else if (unc != dom_unc) {
1011 // If we trap we won't know what CmpI would have caused the trap
1012 // so use a special trap reason to mark this pair of CmpI nodes as
1013 // bad candidate for folding. On recompilation we won't fold them
1014 // and we may trap again but this time we'll know what branch
1015 // traps
1016 trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_unstable_fused_if, action);
1017 }
1018 igvn->replace_input_of(dom_unc, TypeFunc::Parms, igvn->intcon(trap_request));
1019 }
1020
1021 // If we are turning 2 CmpI nodes into a CmpU that follows the pattern
1022 // of a rangecheck on index i, on 64 bit the compares may be followed
1023 // by memory accesses using i as index. In that case, the CmpU tells
1024 // us something about the values taken by i that can help the compiler
1025 // (see Compile::conv_I2X_index())
1026 void IfNode::improve_address_types(Node* l, Node* r, ProjNode* fail, PhaseIterGVN* igvn) {
1027 #ifdef _LP64
1028 ResourceMark rm;
1029 Node_Stack stack(2);
1030
1031 assert(r->Opcode() == Op_LoadRange, "unexpected range check");
1032 const TypeInt* array_size = igvn->type(r)->is_int();
1033
1034 stack.push(l, 0);
1035
1036 while(stack.size() > 0) {
1037 Node* n = stack.node();
1038 uint start = stack.index();
1192 Node* IfNode::fold_compares(PhaseIterGVN* igvn) {
1193 if (Opcode() != Op_If) return NULL;
1194
1195 if (cmpi_folds(igvn)) {
1196 Node* ctrl = in(0);
1197 if (is_ctrl_folds(ctrl, igvn) &&
1198 ctrl->outcnt() == 1) {
1199 // A integer comparison immediately dominated by another integer
1200 // comparison
1201 ProjNode* success = NULL;
1202 ProjNode* fail = NULL;
1203 ProjNode* dom_cmp = ctrl->as_Proj();
1204 if (has_shared_region(dom_cmp, success, fail) &&
1205 // Next call modifies graph so must be last
1206 fold_compares_helper(dom_cmp, success, fail, igvn)) {
1207 return this;
1208 }
1209 if (has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1210 // Next call modifies graph so must be last
1211 fold_compares_helper(dom_cmp, success, fail, igvn)) {
1212 merge_uncommon_traps(dom_cmp, success, fail, igvn);
1213 return this;
1214 }
1215 return NULL;
1216 } else if (ctrl->in(0) != NULL &&
1217 ctrl->in(0)->in(0) != NULL) {
1218 ProjNode* success = NULL;
1219 ProjNode* fail = NULL;
1220 Node* dom = ctrl->in(0)->in(0);
1221 ProjNode* dom_cmp = dom->isa_Proj();
1222 ProjNode* other_cmp = ctrl->isa_Proj();
1223
1224 // Check if it's an integer comparison dominated by another
1225 // integer comparison with another test in between
1226 if (is_ctrl_folds(dom, igvn) &&
1227 has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1228 is_side_effect_free_test(other_cmp, igvn) &&
1229 // Next call modifies graph so must be last
1230 fold_compares_helper(dom_cmp, success, fail, igvn)) {
1231 reroute_side_effect_free_unc(other_cmp, dom_cmp, igvn);
1232 merge_uncommon_traps(dom_cmp, success, fail, igvn);
1233 return this;
1234 }
1235 }
1236 }
1237 return NULL;
1238 }
1239
1240 //------------------------------remove_useless_bool----------------------------
1241 // Check for people making a useless boolean: things like
1242 // if( (x < y ? true : false) ) { ... }
1243 // Replace with if( x < y ) { ... }
1244 static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) {
1245 Node *i1 = iff->in(1);
1246 if( !i1->is_Bool() ) return NULL;
1247 BoolNode *bol = i1->as_Bool();
1248
1249 Node *cmp = bol->in(1);
1250 if( cmp->Opcode() != Op_CmpI ) return NULL;
1251
1252 // Must be comparing against a bool
1253 const Type *cmp2_t = phase->type( cmp->in(2) );
1294 }
1295 if( true_path == 2 ) {
1296 flip = 1-flip;
1297 }
1298
1299 Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2);
1300 assert(new_bol != iff->in(1), "must make progress");
1301 iff->set_req(1, new_bol);
1302 // Intervening diamond probably goes dead
1303 phase->C->set_major_progress();
1304 return iff;
1305 }
1306
1307 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff);
1308
1309 struct RangeCheck {
1310 Node* ctl;
1311 jint off;
1312 };
1313
1314 //------------------------------Ideal------------------------------------------
1315 // Return a node which is more "ideal" than the current node. Strip out
1316 // control copies
1317 Node *IfNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1318 if (remove_dead_region(phase, can_reshape)) return this;
1319 // No Def-Use info?
1320 if (!can_reshape) return NULL;
1321 PhaseIterGVN *igvn = phase->is_IterGVN();
1322
1323 // Don't bother trying to transform a dead if
1324 if (in(0)->is_top()) return NULL;
1325 // Don't bother trying to transform an if with a dead test
1326 if (in(1)->is_top()) return NULL;
1327 // Another variation of a dead test
1328 if (in(1)->is_Con()) return NULL;
1329 // Another variation of a dead if
1330 if (outcnt() < 2) return NULL;
1331
1332 // Canonicalize the test.
1333 Node* idt_if = idealize_test(phase, this);
1334 if (idt_if != NULL) return idt_if;
1335
1336 // Try to split the IF
1337 Node *s = split_if(this, igvn);
1338 if (s != NULL) return s;
1339
1340 // Check for people making a useless boolean: things like
1341 // if( (x < y ? true : false) ) { ... }
1342 // Replace with if( x < y ) { ... }
1343 Node *bol2 = remove_useless_bool(this, phase);
1344 if( bol2 ) return bol2;
1345
1346 // Setup to scan up the CFG looking for a dominating test
1347 Node *dom = in(0);
1348 Node *prev_dom = this;
1349
1350 // Check for range-check vs other kinds of tests
1351 Node *index1, *range1;
1352 jint offset1;
1353 int flip1 = is_range_check(range1, index1, offset1);
1354 if( flip1 ) {
1355 // Try to remove extra range checks. All 'up_one_dom' gives up at merges
1356 // so all checks we inspect post-dominate the top-most check we find.
1357 // If we are going to fail the current check and we reach the top check
1358 // then we are guaranteed to fail, so just start interpreting there.
1359 // We 'expand' the top 3 range checks to include all post-dominating
1360 // checks.
1361
1362 // The top 3 range checks seen
1363 const int NRC =3;
1364 RangeCheck prev_checks[NRC];
1365 int nb_checks = 0;
1366
1367 // Low and high offsets seen so far
1368 jint off_lo = offset1;
1369 jint off_hi = offset1;
1370
1371 bool found_immediate_dominator = false;
1372
1373 // Scan for the top checks and collect range of offsets
1374 for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit
1375 if (dom->Opcode() == Op_If && // Not same opcode?
1376 prev_dom->in(0) == dom) { // One path of test does dominate?
1377 if (dom == this) return NULL; // dead loop
1378 // See if this is a range check
1379 Node *index2, *range2;
1380 jint offset2;
1381 int flip2 = dom->as_If()->is_range_check(range2, index2, offset2);
1382 // See if this is a _matching_ range check, checking against
1383 // the same array bounds.
1384 if (flip2 == flip1 && range2 == range1 && index2 == index1 &&
1385 dom->outcnt() == 2) {
1386 if (nb_checks == 0 && dom->in(1) == in(1)) {
1387 // Found an immediately dominating test at the same offset.
1388 // This kind of back-to-back test can be eliminated locally,
1389 // and there is no need to search further for dominating tests.
1390 assert(offset2 == offset1, "Same test but different offsets");
1391 found_immediate_dominator = true;
1392 break;
1393 }
1394 // Gather expanded bounds
1395 off_lo = MIN2(off_lo,offset2);
1396 off_hi = MAX2(off_hi,offset2);
1397 // Record top NRC range checks
1398 prev_checks[nb_checks%NRC].ctl = prev_dom;
1399 prev_checks[nb_checks%NRC].off = offset2;
1400 nb_checks++;
1401 }
1402 }
1403 prev_dom = dom;
1404 dom = up_one_dom(dom);
1405 if (!dom) break;
1406 }
1407
1408 if (!found_immediate_dominator) {
1409 // Attempt to widen the dominating range check to cover some later
1410 // ones. Since range checks "fail" by uncommon-trapping to the
1411 // interpreter, widening a check can make us speculatively enter
1412 // the interpreter. If we see range-check deopt's, do not widen!
1413 if (!phase->C->allow_range_check_smearing()) return NULL;
1414
1415 // Didn't find prior covering check, so cannot remove anything.
1416 if (nb_checks == 0) {
1417 return NULL;
1418 }
1419 // Constant indices only need to check the upper bound.
1420 // Non-constant indices must check both low and high.
1421 int chk0 = (nb_checks - 1) % NRC;
1422 if (index1) {
1423 if (nb_checks == 1) {
1424 return NULL;
1425 } else {
1426 // If the top range check's constant is the min or max of
1427 // all constants we widen the next one to cover the whole
1428 // range of constants.
1429 RangeCheck rc0 = prev_checks[chk0];
1430 int chk1 = (nb_checks - 2) % NRC;
1431 RangeCheck rc1 = prev_checks[chk1];
1432 if (rc0.off == off_lo) {
1433 adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1434 prev_dom = rc1.ctl;
1435 } else if (rc0.off == off_hi) {
1436 adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
1437 prev_dom = rc1.ctl;
1438 } else {
1439 // If the top test's constant is not the min or max of all
1440 // constants, we need 3 range checks. We must leave the
1441 // top test unchanged because widening it would allow the
1442 // accesses it protects to successfully read/write out of
1443 // bounds.
1444 if (nb_checks == 2) {
1445 return NULL;
1446 }
1447 int chk2 = (nb_checks - 3) % NRC;
1448 RangeCheck rc2 = prev_checks[chk2];
1449 // The top range check a+i covers interval: -a <= i < length-a
1450 // The second range check b+i covers interval: -b <= i < length-b
1451 if (rc1.off <= rc0.off) {
1452 // if b <= a, we change the second range check to:
1453 // -min_of_all_constants <= i < length-min_of_all_constants
1454 // Together top and second range checks now cover:
1455 // -min_of_all_constants <= i < length-a
1456 // which is more restrictive than -b <= i < length-b:
1457 // -b <= -min_of_all_constants <= i < length-a <= length-b
1458 // The third check is then changed to:
1459 // -max_of_all_constants <= i < length-max_of_all_constants
1460 // so 2nd and 3rd checks restrict allowed values of i to:
1461 // -min_of_all_constants <= i < length-max_of_all_constants
1462 adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
1463 adjust_check(rc2.ctl, range1, index1, flip1, off_hi, igvn);
1464 } else {
1465 // if b > a, we change the second range check to:
1466 // -max_of_all_constants <= i < length-max_of_all_constants
1467 // Together top and second range checks now cover:
1468 // -a <= i < length-max_of_all_constants
1469 // which is more restrictive than -b <= i < length-b:
1470 // -b < -a <= i < length-max_of_all_constants <= length-b
1471 // The third check is then changed to:
1472 // -max_of_all_constants <= i < length-max_of_all_constants
1473 // so 2nd and 3rd checks restrict allowed values of i to:
1474 // -min_of_all_constants <= i < length-max_of_all_constants
1475 adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1476 adjust_check(rc2.ctl, range1, index1, flip1, off_lo, igvn);
1477 }
1478 prev_dom = rc2.ctl;
1479 }
1480 }
1481 } else {
1482 RangeCheck rc0 = prev_checks[chk0];
1483 // 'Widen' the offset of the 1st and only covering check
1484 adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn);
1485 // Test is now covered by prior checks, dominate it out
1486 prev_dom = rc0.ctl;
1487 }
1488 }
1489
1490 } else { // Scan for an equivalent test
1491
1492 Node *cmp;
1493 int dist = 0; // Cutoff limit for search
1494 int op = Opcode();
1495 if( op == Op_If &&
1496 (cmp=in(1)->in(1))->Opcode() == Op_CmpP ) {
1497 if( cmp->in(2) != NULL && // make sure cmp is not already dead
1498 cmp->in(2)->bottom_type() == TypePtr::NULL_PTR ) {
1499 dist = 64; // Limit for null-pointer scans
1500 } else {
1501 dist = 4; // Do not bother for random pointer tests
1502 }
1503 } else {
1504 dist = 4; // Limit for random junky scans
1505 }
1506
1507 // Normal equivalent-test check.
1508 if( !dom ) return NULL; // Dead loop?
1509
1510 Node* result = fold_compares(igvn);
1511 if (result != NULL) {
1512 return result;
1513 }
1514
1515 // Search up the dominator tree for an If with an identical test
1516 while( dom->Opcode() != op || // Not same opcode?
1517 dom->in(1) != in(1) || // Not same input 1?
1518 (req() == 3 && dom->in(2) != in(2)) || // Not same input 2?
1519 prev_dom->in(0) != dom ) { // One path of test does not dominate?
1520 if( dist < 0 ) return NULL;
1521
1522 dist--;
1523 prev_dom = dom;
1524 dom = up_one_dom( dom );
1525 if( !dom ) return NULL;
1526 }
1527
1528 // Check that we did not follow a loop back to ourselves
1529 if( this == dom )
1530 return NULL;
1531
1532 if( dist > 2 ) // Add to count of NULL checks elided
1533 explicit_null_checks_elided++;
1534
1535 } // End of Else scan for an equivalent test
1536
1537 // Hit! Remove this IF
1538 #ifndef PRODUCT
1539 if( TraceIterativeGVN ) {
1540 tty->print(" Removing IfNode: "); this->dump();
1541 }
1542 if( VerifyOpto && !phase->allow_progress() ) {
1543 // Found an equivalent dominating test,
1544 // we can not guarantee reaching a fix-point for these during iterativeGVN
1545 // since intervening nodes may not change.
1546 return NULL;
1547 }
1548 #endif
1549
1550 // Replace dominated IfNode
1551 dominated_by( prev_dom, igvn );
1552
1553 // Must return either the original node (now dead) or a new node
1554 // (Do not return a top here, since that would break the uniqueness of top.)
1555 return new ConINode(TypeInt::ZERO);
1556 }
1557
1558 //------------------------------dominated_by-----------------------------------
1559 void IfNode::dominated_by( Node *prev_dom, PhaseIterGVN *igvn ) {
1560 igvn->hash_delete(this); // Remove self to prevent spurious V-N
1561 Node *idom = in(0);
1562 // Need opcode to decide which way 'this' test goes
1563 int prev_op = prev_dom->Opcode();
1564 Node *top = igvn->C->top(); // Shortcut to top
1565
1566 // Loop predicates may have depending checks which should not
1567 // be skipped. For example, range check predicate has two checks
1568 // for lower and upper bounds.
1569 ProjNode* unc_proj = proj_out(1 - prev_dom->as_Proj()->_con)->as_Proj();
1570 if (unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate) != NULL)
1571 prev_dom = idom;
1572
1573 // Now walk the current IfNode's projections.
1574 // Loop ends when 'this' has no more uses.
1575 for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {
1576 Node *ifp = last_out(i); // Get IfTrue/IfFalse
1577 igvn->add_users_to_worklist(ifp);
1578 // Check which projection it is and set target.
1579 // Data-target is either the dominating projection of the same type
1587 // For each child of an IfTrue/IfFalse projection, reroute.
1588 // Loop ends when projection has no more uses.
1589 for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) {
1590 Node* s = ifp->last_out(j); // Get child of IfTrue/IfFalse
1591 if( !s->depends_only_on_test() ) {
1592 // Find the control input matching this def-use edge.
1593 // For Regions it may not be in slot 0.
1594 uint l;
1595 for( l = 0; s->in(l) != ifp; l++ ) { }
1596 igvn->replace_input_of(s, l, ctrl_target);
1597 } else { // Else, for control producers,
1598 igvn->replace_input_of(s, 0, data_target); // Move child to data-target
1599 }
1600 } // End for each child of a projection
1601
1602 igvn->remove_dead_node(ifp);
1603 } // End for each IfTrue/IfFalse child of If
1604
1605 // Kill the IfNode
1606 igvn->remove_dead_node(this);
1607 }
1608
1609 //------------------------------Identity---------------------------------------
1610 // If the test is constant & we match, then we are the input Control
1611 Node *IfProjNode::Identity(PhaseTransform *phase) {
1612 // Can only optimize if cannot go the other way
1613 const TypeTuple *t = phase->type(in(0))->is_tuple();
1614 if (t == TypeTuple::IFNEITHER ||
1615 // kill dead branch first otherwise the IfNode's control will
1616 // have 2 control uses (the IfNode that doesn't go away because
1617 // it still has uses and this branch of the
1618 // If). Node::has_special_unique_user() will cause this node to
1619 // be reprocessed once the dead branch is killed.
1620 (always_taken(t) && in(0)->outcnt() == 1)) {
1621 // IfNode control
1622 return in(0)->in(0);
1623 }
1624 // no progress
1625 return this;
1626 }
1678 // whether they are testing a 'gt' or 'lt' condition. The 'gt' condition
1679 // happens in count-down loops
1680 if (iff->is_CountedLoopEnd()) return NULL;
1681 if (!iff->in(1)->is_Bool()) return NULL; // Happens for partially optimized IF tests
1682 BoolNode *b = iff->in(1)->as_Bool();
1683 BoolTest bt = b->_test;
1684 // Test already in good order?
1685 if( bt.is_canonical() )
1686 return NULL;
1687
1688 // Flip test to be canonical. Requires flipping the IfFalse/IfTrue and
1689 // cloning the IfNode.
1690 Node* new_b = phase->transform( new BoolNode(b->in(1), bt.negate()) );
1691 if( !new_b->is_Bool() ) return NULL;
1692 b = new_b->as_Bool();
1693
1694 PhaseIterGVN *igvn = phase->is_IterGVN();
1695 assert( igvn, "Test is not canonical in parser?" );
1696
1697 // The IF node never really changes, but it needs to be cloned
1698 iff = new IfNode( iff->in(0), b, 1.0-iff->_prob, iff->_fcnt);
1699
1700 Node *prior = igvn->hash_find_insert(iff);
1701 if( prior ) {
1702 igvn->remove_dead_node(iff);
1703 iff = (IfNode*)prior;
1704 } else {
1705 // Cannot call transform on it just yet
1706 igvn->set_type_bottom(iff);
1707 }
1708 igvn->_worklist.push(iff);
1709
1710 // Now handle projections. Cloning not required.
1711 Node* new_if_f = (Node*)(new IfFalseNode( iff ));
1712 Node* new_if_t = (Node*)(new IfTrueNode ( iff ));
1713
1714 igvn->register_new_node_with_optimizer(new_if_f);
1715 igvn->register_new_node_with_optimizer(new_if_t);
1716 // Flip test, so flip trailing control
1717 igvn->replace_node(old_if_f, new_if_t);
1718 igvn->replace_node(old_if_t, new_if_f);
1719
1720 // Progress
1721 return iff;
1722 }
|
288 igvn->register_new_node_with_optimizer( region_c );
289 igvn->register_new_node_with_optimizer( region_x );
290 // Prevent the untimely death of phi_x. Currently he has no uses. He is
291 // about to get one. If this only use goes away, then phi_x will look dead.
292 // However, he will be picking up some more uses down below.
293 Node *hook = new Node(4);
294 hook->init_req(0, phi_x);
295 hook->init_req(1, phi_c);
296 phi_x = phase->transform( phi_x );
297
298 // Make the compare
299 Node *cmp_c = phase->makecon(t);
300 Node *cmp_x = cmp->clone();
301 cmp_x->set_req(1,phi_x);
302 cmp_x->set_req(2,con2);
303 cmp_x = phase->transform(cmp_x);
304 // Make the bool
305 Node *b_c = phase->transform(new BoolNode(cmp_c,b->_test._test));
306 Node *b_x = phase->transform(new BoolNode(cmp_x,b->_test._test));
307 // Make the IfNode
308 IfNode *iff_c = iff->clone()->as_If();
309 iff_c->set_req(0, region_c);
310 iff_c->set_req(1, b_c);
311 igvn->set_type_bottom(iff_c);
312 igvn->_worklist.push(iff_c);
313 hook->init_req(2, iff_c);
314
315 IfNode *iff_x = iff->clone()->as_If();
316 iff_x->set_req(0, region_x);
317 iff_x->set_req(1, b_x);
318 igvn->set_type_bottom(iff_x);
319 igvn->_worklist.push(iff_x);
320 hook->init_req(3, iff_x);
321
322 // Make the true/false arms
323 Node *iff_c_t = phase->transform(new IfTrueNode (iff_c));
324 Node *iff_c_f = phase->transform(new IfFalseNode(iff_c));
325 if (predicate_c != NULL) {
326 assert(predicate_x == NULL, "only one predicate entry expected");
327 // Clone loop predicates to each path
328 iff_c_t = igvn->clone_loop_predicates(predicate_c, iff_c_t, !counted_loop);
329 iff_c_f = igvn->clone_loop_predicates(predicate_c, iff_c_f, !counted_loop);
330 }
331 Node *iff_x_t = phase->transform(new IfTrueNode (iff_x));
332 Node *iff_x_f = phase->transform(new IfFalseNode(iff_x));
333 if (predicate_x != NULL) {
334 assert(predicate_c == NULL, "only one predicate entry expected");
335 // Clone loop predicates to each path
336 iff_x_t = igvn->clone_loop_predicates(predicate_x, iff_x_t, !counted_loop);
337 iff_x_f = igvn->clone_loop_predicates(predicate_x, iff_x_f, !counted_loop);
482 flip_test = 2;
483 } else if (bn->_test._test != BoolTest::lt) {
484 return NULL;
485 }
486 if (l->is_top()) return NULL; // Top input means dead test
487 if (r->Opcode() != Op_LoadRange) return NULL;
488
489 // We have recognized one of these forms:
490 // Flip 1: If (Bool[<] CmpU(l, LoadRange)) ...
491 // Flip 2: If (Bool[<=] CmpU(LoadRange, l)) ...
492
493 ProjNode* iftrap = proj_out(flip_test == 2 ? true : false);
494 return iftrap;
495 }
496
497
498 //------------------------------is_range_check---------------------------------
499 // Return 0 if not a range check. Return 1 if a range check and set index and
500 // offset. Return 2 if we had to negate the test. Index is NULL if the check
501 // is versus a constant.
502 int RangeCheckNode::is_range_check(Node* &range, Node* &index, jint &offset) {
503 int flip_test = 0;
504 Node* l = NULL;
505 Node* r = NULL;
506 ProjNode* iftrap = range_check_trap_proj(flip_test, l, r);
507
508 if (iftrap == NULL) {
509 return 0;
510 }
511
512 // Make sure it's a real range check by requiring an uncommon trap
513 // along the OOB path. Otherwise, it's possible that the user wrote
514 // something which optimized to look like a range check but behaves
515 // in some other way.
516 if (iftrap->is_uncommon_trap_proj(Deoptimization::Reason_range_check) == NULL) {
517 return 0;
518 }
519
520 // Look for index+offset form
521 Node* ind = l;
522 jint off = 0;
710 // / unc
711 //
712
713 // Is the comparison for this If suitable for folding?
714 bool IfNode::cmpi_folds(PhaseIterGVN* igvn) {
715 return in(1) != NULL &&
716 in(1)->is_Bool() &&
717 in(1)->in(1) != NULL &&
718 in(1)->in(1)->Opcode() == Op_CmpI &&
719 in(1)->in(1)->in(2) != NULL &&
720 in(1)->in(1)->in(2) != igvn->C->top() &&
721 (in(1)->as_Bool()->_test.is_less() ||
722 in(1)->as_Bool()->_test.is_greater());
723 }
724
725 // Is a dominating control suitable for folding with this if?
726 bool IfNode::is_ctrl_folds(Node* ctrl, PhaseIterGVN* igvn) {
727 return ctrl != NULL &&
728 ctrl->is_Proj() &&
729 ctrl->in(0) != NULL &&
730 ctrl->in(0)->Opcode() == Op_If &&
731 ctrl->in(0)->outcnt() == 2 &&
732 ctrl->in(0)->as_If()->cmpi_folds(igvn) &&
733 // Must compare same value
734 ctrl->in(0)->in(1)->in(1)->in(1) != NULL &&
735 ctrl->in(0)->in(1)->in(1)->in(1) == in(1)->in(1)->in(1);
736 }
737
738 // Do this If and the dominating If share a region?
739 bool IfNode::has_shared_region(ProjNode* proj, ProjNode*& success, ProjNode*& fail) {
740 ProjNode* otherproj = proj->other_if_proj();
741 Node* otherproj_ctrl_use = otherproj->unique_ctrl_out();
742 RegionNode* region = (otherproj_ctrl_use != NULL && otherproj_ctrl_use->is_Region()) ? otherproj_ctrl_use->as_Region() : NULL;
743 success = NULL;
744 fail = NULL;
745
746 if (otherproj->outcnt() == 1 && region != NULL && !region->has_phi()) {
747 for (int i = 0; i < 2; i++) {
748 ProjNode* proj = proj_out(i);
749 if (success == NULL && proj->outcnt() == 1 && proj->unique_out() == region) {
750 success = proj;
928 } else if (lo_test == BoolTest::le) {
929 if (hi_test == BoolTest::lt || hi_test == BoolTest::ge) {
930 lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
931 cond = BoolTest::ge;
932 } else {
933 assert(hi_test == BoolTest::le || hi_test == BoolTest::gt, "bad test");
934 adjusted_lim = igvn->transform(new SubINode(hi, lo));
935 lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
936 cond = BoolTest::ge;
937 }
938 }
939 } else {
940 const TypeInt* failtype = filtered_int_type(igvn, n, proj);
941 if (failtype != NULL) {
942 const TypeInt* type2 = filtered_int_type(igvn, n, fail);
943 if (type2 != NULL) {
944 failtype = failtype->join(type2)->is_int();
945 if (failtype->_lo > failtype->_hi) {
946 // previous if determines the result of this if so
947 // replace Bool with constant
948 igvn->_worklist.push(in(1));
949 igvn->replace_input_of(this, 1, igvn->intcon(success->_con));
950 return true;
951 }
952 }
953 }
954 lo = NULL;
955 hi = NULL;
956 }
957
958 if (lo && hi) {
959 // Merge the two compares into a single unsigned compare by building (CmpU (n - lo) (hi - lo))
960 Node* adjusted_val = igvn->transform(new SubINode(n, lo));
961 if (adjusted_lim == NULL) {
962 adjusted_lim = igvn->transform(new SubINode(hi, lo));
963 }
964 Node* newcmp = igvn->transform(new CmpUNode(adjusted_val, adjusted_lim));
965 Node* newbool = igvn->transform(new BoolNode(newcmp, cond));
966
967 igvn->replace_input_of(dom_iff, 1, igvn->intcon(proj->_con));
968 igvn->_worklist.push(in(1));
969 igvn->replace_input_of(this, 1, newbool);
970
971 return true;
972 }
973 return false;
974 }
975
976 // Merge the branches that trap for this If and the dominating If into
977 // a single region that branches to the uncommon trap for the
978 // dominating If
979 Node* IfNode::merge_uncommon_traps(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) {
980 Node* res = this;
981 assert(success->in(0) == this, "bad projection");
982
983 ProjNode* otherproj = proj->other_if_proj();
984
985 CallStaticJavaNode* unc = success->is_uncommon_trap_proj(Deoptimization::Reason_none);
986 CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(Deoptimization::Reason_none);
987
988 if (unc != dom_unc) {
989 Node* r = new RegionNode(3);
990
991 r->set_req(1, otherproj);
992 r->set_req(2, success);
993 r = igvn->transform(r);
994 assert(r->is_Region(), "can't go away");
995
996 // Make both If trap at the state of the first If: once the CmpI
997 // nodes are merged, if we trap we don't know which of the CmpI
998 // nodes would have caused the trap so we have to restart
999 // execution at the first one
1000 igvn->replace_input_of(dom_unc, 0, r);
1001 igvn->replace_input_of(unc, 0, igvn->C->top());
1002 }
1003 int trap_request = dom_unc->uncommon_trap_request();
1004 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
1005 Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request);
1006
1007 int flip_test = 0;
1008 Node* l = NULL;
1009 Node* r = NULL;
1010
1011 if (success->in(0)->as_If()->range_check_trap_proj(flip_test, l, r) != NULL) {
1012 // If this looks like a range check, change the trap to
1013 // Reason_range_check so the compiler recognizes it as a range
1014 // check and applies the corresponding optimizations
1015 trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_range_check, action);
1016
1017 improve_address_types(l, r, fail, igvn);
1018
1019 res = igvn->transform(new RangeCheckNode(in(0), in(1), _prob, _fcnt));
1020 } else if (unc != dom_unc) {
1021 // If we trap we won't know what CmpI would have caused the trap
1022 // so use a special trap reason to mark this pair of CmpI nodes as
1023 // bad candidate for folding. On recompilation we won't fold them
1024 // and we may trap again but this time we'll know what branch
1025 // traps
1026 trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_unstable_fused_if, action);
1027 }
1028 igvn->replace_input_of(dom_unc, TypeFunc::Parms, igvn->intcon(trap_request));
1029 return res;
1030 }
1031
1032 // If we are turning 2 CmpI nodes into a CmpU that follows the pattern
1033 // of a rangecheck on index i, on 64 bit the compares may be followed
1034 // by memory accesses using i as index. In that case, the CmpU tells
1035 // us something about the values taken by i that can help the compiler
1036 // (see Compile::conv_I2X_index())
1037 void IfNode::improve_address_types(Node* l, Node* r, ProjNode* fail, PhaseIterGVN* igvn) {
1038 #ifdef _LP64
1039 ResourceMark rm;
1040 Node_Stack stack(2);
1041
1042 assert(r->Opcode() == Op_LoadRange, "unexpected range check");
1043 const TypeInt* array_size = igvn->type(r)->is_int();
1044
1045 stack.push(l, 0);
1046
1047 while(stack.size() > 0) {
1048 Node* n = stack.node();
1049 uint start = stack.index();
1203 Node* IfNode::fold_compares(PhaseIterGVN* igvn) {
1204 if (Opcode() != Op_If) return NULL;
1205
1206 if (cmpi_folds(igvn)) {
1207 Node* ctrl = in(0);
1208 if (is_ctrl_folds(ctrl, igvn) &&
1209 ctrl->outcnt() == 1) {
1210 // A integer comparison immediately dominated by another integer
1211 // comparison
1212 ProjNode* success = NULL;
1213 ProjNode* fail = NULL;
1214 ProjNode* dom_cmp = ctrl->as_Proj();
1215 if (has_shared_region(dom_cmp, success, fail) &&
1216 // Next call modifies graph so must be last
1217 fold_compares_helper(dom_cmp, success, fail, igvn)) {
1218 return this;
1219 }
1220 if (has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1221 // Next call modifies graph so must be last
1222 fold_compares_helper(dom_cmp, success, fail, igvn)) {
1223 return merge_uncommon_traps(dom_cmp, success, fail, igvn);
1224 }
1225 return NULL;
1226 } else if (ctrl->in(0) != NULL &&
1227 ctrl->in(0)->in(0) != NULL) {
1228 ProjNode* success = NULL;
1229 ProjNode* fail = NULL;
1230 Node* dom = ctrl->in(0)->in(0);
1231 ProjNode* dom_cmp = dom->isa_Proj();
1232 ProjNode* other_cmp = ctrl->isa_Proj();
1233
1234 // Check if it's an integer comparison dominated by another
1235 // integer comparison with another test in between
1236 if (is_ctrl_folds(dom, igvn) &&
1237 has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1238 is_side_effect_free_test(other_cmp, igvn) &&
1239 // Next call modifies graph so must be last
1240 fold_compares_helper(dom_cmp, success, fail, igvn)) {
1241 reroute_side_effect_free_unc(other_cmp, dom_cmp, igvn);
1242 return merge_uncommon_traps(dom_cmp, success, fail, igvn);
1243 }
1244 }
1245 }
1246 return NULL;
1247 }
1248
1249 //------------------------------remove_useless_bool----------------------------
1250 // Check for people making a useless boolean: things like
1251 // if( (x < y ? true : false) ) { ... }
1252 // Replace with if( x < y ) { ... }
1253 static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) {
1254 Node *i1 = iff->in(1);
1255 if( !i1->is_Bool() ) return NULL;
1256 BoolNode *bol = i1->as_Bool();
1257
1258 Node *cmp = bol->in(1);
1259 if( cmp->Opcode() != Op_CmpI ) return NULL;
1260
1261 // Must be comparing against a bool
1262 const Type *cmp2_t = phase->type( cmp->in(2) );
1303 }
1304 if( true_path == 2 ) {
1305 flip = 1-flip;
1306 }
1307
1308 Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2);
1309 assert(new_bol != iff->in(1), "must make progress");
1310 iff->set_req(1, new_bol);
1311 // Intervening diamond probably goes dead
1312 phase->C->set_major_progress();
1313 return iff;
1314 }
1315
1316 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff);
1317
1318 struct RangeCheck {
1319 Node* ctl;
1320 jint off;
1321 };
1322
1323 Node* IfNode::Ideal_common(PhaseGVN *phase, bool can_reshape) {
1324 if (remove_dead_region(phase, can_reshape)) return this;
1325 // No Def-Use info?
1326 if (!can_reshape) return NULL;
1327
1328 // Don't bother trying to transform a dead if
1329 if (in(0)->is_top()) return NULL;
1330 // Don't bother trying to transform an if with a dead test
1331 if (in(1)->is_top()) return NULL;
1332 // Another variation of a dead test
1333 if (in(1)->is_Con()) return NULL;
1334 // Another variation of a dead if
1335 if (outcnt() < 2) return NULL;
1336
1337 // Canonicalize the test.
1338 Node* idt_if = idealize_test(phase, this);
1339 if (idt_if != NULL) return idt_if;
1340
1341 // Try to split the IF
1342 PhaseIterGVN *igvn = phase->is_IterGVN();
1343 Node *s = split_if(this, igvn);
1344 if (s != NULL) return s;
1345
1346 return NodeSentinel;
1347 }
1348
1349 //------------------------------Ideal------------------------------------------
1350 // Return a node which is more "ideal" than the current node. Strip out
1351 // control copies
1352 Node *IfNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1353 Node* res = Ideal_common(phase, can_reshape);
1354 if (res != NodeSentinel) {
1355 return res;
1356 }
1357
1358 // Check for people making a useless boolean: things like
1359 // if( (x < y ? true : false) ) { ... }
1360 // Replace with if( x < y ) { ... }
1361 Node *bol2 = remove_useless_bool(this, phase);
1362 if( bol2 ) return bol2;
1363
1364 if (in(0) == NULL) return NULL; // Dead loop?
1365
1366 PhaseIterGVN *igvn = phase->is_IterGVN();
1367 Node* result = fold_compares(igvn);
1368 if (result != NULL) {
1369 return result;
1370 }
1371
1372 // Scan for an equivalent test
1373 Node *cmp;
1374 int dist = 0; // Cutoff limit for search
1375 int op = Opcode();
1376 if( op == Op_If &&
1377 (cmp=in(1)->in(1))->Opcode() == Op_CmpP ) {
1378 if( cmp->in(2) != NULL && // make sure cmp is not already dead
1379 cmp->in(2)->bottom_type() == TypePtr::NULL_PTR ) {
1380 dist = 64; // Limit for null-pointer scans
1381 } else {
1382 dist = 4; // Do not bother for random pointer tests
1383 }
1384 } else {
1385 dist = 4; // Limit for random junky scans
1386 }
1387
1388 Node* prev_dom = search_identical(dist);
1389
1390 if (prev_dom == NULL) {
1391 return NULL;
1392 }
1393
1394 // Replace dominated IfNode
1395 return dominated_by(prev_dom, igvn);
1396 }
1397
1398 //------------------------------dominated_by-----------------------------------
1399 Node* IfNode::dominated_by( Node *prev_dom, PhaseIterGVN *igvn ) {
1400 #ifndef PRODUCT
1401 if (TraceIterativeGVN) {
1402 tty->print(" Removing IfNode: "); this->dump();
1403 }
1404 if (VerifyOpto && !igvn->allow_progress()) {
1405 // Found an equivalent dominating test,
1406 // we can not guarantee reaching a fix-point for these during iterativeGVN
1407 // since intervening nodes may not change.
1408 return NULL;
1409 }
1410 #endif
1411
1412 igvn->hash_delete(this); // Remove self to prevent spurious V-N
1413 Node *idom = in(0);
1414 // Need opcode to decide which way 'this' test goes
1415 int prev_op = prev_dom->Opcode();
1416 Node *top = igvn->C->top(); // Shortcut to top
1417
1418 // Loop predicates may have depending checks which should not
1419 // be skipped. For example, range check predicate has two checks
1420 // for lower and upper bounds.
1421 ProjNode* unc_proj = proj_out(1 - prev_dom->as_Proj()->_con)->as_Proj();
1422 if (unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate) != NULL)
1423 prev_dom = idom;
1424
1425 // Now walk the current IfNode's projections.
1426 // Loop ends when 'this' has no more uses.
1427 for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {
1428 Node *ifp = last_out(i); // Get IfTrue/IfFalse
1429 igvn->add_users_to_worklist(ifp);
1430 // Check which projection it is and set target.
1431 // Data-target is either the dominating projection of the same type
1439 // For each child of an IfTrue/IfFalse projection, reroute.
1440 // Loop ends when projection has no more uses.
1441 for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) {
1442 Node* s = ifp->last_out(j); // Get child of IfTrue/IfFalse
1443 if( !s->depends_only_on_test() ) {
1444 // Find the control input matching this def-use edge.
1445 // For Regions it may not be in slot 0.
1446 uint l;
1447 for( l = 0; s->in(l) != ifp; l++ ) { }
1448 igvn->replace_input_of(s, l, ctrl_target);
1449 } else { // Else, for control producers,
1450 igvn->replace_input_of(s, 0, data_target); // Move child to data-target
1451 }
1452 } // End for each child of a projection
1453
1454 igvn->remove_dead_node(ifp);
1455 } // End for each IfTrue/IfFalse child of If
1456
1457 // Kill the IfNode
1458 igvn->remove_dead_node(this);
1459
1460 // Must return either the original node (now dead) or a new node
1461 // (Do not return a top here, since that would break the uniqueness of top.)
1462 return new ConINode(TypeInt::ZERO);
1463 }
1464
1465 Node* IfNode::search_identical(int dist) {
1466 // Setup to scan up the CFG looking for a dominating test
1467 Node *dom = in(0);
1468 Node *prev_dom = this;
1469 int op = Opcode();
1470 // Search up the dominator tree for an If with an identical test
1471 while( dom->Opcode() != op || // Not same opcode?
1472 dom->in(1) != in(1) || // Not same input 1?
1473 (req() == 3 && dom->in(2) != in(2)) || // Not same input 2?
1474 prev_dom->in(0) != dom ) { // One path of test does not dominate?
1475 if( dist < 0 ) return NULL;
1476
1477 dist--;
1478 prev_dom = dom;
1479 dom = up_one_dom( dom );
1480 if( !dom ) return NULL;
1481 }
1482
1483 // Check that we did not follow a loop back to ourselves
1484 if( this == dom )
1485 return NULL;
1486
1487 if( dist > 2 ) // Add to count of NULL checks elided
1488 explicit_null_checks_elided++;
1489
1490 return prev_dom;
1491 }
1492
1493 //------------------------------Identity---------------------------------------
1494 // If the test is constant & we match, then we are the input Control
1495 Node *IfProjNode::Identity(PhaseTransform *phase) {
1496 // Can only optimize if cannot go the other way
1497 const TypeTuple *t = phase->type(in(0))->is_tuple();
1498 if (t == TypeTuple::IFNEITHER ||
1499 // kill dead branch first otherwise the IfNode's control will
1500 // have 2 control uses (the IfNode that doesn't go away because
1501 // it still has uses and this branch of the
1502 // If). Node::has_special_unique_user() will cause this node to
1503 // be reprocessed once the dead branch is killed.
1504 (always_taken(t) && in(0)->outcnt() == 1)) {
1505 // IfNode control
1506 return in(0)->in(0);
1507 }
1508 // no progress
1509 return this;
1510 }
1562 // whether they are testing a 'gt' or 'lt' condition. The 'gt' condition
1563 // happens in count-down loops
1564 if (iff->is_CountedLoopEnd()) return NULL;
1565 if (!iff->in(1)->is_Bool()) return NULL; // Happens for partially optimized IF tests
1566 BoolNode *b = iff->in(1)->as_Bool();
1567 BoolTest bt = b->_test;
1568 // Test already in good order?
1569 if( bt.is_canonical() )
1570 return NULL;
1571
1572 // Flip test to be canonical. Requires flipping the IfFalse/IfTrue and
1573 // cloning the IfNode.
1574 Node* new_b = phase->transform( new BoolNode(b->in(1), bt.negate()) );
1575 if( !new_b->is_Bool() ) return NULL;
1576 b = new_b->as_Bool();
1577
1578 PhaseIterGVN *igvn = phase->is_IterGVN();
1579 assert( igvn, "Test is not canonical in parser?" );
1580
1581 // The IF node never really changes, but it needs to be cloned
1582 iff = iff->clone()->as_If();
1583 iff->set_req(1, b);
1584 iff->_prob = 1.0-iff->_prob;
1585
1586 Node *prior = igvn->hash_find_insert(iff);
1587 if( prior ) {
1588 igvn->remove_dead_node(iff);
1589 iff = (IfNode*)prior;
1590 } else {
1591 // Cannot call transform on it just yet
1592 igvn->set_type_bottom(iff);
1593 }
1594 igvn->_worklist.push(iff);
1595
1596 // Now handle projections. Cloning not required.
1597 Node* new_if_f = (Node*)(new IfFalseNode( iff ));
1598 Node* new_if_t = (Node*)(new IfTrueNode ( iff ));
1599
1600 igvn->register_new_node_with_optimizer(new_if_f);
1601 igvn->register_new_node_with_optimizer(new_if_t);
1602 // Flip test, so flip trailing control
1603 igvn->replace_node(old_if_f, new_if_t);
1604 igvn->replace_node(old_if_t, new_if_f);
1605
1606 // Progress
1607 return iff;
1608 }
1609
1610 Node *RangeCheckNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1611 Node* res = Ideal_common(phase, can_reshape);
1612 if (res != NodeSentinel) {
1613 return res;
1614 }
1615
1616 PhaseIterGVN *igvn = phase->is_IterGVN();
1617 // Setup to scan up the CFG looking for a dominating test
1618 Node *prev_dom = this;
1619
1620 // Check for range-check vs other kinds of tests
1621 Node *index1, *range1;
1622 jint offset1;
1623 int flip1 = is_range_check(range1, index1, offset1);
1624 if (flip1) {
1625 Node *dom = in(0);
1626 // Try to remove extra range checks. All 'up_one_dom' gives up at merges
1627 // so all checks we inspect post-dominate the top-most check we find.
1628 // If we are going to fail the current check and we reach the top check
1629 // then we are guaranteed to fail, so just start interpreting there.
1630 // We 'expand' the top 3 range checks to include all post-dominating
1631 // checks.
1632
1633 // The top 3 range checks seen
1634 const int NRC =3;
1635 RangeCheck prev_checks[NRC];
1636 int nb_checks = 0;
1637
1638 // Low and high offsets seen so far
1639 jint off_lo = offset1;
1640 jint off_hi = offset1;
1641
1642 bool found_immediate_dominator = false;
1643
1644 // Scan for the top checks and collect range of offsets
1645 for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit
1646 if (dom->Opcode() == Op_RangeCheck && // Not same opcode?
1647 prev_dom->in(0) == dom) { // One path of test does dominate?
1648 if (dom == this) return NULL; // dead loop
1649 // See if this is a range check
1650 Node *index2, *range2;
1651 jint offset2;
1652 int flip2 = dom->as_RangeCheck()->is_range_check(range2, index2, offset2);
1653 // See if this is a _matching_ range check, checking against
1654 // the same array bounds.
1655 if (flip2 == flip1 && range2 == range1 && index2 == index1 &&
1656 dom->outcnt() == 2) {
1657 if (nb_checks == 0 && dom->in(1) == in(1)) {
1658 // Found an immediately dominating test at the same offset.
1659 // This kind of back-to-back test can be eliminated locally,
1660 // and there is no need to search further for dominating tests.
1661 assert(offset2 == offset1, "Same test but different offsets");
1662 found_immediate_dominator = true;
1663 break;
1664 }
1665 // Gather expanded bounds
1666 off_lo = MIN2(off_lo,offset2);
1667 off_hi = MAX2(off_hi,offset2);
1668 // Record top NRC range checks
1669 prev_checks[nb_checks%NRC].ctl = prev_dom;
1670 prev_checks[nb_checks%NRC].off = offset2;
1671 nb_checks++;
1672 }
1673 }
1674 prev_dom = dom;
1675 dom = up_one_dom(dom);
1676 if (!dom) break;
1677 }
1678
1679 if (!found_immediate_dominator) {
1680 // Attempt to widen the dominating range check to cover some later
1681 // ones. Since range checks "fail" by uncommon-trapping to the
1682 // interpreter, widening a check can make us speculatively enter
1683 // the interpreter. If we see range-check deopt's, do not widen!
1684 if (!phase->C->allow_range_check_smearing()) return NULL;
1685
1686 // Didn't find prior covering check, so cannot remove anything.
1687 if (nb_checks == 0) {
1688 return NULL;
1689 }
1690 // Constant indices only need to check the upper bound.
1691 // Non-constant indices must check both low and high.
1692 int chk0 = (nb_checks - 1) % NRC;
1693 if (index1) {
1694 if (nb_checks == 1) {
1695 return NULL;
1696 } else {
1697 // If the top range check's constant is the min or max of
1698 // all constants we widen the next one to cover the whole
1699 // range of constants.
1700 RangeCheck rc0 = prev_checks[chk0];
1701 int chk1 = (nb_checks - 2) % NRC;
1702 RangeCheck rc1 = prev_checks[chk1];
1703 if (rc0.off == off_lo) {
1704 adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1705 prev_dom = rc1.ctl;
1706 } else if (rc0.off == off_hi) {
1707 adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
1708 prev_dom = rc1.ctl;
1709 } else {
1710 // If the top test's constant is not the min or max of all
1711 // constants, we need 3 range checks. We must leave the
1712 // top test unchanged because widening it would allow the
1713 // accesses it protects to successfully read/write out of
1714 // bounds.
1715 if (nb_checks == 2) {
1716 return NULL;
1717 }
1718 int chk2 = (nb_checks - 3) % NRC;
1719 RangeCheck rc2 = prev_checks[chk2];
1720 // The top range check a+i covers interval: -a <= i < length-a
1721 // The second range check b+i covers interval: -b <= i < length-b
1722 if (rc1.off <= rc0.off) {
1723 // if b <= a, we change the second range check to:
1724 // -min_of_all_constants <= i < length-min_of_all_constants
1725 // Together top and second range checks now cover:
1726 // -min_of_all_constants <= i < length-a
1727 // which is more restrictive than -b <= i < length-b:
1728 // -b <= -min_of_all_constants <= i < length-a <= length-b
1729 // The third check is then changed to:
1730 // -max_of_all_constants <= i < length-max_of_all_constants
1731 // so 2nd and 3rd checks restrict allowed values of i to:
1732 // -min_of_all_constants <= i < length-max_of_all_constants
1733 adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
1734 adjust_check(rc2.ctl, range1, index1, flip1, off_hi, igvn);
1735 } else {
1736 // if b > a, we change the second range check to:
1737 // -max_of_all_constants <= i < length-max_of_all_constants
1738 // Together top and second range checks now cover:
1739 // -a <= i < length-max_of_all_constants
1740 // which is more restrictive than -b <= i < length-b:
1741 // -b < -a <= i < length-max_of_all_constants <= length-b
1742 // The third check is then changed to:
1743 // -max_of_all_constants <= i < length-max_of_all_constants
1744 // so 2nd and 3rd checks restrict allowed values of i to:
1745 // -min_of_all_constants <= i < length-max_of_all_constants
1746 adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1747 adjust_check(rc2.ctl, range1, index1, flip1, off_lo, igvn);
1748 }
1749 prev_dom = rc2.ctl;
1750 }
1751 }
1752 } else {
1753 RangeCheck rc0 = prev_checks[chk0];
1754 // 'Widen' the offset of the 1st and only covering check
1755 adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn);
1756 // Test is now covered by prior checks, dominate it out
1757 prev_dom = rc0.ctl;
1758 }
1759 }
1760 } else {
1761 prev_dom = search_identical(4);
1762
1763 if (prev_dom == NULL) {
1764 return NULL;
1765 }
1766 }
1767
1768 // Replace dominated IfNode
1769 return dominated_by(prev_dom, igvn);
1770 }
|