< prev index next >

src/share/vm/opto/stringopts.cpp

Print this page




 581   return NULL;
 582 }
 583 
 584 
 585 PhaseStringOpts::PhaseStringOpts(PhaseGVN* gvn, Unique_Node_List*):
 586   Phase(StringOpts),
 587   _gvn(gvn),
 588   _visited(Thread::current()->resource_area()) {
 589 
 590   assert(OptimizeStringConcat, "shouldn't be here");
 591 
 592   size_table_field = C->env()->Integer_klass()->get_field_by_name(ciSymbol::make("sizeTable"),
 593                                                                   ciSymbol::make("[I"), true);
 594   if (size_table_field == NULL) {
 595     // Something wrong so give up.
 596     assert(false, "why can't we find Integer.sizeTable?");
 597     return;
 598   }
 599 
 600   // Collect the types needed to talk about the various slices of memory
 601   char_adr_idx = C->get_alias_index(TypeAryPtr::CHARS);
 602 
 603   // For each locally allocated StringBuffer see if the usages can be
 604   // collapsed into a single String construction.
 605 
 606   // Run through the list of allocation looking for SB.toString to see
 607   // if it's possible to fuse the usage of the SB into a single String
 608   // construction.
 609   GrowableArray<StringConcat*> concats;
 610   Node_List toStrings = collect_toString_calls();
 611   while (toStrings.size() > 0) {
 612     StringConcat* sc = build_candidate(toStrings.pop()->as_CallStaticJava());
 613     if (sc != NULL) {
 614       concats.push(sc);
 615     }
 616   }
 617 
 618   // try to coalesce separate concats
 619  restart:
 620   for (int c = 0; c < concats.length(); c++) {
 621     StringConcat* sc = concats.at(c);


1111       ciObject* con = field->constant_value().as_object();
1112       // Do not "join" in the previous type; it doesn't add value,
1113       // and may yield a vacuous result if the field is of interface type.
1114       type = TypeOopPtr::make_from_constant(con, true)->isa_oopptr();
1115       assert(type != NULL, "field singleton type must be consistent");
1116       return __ makecon(type);
1117     } else {
1118       type = TypeOopPtr::make_from_klass(field_klass->as_klass());
1119     }
1120   } else {
1121     type = Type::get_const_basic_type(bt);
1122   }
1123 
1124   return kit.make_load(NULL, kit.basic_plus_adr(klass_node, field->offset_in_bytes()),
1125                        type, T_OBJECT,
1126                        C->get_alias_index(mirror_type->add_offset(field->offset_in_bytes())),
1127                        MemNode::unordered);
1128 }
1129 
1130 Node* PhaseStringOpts::int_stringSize(GraphKit& kit, Node* arg) {



















1131   RegionNode *final_merge = new RegionNode(3);
1132   kit.gvn().set_type(final_merge, Type::CONTROL);
1133   Node* final_size = new PhiNode(final_merge, TypeInt::INT);
1134   kit.gvn().set_type(final_size, TypeInt::INT);
1135 
1136   IfNode* iff = kit.create_and_map_if(kit.control(),
1137                                       __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne),
1138                                       PROB_FAIR, COUNT_UNKNOWN);
1139   Node* is_min = __ IfFalse(iff);
1140   final_merge->init_req(1, is_min);
1141   final_size->init_req(1, __ intcon(11));
1142 
1143   kit.set_control(__ IfTrue(iff));
1144   if (kit.stopped()) {
1145     final_merge->init_req(2, C->top());
1146     final_size->init_req(2, C->top());
1147   } else {
1148 
1149     // int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
1150     RegionNode *r = new RegionNode(3);


1195     Node* greater = __ IfFalse(iff2);
1196 
1197     loop->init_req(2, greater);
1198     index->init_req(2, __ AddI(index, __ intcon(1)));
1199 
1200     kit.set_control(lessEqual);
1201     C->record_for_igvn(loop);
1202     C->record_for_igvn(index);
1203 
1204     final_merge->init_req(2, kit.control());
1205     final_size->init_req(2, __ AddI(__ AddI(index, size), __ intcon(1)));
1206   }
1207 
1208   kit.set_control(final_merge);
1209   C->record_for_igvn(final_merge);
1210   C->record_for_igvn(final_size);
1211 
1212   return final_size;
1213 }
1214 
1215 void PhaseStringOpts::int_getChars(GraphKit& kit, Node* arg, Node* char_array, Node* start, Node* end) {
1216   RegionNode *final_merge = new RegionNode(4);
1217   kit.gvn().set_type(final_merge, Type::CONTROL);
1218   Node *final_mem = PhiNode::make(final_merge, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS);
1219   kit.gvn().set_type(final_mem, Type::MEMORY);
1220 
1221   // need to handle Integer.MIN_VALUE specially because negating doesn't make it positive
1222   {
1223     // i == MIN_VALUE
1224     IfNode* iff = kit.create_and_map_if(kit.control(),
1225                                         __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne),
1226                                         PROB_FAIR, COUNT_UNKNOWN);
1227 
1228     Node* old_mem = kit.memory(char_adr_idx);
1229 
1230     kit.set_control(__ IfFalse(iff));
1231     if (kit.stopped()) {
1232       // Statically not equal to MIN_VALUE so this path is dead
1233       final_merge->init_req(3, kit.control());
1234     } else {
1235       copy_string(kit, __ makecon(TypeInstPtr::make(C->env()->the_min_jint_string())),
1236                   char_array, start);
1237       final_merge->init_req(3, kit.control());
1238       final_mem->init_req(3, kit.memory(char_adr_idx));
1239     }
1240 
1241     kit.set_control(__ IfTrue(iff));
1242     kit.set_memory(old_mem, char_adr_idx);
1243   }
1244 
1245 
1246   // Simplified version of Integer.getChars
1247 
1248   // int q, r;
1249   // int charPos = index;
1250   Node* charPos = end;
1251 
1252   // char sign = 0;
1253 
1254   Node* i = arg;
1255   Node* sign = __ intcon(0);
1256 
1257   // if (i < 0) {
1258   //     sign = '-';
1259   //     i = -i;
1260   // }
1261   {
1262     IfNode* iff = kit.create_and_map_if(kit.control(),
1263                                         __ Bool(__ CmpI(arg, __ intcon(0)), BoolTest::lt),
1264                                         PROB_FAIR, COUNT_UNKNOWN);
1265 
1266     RegionNode *merge = new RegionNode(3);
1267     kit.gvn().set_type(merge, Type::CONTROL);
1268     i = new PhiNode(merge, TypeInt::INT);
1269     kit.gvn().set_type(i, TypeInt::INT);
1270     sign = new PhiNode(merge, TypeInt::INT);
1271     kit.gvn().set_type(sign, TypeInt::INT);
1272 
1273     merge->init_req(1, __ IfTrue(iff));
1274     i->init_req(1, __ SubI(__ intcon(0), arg));
1275     sign->init_req(1, __ intcon('-'));
1276     merge->init_req(2, __ IfFalse(iff));
1277     i->init_req(2, arg);
1278     sign->init_req(2, __ intcon(0));
1279 
1280     kit.set_control(merge);
1281 
1282     C->record_for_igvn(merge);
1283     C->record_for_igvn(i);
1284     C->record_for_igvn(sign);
1285   }
1286 
1287   // for (;;) {
1288   //     q = i / 10;
1289   //     r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...
1290   //     buf [--charPos] = digits [r];
1291   //     i = q;
1292   //     if (i == 0) break;
1293   // }
1294 
1295   {
1296     // Add loop predicate first.
1297     kit.add_predicate();
1298 
1299     RegionNode *head = new RegionNode(3);
1300     head->init_req(1, kit.control());

1301     kit.gvn().set_type(head, Type::CONTROL);
1302     Node *i_phi = new PhiNode(head, TypeInt::INT);
1303     i_phi->init_req(1, i);
1304     kit.gvn().set_type(i_phi, TypeInt::INT);
1305     charPos = PhiNode::make(head, charPos);

1306     kit.gvn().set_type(charPos, TypeInt::INT);
1307     Node *mem = PhiNode::make(head, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS);
1308     kit.gvn().set_type(mem, Type::MEMORY);

1309     kit.set_control(head);
1310     kit.set_memory(mem, char_adr_idx);
1311 
1312     Node* q = __ DivI(NULL, i_phi, __ intcon(10));
1313     Node* r = __ SubI(i_phi, __ AddI(__ LShiftI(q, __ intcon(3)),
1314                                      __ LShiftI(q, __ intcon(1))));
1315     Node* m1 = __ SubI(charPos, __ intcon(1));
1316     Node* ch = __ AddI(r, __ intcon('0'));


1317 
1318     Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR),
1319                                   ch, T_CHAR, char_adr_idx, MemNode::unordered);
1320 
1321 
1322     IfNode* iff = kit.create_and_map_if(head, __ Bool(__ CmpI(q, __ intcon(0)), BoolTest::ne),
1323                                         PROB_FAIR, COUNT_UNKNOWN);
1324     Node* ne = __ IfTrue(iff);
1325     Node* eq = __ IfFalse(iff);
1326 
1327     head->init_req(2, ne);
1328     mem->init_req(2, st);
1329     i_phi->init_req(2, q);
1330     charPos->init_req(2, m1);
1331 
1332     charPos = m1;


