--- old/src/share/vm/opto/cfgnode.hpp 2015-09-29 14:27:50.587019342 +0200 +++ new/src/share/vm/opto/cfgnode.hpp 2015-09-29 14:27:50.451019348 +0200 @@ -395,10 +395,12 @@ public: IfProjNode(IfNode *ifnode, uint idx) : CProjNode(ifnode,idx) {} virtual Node *Identity(PhaseTransform *phase); + virtual Node* Ideal(PhaseGVN* phase, bool can_reshape); protected: // Type of If input when this branch is always taken virtual bool always_taken(const TypeTuple* t) const = 0; + virtual bool never_taken(const TypeTuple* t) const = 0; #ifndef PRODUCT public: @@ -415,6 +417,7 @@ protected: virtual bool always_taken(const TypeTuple* t) const { return t == TypeTuple::IFTRUE; } + virtual bool never_taken(const TypeTuple* t) const { return t == TypeTuple::IFFALSE; } }; class IfFalseNode : public IfProjNode { @@ -426,6 +429,7 @@ protected: virtual bool always_taken(const TypeTuple* t) const { return t == TypeTuple::IFFALSE; } + virtual bool never_taken(const TypeTuple* t) const { return t == TypeTuple::IFTRUE; } }; --- old/src/share/vm/opto/ifnode.cpp 2015-09-29 14:27:51.319019308 +0200 +++ new/src/share/vm/opto/ifnode.cpp 2015-09-29 14:27:51.151019315 +0200 @@ -1625,6 +1625,19 @@ return this; } +Node* IfProjNode::Ideal(PhaseGVN* phase, bool can_reshape) { + if (in(0)->is_top()) { + return NULL; + } + const TypeTuple* t = phase->type(in(0))->is_tuple(); + if (never_taken(t)) { + // Disconnect never taken branch + set_req(0, phase->C->top()); + return this; + } + return NULL; +} + #ifndef PRODUCT //-------------------------------related--------------------------------------- // An IfProjNode's related node set consists of its input (an IfNode) including --- old/src/share/vm/opto/stringopts.cpp 2015-09-29 14:27:52.071019273 +0200 +++ new/src/share/vm/opto/stringopts.cpp 2015-09-29 14:27:51.899019281 +0200 @@ -822,11 +822,10 @@ } } else if (ctrl->is_IfTrue()) { // null checks, class checks iff = ctrl->in(0)->as_If(); - assert(iff->is_If(), "must be if"); // Verify that the other arm is an uncommon trap Node* otherproj = iff->proj_out(1 - ctrl->as_Proj()->_con); CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava(); - assert(strcmp(call->_name, "uncommon_trap") == 0, "must be uncommond trap"); + assert(strcmp(call->_name, "uncommon_trap") == 0, "must be uncommon trap"); ctrl = iff->in(0); } else { break; @@ -914,6 +913,13 @@ BoolNode* b = iff->in(1)->isa_Bool(); if (b == NULL) { +#ifndef PRODUCT + if (PrintOptimizeStringConcat) { + tty->print_cr("unexpected input to IfNode"); + iff->in(1)->dump(); + tty->cr(); + } +#endif fail = true; break; } --- /dev/null 2015-09-29 07:28:17.018874950 +0200 +++ new/test/compiler/stringopts/TestPresizedStringBuilder.java 2015-09-29 14:27:52.667019245 +0200 @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8136469 + * @summary C2's string concatenation optimization fails with pre-sized StringBuilder + * @run main/othervm -XX:CompileCommand=dontinline,TestPresizedStringBuilder::test TestPresizedStringBuilder + */ +public class TestPresizedStringBuilder { + + static final boolean count; + static { + count = true; + } + + public static void main(String[] args) { + for (int i = 0; i < 100_000; ++i) { + test(); + } + } + + public static String test() { + return new StringBuilder(count ? 1 : 2).toString(); + } +} +