1333 
1334     kit.set_control(eq);
1335     kit.set_memory(st, char_adr_idx);
1336 
1337     C->record_for_igvn(head);
1338     C->record_for_igvn(mem);
1339     C->record_for_igvn(i_phi);
1340     C->record_for_igvn(charPos);
1341   }
1342 
1343   {
1344     // if (sign != 0) {
1345     //     buf [--charPos] = sign;
1346     // }
1347     IfNode* iff = kit.create_and_map_if(kit.control(),
1348                                         __ Bool(__ CmpI(sign, __ intcon(0)), BoolTest::ne),
1349                                         PROB_FAIR, COUNT_UNKNOWN);
1350 
1351     final_merge->init_req(2, __ IfFalse(iff));
1352     final_mem->init_req(2, kit.memory(char_adr_idx));
1353 
1354     kit.set_control(__ IfTrue(iff));
1355     if (kit.stopped()) {
1356       final_merge->init_req(1, C->top());
1357       final_mem->init_req(1, C->top());
1358     } else {
1359       Node* m1 = __ SubI(charPos, __ intcon(1));
1360       Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR),
1361                                     sign, T_CHAR, char_adr_idx, MemNode::unordered);
1362 
1363       final_merge->init_req(1, kit.control());
1364       final_mem->init_req(1, st);
1365     }





















1366 









































1367     kit.set_control(final_merge);
1368     kit.set_memory(final_mem, char_adr_idx);
1369 
1370     C->record_for_igvn(final_merge);
1371     C->record_for_igvn(final_mem);



































































1372   }
1373 }
1374 















1375 
1376 Node* PhaseStringOpts::copy_string(GraphKit& kit, Node* str, Node* char_array, Node* start) {
1377   Node* string = str;
1378   Node* offset = kit.load_String_offset(kit.control(), string);
1379   Node* count  = kit.load_String_length(kit.control(), string);
1380   Node* value  = kit.load_String_value (kit.control(), string);
























































1381 
1382   // copy the contents
1383   if (offset->is_Con() && count->is_Con() && value->is_Con() && count->get_int() < unroll_string_copy_length) {
1384     // For small constant strings just emit individual stores.
1385     // A length of 6 seems like a good space/speed tradeof.
1386     int c = count->get_int();
1387     int o = offset->get_int();
1388     const TypeOopPtr* t = kit.gvn().type(value)->isa_oopptr();
1389     ciTypeArray* value_array = t->const_oop()->as_type_array();
1390     for (int e = 0; e < c; e++) {
1391       __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR),
1392                          __ intcon(value_array->char_at(o + e)), T_CHAR, char_adr_idx,
1393                          MemNode::unordered);
1394       start = __ AddI(start, __ intcon(1));


1395     }
1396   } else {
1397     Node* src_ptr = kit.array_element_address(value, offset, T_CHAR);
1398     Node* dst_ptr = kit.array_element_address(char_array, start, T_CHAR);
1399     Node* c = count;
1400     Node* extra = NULL;
1401 #ifdef _LP64
1402     c = __ ConvI2L(c);
1403     extra = C->top();
1404 #endif
1405     Node* call = kit.make_runtime_call(GraphKit::RC_LEAF|GraphKit::RC_NO_FP,
1406                                        OptoRuntime::fast_arraycopy_Type(),
1407                                        CAST_FROM_FN_PTR(address, StubRoutines::jshort_disjoint_arraycopy()),
1408                                        "jshort_disjoint_arraycopy", TypeAryPtr::CHARS,
1409                                        src_ptr, dst_ptr, c, extra);
1410     start = __ AddI(start, count);







































































1411   }
1412   return start;
1413 }
1414 















1415 
1416 void PhaseStringOpts::replace_string_concat(StringConcat* sc) {
1417   // Log a little info about the transformation
1418   sc->maybe_log_transform();
1419 
1420   // pull the JVMState of the allocation into a SafePointNode to serve as
1421   // as a shim for the insertion of the new code.
1422   JVMState* jvms     = sc->begin()->jvms()->clone_shallow(C);
1423   uint size = sc->begin()->req();
1424   SafePointNode* map = new SafePointNode(size, jvms);
1425 
1426   // copy the control and memory state from the final call into our
1427   // new starting state.  This allows any preceeding tests to feed
1428   // into the new section of code.
1429   for (uint i1 = 0; i1 < TypeFunc::Parms; i1++) {
1430     map->init_req(i1, sc->end()->in(i1));
1431   }
1432   // blow away old allocation arguments
1433   for (uint i1 = TypeFunc::Parms; i1 < jvms->debug_start(); i1++) {
1434     map->init_req(i1, C->top());
1435   }
1436   // Copy the rest of the inputs for the JVMState
1437   for (uint i1 = jvms->debug_start(); i1 < sc->begin()->req(); i1++) {
1438     map->init_req(i1, sc->begin()->in(i1));
1439   }
1440   // Make sure the memory state is a MergeMem for parsing.
1441   if (!map->in(TypeFunc::Memory)->is_MergeMem()) {
1442     map->set_req(TypeFunc::Memory, MergeMemNode::make(map->in(TypeFunc::Memory)));
1443   }
1444 
1445   jvms->set_map(map);
1446   map->ensure_stack(jvms, jvms->method()->max_stack());
1447 
1448 
1449   // disconnect all the old StringBuilder calls from the graph
1450   sc->eliminate_unneeded_control();
1451 
1452   // At this point all the old work has been completely removed from
1453   // the graph and the saved JVMState exists at the point where the
1454   // final toString call used to be.
1455   GraphKit kit(jvms);
1456 
1457   // There may be uncommon traps which are still using the
1458   // intermediate states and these need to be rewritten to point at
1459   // the JVMState at the beginning of the transformation.
1460   sc->convert_uncommon_traps(kit, jvms);
1461 
1462   // Now insert the logic to compute the size of the string followed
1463   // by all the logic to construct array and resulting string.
1464 
1465   Node* null_string = __ makecon(TypeInstPtr::make(C->env()->the_null_string()));
1466 
1467   // Create a region for the overflow checks to merge into.
1468   int args = MAX2(sc->num_arguments(), 1);
1469   RegionNode* overflow = new RegionNode(args);
1470   kit.gvn().set_type(overflow, Type::CONTROL);
1471 
1472   // Create a hook node to hold onto the individual sizes since they
1473   // are need for the copying phase.
1474   Node* string_sizes = new Node(args);
1475 

1476   Node* length = __ intcon(0);









1477   for (int argi = 0; argi < sc->num_arguments(); argi++) {
1478     Node* arg = sc->argument(argi);
1479     switch (sc->mode(argi)) {
1480       case StringConcat::IntMode: {
1481         Node* string_size = int_stringSize(kit, arg);
1482 
1483         // accumulate total
1484         length = __ AddI(length, string_size);
1485 
1486         // Cache this value for the use by int_toString
1487         string_sizes->init_req(argi, string_size);
1488         break;
1489       }
1490       case StringConcat::StringNullCheckMode: {
1491         const Type* type = kit.gvn().type(arg);
1492         assert(type != TypePtr::NULL_PTR, "missing check");
1493         if (!type->higher_equal(TypeInstPtr::NOTNULL)) {
1494           // Null check with uncommont trap since
1495           // StringBuilder(null) throws exception.
1496           // Use special uncommon trap instead of
1497           // calling normal do_null_check().
1498           Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne);
1499           IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN);
1500           overflow->add_req(__ IfFalse(iff));
1501           Node* notnull = __ IfTrue(iff);
1502           kit.set_control(notnull); // set control for the cast_not_null
1503           arg = kit.cast_not_null(arg, false);
1504           sc->set_argument(argi, arg);
1505         }
1506         assert(kit.gvn().type(arg)->higher_equal(TypeInstPtr::NOTNULL), "sanity");
1507         // Fallthrough to add string length.
1508       }
1509       case StringConcat::StringMode: {
1510         const Type* type = kit.gvn().type(arg);
1511         Node* count = NULL;

1512         if (type == TypePtr::NULL_PTR) {
1513           // replace the argument with the null checked version
1514           arg = null_string;
1515           sc->set_argument(argi, arg);
1516           count = kit.load_String_length(kit.control(), arg);

1517         } else if (!type->higher_equal(TypeInstPtr::NOTNULL)) {
1518           // s = s != null ? s : "null";
1519           // length = length + (s.count - s.offset);
1520           RegionNode *r = new RegionNode(3);
1521           kit.gvn().set_type(r, Type::CONTROL);
1522           Node *phi = new PhiNode(r, type);
1523           kit.gvn().set_type(phi, phi->bottom_type());
1524           Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne);
1525           IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN);
1526           Node* notnull = __ IfTrue(iff);
1527           Node* isnull =  __ IfFalse(iff);
1528           kit.set_control(notnull); // set control for the cast_not_null
1529           r->init_req(1, notnull);
1530           phi->init_req(1, kit.cast_not_null(arg, false));
1531           r->init_req(2, isnull);
1532           phi->init_req(2, null_string);
1533           kit.set_control(r);
1534           C->record_for_igvn(r);
1535           C->record_for_igvn(phi);
1536           // replace the argument with the null checked version
1537           arg = phi;
1538           sc->set_argument(argi, arg);
1539           count = kit.load_String_length(kit.control(), arg);

1540         } else {
1541           // A corresponding nullcheck will be connected during IGVN MemNode::Ideal_common_DU_postCCP
1542           // kit.control might be a different test, that can be hoisted above the actual nullcheck
1543           // in case, that the control input is not null, Ideal_common_DU_postCCP will not look for a nullcheck.
1544           count = kit.load_String_length(NULL, arg);




















1545         }
1546         length = __ AddI(length, count);
1547         string_sizes->init_req(argi, NULL);
1548         break;
1549       }
1550       case StringConcat::CharMode: {
1551         // one character only




























1552         length = __ AddI(length, __ intcon(1));
1553         break;
1554       }
1555       default:
1556         ShouldNotReachHere();
1557     }
1558     if (argi > 0) {
1559       // Check that the sum hasn't overflowed
1560       IfNode* iff = kit.create_and_map_if(kit.control(),
1561                                           __ Bool(__ CmpI(length, __ intcon(0)), BoolTest::lt),
1562                                           PROB_MIN, COUNT_UNKNOWN);
1563       kit.set_control(__ IfFalse(iff));
1564       overflow->set_req(argi, __ IfTrue(iff));
1565     }
1566   }
1567 
1568   {
1569     // Hook
1570     PreserveJVMState pjvms(&kit);
1571     kit.set_control(overflow);
1572     C->record_for_igvn(overflow);
1573     kit.uncommon_trap(Deoptimization::Reason_intrinsic,
1574                       Deoptimization::Action_make_not_entrant);
1575   }
1576 
1577   Node* result;
1578   if (!kit.stopped()) {
1579     Node* char_array = NULL;



1580     if (sc->num_arguments() == 1 &&
1581           (sc->mode(0) == StringConcat::StringMode ||
1582            sc->mode(0) == StringConcat::StringNullCheckMode)) {
1583       // Handle the case when there is only a single String argument.
1584       // In this case, we can just pull the value from the String itself.
1585       char_array = kit.load_String_value(kit.control(), sc->argument(0));
1586     } else {
1587       // length now contains the number of characters needed for the
1588       // char[] so create a new AllocateArray for the char[]
1589       {
1590         PreserveReexecuteState preexecs(&kit);
1591         // The original jvms is for an allocation of either a String or
1592         // StringBuffer so no stack adjustment is necessary for proper
1593         // reexecution.  If we deoptimize in the slow path the bytecode
1594         // will be reexecuted and the char[] allocation will be thrown away.
1595         kit.jvms()->set_should_reexecute(true);
1596         char_array = kit.new_array(__ makecon(TypeKlassPtr::make(ciTypeArrayKlass::make(T_CHAR))),
1597                                    length, 1);
1598       }
1599 
1600       // Mark the allocation so that zeroing is skipped since the code
1601       // below will overwrite the entire array
1602       AllocateArrayNode* char_alloc = AllocateArrayNode::Ideal_array_allocation(char_array, _gvn);
1603       char_alloc->maybe_set_complete(_gvn);
1604 
1605       // Now copy the string representations into the final char[]
1606       Node* start = __ intcon(0);
1607       for (int argi = 0; argi < sc->num_arguments(); argi++) {
1608         Node* arg = sc->argument(argi);
1609         switch (sc->mode(argi)) {
1610           case StringConcat::IntMode: {
1611             Node* end = __ AddI(start, string_sizes->in(argi));
1612             // getChars words backwards so pass the ending point as well as the start
1613             int_getChars(kit, arg, char_array, start, end);
1614             start = end;
1615             break;
1616           }
1617           case StringConcat::StringNullCheckMode:
1618           case StringConcat::StringMode: {
1619             start = copy_string(kit, arg, char_array, start);
1620             break;
1621           }
1622           case StringConcat::CharMode: {
1623             __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR),
1624                                arg, T_CHAR, char_adr_idx, MemNode::unordered);
1625             start = __ AddI(start, __ intcon(1));
1626             break;
1627           }
1628           default:
1629             ShouldNotReachHere();
1630         }
1631       }
1632     }
1633 
1634     // If we're not reusing an existing String allocation then allocate one here.
1635     result = sc->string_alloc();
1636     if (result == NULL) {
1637       PreserveReexecuteState preexecs(&kit);
1638       // The original jvms is for an allocation of either a String or
1639       // StringBuffer so no stack adjustment is necessary for proper
1640       // reexecution.
1641       kit.jvms()->set_should_reexecute(true);
1642       result = kit.new_instance(__ makecon(TypeKlassPtr::make(C->env()->String_klass())));
1643     }
1644 
1645     // Intialize the string
1646     if (java_lang_String::has_offset_field()) {
1647       kit.store_String_offset(kit.control(), result, __ intcon(0));
1648       kit.store_String_length(kit.control(), result, length);
1649     }
1650     kit.store_String_value(kit.control(), result, char_array);
1651   } else {
1652     result = C->top();
1653   }
1654   // hook up the outgoing control and result
1655   kit.replace_call(sc->end(), result);
1656 
1657   // Unhook any hook nodes
1658   string_sizes->disconnect_inputs(NULL, C);
1659   sc->cleanup();
1660 }


 581   return NULL;
 582 }
 583 
 584 
 585 PhaseStringOpts::PhaseStringOpts(PhaseGVN* gvn, Unique_Node_List*):
 586   Phase(StringOpts),
 587   _gvn(gvn),
 588   _visited(Thread::current()->resource_area()) {
 589 
 590   assert(OptimizeStringConcat, "shouldn't be here");
 591 
 592   size_table_field = C->env()->Integer_klass()->get_field_by_name(ciSymbol::make("sizeTable"),
 593                                                                   ciSymbol::make("[I"), true);
 594   if (size_table_field == NULL) {
 595     // Something wrong so give up.
 596     assert(false, "why can't we find Integer.sizeTable?");
 597     return;
 598   }
 599 
 600   // Collect the types needed to talk about the various slices of memory
 601   byte_adr_idx = C->get_alias_index(TypeAryPtr::BYTES);
 602 
 603   // For each locally allocated StringBuffer see if the usages can be
 604   // collapsed into a single String construction.
 605 
 606   // Run through the list of allocation looking for SB.toString to see
 607   // if it's possible to fuse the usage of the SB into a single String
 608   // construction.
 609   GrowableArray<StringConcat*> concats;
 610   Node_List toStrings = collect_toString_calls();
 611   while (toStrings.size() > 0) {
 612     StringConcat* sc = build_candidate(toStrings.pop()->as_CallStaticJava());
 613     if (sc != NULL) {
 614       concats.push(sc);
 615     }
 616   }
 617 
 618   // try to coalesce separate concats
 619  restart:
 620   for (int c = 0; c < concats.length(); c++) {
 621     StringConcat* sc = concats.at(c);


1111       ciObject* con = field->constant_value().as_object();
1112       // Do not "join" in the previous type; it doesn't add value,
1113       // and may yield a vacuous result if the field is of interface type.
1114       type = TypeOopPtr::make_from_constant(con, true)->isa_oopptr();
1115       assert(type != NULL, "field singleton type must be consistent");
1116       return __ makecon(type);
1117     } else {
1118       type = TypeOopPtr::make_from_klass(field_klass->as_klass());
1119     }
1120   } else {
1121     type = Type::get_const_basic_type(bt);
1122   }
1123 
1124   return kit.make_load(NULL, kit.basic_plus_adr(klass_node, field->offset_in_bytes()),
1125                        type, T_OBJECT,
1126                        C->get_alias_index(mirror_type->add_offset(field->offset_in_bytes())),
1127                        MemNode::unordered);
1128 }
1129 
1130 Node* PhaseStringOpts::int_stringSize(GraphKit& kit, Node* arg) {
1131   if (arg->is_Con()) {
1132     // Constant integer. Compute constant length using Integer.sizeTable
1133     int arg_val = arg->get_int();
1134     int count = 1;
1135     if (arg_val < 0) {
1136       arg_val = -arg_val;
1137       count++;
1138     }
1139 
1140     ciArray* size_table = (ciArray*)size_table_field->constant_value().as_object();
1141     for (int i = 0; i < size_table->length(); i++) {
1142       if (arg_val <= size_table->element_value(i).as_int()) {
1143         count += i;
1144         break;
1145       }
1146     }
1147     return __ intcon(count);
1148   }
1149 
1150   RegionNode *final_merge = new RegionNode(3);
1151   kit.gvn().set_type(final_merge, Type::CONTROL);
1152   Node* final_size = new PhiNode(final_merge, TypeInt::INT);
1153   kit.gvn().set_type(final_size, TypeInt::INT);
1154 
1155   IfNode* iff = kit.create_and_map_if(kit.control(),
1156                                       __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne),
1157                                       PROB_FAIR, COUNT_UNKNOWN);
1158   Node* is_min = __ IfFalse(iff);
1159   final_merge->init_req(1, is_min);
1160   final_size->init_req(1, __ intcon(11));
1161 
1162   kit.set_control(__ IfTrue(iff));
1163   if (kit.stopped()) {
1164     final_merge->init_req(2, C->top());
1165     final_size->init_req(2, C->top());
1166   } else {
1167 
1168     // int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
1169     RegionNode *r = new RegionNode(3);


1214     Node* greater = __ IfFalse(iff2);
1215 
1216     loop->init_req(2, greater);
1217     index->init_req(2, __ AddI(index, __ intcon(1)));
1218 
1219     kit.set_control(lessEqual);
1220     C->record_for_igvn(loop);
1221     C->record_for_igvn(index);
1222 
1223     final_merge->init_req(2, kit.control());
1224     final_size->init_req(2, __ AddI(__ AddI(index, size), __ intcon(1)));
1225   }
1226 
1227   kit.set_control(final_merge);
1228   C->record_for_igvn(final_merge);
1229   C->record_for_igvn(final_size);
1230 
1231   return final_size;
1232 }
1233 
1234 // Simplified version of Integer.getChars
1235 void PhaseStringOpts::getChars(GraphKit& kit, Node* arg, Node* dst_array, BasicType bt, Node* end, Node* final_merge, Node* final_mem, int merge_index) {








































1236   // if (i < 0) {
1237   //     sign = '-';
1238   //     i = -i;
1239   // }
1240   IfNode* iff = kit.create_and_map_if(kit.control(), __ Bool(__ CmpI(arg, __ intcon(0)), BoolTest::lt),


1241                                       PROB_FAIR, COUNT_UNKNOWN);
1242 
1243   RegionNode* merge = new RegionNode(3);
1244   kit.gvn().set_type(merge, Type::CONTROL);
1245   Node* i = new PhiNode(merge, TypeInt::INT);
1246   kit.gvn().set_type(i, TypeInt::INT);
1247   Node* sign = new PhiNode(merge, TypeInt::INT);
1248   kit.gvn().set_type(sign, TypeInt::INT);
1249 
1250   merge->init_req(1, __ IfTrue(iff));
1251   i->init_req(1, __ SubI(__ intcon(0), arg));
1252   sign->init_req(1, __ intcon('-'));
1253   merge->init_req(2, __ IfFalse(iff));
1254   i->init_req(2, arg);
1255   sign->init_req(2, __ intcon(0));
1256 
1257   kit.set_control(merge);
1258 
1259   C->record_for_igvn(merge);
1260   C->record_for_igvn(i);
1261   C->record_for_igvn(sign);

1262 
1263   // for (;;) {
1264   //     q = i / 10;
1265   //     r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...
1266   //     buf [--charPos] = digits [r];
1267   //     i = q;
1268   //     if (i == 0) break;
1269   // }
1270 

1271   // Add loop predicate first.
1272   kit.add_predicate();
1273 
1274   RegionNode* head = new RegionNode(3);
1275   head->init_req(1, kit.control());
1276 
1277   kit.gvn().set_type(head, Type::CONTROL);
1278   Node* i_phi = new PhiNode(head, TypeInt::INT);
1279   i_phi->init_req(1, i);
1280   kit.gvn().set_type(i_phi, TypeInt::INT);
1281   Node* charPos = new PhiNode(head, TypeInt::INT);
1282   charPos->init_req(1, end);
1283   kit.gvn().set_type(charPos, TypeInt::INT);
1284   Node* mem = PhiNode::make(head, kit.memory(byte_adr_idx), Type::MEMORY, TypeAryPtr::BYTES);
1285   kit.gvn().set_type(mem, Type::MEMORY);
1286 
1287   kit.set_control(head);
1288   kit.set_memory(mem, byte_adr_idx);
1289 
1290   Node* q = __ DivI(kit.null(), i_phi, __ intcon(10));
1291   Node* r = __ SubI(i_phi, __ AddI(__ LShiftI(q, __ intcon(3)),
1292                                    __ LShiftI(q, __ intcon(1))));
1293   Node* index = __ SubI(charPos, __ intcon((bt == T_BYTE) ? 1 : 2));
1294   Node* ch = __ AddI(r, __ intcon('0'));
1295   Node* st = __ store_to_memory(kit.control(), kit.array_element_address(dst_array, index, T_BYTE),
1296                                 ch, bt, byte_adr_idx, MemNode::unordered);
1297 
1298   iff = kit.create_and_map_if(head, __ Bool(__ CmpI(q, __ intcon(0)), BoolTest::ne),




1299                               PROB_FAIR, COUNT_UNKNOWN);
1300   Node* ne = __ IfTrue(iff);
1301   Node* eq = __ IfFalse(iff);
1302 
1303   head->init_req(2, ne);
1304   mem->init_req(2, st);


1305 
1306   i_phi->init_req(2, q);
1307   charPos->init_req(2, index);
1308   charPos = index;
1309 
1310   kit.set_control(eq);
1311   kit.set_memory(st, byte_adr_idx);
1312 
1313   C->record_for_igvn(head);
1314   C->record_for_igvn(mem);
1315   C->record_for_igvn(i_phi);
1316   C->record_for_igvn(charPos);

1317 

1318   // if (sign != 0) {
1319   //     buf [--charPos] = sign;
1320   // }
1321   iff = kit.create_and_map_if(kit.control(), __ Bool(__ CmpI(sign, __ intcon(0)), BoolTest::ne),

1322                               PROB_FAIR, COUNT_UNKNOWN);
1323 
1324   final_merge->init_req(merge_index + 2, __ IfFalse(iff));
1325   final_mem->init_req(merge_index + 2, kit.memory(byte_adr_idx));
1326 
1327   kit.set_control(__ IfTrue(iff));
1328   if (kit.stopped()) {
1329     final_merge->init_req(merge_index + 1, C->top());
1330     final_mem->init_req(merge_index + 1, C->top());
1331   } else {
1332     Node* index = __ SubI(charPos, __ intcon((bt == T_BYTE) ? 1 : 2));
1333     st = __ store_to_memory(kit.control(), kit.array_element_address(dst_array, index, T_BYTE),
1334                             sign, bt, byte_adr_idx, MemNode::unordered);
1335 
1336     final_merge->init_req(merge_index + 1, kit.control());
1337     final_mem->init_req(merge_index + 1, st);
1338   }
1339 }
1340 
1341 // Copy the characters representing arg into dst_array starting at start
1342 Node* PhaseStringOpts::int_getChars(GraphKit& kit, Node* arg, Node* dst_array, Node* dst_coder, Node* start, Node* size) {
1343   bool dcon = dst_coder->is_Con();
1344   bool dbyte = dcon ? (dst_coder->get_int() == java_lang_String::CODER_LATIN1) : false;
1345   Node* end = __ AddI(start, __ LShiftI(size, dst_coder));
1346 
1347   // The final_merge node has 4 entries in case the encoding is known:
1348   // (0) Control, (1) result w/ sign, (2) result w/o sign, (3) result for Integer.min_value
1349   // or 6 entries in case the encoding is not known:
1350   // (0) Control, (1) Latin1 w/ sign, (2) Latin1 w/o sign, (3) min_value, (4) UTF16 w/ sign, (5) UTF16 w/o sign
1351   RegionNode* final_merge = new RegionNode(dcon ? 4 : 6);
1352   kit.gvn().set_type(final_merge, Type::CONTROL);
1353 
1354   Node* final_mem = PhiNode::make(final_merge, kit.memory(byte_adr_idx), Type::MEMORY, TypeAryPtr::BYTES);
1355   kit.gvn().set_type(final_mem, Type::MEMORY);
1356 
1357   // need to handle arg == Integer.MIN_VALUE specially because negating doesn't make it positive
1358   IfNode* iff = kit.create_and_map_if(kit.control(), __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne),
1359                                       PROB_FAIR, COUNT_UNKNOWN);
1360 
1361   Node* old_mem = kit.memory(byte_adr_idx);
1362 
1363   kit.set_control(__ IfFalse(iff));
1364   if (kit.stopped()) {
1365     // Statically not equal to MIN_VALUE so this path is dead
1366     final_merge->init_req(3, kit.control());
1367   } else {
1368     copy_string(kit, __ makecon(TypeInstPtr::make(C->env()->the_min_jint_string())),
1369                 dst_array, dst_coder, start);
1370     final_merge->init_req(3, kit.control());
1371     final_mem->init_req(3, kit.memory(byte_adr_idx));
1372   }
1373 
1374   kit.set_control(__ IfTrue(iff));
1375   kit.set_memory(old_mem, byte_adr_idx);
1376 
1377   if (!dcon) {
1378     // Check encoding of destination
1379     iff = kit.create_and_map_if(kit.control(), __ Bool(__ CmpI(dst_coder, __ intcon(0)), BoolTest::eq),
1380                                 PROB_FAIR, COUNT_UNKNOWN);
1381     old_mem = kit.memory(byte_adr_idx);
1382   }
1383   if (!dcon || dbyte) {
1384     // Destination is Latin1,
1385     if (!dcon) {
1386       kit.set_control(__ IfTrue(iff));
1387     }
1388     getChars(kit, arg, dst_array, T_BYTE, end, final_merge, final_mem);
1389   }
1390   if (!dcon || !dbyte) {
1391     // Destination is UTF16
1392     int merge_index = 0;
1393     if (!dcon) {
1394       kit.set_control(__ IfFalse(iff));
1395       kit.set_memory(old_mem, byte_adr_idx);
1396       merge_index = 3; // Account for Latin1 case
1397     }
1398     getChars(kit, arg, dst_array, T_CHAR, end, final_merge, final_mem, merge_index);
1399   }
1400 
1401   // Final merge point for Latin1 and UTF16 case
1402   kit.set_control(final_merge);
1403   kit.set_memory(final_mem, byte_adr_idx);
1404 
1405   C->record_for_igvn(final_merge);
1406   C->record_for_igvn(final_mem);
1407   return end;
1408 }
1409 
1410 // Copy 'count' bytes/chars from src_array to dst_array starting at index start
1411 void PhaseStringOpts::arraycopy(GraphKit& kit, IdealKit& ideal, Node* src_array, Node* dst_array, BasicType elembt, Node* start, Node* count) {
1412   assert(elembt == T_BYTE || elembt == T_CHAR, "Invalid type for arraycopy");
1413 
1414   if (elembt == T_CHAR) {
1415     // Get number of chars
1416     count = __ RShiftI(count, __ intcon(1));
1417   }
1418 
1419   Node* extra = NULL;
1420 #ifdef _LP64
1421   count = __ ConvI2L(count);
1422   extra = C->top();
1423 #endif
1424 
1425   Node* src_ptr = __ array_element_address(src_array, __ intcon(0), T_BYTE);
1426   Node* dst_ptr = __ array_element_address(dst_array, start, T_BYTE);
1427   // Check if destination address is aligned to HeapWordSize
1428   const TypeInt* tdst = __ gvn().type(start)->is_int();
1429   bool aligned = tdst->is_con() && ((tdst->get_con() * type2aelembytes(T_BYTE)) % HeapWordSize == 0);
1430   // Figure out which arraycopy runtime method to call (disjoint, uninitialized).
1431   const char* copyfunc_name = "arraycopy";
1432   address     copyfunc_addr = StubRoutines::select_arraycopy_function(elembt, aligned, true, copyfunc_name, true);
1433   ideal.make_leaf_call_no_fp(OptoRuntime::fast_arraycopy_Type(), copyfunc_addr, copyfunc_name,
1434                              TypeAryPtr::BYTES, src_ptr, dst_ptr, count, extra);
1435 }
1436 
1437 #undef __
1438 #define __ ideal.
1439 
1440 // Copy contents of a Latin1 encoded string from src_array to dst_array
1441 void PhaseStringOpts::copy_latin1_string(GraphKit& kit, IdealKit& ideal, Node* src_array, IdealVariable& count,
1442                                          Node* dst_array, Node* dst_coder, Node* start) {
1443   bool dcon = dst_coder->is_Con();
1444   bool dbyte = dcon ? (dst_coder->get_int() == java_lang_String::CODER_LATIN1) : false;
1445 
1446   if (!dcon) {
1447     __ if_then(dst_coder, BoolTest::eq, __ ConI(java_lang_String::CODER_LATIN1));
1448   }
1449   if (!dcon || dbyte) {
1450     // Destination is Latin1. Simply emit a byte arraycopy.
1451     arraycopy(kit, ideal, src_array, dst_array, T_BYTE, start, __ value(count));
1452   }
1453   if (!dcon) {
1454     __ else_();
1455   }
1456   if (!dcon || !dbyte) {
1457     // Destination is UTF16. Inflate src_array into dst_array.
1458     kit.sync_kit(ideal);
1459     if (Matcher::match_rule_supported(Op_StrInflatedCopy)) {
1460       // Use fast intrinsic
1461       Node* src = kit.array_element_address(src_array, kit.intcon(0), T_BYTE);
1462       Node* dst = kit.array_element_address(dst_array, start, T_BYTE);
1463       kit.inflate_string(src, dst, __ value(count));
1464     } else {
1465       // No intrinsic available, use slow method
1466       kit.inflate_string_slow(src_array, dst_array, start, __ value(count));
1467     }
1468     ideal.sync_kit(&kit);
1469     // Multiply count by two since we now need two bytes per char
1470     __ set(count, __ LShiftI(__ value(count), __ ConI(1)));
1471   }
1472   if (!dcon) {
1473     __ end_if();
1474   }
1475 }
1476 
1477 // Read two bytes from index and index+1 and convert them to a char
1478 static jchar readChar(ciTypeArray* array, int index) {
1479   int shift_high, shift_low;
1480 #ifdef VM_LITTLE_ENDIAN
1481     shift_high = 0;
1482     shift_low = 8;
1483 #else
1484     shift_high = 8;
1485     shift_low = 0;
1486 #endif
1487 
1488   jchar b1 = ((jchar) array->byte_at(index)) & 0xff;
1489   jchar b2 = ((jchar) array->byte_at(index+1)) & 0xff;
1490   return (b1 << shift_high) | (b2 << shift_low);
1491 }
1492 
1493 // Copy contents of constant src_array to dst_array by emitting individual stores
1494 void PhaseStringOpts::copy_constant_string(GraphKit& kit, IdealKit& ideal, ciTypeArray* src_array, IdealVariable& count,
1495                                            bool src_is_byte, Node* dst_array, Node* dst_coder, Node* start) {
1496   bool dcon = dst_coder->is_Con();
1497   bool dbyte = dcon ? (dst_coder->get_int() == java_lang_String::CODER_LATIN1) : false;
1498   int length = src_array->length();
1499 
1500   if (!dcon) {
1501     __ if_then(dst_coder, BoolTest::eq, __ ConI(java_lang_String::CODER_LATIN1));
1502   }
1503   if (!dcon || dbyte) {
1504     // Destination is Latin1. Copy each byte of src_array into dst_array.
1505     Node* index = start;
1506     for (int i = 0; i < length; i++) {
1507       Node* adr = kit.array_element_address(dst_array, index, T_BYTE);
1508       Node* val = __ ConI(src_array->byte_at(i));
1509       __ store(__ ctrl(), adr, val, T_BYTE, byte_adr_idx, MemNode::unordered);
1510       index = __ AddI(index, __ ConI(1));
1511     }
1512   }
1513   if (!dcon) {
1514     __ else_();
1515   }
1516   if (!dcon || !dbyte) {
1517     // Destination is UTF16. Copy each char of src_array into dst_array.
1518     Node* index = start;
1519     for (int i = 0; i < length; i++) {
1520       Node* adr = kit.array_element_address(dst_array, index, T_BYTE);
1521       jchar val;
1522       if (src_is_byte) {
1523         val = src_array->byte_at(i);
1524       } else {
1525         val = readChar(src_array, i++);
1526       }
1527       __ store(__ ctrl(), adr, __ ConI(val), T_CHAR, byte_adr_idx, MemNode::unordered);
1528       index = __ AddI(index, __ ConI(2));
1529     }
1530     if (src_is_byte) {
1531       // Multiply count by two since we now need two bytes per char
1532       __ set(count, __ ConI(2 * length));
1533     }
1534   }
1535   if (!dcon) {
1536     __ end_if();
1537   }
1538 }
1539 
1540 // Compress copy contents of the byte/char String str into dst_array starting at index start.
1541 Node* PhaseStringOpts::copy_string(GraphKit& kit, Node* str, Node* dst_array, Node* dst_coder, Node* start) {
1542   Node* src_array = kit.load_String_value(kit.control(), str);
1543 
1544   IdealKit ideal(&kit, true, true);
1545   IdealVariable count(ideal); __ declarations_done();
1546 
1547   if (str->is_Con()) {
1548     // Constant source string
1549     const TypeOopPtr* t = kit.gvn().type(src_array)->isa_oopptr();
1550     ciTypeArray* src_array_type = t->const_oop()->as_type_array();
1551 
1552     // Check encoding of constant string
1553     bool src_is_byte = (get_constant_coder(kit, str) == java_lang_String::CODER_LATIN1);
1554 


1555     // For small constant strings just emit individual stores.
1556     // A length of 6 seems like a good space/speed tradeof.
1557     __ set(count, __ ConI(src_array_type->length()));
1558     int src_len = src_array_type->length() / (src_is_byte ? 1 : 2);
1559     if (src_len < unroll_string_copy_length) {
1560       // Small constant string
1561       copy_constant_string(kit, ideal, src_array_type, count, src_is_byte, dst_array, dst_coder, start);
1562     } else if (src_is_byte) {
1563       // Source is Latin1
1564       copy_latin1_string(kit, ideal, src_array, count, dst_array, dst_coder, start);
1565     } else {
1566       // Source is UTF16 (destination too). Simply emit a char arraycopy.
1567       arraycopy(kit, ideal, src_array, dst_array, T_CHAR, start, __ value(count));
1568     }
1569   } else {
1570     Node* size = kit.load_array_length(src_array);
1571     __ set(count, size);
1572     // Non-constant source string
1573     if (CompactStrings) {
1574       // Emit runtime check for coder
1575       Node* coder = kit.load_String_coder(__ ctrl(), str);
1576       __ if_then(coder, BoolTest::eq, __ ConI(java_lang_String::CODER_LATIN1)); {
1577         // Source is Latin1
1578         copy_latin1_string(kit, ideal, src_array, count, dst_array, dst_coder, start);
1579       } __ else_();
1580     }
1581     // Source is UTF16 (destination too). Simply emit a char arraycopy.
1582     arraycopy(kit, ideal, src_array, dst_array, T_CHAR, start, __ value(count));
1583 
1584     if (CompactStrings) {
1585       __ end_if();
1586     }
1587   }
1588 
1589   // Finally sync IdealKit and GraphKit.
1590   kit.sync_kit(ideal);
1591   return __ AddI(start, __ value(count));
1592 }
1593 
1594 // Compress copy the char into dst_array at index start.
1595 Node* PhaseStringOpts::copy_char(GraphKit& kit, Node* val, Node* dst_array, Node* dst_coder, Node* start) {
1596   bool dcon = (dst_coder != NULL) && dst_coder->is_Con();
1597   bool dbyte = dcon ? (dst_coder->get_int() == java_lang_String::CODER_LATIN1) : false;
1598 
1599   IdealKit ideal(&kit, true, true);
1600   IdealVariable end(ideal); __ declarations_done();
1601   Node* adr = kit.array_element_address(dst_array, start, T_BYTE);
1602   if (!dcon){
1603     __ if_then(dst_coder, BoolTest::eq, __ ConI(java_lang_String::CODER_LATIN1));
1604   }
1605   if (!dcon || dbyte) {
1606     // Destination is Latin1. Store a byte.
1607     __ store(__ ctrl(), adr, val, T_BYTE, byte_adr_idx, MemNode::unordered);
1608     __ set(end, __ AddI(start, __ ConI(1)));
1609   }
1610   if (!dcon) {
1611     __ else_();
1612   }
1613   if (!dcon || !dbyte) {
1614     // Destination is UTF16. Store a char.
1615     __ store(__ ctrl(), adr, val, T_CHAR, byte_adr_idx, MemNode::unordered);
1616     __ set(end, __ AddI(start, __ ConI(2)));
1617   }
1618   if (!dcon) {
1619     __ end_if();
1620   }
1621   // Finally sync IdealKit and GraphKit.
1622   kit.sync_kit(ideal);
1623   return __ value(end);
1624 }
1625 
1626 #undef __
1627 #define __ kit.
1628 
1629 // Allocate a byte array of specified length.
1630 Node* PhaseStringOpts::allocate_byte_array(GraphKit& kit, IdealKit* ideal, Node* length) {
1631   if (ideal != NULL) {
1632     // Sync IdealKit and graphKit.
1633     kit.sync_kit(*ideal);
1634   }
1635   Node* byte_array = NULL;
1636   {
1637     PreserveReexecuteState preexecs(&kit);
1638     // The original jvms is for an allocation of either a String or
1639     // StringBuffer so no stack adjustment is necessary for proper
1640     // reexecution.  If we deoptimize in the slow path the bytecode
1641     // will be reexecuted and the char[] allocation will be thrown away.
1642     kit.jvms()->set_should_reexecute(true);
1643     byte_array = kit.new_array(__ makecon(TypeKlassPtr::make(ciTypeArrayKlass::make(T_BYTE))),
1644                                length, 1);
1645   }
1646 
1647   // Mark the allocation so that zeroing is skipped since the code
1648   // below will overwrite the entire array
1649   AllocateArrayNode* byte_alloc = AllocateArrayNode::Ideal_array_allocation(byte_array, _gvn);
1650   byte_alloc->maybe_set_complete(_gvn);
1651 
1652   if (ideal != NULL) {
1653     // Sync IdealKit and graphKit.
1654     ideal->sync_kit(&kit);
1655   }
1656   return byte_array;
1657 }
1658 
1659 jbyte PhaseStringOpts::get_constant_coder(GraphKit& kit, Node* str) {
1660   assert(str->is_Con(), "String must be constant");
1661   const TypeOopPtr* str_type = kit.gvn().type(str)->isa_oopptr();
1662   ciInstance* str_instance = str_type->const_oop()->as_instance();
1663   jbyte coder = str_instance->field_value_by_offset(java_lang_String::coder_offset_in_bytes()).as_byte();
1664   assert(CompactStrings || (coder == java_lang_String::CODER_UTF16), "Strings must be UTF16 encoded");
1665   return coder;
1666 }
1667 
1668 int PhaseStringOpts::get_constant_length(GraphKit& kit, Node* str) {
1669   assert(str->is_Con(), "String must be constant");
1670   Node* src_array = kit.load_String_value(kit.control(), str);
1671   const TypeOopPtr* t = kit.gvn().type(src_array)->isa_oopptr();
1672   return t->const_oop()->as_type_array()->length();
1673 }
1674 
1675 void PhaseStringOpts::replace_string_concat(StringConcat* sc) {
1676   // Log a little info about the transformation
1677   sc->maybe_log_transform();
1678 
1679   // pull the JVMState of the allocation into a SafePointNode to serve as
1680   // as a shim for the insertion of the new code.
1681   JVMState* jvms     = sc->begin()->jvms()->clone_shallow(C);
1682   uint size = sc->begin()->req();
1683   SafePointNode* map = new SafePointNode(size, jvms);
1684 
1685   // copy the control and memory state from the final call into our
1686   // new starting state.  This allows any preceeding tests to feed
1687   // into the new section of code.
1688   for (uint i1 = 0; i1 < TypeFunc::Parms; i1++) {
1689     map->init_req(i1, sc->end()->in(i1));
1690   }
1691   // blow away old allocation arguments
1692   for (uint i1 = TypeFunc::Parms; i1 < jvms->debug_start(); i1++) {
1693     map->init_req(i1, C->top());
1694   }
1695   // Copy the rest of the inputs for the JVMState
1696   for (uint i1 = jvms->debug_start(); i1 < sc->begin()->req(); i1++) {
1697     map->init_req(i1, sc->begin()->in(i1));
1698   }
1699   // Make sure the memory state is a MergeMem for parsing.
1700   if (!map->in(TypeFunc::Memory)->is_MergeMem()) {
1701     map->set_req(TypeFunc::Memory, MergeMemNode::make(map->in(TypeFunc::Memory)));
1702   }
1703 
1704   jvms->set_map(map);
1705   map->ensure_stack(jvms, jvms->method()->max_stack());
1706 

1707   // disconnect all the old StringBuilder calls from the graph
1708   sc->eliminate_unneeded_control();
1709 
1710   // At this point all the old work has been completely removed from
1711   // the graph and the saved JVMState exists at the point where the
1712   // final toString call used to be.
1713   GraphKit kit(jvms);
1714 
1715   // There may be uncommon traps which are still using the
1716   // intermediate states and these need to be rewritten to point at
1717   // the JVMState at the beginning of the transformation.
1718   sc->convert_uncommon_traps(kit, jvms);
1719 
1720   // Now insert the logic to compute the size of the string followed
1721   // by all the logic to construct array and resulting string.
1722 
1723   Node* null_string = __ makecon(TypeInstPtr::make(C->env()->the_null_string()));
1724 
1725   // Create a region for the overflow checks to merge into.
1726   int args = MAX2(sc->num_arguments(), 1);
1727   RegionNode* overflow = new RegionNode(args);
1728   kit.gvn().set_type(overflow, Type::CONTROL);
1729 
1730   // Create a hook node to hold onto the individual sizes since they
1731   // are need for the copying phase.
1732   Node* string_sizes = new Node(args);
1733 
1734   Node* coder = __ intcon(0);
1735   Node* length = __ intcon(0);
1736   // If at least one argument is UTF16 encoded, we can fix the encoding.
1737   bool coder_fixed = false;
1738 
1739   if (!CompactStrings) {
1740     // Fix encoding of result string to UTF16
1741     coder_fixed = true;
1742     coder = __ intcon(java_lang_String::CODER_UTF16);
1743   }
1744 
1745   for (int argi = 0; argi < sc->num_arguments(); argi++) {
1746     Node* arg = sc->argument(argi);
1747     switch (sc->mode(argi)) {
1748       case StringConcat::IntMode: {
1749         Node* string_size = int_stringSize(kit, arg);
1750 
1751         // accumulate total
1752         length = __ AddI(length, string_size);
1753 
1754         // Cache this value for the use by int_toString
1755         string_sizes->init_req(argi, string_size);
1756         break;
1757       }
1758       case StringConcat::StringNullCheckMode: {
1759         const Type* type = kit.gvn().type(arg);
1760         assert(type != TypePtr::NULL_PTR, "missing check");
1761         if (!type->higher_equal(TypeInstPtr::NOTNULL)) {
1762           // Null check with uncommon trap since
1763           // StringBuilder(null) throws exception.
1764           // Use special uncommon trap instead of
1765           // calling normal do_null_check().
1766           Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne);
1767           IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN);
1768           overflow->add_req(__ IfFalse(iff));
1769           Node* notnull = __ IfTrue(iff);
1770           kit.set_control(notnull); // set control for the cast_not_null
1771           arg = kit.cast_not_null(arg, false);
1772           sc->set_argument(argi, arg);
1773         }
1774         assert(kit.gvn().type(arg)->higher_equal(TypeInstPtr::NOTNULL), "sanity");
1775         // Fallthrough to add string length.
1776       }
1777       case StringConcat::StringMode: {
1778         const Type* type = kit.gvn().type(arg);
1779         Node* count = NULL;
1780         Node* arg_coder = NULL;
1781         if (type == TypePtr::NULL_PTR) {
1782           // replace the argument with the null checked version
1783           arg = null_string;
1784           sc->set_argument(argi, arg);
1785           count = kit.load_String_length(kit.control(), arg);
1786           arg_coder = kit.load_String_coder(kit.control(), arg);
1787         } else if (!type->higher_equal(TypeInstPtr::NOTNULL)) {
1788           // s = s != null ? s : "null";
1789           // length = length + (s.count - s.offset);
1790           RegionNode *r = new RegionNode(3);
1791           kit.gvn().set_type(r, Type::CONTROL);
1792           Node *phi = new PhiNode(r, type);
1793           kit.gvn().set_type(phi, phi->bottom_type());
1794           Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne);
1795           IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN);
1796           Node* notnull = __ IfTrue(iff);
1797           Node* isnull =  __ IfFalse(iff);
1798           kit.set_control(notnull); // set control for the cast_not_null
1799           r->init_req(1, notnull);
1800           phi->init_req(1, kit.cast_not_null(arg, false));
1801           r->init_req(2, isnull);
1802           phi->init_req(2, null_string);
1803           kit.set_control(r);
1804           C->record_for_igvn(r);
1805           C->record_for_igvn(phi);
1806           // replace the argument with the null checked version
1807           arg = phi;
1808           sc->set_argument(argi, arg);
1809           count = kit.load_String_length(kit.control(), arg);
1810           arg_coder = kit.load_String_coder(kit.control(), arg);
1811         } else {
1812           // A corresponding nullcheck will be connected during IGVN MemNode::Ideal_common_DU_postCCP
1813           // kit.control might be a different test, that can be hoisted above the actual nullcheck
1814           // in case, that the control input is not null, Ideal_common_DU_postCCP will not look for a nullcheck.
1815           count = kit.load_String_length(NULL, arg);
1816           arg_coder = kit.load_String_coder(NULL, arg);
1817         }
1818         if (arg->is_Con()) {
1819           // Constant string. Get constant coder and length.
1820           jbyte const_coder = get_constant_coder(kit, arg);
1821           int const_length = get_constant_length(kit, arg);
1822           if (const_coder == java_lang_String::CODER_LATIN1) {
1823             // Can be latin1 encoded
1824             arg_coder = __ intcon(const_coder);
1825             count = __ intcon(const_length);
1826           } else {
1827             // Found UTF16 encoded string. Fix result array encoding to UTF16.
1828             coder_fixed = true;
1829             coder = __ intcon(const_coder);
1830             count = __ intcon(const_length / 2);
1831           }
1832         }
1833 
1834         if (!coder_fixed) {
1835           coder = __ OrI(coder, arg_coder);
1836         }
1837         length = __ AddI(length, count);
1838         string_sizes->init_req(argi, NULL);
1839         break;
1840       }
1841       case StringConcat::CharMode: {
1842         // one character only
1843         const TypeInt* t = kit.gvn().type(arg)->is_int();
1844         if (!coder_fixed && t->is_con()) {
1845           // Constant char
1846           if (t->get_con() <= 255) {
1847             // Can be latin1 encoded
1848             coder = __ OrI(coder, __ intcon(java_lang_String::CODER_LATIN1));
1849           } else {
1850             // Must be UTF16 encoded. Fix result array encoding to UTF16.
1851             coder_fixed = true;
1852             coder = __ intcon(java_lang_String::CODER_UTF16);
1853           }
1854         } else if (!coder_fixed) {
1855           // Not constant
1856 #undef __
1857 #define __ ideal.
1858           IdealKit ideal(&kit, true, true);
1859           IdealVariable char_coder(ideal); __ declarations_done();
1860           // Check if character can be latin1 encoded
1861           __ if_then(arg, BoolTest::le, __ ConI(0xFF));
1862             __ set(char_coder, __ ConI(java_lang_String::CODER_LATIN1));
1863           __ else_();
1864             __ set(char_coder, __ ConI(java_lang_String::CODER_UTF16));
1865           __ end_if();
1866           kit.sync_kit(ideal);
1867           coder = __ OrI(coder, __ value(char_coder));
1868 #undef __
1869 #define __ kit.
1870         }
1871         length = __ AddI(length, __ intcon(1));
1872         break;
1873       }
1874       default:
1875         ShouldNotReachHere();
1876     }
1877     if (argi > 0) {
1878       // Check that the sum hasn't overflowed
1879       IfNode* iff = kit.create_and_map_if(kit.control(),
1880                                           __ Bool(__ CmpI(length, __ intcon(0)), BoolTest::lt),
1881                                           PROB_MIN, COUNT_UNKNOWN);
1882       kit.set_control(__ IfFalse(iff));
1883       overflow->set_req(argi, __ IfTrue(iff));
1884     }
1885   }
1886 
1887   {
1888     // Hook
1889     PreserveJVMState pjvms(&kit);
1890     kit.set_control(overflow);
1891     C->record_for_igvn(overflow);
1892     kit.uncommon_trap(Deoptimization::Reason_intrinsic,
1893                       Deoptimization::Action_make_not_entrant);
1894   }
1895 
1896   Node* result;
1897   if (!kit.stopped()) {
1898     assert(CompactStrings || (coder->is_Con() && coder->get_int() == java_lang_String::CODER_UTF16),
1899            "Result string must be UTF16 encoded if CompactStrings is disabled");
1900 
1901     Node* dst_array = NULL;
1902     if (sc->num_arguments() == 1 &&
1903         (sc->mode(0) == StringConcat::StringMode ||
1904          sc->mode(0) == StringConcat::StringNullCheckMode)) {
1905       // Handle the case when there is only a single String argument.
1906       // In this case, we can just pull the value from the String itself.
1907       dst_array = kit.load_String_value(kit.control(), sc->argument(0));
1908     } else {
1909       // Allocate destination byte array according to coder
1910       dst_array = allocate_byte_array(kit, NULL, __ LShiftI(length, coder));










1911 
1912       // Now copy the string representations into the final byte[]





1913       Node* start = __ intcon(0);
1914       for (int argi = 0; argi < sc->num_arguments(); argi++) {
1915         Node* arg = sc->argument(argi);
1916         switch (sc->mode(argi)) {
1917           case StringConcat::IntMode: {
1918             start = int_getChars(kit, arg, dst_array, coder, start, string_sizes->in(argi));



1919             break;
1920           }
1921           case StringConcat::StringNullCheckMode:
1922           case StringConcat::StringMode: {
1923             start = copy_string(kit, arg, dst_array, coder, start);
1924             break;
1925           }
1926           case StringConcat::CharMode: {
1927             start = copy_char(kit, arg, dst_array, coder, start);


1928           break;
1929           }
1930           default:
1931             ShouldNotReachHere();
1932         }
1933       }
1934     }
1935 
1936     // If we're not reusing an existing String allocation then allocate one here.
1937     result = sc->string_alloc();
1938     if (result == NULL) {
1939       PreserveReexecuteState preexecs(&kit);
1940       // The original jvms is for an allocation of either a String or
1941       // StringBuffer so no stack adjustment is necessary for proper
1942       // reexecution.
1943       kit.jvms()->set_should_reexecute(true);
1944       result = kit.new_instance(__ makecon(TypeKlassPtr::make(C->env()->String_klass())));
1945     }
1946 
1947     // Initialize the string
1948     kit.store_String_value(kit.control(), result, dst_array);
1949     kit.store_String_coder(kit.control(), result, coder);



1950   } else {
1951     result = C->top();
1952   }
1953   // hook up the outgoing control and result
1954   kit.replace_call(sc->end(), result);
1955 
1956   // Unhook any hook nodes
1957   string_sizes->disconnect_inputs(NULL, C);
1958   sc->cleanup();
1959 }
< prev index next >