src/share/vm/opto/compile.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7385
9e69e8d1c900
parent 6876
710a3c8b516e
child 7994
04ff2f6cd0eb
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@1 25 /*
aoqi@1 26 * This file has been modified by Loongson Technology in 2015. These
aoqi@1 27 * modifications are Copyright (c) 2015 Loongson Technology, and are made
aoqi@1 28 * available on the same license terms set forth above.
aoqi@1 29 */
aoqi@1 30
aoqi@0 31 #include "precompiled.hpp"
aoqi@0 32 #include "asm/macroAssembler.hpp"
aoqi@0 33 #include "asm/macroAssembler.inline.hpp"
aoqi@0 34 #include "ci/ciReplay.hpp"
aoqi@0 35 #include "classfile/systemDictionary.hpp"
aoqi@0 36 #include "code/exceptionHandlerTable.hpp"
aoqi@0 37 #include "code/nmethod.hpp"
aoqi@0 38 #include "compiler/compileLog.hpp"
aoqi@0 39 #include "compiler/disassembler.hpp"
aoqi@0 40 #include "compiler/oopMap.hpp"
aoqi@0 41 #include "opto/addnode.hpp"
aoqi@0 42 #include "opto/block.hpp"
aoqi@0 43 #include "opto/c2compiler.hpp"
aoqi@0 44 #include "opto/callGenerator.hpp"
aoqi@0 45 #include "opto/callnode.hpp"
aoqi@0 46 #include "opto/cfgnode.hpp"
aoqi@0 47 #include "opto/chaitin.hpp"
aoqi@0 48 #include "opto/compile.hpp"
aoqi@0 49 #include "opto/connode.hpp"
aoqi@0 50 #include "opto/divnode.hpp"
aoqi@0 51 #include "opto/escape.hpp"
aoqi@0 52 #include "opto/idealGraphPrinter.hpp"
aoqi@0 53 #include "opto/loopnode.hpp"
aoqi@0 54 #include "opto/machnode.hpp"
aoqi@0 55 #include "opto/macro.hpp"
aoqi@0 56 #include "opto/matcher.hpp"
aoqi@0 57 #include "opto/mathexactnode.hpp"
aoqi@0 58 #include "opto/memnode.hpp"
aoqi@0 59 #include "opto/mulnode.hpp"
aoqi@0 60 #include "opto/node.hpp"
aoqi@0 61 #include "opto/opcodes.hpp"
aoqi@0 62 #include "opto/output.hpp"
aoqi@0 63 #include "opto/parse.hpp"
aoqi@0 64 #include "opto/phaseX.hpp"
aoqi@0 65 #include "opto/rootnode.hpp"
aoqi@0 66 #include "opto/runtime.hpp"
aoqi@0 67 #include "opto/stringopts.hpp"
aoqi@0 68 #include "opto/type.hpp"
aoqi@0 69 #include "opto/vectornode.hpp"
aoqi@0 70 #include "runtime/arguments.hpp"
aoqi@0 71 #include "runtime/signature.hpp"
aoqi@0 72 #include "runtime/stubRoutines.hpp"
aoqi@0 73 #include "runtime/timer.hpp"
aoqi@0 74 #include "trace/tracing.hpp"
aoqi@0 75 #include "utilities/copy.hpp"
aoqi@0 76 #ifdef TARGET_ARCH_MODEL_x86_32
aoqi@0 77 # include "adfiles/ad_x86_32.hpp"
aoqi@0 78 #endif
aoqi@0 79 #ifdef TARGET_ARCH_MODEL_x86_64
aoqi@0 80 # include "adfiles/ad_x86_64.hpp"
aoqi@0 81 #endif
aoqi@1 82 #ifdef TARGET_ARCH_MODEL_mips_64
aoqi@1 83 # include "adfiles/ad_mips_64.hpp"
aoqi@1 84 #endif
aoqi@0 85 #ifdef TARGET_ARCH_MODEL_sparc
aoqi@0 86 # include "adfiles/ad_sparc.hpp"
aoqi@0 87 #endif
aoqi@0 88 #ifdef TARGET_ARCH_MODEL_zero
aoqi@0 89 # include "adfiles/ad_zero.hpp"
aoqi@0 90 #endif
aoqi@0 91 #ifdef TARGET_ARCH_MODEL_arm
aoqi@0 92 # include "adfiles/ad_arm.hpp"
aoqi@0 93 #endif
aoqi@0 94 #ifdef TARGET_ARCH_MODEL_ppc_32
aoqi@0 95 # include "adfiles/ad_ppc_32.hpp"
aoqi@0 96 #endif
aoqi@0 97 #ifdef TARGET_ARCH_MODEL_ppc_64
aoqi@0 98 # include "adfiles/ad_ppc_64.hpp"
aoqi@0 99 #endif
aoqi@0 100
aoqi@0 101
aoqi@0 102 // -------------------- Compile::mach_constant_base_node -----------------------
aoqi@0 103 // Constant table base node singleton.
aoqi@0 104 MachConstantBaseNode* Compile::mach_constant_base_node() {
aoqi@0 105 if (_mach_constant_base_node == NULL) {
aoqi@0 106 _mach_constant_base_node = new (C) MachConstantBaseNode();
aoqi@0 107 _mach_constant_base_node->add_req(C->root());
aoqi@0 108 }
aoqi@0 109 return _mach_constant_base_node;
aoqi@0 110 }
aoqi@0 111
aoqi@0 112
aoqi@0 113 /// Support for intrinsics.
aoqi@0 114
aoqi@0 115 // Return the index at which m must be inserted (or already exists).
aoqi@0 116 // The sort order is by the address of the ciMethod, with is_virtual as minor key.
aoqi@0 117 int Compile::intrinsic_insertion_index(ciMethod* m, bool is_virtual) {
aoqi@0 118 #ifdef ASSERT
aoqi@0 119 for (int i = 1; i < _intrinsics->length(); i++) {
aoqi@0 120 CallGenerator* cg1 = _intrinsics->at(i-1);
aoqi@0 121 CallGenerator* cg2 = _intrinsics->at(i);
aoqi@0 122 assert(cg1->method() != cg2->method()
aoqi@0 123 ? cg1->method() < cg2->method()
aoqi@0 124 : cg1->is_virtual() < cg2->is_virtual(),
aoqi@0 125 "compiler intrinsics list must stay sorted");
aoqi@0 126 }
aoqi@0 127 #endif
aoqi@0 128 // Binary search sorted list, in decreasing intervals [lo, hi].
aoqi@0 129 int lo = 0, hi = _intrinsics->length()-1;
aoqi@0 130 while (lo <= hi) {
aoqi@0 131 int mid = (uint)(hi + lo) / 2;
aoqi@0 132 ciMethod* mid_m = _intrinsics->at(mid)->method();
aoqi@0 133 if (m < mid_m) {
aoqi@0 134 hi = mid-1;
aoqi@0 135 } else if (m > mid_m) {
aoqi@0 136 lo = mid+1;
aoqi@0 137 } else {
aoqi@0 138 // look at minor sort key
aoqi@0 139 bool mid_virt = _intrinsics->at(mid)->is_virtual();
aoqi@0 140 if (is_virtual < mid_virt) {
aoqi@0 141 hi = mid-1;
aoqi@0 142 } else if (is_virtual > mid_virt) {
aoqi@0 143 lo = mid+1;
aoqi@0 144 } else {
aoqi@0 145 return mid; // exact match
aoqi@0 146 }
aoqi@0 147 }
aoqi@0 148 }
aoqi@0 149 return lo; // inexact match
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 void Compile::register_intrinsic(CallGenerator* cg) {
aoqi@0 153 if (_intrinsics == NULL) {
aoqi@0 154 _intrinsics = new (comp_arena())GrowableArray<CallGenerator*>(comp_arena(), 60, 0, NULL);
aoqi@0 155 }
aoqi@0 156 // This code is stolen from ciObjectFactory::insert.
aoqi@0 157 // Really, GrowableArray should have methods for
aoqi@0 158 // insert_at, remove_at, and binary_search.
aoqi@0 159 int len = _intrinsics->length();
aoqi@0 160 int index = intrinsic_insertion_index(cg->method(), cg->is_virtual());
aoqi@0 161 if (index == len) {
aoqi@0 162 _intrinsics->append(cg);
aoqi@0 163 } else {
aoqi@0 164 #ifdef ASSERT
aoqi@0 165 CallGenerator* oldcg = _intrinsics->at(index);
aoqi@0 166 assert(oldcg->method() != cg->method() || oldcg->is_virtual() != cg->is_virtual(), "don't register twice");
aoqi@0 167 #endif
aoqi@0 168 _intrinsics->append(_intrinsics->at(len-1));
aoqi@0 169 int pos;
aoqi@0 170 for (pos = len-2; pos >= index; pos--) {
aoqi@0 171 _intrinsics->at_put(pos+1,_intrinsics->at(pos));
aoqi@0 172 }
aoqi@0 173 _intrinsics->at_put(index, cg);
aoqi@0 174 }
aoqi@0 175 assert(find_intrinsic(cg->method(), cg->is_virtual()) == cg, "registration worked");
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 CallGenerator* Compile::find_intrinsic(ciMethod* m, bool is_virtual) {
aoqi@0 179 assert(m->is_loaded(), "don't try this on unloaded methods");
aoqi@0 180 if (_intrinsics != NULL) {
aoqi@0 181 int index = intrinsic_insertion_index(m, is_virtual);
aoqi@0 182 if (index < _intrinsics->length()
aoqi@0 183 && _intrinsics->at(index)->method() == m
aoqi@0 184 && _intrinsics->at(index)->is_virtual() == is_virtual) {
aoqi@0 185 return _intrinsics->at(index);
aoqi@0 186 }
aoqi@0 187 }
aoqi@0 188 // Lazily create intrinsics for intrinsic IDs well-known in the runtime.
aoqi@0 189 if (m->intrinsic_id() != vmIntrinsics::_none &&
aoqi@0 190 m->intrinsic_id() <= vmIntrinsics::LAST_COMPILER_INLINE) {
aoqi@0 191 CallGenerator* cg = make_vm_intrinsic(m, is_virtual);
aoqi@0 192 if (cg != NULL) {
aoqi@0 193 // Save it for next time:
aoqi@0 194 register_intrinsic(cg);
aoqi@0 195 return cg;
aoqi@0 196 } else {
aoqi@0 197 gather_intrinsic_statistics(m->intrinsic_id(), is_virtual, _intrinsic_disabled);
aoqi@0 198 }
aoqi@0 199 }
aoqi@0 200 return NULL;
aoqi@0 201 }
aoqi@0 202
aoqi@0 203 // Compile:: register_library_intrinsics and make_vm_intrinsic are defined
aoqi@0 204 // in library_call.cpp.
aoqi@0 205
aoqi@0 206
aoqi@0 207 #ifndef PRODUCT
aoqi@0 208 // statistics gathering...
aoqi@0 209
aoqi@0 210 juint Compile::_intrinsic_hist_count[vmIntrinsics::ID_LIMIT] = {0};
aoqi@0 211 jubyte Compile::_intrinsic_hist_flags[vmIntrinsics::ID_LIMIT] = {0};
aoqi@0 212
aoqi@0 213 bool Compile::gather_intrinsic_statistics(vmIntrinsics::ID id, bool is_virtual, int flags) {
aoqi@0 214 assert(id > vmIntrinsics::_none && id < vmIntrinsics::ID_LIMIT, "oob");
aoqi@0 215 int oflags = _intrinsic_hist_flags[id];
aoqi@0 216 assert(flags != 0, "what happened?");
aoqi@0 217 if (is_virtual) {
aoqi@0 218 flags |= _intrinsic_virtual;
aoqi@0 219 }
aoqi@0 220 bool changed = (flags != oflags);
aoqi@0 221 if ((flags & _intrinsic_worked) != 0) {
aoqi@0 222 juint count = (_intrinsic_hist_count[id] += 1);
aoqi@0 223 if (count == 1) {
aoqi@0 224 changed = true; // first time
aoqi@0 225 }
aoqi@0 226 // increment the overall count also:
aoqi@0 227 _intrinsic_hist_count[vmIntrinsics::_none] += 1;
aoqi@0 228 }
aoqi@0 229 if (changed) {
aoqi@0 230 if (((oflags ^ flags) & _intrinsic_virtual) != 0) {
aoqi@0 231 // Something changed about the intrinsic's virtuality.
aoqi@0 232 if ((flags & _intrinsic_virtual) != 0) {
aoqi@0 233 // This is the first use of this intrinsic as a virtual call.
aoqi@0 234 if (oflags != 0) {
aoqi@0 235 // We already saw it as a non-virtual, so note both cases.
aoqi@0 236 flags |= _intrinsic_both;
aoqi@0 237 }
aoqi@0 238 } else if ((oflags & _intrinsic_both) == 0) {
aoqi@0 239 // This is the first use of this intrinsic as a non-virtual
aoqi@0 240 flags |= _intrinsic_both;
aoqi@0 241 }
aoqi@0 242 }
aoqi@0 243 _intrinsic_hist_flags[id] = (jubyte) (oflags | flags);
aoqi@0 244 }
aoqi@0 245 // update the overall flags also:
aoqi@0 246 _intrinsic_hist_flags[vmIntrinsics::_none] |= (jubyte) flags;
aoqi@0 247 return changed;
aoqi@0 248 }
aoqi@0 249
aoqi@0 250 static char* format_flags(int flags, char* buf) {
aoqi@0 251 buf[0] = 0;
aoqi@0 252 if ((flags & Compile::_intrinsic_worked) != 0) strcat(buf, ",worked");
aoqi@0 253 if ((flags & Compile::_intrinsic_failed) != 0) strcat(buf, ",failed");
aoqi@0 254 if ((flags & Compile::_intrinsic_disabled) != 0) strcat(buf, ",disabled");
aoqi@0 255 if ((flags & Compile::_intrinsic_virtual) != 0) strcat(buf, ",virtual");
aoqi@0 256 if ((flags & Compile::_intrinsic_both) != 0) strcat(buf, ",nonvirtual");
aoqi@0 257 if (buf[0] == 0) strcat(buf, ",");
aoqi@0 258 assert(buf[0] == ',', "must be");
aoqi@0 259 return &buf[1];
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 void Compile::print_intrinsic_statistics() {
aoqi@0 263 char flagsbuf[100];
aoqi@0 264 ttyLocker ttyl;
aoqi@0 265 if (xtty != NULL) xtty->head("statistics type='intrinsic'");
aoqi@0 266 tty->print_cr("Compiler intrinsic usage:");
aoqi@0 267 juint total = _intrinsic_hist_count[vmIntrinsics::_none];
aoqi@0 268 if (total == 0) total = 1; // avoid div0 in case of no successes
aoqi@0 269 #define PRINT_STAT_LINE(name, c, f) \
aoqi@0 270 tty->print_cr(" %4d (%4.1f%%) %s (%s)", (int)(c), ((c) * 100.0) / total, name, f);
aoqi@0 271 for (int index = 1 + (int)vmIntrinsics::_none; index < (int)vmIntrinsics::ID_LIMIT; index++) {
aoqi@0 272 vmIntrinsics::ID id = (vmIntrinsics::ID) index;
aoqi@0 273 int flags = _intrinsic_hist_flags[id];
aoqi@0 274 juint count = _intrinsic_hist_count[id];
aoqi@0 275 if ((flags | count) != 0) {
aoqi@0 276 PRINT_STAT_LINE(vmIntrinsics::name_at(id), count, format_flags(flags, flagsbuf));
aoqi@0 277 }
aoqi@0 278 }
aoqi@0 279 PRINT_STAT_LINE("total", total, format_flags(_intrinsic_hist_flags[vmIntrinsics::_none], flagsbuf));
aoqi@0 280 if (xtty != NULL) xtty->tail("statistics");
aoqi@0 281 }
aoqi@0 282
aoqi@0 283 void Compile::print_statistics() {
aoqi@0 284 { ttyLocker ttyl;
aoqi@0 285 if (xtty != NULL) xtty->head("statistics type='opto'");
aoqi@0 286 Parse::print_statistics();
aoqi@0 287 PhaseCCP::print_statistics();
aoqi@0 288 PhaseRegAlloc::print_statistics();
aoqi@0 289 Scheduling::print_statistics();
aoqi@0 290 PhasePeephole::print_statistics();
aoqi@0 291 PhaseIdealLoop::print_statistics();
aoqi@0 292 if (xtty != NULL) xtty->tail("statistics");
aoqi@0 293 }
aoqi@0 294 if (_intrinsic_hist_flags[vmIntrinsics::_none] != 0) {
aoqi@0 295 // put this under its own <statistics> element.
aoqi@0 296 print_intrinsic_statistics();
aoqi@0 297 }
aoqi@0 298 }
aoqi@0 299 #endif //PRODUCT
aoqi@0 300
aoqi@0 301 // Support for bundling info
aoqi@0 302 Bundle* Compile::node_bundling(const Node *n) {
aoqi@0 303 assert(valid_bundle_info(n), "oob");
aoqi@0 304 return &_node_bundling_base[n->_idx];
aoqi@0 305 }
aoqi@0 306
aoqi@0 307 bool Compile::valid_bundle_info(const Node *n) {
aoqi@0 308 return (_node_bundling_limit > n->_idx);
aoqi@0 309 }
aoqi@0 310
aoqi@0 311
aoqi@0 312 void Compile::gvn_replace_by(Node* n, Node* nn) {
aoqi@0 313 for (DUIterator_Last imin, i = n->last_outs(imin); i >= imin; ) {
aoqi@0 314 Node* use = n->last_out(i);
aoqi@0 315 bool is_in_table = initial_gvn()->hash_delete(use);
aoqi@0 316 uint uses_found = 0;
aoqi@0 317 for (uint j = 0; j < use->len(); j++) {
aoqi@0 318 if (use->in(j) == n) {
aoqi@0 319 if (j < use->req())
aoqi@0 320 use->set_req(j, nn);
aoqi@0 321 else
aoqi@0 322 use->set_prec(j, nn);
aoqi@0 323 uses_found++;
aoqi@0 324 }
aoqi@0 325 }
aoqi@0 326 if (is_in_table) {
aoqi@0 327 // reinsert into table
aoqi@0 328 initial_gvn()->hash_find_insert(use);
aoqi@0 329 }
aoqi@0 330 record_for_igvn(use);
aoqi@0 331 i -= uses_found; // we deleted 1 or more copies of this edge
aoqi@0 332 }
aoqi@0 333 }
aoqi@0 334
aoqi@0 335
aoqi@0 336 static inline bool not_a_node(const Node* n) {
aoqi@0 337 if (n == NULL) return true;
aoqi@0 338 if (((intptr_t)n & 1) != 0) return true; // uninitialized, etc.
aoqi@0 339 if (*(address*)n == badAddress) return true; // kill by Node::destruct
aoqi@0 340 return false;
aoqi@0 341 }
aoqi@0 342
aoqi@0 343 // Identify all nodes that are reachable from below, useful.
aoqi@0 344 // Use breadth-first pass that records state in a Unique_Node_List,
aoqi@0 345 // recursive traversal is slower.
aoqi@0 346 void Compile::identify_useful_nodes(Unique_Node_List &useful) {
aoqi@0 347 int estimated_worklist_size = unique();
aoqi@0 348 useful.map( estimated_worklist_size, NULL ); // preallocate space
aoqi@0 349
aoqi@0 350 // Initialize worklist
aoqi@0 351 if (root() != NULL) { useful.push(root()); }
aoqi@0 352 // If 'top' is cached, declare it useful to preserve cached node
aoqi@0 353 if( cached_top_node() ) { useful.push(cached_top_node()); }
aoqi@0 354
aoqi@0 355 // Push all useful nodes onto the list, breadthfirst
aoqi@0 356 for( uint next = 0; next < useful.size(); ++next ) {
aoqi@0 357 assert( next < unique(), "Unique useful nodes < total nodes");
aoqi@0 358 Node *n = useful.at(next);
aoqi@0 359 uint max = n->len();
aoqi@0 360 for( uint i = 0; i < max; ++i ) {
aoqi@0 361 Node *m = n->in(i);
aoqi@0 362 if (not_a_node(m)) continue;
aoqi@0 363 useful.push(m);
aoqi@0 364 }
aoqi@0 365 }
aoqi@0 366 }
aoqi@0 367
aoqi@0 368 // Update dead_node_list with any missing dead nodes using useful
aoqi@0 369 // list. Consider all non-useful nodes to be useless i.e., dead nodes.
aoqi@0 370 void Compile::update_dead_node_list(Unique_Node_List &useful) {
aoqi@0 371 uint max_idx = unique();
aoqi@0 372 VectorSet& useful_node_set = useful.member_set();
aoqi@0 373
aoqi@0 374 for (uint node_idx = 0; node_idx < max_idx; node_idx++) {
aoqi@0 375 // If node with index node_idx is not in useful set,
aoqi@0 376 // mark it as dead in dead node list.
aoqi@0 377 if (! useful_node_set.test(node_idx) ) {
aoqi@0 378 record_dead_node(node_idx);
aoqi@0 379 }
aoqi@0 380 }
aoqi@0 381 }
aoqi@0 382
aoqi@0 383 void Compile::remove_useless_late_inlines(GrowableArray<CallGenerator*>* inlines, Unique_Node_List &useful) {
aoqi@0 384 int shift = 0;
aoqi@0 385 for (int i = 0; i < inlines->length(); i++) {
aoqi@0 386 CallGenerator* cg = inlines->at(i);
aoqi@0 387 CallNode* call = cg->call_node();
aoqi@0 388 if (shift > 0) {
aoqi@0 389 inlines->at_put(i-shift, cg);
aoqi@0 390 }
aoqi@0 391 if (!useful.member(call)) {
aoqi@0 392 shift++;
aoqi@0 393 }
aoqi@0 394 }
aoqi@0 395 inlines->trunc_to(inlines->length()-shift);
aoqi@0 396 }
aoqi@0 397
aoqi@0 398 // Disconnect all useless nodes by disconnecting those at the boundary.
aoqi@0 399 void Compile::remove_useless_nodes(Unique_Node_List &useful) {
aoqi@0 400 uint next = 0;
aoqi@0 401 while (next < useful.size()) {
aoqi@0 402 Node *n = useful.at(next++);
roland@7041 403 if (n->is_SafePoint()) {
roland@7041 404 // We're done with a parsing phase. Replaced nodes are not valid
roland@7041 405 // beyond that point.
roland@7041 406 n->as_SafePoint()->delete_replaced_nodes();
roland@7041 407 }
aoqi@0 408 // Use raw traversal of out edges since this code removes out edges
aoqi@0 409 int max = n->outcnt();
aoqi@0 410 for (int j = 0; j < max; ++j) {
aoqi@0 411 Node* child = n->raw_out(j);
aoqi@0 412 if (! useful.member(child)) {
aoqi@0 413 assert(!child->is_top() || child != top(),
aoqi@0 414 "If top is cached in Compile object it is in useful list");
aoqi@0 415 // Only need to remove this out-edge to the useless node
aoqi@0 416 n->raw_del_out(j);
aoqi@0 417 --j;
aoqi@0 418 --max;
aoqi@0 419 }
aoqi@0 420 }
aoqi@0 421 if (n->outcnt() == 1 && n->has_special_unique_user()) {
aoqi@0 422 record_for_igvn(n->unique_out());
aoqi@0 423 }
aoqi@0 424 }
aoqi@0 425 // Remove useless macro and predicate opaq nodes
aoqi@0 426 for (int i = C->macro_count()-1; i >= 0; i--) {
aoqi@0 427 Node* n = C->macro_node(i);
aoqi@0 428 if (!useful.member(n)) {
aoqi@0 429 remove_macro_node(n);
aoqi@0 430 }
aoqi@0 431 }
aoqi@0 432 // Remove useless expensive node
aoqi@0 433 for (int i = C->expensive_count()-1; i >= 0; i--) {
aoqi@0 434 Node* n = C->expensive_node(i);
aoqi@0 435 if (!useful.member(n)) {
aoqi@0 436 remove_expensive_node(n);
aoqi@0 437 }
aoqi@0 438 }
aoqi@0 439 // clean up the late inline lists
aoqi@0 440 remove_useless_late_inlines(&_string_late_inlines, useful);
aoqi@0 441 remove_useless_late_inlines(&_boxing_late_inlines, useful);
aoqi@0 442 remove_useless_late_inlines(&_late_inlines, useful);
aoqi@0 443 debug_only(verify_graph_edges(true/*check for no_dead_code*/);)
aoqi@0 444 }
aoqi@0 445
aoqi@0 446 //------------------------------frame_size_in_words-----------------------------
aoqi@0 447 // frame_slots in units of words
aoqi@0 448 int Compile::frame_size_in_words() const {
aoqi@0 449 // shift is 0 in LP32 and 1 in LP64
aoqi@0 450 const int shift = (LogBytesPerWord - LogBytesPerInt);
aoqi@0 451 int words = _frame_slots >> shift;
aoqi@0 452 assert( words << shift == _frame_slots, "frame size must be properly aligned in LP64" );
aoqi@0 453 return words;
aoqi@0 454 }
aoqi@0 455
aoqi@0 456 // To bang the stack of this compiled method we use the stack size
aoqi@0 457 // that the interpreter would need in case of a deoptimization. This
aoqi@0 458 // removes the need to bang the stack in the deoptimization blob which
aoqi@0 459 // in turn simplifies stack overflow handling.
aoqi@0 460 int Compile::bang_size_in_bytes() const {
aoqi@0 461 return MAX2(_interpreter_frame_size, frame_size_in_bytes());
aoqi@0 462 }
aoqi@0 463
aoqi@0 464 // ============================================================================
aoqi@0 465 //------------------------------CompileWrapper---------------------------------
aoqi@0 466 class CompileWrapper : public StackObj {
aoqi@0 467 Compile *const _compile;
aoqi@0 468 public:
aoqi@0 469 CompileWrapper(Compile* compile);
aoqi@0 470
aoqi@0 471 ~CompileWrapper();
aoqi@0 472 };
aoqi@0 473
aoqi@0 474 CompileWrapper::CompileWrapper(Compile* compile) : _compile(compile) {
aoqi@0 475 // the Compile* pointer is stored in the current ciEnv:
aoqi@0 476 ciEnv* env = compile->env();
aoqi@0 477 assert(env == ciEnv::current(), "must already be a ciEnv active");
aoqi@0 478 assert(env->compiler_data() == NULL, "compile already active?");
aoqi@0 479 env->set_compiler_data(compile);
aoqi@0 480 assert(compile == Compile::current(), "sanity");
aoqi@0 481
aoqi@0 482 compile->set_type_dict(NULL);
aoqi@0 483 compile->set_type_hwm(NULL);
aoqi@0 484 compile->set_type_last_size(0);
aoqi@0 485 compile->set_last_tf(NULL, NULL);
aoqi@0 486 compile->set_indexSet_arena(NULL);
aoqi@0 487 compile->set_indexSet_free_block_list(NULL);
aoqi@0 488 compile->init_type_arena();
aoqi@0 489 Type::Initialize(compile);
aoqi@0 490 _compile->set_scratch_buffer_blob(NULL);
aoqi@0 491 _compile->begin_method();
aoqi@0 492 }
aoqi@0 493 CompileWrapper::~CompileWrapper() {
aoqi@0 494 _compile->end_method();
aoqi@0 495 if (_compile->scratch_buffer_blob() != NULL)
aoqi@0 496 BufferBlob::free(_compile->scratch_buffer_blob());
aoqi@0 497 _compile->env()->set_compiler_data(NULL);
aoqi@0 498 }
aoqi@0 499
aoqi@0 500
aoqi@0 501 //----------------------------print_compile_messages---------------------------
aoqi@0 502 void Compile::print_compile_messages() {
aoqi@0 503 #ifndef PRODUCT
aoqi@0 504 // Check if recompiling
aoqi@0 505 if (_subsume_loads == false && PrintOpto) {
aoqi@0 506 // Recompiling without allowing machine instructions to subsume loads
aoqi@0 507 tty->print_cr("*********************************************************");
aoqi@0 508 tty->print_cr("** Bailout: Recompile without subsuming loads **");
aoqi@0 509 tty->print_cr("*********************************************************");
aoqi@0 510 }
aoqi@0 511 if (_do_escape_analysis != DoEscapeAnalysis && PrintOpto) {
aoqi@0 512 // Recompiling without escape analysis
aoqi@0 513 tty->print_cr("*********************************************************");
aoqi@0 514 tty->print_cr("** Bailout: Recompile without escape analysis **");
aoqi@0 515 tty->print_cr("*********************************************************");
aoqi@0 516 }
aoqi@0 517 if (_eliminate_boxing != EliminateAutoBox && PrintOpto) {
aoqi@0 518 // Recompiling without boxing elimination
aoqi@0 519 tty->print_cr("*********************************************************");
aoqi@0 520 tty->print_cr("** Bailout: Recompile without boxing elimination **");
aoqi@0 521 tty->print_cr("*********************************************************");
aoqi@0 522 }
aoqi@0 523 if (env()->break_at_compile()) {
aoqi@0 524 // Open the debugger when compiling this method.
aoqi@0 525 tty->print("### Breaking when compiling: ");
aoqi@0 526 method()->print_short_name();
aoqi@0 527 tty->cr();
aoqi@0 528 BREAKPOINT;
aoqi@0 529 }
aoqi@0 530
aoqi@0 531 if( PrintOpto ) {
aoqi@0 532 if (is_osr_compilation()) {
aoqi@0 533 tty->print("[OSR]%3d", _compile_id);
aoqi@0 534 } else {
aoqi@0 535 tty->print("%3d", _compile_id);
aoqi@0 536 }
aoqi@0 537 }
aoqi@0 538 #endif
aoqi@0 539 }
aoqi@0 540
aoqi@0 541
aoqi@0 542 //-----------------------init_scratch_buffer_blob------------------------------
aoqi@0 543 // Construct a temporary BufferBlob and cache it for this compile.
aoqi@0 544 void Compile::init_scratch_buffer_blob(int const_size) {
aoqi@0 545 // If there is already a scratch buffer blob allocated and the
aoqi@0 546 // constant section is big enough, use it. Otherwise free the
aoqi@0 547 // current and allocate a new one.
aoqi@0 548 BufferBlob* blob = scratch_buffer_blob();
aoqi@0 549 if ((blob != NULL) && (const_size <= _scratch_const_size)) {
aoqi@0 550 // Use the current blob.
aoqi@0 551 } else {
aoqi@0 552 if (blob != NULL) {
aoqi@0 553 BufferBlob::free(blob);
aoqi@0 554 }
aoqi@0 555
aoqi@0 556 ResourceMark rm;
aoqi@0 557 _scratch_const_size = const_size;
aoqi@0 558 int size = (MAX_inst_size + MAX_stubs_size + _scratch_const_size);
aoqi@0 559 blob = BufferBlob::create("Compile::scratch_buffer", size);
aoqi@0 560 // Record the buffer blob for next time.
aoqi@0 561 set_scratch_buffer_blob(blob);
aoqi@0 562 // Have we run out of code space?
aoqi@0 563 if (scratch_buffer_blob() == NULL) {
aoqi@0 564 // Let CompilerBroker disable further compilations.
aoqi@0 565 record_failure("Not enough space for scratch buffer in CodeCache");
aoqi@0 566 return;
aoqi@0 567 }
aoqi@0 568 }
aoqi@0 569
aoqi@0 570 // Initialize the relocation buffers
aoqi@0 571 relocInfo* locs_buf = (relocInfo*) blob->content_end() - MAX_locs_size;
aoqi@0 572 set_scratch_locs_memory(locs_buf);
aoqi@0 573 }
aoqi@0 574
aoqi@0 575
aoqi@0 576 //-----------------------scratch_emit_size-------------------------------------
aoqi@0 577 // Helper function that computes size by emitting code
aoqi@0 578 uint Compile::scratch_emit_size(const Node* n) {
aoqi@0 579 // Start scratch_emit_size section.
aoqi@0 580 set_in_scratch_emit_size(true);
aoqi@0 581
aoqi@0 582 // Emit into a trash buffer and count bytes emitted.
aoqi@0 583 // This is a pretty expensive way to compute a size,
aoqi@0 584 // but it works well enough if seldom used.
aoqi@0 585 // All common fixed-size instructions are given a size
aoqi@0 586 // method by the AD file.
aoqi@0 587 // Note that the scratch buffer blob and locs memory are
aoqi@0 588 // allocated at the beginning of the compile task, and
aoqi@0 589 // may be shared by several calls to scratch_emit_size.
aoqi@0 590 // The allocation of the scratch buffer blob is particularly
aoqi@0 591 // expensive, since it has to grab the code cache lock.
aoqi@0 592 BufferBlob* blob = this->scratch_buffer_blob();
aoqi@0 593 assert(blob != NULL, "Initialize BufferBlob at start");
aoqi@0 594 assert(blob->size() > MAX_inst_size, "sanity");
aoqi@0 595 relocInfo* locs_buf = scratch_locs_memory();
aoqi@0 596 address blob_begin = blob->content_begin();
aoqi@0 597 address blob_end = (address)locs_buf;
aoqi@0 598 assert(blob->content_contains(blob_end), "sanity");
aoqi@0 599 CodeBuffer buf(blob_begin, blob_end - blob_begin);
aoqi@0 600 buf.initialize_consts_size(_scratch_const_size);
aoqi@0 601 buf.initialize_stubs_size(MAX_stubs_size);
aoqi@0 602 assert(locs_buf != NULL, "sanity");
aoqi@0 603 int lsize = MAX_locs_size / 3;
aoqi@0 604 buf.consts()->initialize_shared_locs(&locs_buf[lsize * 0], lsize);
aoqi@0 605 buf.insts()->initialize_shared_locs( &locs_buf[lsize * 1], lsize);
aoqi@0 606 buf.stubs()->initialize_shared_locs( &locs_buf[lsize * 2], lsize);
aoqi@0 607
aoqi@0 608 // Do the emission.
aoqi@0 609
aoqi@0 610 Label fakeL; // Fake label for branch instructions.
aoqi@0 611 Label* saveL = NULL;
aoqi@0 612 uint save_bnum = 0;
aoqi@0 613 bool is_branch = n->is_MachBranch();
aoqi@0 614 if (is_branch) {
aoqi@0 615 MacroAssembler masm(&buf);
aoqi@0 616 masm.bind(fakeL);
aoqi@0 617 n->as_MachBranch()->save_label(&saveL, &save_bnum);
aoqi@0 618 n->as_MachBranch()->label_set(&fakeL, 0);
aoqi@0 619 }
aoqi@0 620 n->emit(buf, this->regalloc());
aoqi@0 621 if (is_branch) // Restore label.
aoqi@0 622 n->as_MachBranch()->label_set(saveL, save_bnum);
aoqi@0 623
aoqi@0 624 // End scratch_emit_size section.
aoqi@0 625 set_in_scratch_emit_size(false);
aoqi@0 626
aoqi@0 627 return buf.insts_size();
aoqi@0 628 }
aoqi@0 629
aoqi@0 630
aoqi@0 631 // ============================================================================
aoqi@0 632 //------------------------------Compile standard-------------------------------
aoqi@0 633 debug_only( int Compile::_debug_idx = 100000; )
aoqi@0 634
aoqi@0 635 // Compile a method. entry_bci is -1 for normal compilations and indicates
aoqi@0 636 // the continuation bci for on stack replacement.
aoqi@0 637
aoqi@0 638
aoqi@0 639 Compile::Compile( ciEnv* ci_env, C2Compiler* compiler, ciMethod* target, int osr_bci,
aoqi@0 640 bool subsume_loads, bool do_escape_analysis, bool eliminate_boxing )
aoqi@0 641 : Phase(Compiler),
aoqi@0 642 _env(ci_env),
aoqi@0 643 _log(ci_env->log()),
aoqi@0 644 _compile_id(ci_env->compile_id()),
aoqi@0 645 _save_argument_registers(false),
aoqi@0 646 _stub_name(NULL),
aoqi@0 647 _stub_function(NULL),
aoqi@0 648 _stub_entry_point(NULL),
aoqi@0 649 _method(target),
aoqi@0 650 _entry_bci(osr_bci),
aoqi@0 651 _initial_gvn(NULL),
aoqi@0 652 _for_igvn(NULL),
aoqi@0 653 _warm_calls(NULL),
aoqi@0 654 _subsume_loads(subsume_loads),
aoqi@0 655 _do_escape_analysis(do_escape_analysis),
aoqi@0 656 _eliminate_boxing(eliminate_boxing),
aoqi@0 657 _failure_reason(NULL),
aoqi@0 658 _code_buffer("Compile::Fill_buffer"),
aoqi@0 659 _orig_pc_slot(0),
aoqi@0 660 _orig_pc_slot_offset_in_bytes(0),
aoqi@0 661 _has_method_handle_invokes(false),
aoqi@0 662 _mach_constant_base_node(NULL),
aoqi@0 663 _node_bundling_limit(0),
aoqi@0 664 _node_bundling_base(NULL),
aoqi@0 665 _java_calls(0),
aoqi@0 666 _inner_loops(0),
aoqi@0 667 _scratch_const_size(-1),
aoqi@0 668 _in_scratch_emit_size(false),
aoqi@0 669 _dead_node_list(comp_arena()),
aoqi@0 670 _dead_node_count(0),
aoqi@0 671 #ifndef PRODUCT
aoqi@0 672 _trace_opto_output(TraceOptoOutput || method()->has_option("TraceOptoOutput")),
aoqi@0 673 _in_dump_cnt(0),
aoqi@0 674 _printer(IdealGraphPrinter::printer()),
aoqi@0 675 #endif
aoqi@0 676 _congraph(NULL),
zgu@7074 677 _comp_arena(mtCompiler),
zgu@7074 678 _node_arena(mtCompiler),
zgu@7074 679 _old_arena(mtCompiler),
zgu@7074 680 _Compile_types(mtCompiler),
aoqi@0 681 _replay_inline_data(NULL),
aoqi@0 682 _late_inlines(comp_arena(), 2, 0, NULL),
aoqi@0 683 _string_late_inlines(comp_arena(), 2, 0, NULL),
aoqi@0 684 _boxing_late_inlines(comp_arena(), 2, 0, NULL),
aoqi@0 685 _late_inlines_pos(0),
aoqi@0 686 _number_of_mh_late_inlines(0),
aoqi@0 687 _inlining_progress(false),
aoqi@0 688 _inlining_incrementally(false),
aoqi@0 689 _print_inlining_list(NULL),
aoqi@0 690 _print_inlining_idx(0),
vlivanov@7385 691 _interpreter_frame_size(0),
vlivanov@7385 692 _max_node_limit(MaxNodeLimit) {
aoqi@0 693 C = this;
aoqi@0 694
aoqi@0 695 CompileWrapper cw(this);
aoqi@0 696 #ifndef PRODUCT
aoqi@0 697 if (TimeCompiler2) {
aoqi@0 698 tty->print(" ");
aoqi@0 699 target->holder()->name()->print();
aoqi@0 700 tty->print(".");
aoqi@0 701 target->print_short_name();
aoqi@0 702 tty->print(" ");
aoqi@0 703 }
aoqi@0 704 TraceTime t1("Total compilation time", &_t_totalCompilation, TimeCompiler, TimeCompiler2);
aoqi@0 705 TraceTime t2(NULL, &_t_methodCompilation, TimeCompiler, false);
aoqi@0 706 bool print_opto_assembly = PrintOptoAssembly || _method->has_option("PrintOptoAssembly");
aoqi@0 707 if (!print_opto_assembly) {
aoqi@0 708 bool print_assembly = (PrintAssembly || _method->should_print_assembly());
aoqi@0 709 if (print_assembly && !Disassembler::can_decode()) {
aoqi@0 710 tty->print_cr("PrintAssembly request changed to PrintOptoAssembly");
aoqi@0 711 print_opto_assembly = true;
aoqi@0 712 }
aoqi@0 713 }
aoqi@0 714 set_print_assembly(print_opto_assembly);
aoqi@0 715 set_parsed_irreducible_loop(false);
aoqi@0 716
aoqi@0 717 if (method()->has_option("ReplayInline")) {
aoqi@0 718 _replay_inline_data = ciReplay::load_inline_data(method(), entry_bci(), ci_env->comp_level());
aoqi@0 719 }
aoqi@0 720 #endif
aoqi@0 721 set_print_inlining(PrintInlining || method()->has_option("PrintInlining") NOT_PRODUCT( || PrintOptoInlining));
aoqi@0 722 set_print_intrinsics(PrintIntrinsics || method()->has_option("PrintIntrinsics"));
aoqi@0 723 set_has_irreducible_loop(true); // conservative until build_loop_tree() reset it
aoqi@0 724
aoqi@0 725 if (ProfileTraps RTM_OPT_ONLY( || UseRTMLocking )) {
aoqi@0 726 // Make sure the method being compiled gets its own MDO,
aoqi@0 727 // so we can at least track the decompile_count().
aoqi@0 728 // Need MDO to record RTM code generation state.
aoqi@0 729 method()->ensure_method_data();
aoqi@0 730 }
aoqi@0 731
aoqi@0 732 Init(::AliasLevel);
aoqi@0 733
aoqi@0 734
aoqi@0 735 print_compile_messages();
aoqi@0 736
aoqi@0 737 _ilt = InlineTree::build_inline_tree_root();
aoqi@0 738
aoqi@0 739 // Even if NO memory addresses are used, MergeMem nodes must have at least 1 slice
aoqi@0 740 assert(num_alias_types() >= AliasIdxRaw, "");
aoqi@0 741
aoqi@0 742 #define MINIMUM_NODE_HASH 1023
aoqi@0 743 // Node list that Iterative GVN will start with
aoqi@0 744 Unique_Node_List for_igvn(comp_arena());
aoqi@0 745 set_for_igvn(&for_igvn);
aoqi@0 746
aoqi@0 747 // GVN that will be run immediately on new nodes
aoqi@0 748 uint estimated_size = method()->code_size()*4+64;
aoqi@0 749 estimated_size = (estimated_size < MINIMUM_NODE_HASH ? MINIMUM_NODE_HASH : estimated_size);
aoqi@0 750 PhaseGVN gvn(node_arena(), estimated_size);
aoqi@0 751 set_initial_gvn(&gvn);
aoqi@0 752
aoqi@0 753 if (print_inlining() || print_intrinsics()) {
aoqi@0 754 _print_inlining_list = new (comp_arena())GrowableArray<PrintInliningBuffer>(comp_arena(), 1, 1, PrintInliningBuffer());
aoqi@0 755 }
aoqi@0 756 { // Scope for timing the parser
aoqi@0 757 TracePhase t3("parse", &_t_parser, true);
aoqi@0 758
aoqi@0 759 // Put top into the hash table ASAP.
aoqi@0 760 initial_gvn()->transform_no_reclaim(top());
aoqi@0 761
aoqi@0 762 // Set up tf(), start(), and find a CallGenerator.
aoqi@0 763 CallGenerator* cg = NULL;
aoqi@0 764 if (is_osr_compilation()) {
aoqi@0 765 const TypeTuple *domain = StartOSRNode::osr_domain();
aoqi@0 766 const TypeTuple *range = TypeTuple::make_range(method()->signature());
aoqi@0 767 init_tf(TypeFunc::make(domain, range));
aoqi@0 768 StartNode* s = new (this) StartOSRNode(root(), domain);
aoqi@0 769 initial_gvn()->set_type_bottom(s);
aoqi@0 770 init_start(s);
aoqi@0 771 cg = CallGenerator::for_osr(method(), entry_bci());
aoqi@0 772 } else {
aoqi@0 773 // Normal case.
aoqi@0 774 init_tf(TypeFunc::make(method()));
aoqi@0 775 StartNode* s = new (this) StartNode(root(), tf()->domain());
aoqi@0 776 initial_gvn()->set_type_bottom(s);
aoqi@0 777 init_start(s);
aoqi@0 778 if (method()->intrinsic_id() == vmIntrinsics::_Reference_get && UseG1GC) {
aoqi@0 779 // With java.lang.ref.reference.get() we must go through the
aoqi@0 780 // intrinsic when G1 is enabled - even when get() is the root
aoqi@0 781 // method of the compile - so that, if necessary, the value in
aoqi@0 782 // the referent field of the reference object gets recorded by
aoqi@0 783 // the pre-barrier code.
aoqi@0 784 // Specifically, if G1 is enabled, the value in the referent
aoqi@0 785 // field is recorded by the G1 SATB pre barrier. This will
aoqi@0 786 // result in the referent being marked live and the reference
aoqi@0 787 // object removed from the list of discovered references during
aoqi@0 788 // reference processing.
aoqi@0 789 cg = find_intrinsic(method(), false);
aoqi@0 790 }
aoqi@0 791 if (cg == NULL) {
aoqi@0 792 float past_uses = method()->interpreter_invocation_count();
aoqi@0 793 float expected_uses = past_uses;
aoqi@0 794 cg = CallGenerator::for_inline(method(), expected_uses);
aoqi@0 795 }
aoqi@0 796 }
aoqi@0 797 if (failing()) return;
aoqi@0 798 if (cg == NULL) {
aoqi@0 799 record_method_not_compilable_all_tiers("cannot parse method");
aoqi@0 800 return;
aoqi@0 801 }
aoqi@0 802 JVMState* jvms = build_start_state(start(), tf());
roland@7041 803 if ((jvms = cg->generate(jvms)) == NULL) {
aoqi@0 804 record_method_not_compilable("method parse failed");
aoqi@0 805 return;
aoqi@0 806 }
aoqi@0 807 GraphKit kit(jvms);
aoqi@0 808
aoqi@0 809 if (!kit.stopped()) {
aoqi@0 810 // Accept return values, and transfer control we know not where.
aoqi@0 811 // This is done by a special, unique ReturnNode bound to root.
aoqi@0 812 return_values(kit.jvms());
aoqi@0 813 }
aoqi@0 814
aoqi@0 815 if (kit.has_exceptions()) {
aoqi@0 816 // Any exceptions that escape from this call must be rethrown
aoqi@0 817 // to whatever caller is dynamically above us on the stack.
aoqi@0 818 // This is done by a special, unique RethrowNode bound to root.
aoqi@0 819 rethrow_exceptions(kit.transfer_exceptions_into_jvms());
aoqi@0 820 }
aoqi@0 821
aoqi@0 822 assert(IncrementalInline || (_late_inlines.length() == 0 && !has_mh_late_inlines()), "incremental inlining is off");
aoqi@0 823
aoqi@0 824 if (_late_inlines.length() == 0 && !has_mh_late_inlines() && !failing() && has_stringbuilder()) {
aoqi@0 825 inline_string_calls(true);
aoqi@0 826 }
aoqi@0 827
aoqi@0 828 if (failing()) return;
aoqi@0 829
aoqi@0 830 print_method(PHASE_BEFORE_REMOVEUSELESS, 3);
aoqi@0 831
aoqi@0 832 // Remove clutter produced by parsing.
aoqi@0 833 if (!failing()) {
aoqi@0 834 ResourceMark rm;
aoqi@0 835 PhaseRemoveUseless pru(initial_gvn(), &for_igvn);
aoqi@0 836 }
aoqi@0 837 }
aoqi@0 838
aoqi@0 839 // Note: Large methods are capped off in do_one_bytecode().
aoqi@0 840 if (failing()) return;
aoqi@0 841
aoqi@0 842 // After parsing, node notes are no longer automagic.
aoqi@0 843 // They must be propagated by register_new_node_with_optimizer(),
aoqi@0 844 // clone(), or the like.
aoqi@0 845 set_default_node_notes(NULL);
aoqi@0 846
aoqi@0 847 for (;;) {
aoqi@0 848 int successes = Inline_Warm();
aoqi@0 849 if (failing()) return;
aoqi@0 850 if (successes == 0) break;
aoqi@0 851 }
aoqi@0 852
aoqi@0 853 // Drain the list.
aoqi@0 854 Finish_Warm();
aoqi@0 855 #ifndef PRODUCT
aoqi@0 856 if (_printer) {
aoqi@0 857 _printer->print_inlining(this);
aoqi@0 858 }
aoqi@0 859 #endif
aoqi@0 860
aoqi@0 861 if (failing()) return;
aoqi@0 862 NOT_PRODUCT( verify_graph_edges(); )
aoqi@0 863
aoqi@0 864 // Now optimize
aoqi@0 865 Optimize();
aoqi@0 866 if (failing()) return;
aoqi@0 867 NOT_PRODUCT( verify_graph_edges(); )
aoqi@0 868
aoqi@0 869 #ifndef PRODUCT
aoqi@0 870 if (PrintIdeal) {
aoqi@0 871 ttyLocker ttyl; // keep the following output all in one block
aoqi@0 872 // This output goes directly to the tty, not the compiler log.
aoqi@0 873 // To enable tools to match it up with the compilation activity,
aoqi@0 874 // be sure to tag this tty output with the compile ID.
aoqi@0 875 if (xtty != NULL) {
aoqi@0 876 xtty->head("ideal compile_id='%d'%s", compile_id(),
aoqi@0 877 is_osr_compilation() ? " compile_kind='osr'" :
aoqi@0 878 "");
aoqi@0 879 }
aoqi@0 880 root()->dump(9999);
aoqi@0 881 if (xtty != NULL) {
aoqi@0 882 xtty->tail("ideal");
aoqi@0 883 }
aoqi@0 884 }
aoqi@0 885 #endif
aoqi@0 886
aoqi@0 887 NOT_PRODUCT( verify_barriers(); )
aoqi@0 888
aoqi@0 889 // Dump compilation data to replay it.
aoqi@0 890 if (method()->has_option("DumpReplay")) {
aoqi@0 891 env()->dump_replay_data(_compile_id);
aoqi@0 892 }
aoqi@0 893 if (method()->has_option("DumpInline") && (ilt() != NULL)) {
aoqi@0 894 env()->dump_inline_data(_compile_id);
aoqi@0 895 }
aoqi@0 896
aoqi@0 897 // Now that we know the size of all the monitors we can add a fixed slot
aoqi@0 898 // for the original deopt pc.
aoqi@0 899
aoqi@0 900 _orig_pc_slot = fixed_slots();
aoqi@0 901 int next_slot = _orig_pc_slot + (sizeof(address) / VMRegImpl::stack_slot_size);
aoqi@0 902 set_fixed_slots(next_slot);
aoqi@0 903
aoqi@0 904 // Compute when to use implicit null checks. Used by matching trap based
aoqi@0 905 // nodes and NullCheck optimization.
aoqi@0 906 set_allowed_deopt_reasons();
aoqi@0 907
aoqi@0 908 // Now generate code
aoqi@0 909 Code_Gen();
aoqi@0 910 if (failing()) return;
aoqi@0 911
aoqi@0 912 // Check if we want to skip execution of all compiled code.
aoqi@0 913 {
aoqi@0 914 #ifndef PRODUCT
aoqi@0 915 if (OptoNoExecute) {
aoqi@0 916 record_method_not_compilable("+OptoNoExecute"); // Flag as failed
aoqi@0 917 return;
aoqi@0 918 }
aoqi@0 919 TracePhase t2("install_code", &_t_registerMethod, TimeCompiler);
aoqi@0 920 #endif
aoqi@0 921
aoqi@0 922 if (is_osr_compilation()) {
aoqi@0 923 _code_offsets.set_value(CodeOffsets::Verified_Entry, 0);
aoqi@0 924 _code_offsets.set_value(CodeOffsets::OSR_Entry, _first_block_size);
aoqi@0 925 } else {
aoqi@0 926 _code_offsets.set_value(CodeOffsets::Verified_Entry, _first_block_size);
aoqi@0 927 _code_offsets.set_value(CodeOffsets::OSR_Entry, 0);
aoqi@0 928 }
aoqi@0 929
aoqi@0 930 env()->register_method(_method, _entry_bci,
aoqi@0 931 &_code_offsets,
aoqi@0 932 _orig_pc_slot_offset_in_bytes,
aoqi@0 933 code_buffer(),
aoqi@0 934 frame_size_in_words(), _oop_map_set,
aoqi@0 935 &_handler_table, &_inc_table,
aoqi@0 936 compiler,
aoqi@0 937 env()->comp_level(),
aoqi@0 938 has_unsafe_access(),
aoqi@0 939 SharedRuntime::is_wide_vector(max_vector_size()),
aoqi@0 940 rtm_state()
aoqi@0 941 );
aoqi@0 942
aoqi@0 943 if (log() != NULL) // Print code cache state into compiler log
aoqi@0 944 log()->code_cache_state();
aoqi@0 945 }
aoqi@0 946 }
aoqi@0 947
aoqi@0 948 //------------------------------Compile----------------------------------------
aoqi@0 949 // Compile a runtime stub
aoqi@0 950 Compile::Compile( ciEnv* ci_env,
aoqi@0 951 TypeFunc_generator generator,
aoqi@0 952 address stub_function,
aoqi@0 953 const char *stub_name,
aoqi@0 954 int is_fancy_jump,
aoqi@0 955 bool pass_tls,
aoqi@0 956 bool save_arg_registers,
aoqi@0 957 bool return_pc )
aoqi@0 958 : Phase(Compiler),
aoqi@0 959 _env(ci_env),
aoqi@0 960 _log(ci_env->log()),
aoqi@0 961 _compile_id(0),
aoqi@0 962 _save_argument_registers(save_arg_registers),
aoqi@0 963 _method(NULL),
aoqi@0 964 _stub_name(stub_name),
aoqi@0 965 _stub_function(stub_function),
aoqi@0 966 _stub_entry_point(NULL),
aoqi@0 967 _entry_bci(InvocationEntryBci),
aoqi@0 968 _initial_gvn(NULL),
aoqi@0 969 _for_igvn(NULL),
aoqi@0 970 _warm_calls(NULL),
aoqi@0 971 _orig_pc_slot(0),
aoqi@0 972 _orig_pc_slot_offset_in_bytes(0),
aoqi@0 973 _subsume_loads(true),
aoqi@0 974 _do_escape_analysis(false),
aoqi@0 975 _eliminate_boxing(false),
aoqi@0 976 _failure_reason(NULL),
aoqi@0 977 _code_buffer("Compile::Fill_buffer"),
aoqi@0 978 _has_method_handle_invokes(false),
aoqi@0 979 _mach_constant_base_node(NULL),
aoqi@0 980 _node_bundling_limit(0),
aoqi@0 981 _node_bundling_base(NULL),
aoqi@0 982 _java_calls(0),
aoqi@0 983 _inner_loops(0),
aoqi@0 984 #ifndef PRODUCT
aoqi@0 985 _trace_opto_output(TraceOptoOutput),
aoqi@0 986 _in_dump_cnt(0),
aoqi@0 987 _printer(NULL),
aoqi@0 988 #endif
zgu@7074 989 _comp_arena(mtCompiler),
zgu@7074 990 _node_arena(mtCompiler),
zgu@7074 991 _old_arena(mtCompiler),
zgu@7074 992 _Compile_types(mtCompiler),
aoqi@0 993 _dead_node_list(comp_arena()),
aoqi@0 994 _dead_node_count(0),
aoqi@0 995 _congraph(NULL),
aoqi@0 996 _replay_inline_data(NULL),
aoqi@0 997 _number_of_mh_late_inlines(0),
aoqi@0 998 _inlining_progress(false),
aoqi@0 999 _inlining_incrementally(false),
aoqi@0 1000 _print_inlining_list(NULL),
aoqi@0 1001 _print_inlining_idx(0),
aoqi@0 1002 _allowed_reasons(0),
vlivanov@7385 1003 _interpreter_frame_size(0),
vlivanov@7385 1004 _max_node_limit(MaxNodeLimit) {
aoqi@0 1005 C = this;
aoqi@0 1006
aoqi@0 1007 #ifndef PRODUCT
aoqi@0 1008 TraceTime t1(NULL, &_t_totalCompilation, TimeCompiler, false);
aoqi@0 1009 TraceTime t2(NULL, &_t_stubCompilation, TimeCompiler, false);
aoqi@0 1010 set_print_assembly(PrintFrameConverterAssembly);
aoqi@0 1011 set_parsed_irreducible_loop(false);
aoqi@0 1012 #endif
aoqi@0 1013 set_has_irreducible_loop(false); // no loops
aoqi@0 1014
aoqi@0 1015 CompileWrapper cw(this);
aoqi@0 1016 Init(/*AliasLevel=*/ 0);
aoqi@0 1017 init_tf((*generator)());
aoqi@0 1018
aoqi@0 1019 {
aoqi@0 1020 // The following is a dummy for the sake of GraphKit::gen_stub
aoqi@0 1021 Unique_Node_List for_igvn(comp_arena());
aoqi@0 1022 set_for_igvn(&for_igvn); // not used, but some GraphKit guys push on this
aoqi@0 1023 PhaseGVN gvn(Thread::current()->resource_area(),255);
aoqi@0 1024 set_initial_gvn(&gvn); // not significant, but GraphKit guys use it pervasively
aoqi@0 1025 gvn.transform_no_reclaim(top());
aoqi@0 1026
aoqi@0 1027 GraphKit kit;
aoqi@0 1028 kit.gen_stub(stub_function, stub_name, is_fancy_jump, pass_tls, return_pc);
aoqi@0 1029 }
aoqi@0 1030
aoqi@0 1031 NOT_PRODUCT( verify_graph_edges(); )
aoqi@0 1032 Code_Gen();
aoqi@0 1033 if (failing()) return;
aoqi@0 1034
aoqi@0 1035
aoqi@0 1036 // Entry point will be accessed using compile->stub_entry_point();
aoqi@0 1037 if (code_buffer() == NULL) {
aoqi@0 1038 Matcher::soft_match_failure();
aoqi@0 1039 } else {
aoqi@0 1040 if (PrintAssembly && (WizardMode || Verbose))
aoqi@0 1041 tty->print_cr("### Stub::%s", stub_name);
aoqi@0 1042
aoqi@0 1043 if (!failing()) {
aoqi@0 1044 assert(_fixed_slots == 0, "no fixed slots used for runtime stubs");
aoqi@0 1045
aoqi@0 1046 // Make the NMethod
aoqi@0 1047 // For now we mark the frame as never safe for profile stackwalking
aoqi@0 1048 RuntimeStub *rs = RuntimeStub::new_runtime_stub(stub_name,
aoqi@0 1049 code_buffer(),
aoqi@0 1050 CodeOffsets::frame_never_safe,
aoqi@0 1051 // _code_offsets.value(CodeOffsets::Frame_Complete),
aoqi@0 1052 frame_size_in_words(),
aoqi@0 1053 _oop_map_set,
aoqi@0 1054 save_arg_registers);
aoqi@0 1055 assert(rs != NULL && rs->is_runtime_stub(), "sanity check");
aoqi@0 1056
aoqi@0 1057 _stub_entry_point = rs->entry_point();
aoqi@0 1058 }
aoqi@0 1059 }
aoqi@0 1060 }
aoqi@0 1061
aoqi@0 1062 //------------------------------Init-------------------------------------------
aoqi@0 1063 // Prepare for a single compilation
aoqi@0 1064 void Compile::Init(int aliaslevel) {
aoqi@0 1065 _unique = 0;
aoqi@0 1066 _regalloc = NULL;
aoqi@0 1067
aoqi@0 1068 _tf = NULL; // filled in later
aoqi@0 1069 _top = NULL; // cached later
aoqi@0 1070 _matcher = NULL; // filled in later
aoqi@0 1071 _cfg = NULL; // filled in later
aoqi@0 1072
aoqi@0 1073 set_24_bit_selection_and_mode(Use24BitFP, false);
aoqi@0 1074
aoqi@0 1075 _node_note_array = NULL;
aoqi@0 1076 _default_node_notes = NULL;
aoqi@0 1077
aoqi@0 1078 _immutable_memory = NULL; // filled in at first inquiry
aoqi@0 1079
aoqi@0 1080 // Globally visible Nodes
aoqi@0 1081 // First set TOP to NULL to give safe behavior during creation of RootNode
aoqi@0 1082 set_cached_top_node(NULL);
aoqi@0 1083 set_root(new (this) RootNode());
aoqi@0 1084 // Now that you have a Root to point to, create the real TOP
aoqi@0 1085 set_cached_top_node( new (this) ConNode(Type::TOP) );
aoqi@0 1086 set_recent_alloc(NULL, NULL);
aoqi@0 1087
aoqi@0 1088 // Create Debug Information Recorder to record scopes, oopmaps, etc.
aoqi@0 1089 env()->set_oop_recorder(new OopRecorder(env()->arena()));
aoqi@0 1090 env()->set_debug_info(new DebugInformationRecorder(env()->oop_recorder()));
aoqi@0 1091 env()->set_dependencies(new Dependencies(env()));
aoqi@0 1092
aoqi@0 1093 _fixed_slots = 0;
aoqi@0 1094 set_has_split_ifs(false);
aoqi@0 1095 set_has_loops(has_method() && method()->has_loops()); // first approximation
aoqi@0 1096 set_has_stringbuilder(false);
aoqi@0 1097 set_has_boxed_value(false);
aoqi@0 1098 _trap_can_recompile = false; // no traps emitted yet
aoqi@0 1099 _major_progress = true; // start out assuming good things will happen
aoqi@0 1100 set_has_unsafe_access(false);
aoqi@0 1101 set_max_vector_size(0);
aoqi@0 1102 Copy::zero_to_bytes(_trap_hist, sizeof(_trap_hist));
aoqi@0 1103 set_decompile_count(0);
aoqi@0 1104
aoqi@0 1105 set_do_freq_based_layout(BlockLayoutByFrequency || method_has_option("BlockLayoutByFrequency"));
aoqi@0 1106 set_num_loop_opts(LoopOptsCount);
aoqi@0 1107 set_do_inlining(Inline);
aoqi@0 1108 set_max_inline_size(MaxInlineSize);
aoqi@0 1109 set_freq_inline_size(FreqInlineSize);
aoqi@0 1110 set_do_scheduling(OptoScheduling);
aoqi@0 1111 set_do_count_invocations(false);
aoqi@0 1112 set_do_method_data_update(false);
aoqi@0 1113 set_rtm_state(NoRTM); // No RTM lock eliding by default
vlivanov@7385 1114 method_has_option_value("MaxNodeLimit", _max_node_limit);
aoqi@0 1115 #if INCLUDE_RTM_OPT
aoqi@0 1116 if (UseRTMLocking && has_method() && (method()->method_data_or_null() != NULL)) {
aoqi@0 1117 int rtm_state = method()->method_data()->rtm_state();
aoqi@0 1118 if (method_has_option("NoRTMLockEliding") || ((rtm_state & NoRTM) != 0)) {
aoqi@0 1119 // Don't generate RTM lock eliding code.
aoqi@0 1120 set_rtm_state(NoRTM);
aoqi@0 1121 } else if (method_has_option("UseRTMLockEliding") || ((rtm_state & UseRTM) != 0) || !UseRTMDeopt) {
aoqi@0 1122 // Generate RTM lock eliding code without abort ratio calculation code.
aoqi@0 1123 set_rtm_state(UseRTM);
aoqi@0 1124 } else if (UseRTMDeopt) {
aoqi@0 1125 // Generate RTM lock eliding code and include abort ratio calculation
aoqi@0 1126 // code if UseRTMDeopt is on.
aoqi@0 1127 set_rtm_state(ProfileRTM);
aoqi@0 1128 }
aoqi@0 1129 }
aoqi@0 1130 #endif
aoqi@0 1131 if (debug_info()->recording_non_safepoints()) {
aoqi@0 1132 set_node_note_array(new(comp_arena()) GrowableArray<Node_Notes*>
aoqi@0 1133 (comp_arena(), 8, 0, NULL));
aoqi@0 1134 set_default_node_notes(Node_Notes::make(this));
aoqi@0 1135 }
aoqi@0 1136
aoqi@0 1137 // // -- Initialize types before each compile --
aoqi@0 1138 // // Update cached type information
aoqi@0 1139 // if( _method && _method->constants() )
aoqi@0 1140 // Type::update_loaded_types(_method, _method->constants());
aoqi@0 1141
aoqi@0 1142 // Init alias_type map.
aoqi@0 1143 if (!_do_escape_analysis && aliaslevel == 3)
aoqi@0 1144 aliaslevel = 2; // No unique types without escape analysis
aoqi@0 1145 _AliasLevel = aliaslevel;
aoqi@0 1146 const int grow_ats = 16;
aoqi@0 1147 _max_alias_types = grow_ats;
aoqi@0 1148 _alias_types = NEW_ARENA_ARRAY(comp_arena(), AliasType*, grow_ats);
aoqi@0 1149 AliasType* ats = NEW_ARENA_ARRAY(comp_arena(), AliasType, grow_ats);
aoqi@0 1150 Copy::zero_to_bytes(ats, sizeof(AliasType)*grow_ats);
aoqi@0 1151 {
aoqi@0 1152 for (int i = 0; i < grow_ats; i++) _alias_types[i] = &ats[i];
aoqi@0 1153 }
aoqi@0 1154 // Initialize the first few types.
aoqi@0 1155 _alias_types[AliasIdxTop]->Init(AliasIdxTop, NULL);
aoqi@0 1156 _alias_types[AliasIdxBot]->Init(AliasIdxBot, TypePtr::BOTTOM);
aoqi@0 1157 _alias_types[AliasIdxRaw]->Init(AliasIdxRaw, TypeRawPtr::BOTTOM);
aoqi@0 1158 _num_alias_types = AliasIdxRaw+1;
aoqi@0 1159 // Zero out the alias type cache.
aoqi@0 1160 Copy::zero_to_bytes(_alias_cache, sizeof(_alias_cache));
aoqi@0 1161 // A NULL adr_type hits in the cache right away. Preload the right answer.
aoqi@0 1162 probe_alias_cache(NULL)->_index = AliasIdxTop;
aoqi@0 1163
aoqi@0 1164 _intrinsics = NULL;
aoqi@0 1165 _macro_nodes = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8, 0, NULL);
aoqi@0 1166 _predicate_opaqs = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8, 0, NULL);
aoqi@0 1167 _expensive_nodes = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8, 0, NULL);
aoqi@0 1168 register_library_intrinsics();
aoqi@0 1169 }
aoqi@0 1170
aoqi@0 1171 //---------------------------init_start----------------------------------------
aoqi@0 1172 // Install the StartNode on this compile object.
aoqi@0 1173 void Compile::init_start(StartNode* s) {
aoqi@0 1174 if (failing())
aoqi@0 1175 return; // already failing
aoqi@0 1176 assert(s == start(), "");
aoqi@0 1177 }
aoqi@0 1178
aoqi@0 1179 StartNode* Compile::start() const {
aoqi@0 1180 assert(!failing(), "");
aoqi@0 1181 for (DUIterator_Fast imax, i = root()->fast_outs(imax); i < imax; i++) {
aoqi@0 1182 Node* start = root()->fast_out(i);
aoqi@0 1183 if( start->is_Start() )
aoqi@0 1184 return start->as_Start();
aoqi@0 1185 }
aoqi@0 1186 fatal("Did not find Start node!");
aoqi@0 1187 return NULL;
aoqi@0 1188 }
aoqi@0 1189
aoqi@0 1190 //-------------------------------immutable_memory-------------------------------------
aoqi@0 1191 // Access immutable memory
aoqi@0 1192 Node* Compile::immutable_memory() {
aoqi@0 1193 if (_immutable_memory != NULL) {
aoqi@0 1194 return _immutable_memory;
aoqi@0 1195 }
aoqi@0 1196 StartNode* s = start();
aoqi@0 1197 for (DUIterator_Fast imax, i = s->fast_outs(imax); true; i++) {
aoqi@0 1198 Node *p = s->fast_out(i);
aoqi@0 1199 if (p != s && p->as_Proj()->_con == TypeFunc::Memory) {
aoqi@0 1200 _immutable_memory = p;
aoqi@0 1201 return _immutable_memory;
aoqi@0 1202 }
aoqi@0 1203 }
aoqi@0 1204 ShouldNotReachHere();
aoqi@0 1205 return NULL;
aoqi@0 1206 }
aoqi@0 1207
aoqi@0 1208 //----------------------set_cached_top_node------------------------------------
aoqi@0 1209 // Install the cached top node, and make sure Node::is_top works correctly.
aoqi@0 1210 void Compile::set_cached_top_node(Node* tn) {
aoqi@0 1211 if (tn != NULL) verify_top(tn);
aoqi@0 1212 Node* old_top = _top;
aoqi@0 1213 _top = tn;
aoqi@0 1214 // Calling Node::setup_is_top allows the nodes the chance to adjust
aoqi@0 1215 // their _out arrays.
aoqi@0 1216 if (_top != NULL) _top->setup_is_top();
aoqi@0 1217 if (old_top != NULL) old_top->setup_is_top();
aoqi@0 1218 assert(_top == NULL || top()->is_top(), "");
aoqi@0 1219 }
aoqi@0 1220
aoqi@0 1221 #ifdef ASSERT
aoqi@0 1222 uint Compile::count_live_nodes_by_graph_walk() {
aoqi@0 1223 Unique_Node_List useful(comp_arena());
aoqi@0 1224 // Get useful node list by walking the graph.
aoqi@0 1225 identify_useful_nodes(useful);
aoqi@0 1226 return useful.size();
aoqi@0 1227 }
aoqi@0 1228
aoqi@0 1229 void Compile::print_missing_nodes() {
aoqi@0 1230
aoqi@0 1231 // Return if CompileLog is NULL and PrintIdealNodeCount is false.
aoqi@0 1232 if ((_log == NULL) && (! PrintIdealNodeCount)) {
aoqi@0 1233 return;
aoqi@0 1234 }
aoqi@0 1235
aoqi@0 1236 // This is an expensive function. It is executed only when the user
aoqi@0 1237 // specifies VerifyIdealNodeCount option or otherwise knows the
aoqi@0 1238 // additional work that needs to be done to identify reachable nodes
aoqi@0 1239 // by walking the flow graph and find the missing ones using
aoqi@0 1240 // _dead_node_list.
aoqi@0 1241
aoqi@0 1242 Unique_Node_List useful(comp_arena());
aoqi@0 1243 // Get useful node list by walking the graph.
aoqi@0 1244 identify_useful_nodes(useful);
aoqi@0 1245
aoqi@0 1246 uint l_nodes = C->live_nodes();
aoqi@0 1247 uint l_nodes_by_walk = useful.size();
aoqi@0 1248
aoqi@0 1249 if (l_nodes != l_nodes_by_walk) {
aoqi@0 1250 if (_log != NULL) {
aoqi@0 1251 _log->begin_head("mismatched_nodes count='%d'", abs((int) (l_nodes - l_nodes_by_walk)));
aoqi@0 1252 _log->stamp();
aoqi@0 1253 _log->end_head();
aoqi@0 1254 }
aoqi@0 1255 VectorSet& useful_member_set = useful.member_set();
aoqi@0 1256 int last_idx = l_nodes_by_walk;
aoqi@0 1257 for (int i = 0; i < last_idx; i++) {
aoqi@0 1258 if (useful_member_set.test(i)) {
aoqi@0 1259 if (_dead_node_list.test(i)) {
aoqi@0 1260 if (_log != NULL) {
aoqi@0 1261 _log->elem("mismatched_node_info node_idx='%d' type='both live and dead'", i);
aoqi@0 1262 }
aoqi@0 1263 if (PrintIdealNodeCount) {
aoqi@0 1264 // Print the log message to tty
aoqi@0 1265 tty->print_cr("mismatched_node idx='%d' both live and dead'", i);
aoqi@0 1266 useful.at(i)->dump();
aoqi@0 1267 }
aoqi@0 1268 }
aoqi@0 1269 }
aoqi@0 1270 else if (! _dead_node_list.test(i)) {
aoqi@0 1271 if (_log != NULL) {
aoqi@0 1272 _log->elem("mismatched_node_info node_idx='%d' type='neither live nor dead'", i);
aoqi@0 1273 }
aoqi@0 1274 if (PrintIdealNodeCount) {
aoqi@0 1275 // Print the log message to tty
aoqi@0 1276 tty->print_cr("mismatched_node idx='%d' type='neither live nor dead'", i);
aoqi@0 1277 }
aoqi@0 1278 }
aoqi@0 1279 }
aoqi@0 1280 if (_log != NULL) {
aoqi@0 1281 _log->tail("mismatched_nodes");
aoqi@0 1282 }
aoqi@0 1283 }
aoqi@0 1284 }
aoqi@0 1285 #endif
aoqi@0 1286
aoqi@0 1287 #ifndef PRODUCT
aoqi@0 1288 void Compile::verify_top(Node* tn) const {
aoqi@0 1289 if (tn != NULL) {
aoqi@0 1290 assert(tn->is_Con(), "top node must be a constant");
aoqi@0 1291 assert(((ConNode*)tn)->type() == Type::TOP, "top node must have correct type");
aoqi@0 1292 assert(tn->in(0) != NULL, "must have live top node");
aoqi@0 1293 }
aoqi@0 1294 }
aoqi@0 1295 #endif
aoqi@0 1296
aoqi@0 1297
aoqi@0 1298 ///-------------------Managing Per-Node Debug & Profile Info-------------------
aoqi@0 1299
aoqi@0 1300 void Compile::grow_node_notes(GrowableArray<Node_Notes*>* arr, int grow_by) {
aoqi@0 1301 guarantee(arr != NULL, "");
aoqi@0 1302 int num_blocks = arr->length();
aoqi@0 1303 if (grow_by < num_blocks) grow_by = num_blocks;
aoqi@0 1304 int num_notes = grow_by * _node_notes_block_size;
aoqi@0 1305 Node_Notes* notes = NEW_ARENA_ARRAY(node_arena(), Node_Notes, num_notes);
aoqi@0 1306 Copy::zero_to_bytes(notes, num_notes * sizeof(Node_Notes));
aoqi@0 1307 while (num_notes > 0) {
aoqi@0 1308 arr->append(notes);
aoqi@0 1309 notes += _node_notes_block_size;
aoqi@0 1310 num_notes -= _node_notes_block_size;
aoqi@0 1311 }
aoqi@0 1312 assert(num_notes == 0, "exact multiple, please");
aoqi@0 1313 }
aoqi@0 1314
aoqi@0 1315 bool Compile::copy_node_notes_to(Node* dest, Node* source) {
aoqi@0 1316 if (source == NULL || dest == NULL) return false;
aoqi@0 1317
aoqi@0 1318 if (dest->is_Con())
aoqi@0 1319 return false; // Do not push debug info onto constants.
aoqi@0 1320
aoqi@0 1321 #ifdef ASSERT
aoqi@0 1322 // Leave a bread crumb trail pointing to the original node:
aoqi@0 1323 if (dest != NULL && dest != source && dest->debug_orig() == NULL) {
aoqi@0 1324 dest->set_debug_orig(source);
aoqi@0 1325 }
aoqi@0 1326 #endif
aoqi@0 1327
aoqi@0 1328 if (node_note_array() == NULL)
aoqi@0 1329 return false; // Not collecting any notes now.
aoqi@0 1330
aoqi@0 1331 // This is a copy onto a pre-existing node, which may already have notes.
aoqi@0 1332 // If both nodes have notes, do not overwrite any pre-existing notes.
aoqi@0 1333 Node_Notes* source_notes = node_notes_at(source->_idx);
aoqi@0 1334 if (source_notes == NULL || source_notes->is_clear()) return false;
aoqi@0 1335 Node_Notes* dest_notes = node_notes_at(dest->_idx);
aoqi@0 1336 if (dest_notes == NULL || dest_notes->is_clear()) {
aoqi@0 1337 return set_node_notes_at(dest->_idx, source_notes);
aoqi@0 1338 }
aoqi@0 1339
aoqi@0 1340 Node_Notes merged_notes = (*source_notes);
aoqi@0 1341 // The order of operations here ensures that dest notes will win...
aoqi@0 1342 merged_notes.update_from(dest_notes);
aoqi@0 1343 return set_node_notes_at(dest->_idx, &merged_notes);
aoqi@0 1344 }
aoqi@0 1345
aoqi@0 1346
aoqi@0 1347 //--------------------------allow_range_check_smearing-------------------------
aoqi@0 1348 // Gating condition for coalescing similar range checks.
aoqi@0 1349 // Sometimes we try 'speculatively' replacing a series of a range checks by a
aoqi@0 1350 // single covering check that is at least as strong as any of them.
aoqi@0 1351 // If the optimization succeeds, the simplified (strengthened) range check
aoqi@0 1352 // will always succeed. If it fails, we will deopt, and then give up
aoqi@0 1353 // on the optimization.
aoqi@0 1354 bool Compile::allow_range_check_smearing() const {
aoqi@0 1355 // If this method has already thrown a range-check,
aoqi@0 1356 // assume it was because we already tried range smearing
aoqi@0 1357 // and it failed.
aoqi@0 1358 uint already_trapped = trap_count(Deoptimization::Reason_range_check);
aoqi@0 1359 return !already_trapped;
aoqi@0 1360 }
aoqi@0 1361
aoqi@0 1362
aoqi@0 1363 //------------------------------flatten_alias_type-----------------------------
aoqi@0 1364 const TypePtr *Compile::flatten_alias_type( const TypePtr *tj ) const {
aoqi@0 1365 int offset = tj->offset();
aoqi@0 1366 TypePtr::PTR ptr = tj->ptr();
aoqi@0 1367
aoqi@0 1368 // Known instance (scalarizable allocation) alias only with itself.
aoqi@0 1369 bool is_known_inst = tj->isa_oopptr() != NULL &&
aoqi@0 1370 tj->is_oopptr()->is_known_instance();
aoqi@0 1371
aoqi@0 1372 // Process weird unsafe references.
aoqi@0 1373 if (offset == Type::OffsetBot && (tj->isa_instptr() /*|| tj->isa_klassptr()*/)) {
aoqi@0 1374 assert(InlineUnsafeOps, "indeterminate pointers come only from unsafe ops");
aoqi@0 1375 assert(!is_known_inst, "scalarizable allocation should not have unsafe references");
aoqi@0 1376 tj = TypeOopPtr::BOTTOM;
aoqi@0 1377 ptr = tj->ptr();
aoqi@0 1378 offset = tj->offset();
aoqi@0 1379 }
aoqi@0 1380
aoqi@0 1381 // Array pointers need some flattening
aoqi@0 1382 const TypeAryPtr *ta = tj->isa_aryptr();
aoqi@0 1383 if (ta && ta->is_stable()) {
aoqi@0 1384 // Erase stability property for alias analysis.
aoqi@0 1385 tj = ta = ta->cast_to_stable(false);
aoqi@0 1386 }
aoqi@0 1387 if( ta && is_known_inst ) {
aoqi@0 1388 if ( offset != Type::OffsetBot &&
aoqi@0 1389 offset > arrayOopDesc::length_offset_in_bytes() ) {
aoqi@0 1390 offset = Type::OffsetBot; // Flatten constant access into array body only
aoqi@0 1391 tj = ta = TypeAryPtr::make(ptr, ta->ary(), ta->klass(), true, offset, ta->instance_id());
aoqi@0 1392 }
aoqi@0 1393 } else if( ta && _AliasLevel >= 2 ) {
aoqi@0 1394 // For arrays indexed by constant indices, we flatten the alias
aoqi@0 1395 // space to include all of the array body. Only the header, klass
aoqi@0 1396 // and array length can be accessed un-aliased.
aoqi@0 1397 if( offset != Type::OffsetBot ) {
aoqi@0 1398 if( ta->const_oop() ) { // MethodData* or Method*
aoqi@0 1399 offset = Type::OffsetBot; // Flatten constant access into array body
aoqi@0 1400 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),ta->ary(),ta->klass(),false,offset);
aoqi@0 1401 } else if( offset == arrayOopDesc::length_offset_in_bytes() ) {
aoqi@0 1402 // range is OK as-is.
aoqi@0 1403 tj = ta = TypeAryPtr::RANGE;
aoqi@0 1404 } else if( offset == oopDesc::klass_offset_in_bytes() ) {
aoqi@0 1405 tj = TypeInstPtr::KLASS; // all klass loads look alike
aoqi@0 1406 ta = TypeAryPtr::RANGE; // generic ignored junk
aoqi@0 1407 ptr = TypePtr::BotPTR;
aoqi@0 1408 } else if( offset == oopDesc::mark_offset_in_bytes() ) {
aoqi@0 1409 tj = TypeInstPtr::MARK;
aoqi@0 1410 ta = TypeAryPtr::RANGE; // generic ignored junk
aoqi@0 1411 ptr = TypePtr::BotPTR;
aoqi@0 1412 } else { // Random constant offset into array body
aoqi@0 1413 offset = Type::OffsetBot; // Flatten constant access into array body
aoqi@0 1414 tj = ta = TypeAryPtr::make(ptr,ta->ary(),ta->klass(),false,offset);
aoqi@0 1415 }
aoqi@0 1416 }
aoqi@0 1417 // Arrays of fixed size alias with arrays of unknown size.
aoqi@0 1418 if (ta->size() != TypeInt::POS) {
aoqi@0 1419 const TypeAry *tary = TypeAry::make(ta->elem(), TypeInt::POS);
aoqi@0 1420 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,ta->klass(),false,offset);
aoqi@0 1421 }
aoqi@0 1422 // Arrays of known objects become arrays of unknown objects.
aoqi@0 1423 if (ta->elem()->isa_narrowoop() && ta->elem() != TypeNarrowOop::BOTTOM) {
aoqi@0 1424 const TypeAry *tary = TypeAry::make(TypeNarrowOop::BOTTOM, ta->size());
aoqi@0 1425 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,NULL,false,offset);
aoqi@0 1426 }
aoqi@0 1427 if (ta->elem()->isa_oopptr() && ta->elem() != TypeInstPtr::BOTTOM) {
aoqi@0 1428 const TypeAry *tary = TypeAry::make(TypeInstPtr::BOTTOM, ta->size());
aoqi@0 1429 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,NULL,false,offset);
aoqi@0 1430 }
aoqi@0 1431 // Arrays of bytes and of booleans both use 'bastore' and 'baload' so
aoqi@0 1432 // cannot be distinguished by bytecode alone.
aoqi@0 1433 if (ta->elem() == TypeInt::BOOL) {
aoqi@0 1434 const TypeAry *tary = TypeAry::make(TypeInt::BYTE, ta->size());
aoqi@0 1435 ciKlass* aklass = ciTypeArrayKlass::make(T_BYTE);
aoqi@0 1436 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,aklass,false,offset);
aoqi@0 1437 }
aoqi@0 1438 // During the 2nd round of IterGVN, NotNull castings are removed.
aoqi@0 1439 // Make sure the Bottom and NotNull variants alias the same.
aoqi@0 1440 // Also, make sure exact and non-exact variants alias the same.
aoqi@0 1441 if (ptr == TypePtr::NotNull || ta->klass_is_exact() || ta->speculative() != NULL) {
aoqi@0 1442 tj = ta = TypeAryPtr::make(TypePtr::BotPTR,ta->ary(),ta->klass(),false,offset);
aoqi@0 1443 }
aoqi@0 1444 }
aoqi@0 1445
aoqi@0 1446 // Oop pointers need some flattening
aoqi@0 1447 const TypeInstPtr *to = tj->isa_instptr();
aoqi@0 1448 if( to && _AliasLevel >= 2 && to != TypeOopPtr::BOTTOM ) {
aoqi@0 1449 ciInstanceKlass *k = to->klass()->as_instance_klass();
aoqi@0 1450 if( ptr == TypePtr::Constant ) {
aoqi@0 1451 if (to->klass() != ciEnv::current()->Class_klass() ||
aoqi@0 1452 offset < k->size_helper() * wordSize) {
aoqi@0 1453 // No constant oop pointers (such as Strings); they alias with
aoqi@0 1454 // unknown strings.
aoqi@0 1455 assert(!is_known_inst, "not scalarizable allocation");
aoqi@0 1456 tj = to = TypeInstPtr::make(TypePtr::BotPTR,to->klass(),false,0,offset);
aoqi@0 1457 }
aoqi@0 1458 } else if( is_known_inst ) {
aoqi@0 1459 tj = to; // Keep NotNull and klass_is_exact for instance type
aoqi@0 1460 } else if( ptr == TypePtr::NotNull || to->klass_is_exact() ) {
aoqi@0 1461 // During the 2nd round of IterGVN, NotNull castings are removed.
aoqi@0 1462 // Make sure the Bottom and NotNull variants alias the same.
aoqi@0 1463 // Also, make sure exact and non-exact variants alias the same.
aoqi@0 1464 tj = to = TypeInstPtr::make(TypePtr::BotPTR,to->klass(),false,0,offset);
aoqi@0 1465 }
aoqi@0 1466 if (to->speculative() != NULL) {
aoqi@0 1467 tj = to = TypeInstPtr::make(to->ptr(),to->klass(),to->klass_is_exact(),to->const_oop(),to->offset(), to->instance_id());
aoqi@0 1468 }
aoqi@0 1469 // Canonicalize the holder of this field
aoqi@0 1470 if (offset >= 0 && offset < instanceOopDesc::base_offset_in_bytes()) {
aoqi@0 1471 // First handle header references such as a LoadKlassNode, even if the
aoqi@0 1472 // object's klass is unloaded at compile time (4965979).
aoqi@0 1473 if (!is_known_inst) { // Do it only for non-instance types
aoqi@0 1474 tj = to = TypeInstPtr::make(TypePtr::BotPTR, env()->Object_klass(), false, NULL, offset);
aoqi@0 1475 }
aoqi@0 1476 } else if (offset < 0 || offset >= k->size_helper() * wordSize) {
aoqi@0 1477 // Static fields are in the space above the normal instance
aoqi@0 1478 // fields in the java.lang.Class instance.
aoqi@0 1479 if (to->klass() != ciEnv::current()->Class_klass()) {
aoqi@0 1480 to = NULL;
aoqi@0 1481 tj = TypeOopPtr::BOTTOM;
aoqi@0 1482 offset = tj->offset();
aoqi@0 1483 }
aoqi@0 1484 } else {
aoqi@0 1485 ciInstanceKlass *canonical_holder = k->get_canonical_holder(offset);
aoqi@0 1486 if (!k->equals(canonical_holder) || tj->offset() != offset) {
aoqi@0 1487 if( is_known_inst ) {
aoqi@0 1488 tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, true, NULL, offset, to->instance_id());
aoqi@0 1489 } else {
aoqi@0 1490 tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, false, NULL, offset);
aoqi@0 1491 }
aoqi@0 1492 }
aoqi@0 1493 }
aoqi@0 1494 }
aoqi@0 1495
aoqi@0 1496 // Klass pointers to object array klasses need some flattening
aoqi@0 1497 const TypeKlassPtr *tk = tj->isa_klassptr();
aoqi@0 1498 if( tk ) {
aoqi@0 1499 // If we are referencing a field within a Klass, we need
aoqi@0 1500 // to assume the worst case of an Object. Both exact and
aoqi@0 1501 // inexact types must flatten to the same alias class so
aoqi@0 1502 // use NotNull as the PTR.
aoqi@0 1503 if ( offset == Type::OffsetBot || (offset >= 0 && (size_t)offset < sizeof(Klass)) ) {
aoqi@0 1504
aoqi@0 1505 tj = tk = TypeKlassPtr::make(TypePtr::NotNull,
aoqi@0 1506 TypeKlassPtr::OBJECT->klass(),
aoqi@0 1507 offset);
aoqi@0 1508 }
aoqi@0 1509
aoqi@0 1510 ciKlass* klass = tk->klass();
aoqi@0 1511 if( klass->is_obj_array_klass() ) {
aoqi@0 1512 ciKlass* k = TypeAryPtr::OOPS->klass();
aoqi@0 1513 if( !k || !k->is_loaded() ) // Only fails for some -Xcomp runs
aoqi@0 1514 k = TypeInstPtr::BOTTOM->klass();
aoqi@0 1515 tj = tk = TypeKlassPtr::make( TypePtr::NotNull, k, offset );
aoqi@0 1516 }
aoqi@0 1517
aoqi@0 1518 // Check for precise loads from the primary supertype array and force them
aoqi@0 1519 // to the supertype cache alias index. Check for generic array loads from
aoqi@0 1520 // the primary supertype array and also force them to the supertype cache
aoqi@0 1521 // alias index. Since the same load can reach both, we need to merge
aoqi@0 1522 // these 2 disparate memories into the same alias class. Since the
aoqi@0 1523 // primary supertype array is read-only, there's no chance of confusion
aoqi@0 1524 // where we bypass an array load and an array store.
aoqi@0 1525 int primary_supers_offset = in_bytes(Klass::primary_supers_offset());
aoqi@0 1526 if (offset == Type::OffsetBot ||
aoqi@0 1527 (offset >= primary_supers_offset &&
aoqi@0 1528 offset < (int)(primary_supers_offset + Klass::primary_super_limit() * wordSize)) ||
aoqi@0 1529 offset == (int)in_bytes(Klass::secondary_super_cache_offset())) {
aoqi@0 1530 offset = in_bytes(Klass::secondary_super_cache_offset());
aoqi@0 1531 tj = tk = TypeKlassPtr::make( TypePtr::NotNull, tk->klass(), offset );
aoqi@0 1532 }
aoqi@0 1533 }
aoqi@0 1534
aoqi@0 1535 // Flatten all Raw pointers together.
aoqi@0 1536 if (tj->base() == Type::RawPtr)
aoqi@0 1537 tj = TypeRawPtr::BOTTOM;
aoqi@0 1538
aoqi@0 1539 if (tj->base() == Type::AnyPtr)
aoqi@0 1540 tj = TypePtr::BOTTOM; // An error, which the caller must check for.
aoqi@0 1541
aoqi@0 1542 // Flatten all to bottom for now
aoqi@0 1543 switch( _AliasLevel ) {
aoqi@0 1544 case 0:
aoqi@0 1545 tj = TypePtr::BOTTOM;
aoqi@0 1546 break;
aoqi@0 1547 case 1: // Flatten to: oop, static, field or array
aoqi@0 1548 switch (tj->base()) {
aoqi@0 1549 //case Type::AryPtr: tj = TypeAryPtr::RANGE; break;
aoqi@0 1550 case Type::RawPtr: tj = TypeRawPtr::BOTTOM; break;
aoqi@0 1551 case Type::AryPtr: // do not distinguish arrays at all
aoqi@0 1552 case Type::InstPtr: tj = TypeInstPtr::BOTTOM; break;
aoqi@0 1553 case Type::KlassPtr: tj = TypeKlassPtr::OBJECT; break;
aoqi@0 1554 case Type::AnyPtr: tj = TypePtr::BOTTOM; break; // caller checks it
aoqi@0 1555 default: ShouldNotReachHere();
aoqi@0 1556 }
aoqi@0 1557 break;
aoqi@0 1558 case 2: // No collapsing at level 2; keep all splits
aoqi@0 1559 case 3: // No collapsing at level 3; keep all splits
aoqi@0 1560 break;
aoqi@0 1561 default:
aoqi@0 1562 Unimplemented();
aoqi@0 1563 }
aoqi@0 1564
aoqi@0 1565 offset = tj->offset();
aoqi@0 1566 assert( offset != Type::OffsetTop, "Offset has fallen from constant" );
aoqi@0 1567
aoqi@0 1568 assert( (offset != Type::OffsetBot && tj->base() != Type::AryPtr) ||
aoqi@0 1569 (offset == Type::OffsetBot && tj->base() == Type::AryPtr) ||
aoqi@0 1570 (offset == Type::OffsetBot && tj == TypeOopPtr::BOTTOM) ||
aoqi@0 1571 (offset == Type::OffsetBot && tj == TypePtr::BOTTOM) ||
aoqi@0 1572 (offset == oopDesc::mark_offset_in_bytes() && tj->base() == Type::AryPtr) ||
aoqi@0 1573 (offset == oopDesc::klass_offset_in_bytes() && tj->base() == Type::AryPtr) ||
aoqi@0 1574 (offset == arrayOopDesc::length_offset_in_bytes() && tj->base() == Type::AryPtr) ,
aoqi@0 1575 "For oops, klasses, raw offset must be constant; for arrays the offset is never known" );
aoqi@0 1576 assert( tj->ptr() != TypePtr::TopPTR &&
aoqi@0 1577 tj->ptr() != TypePtr::AnyNull &&
aoqi@0 1578 tj->ptr() != TypePtr::Null, "No imprecise addresses" );
aoqi@0 1579 // assert( tj->ptr() != TypePtr::Constant ||
aoqi@0 1580 // tj->base() == Type::RawPtr ||
aoqi@0 1581 // tj->base() == Type::KlassPtr, "No constant oop addresses" );
aoqi@0 1582
aoqi@0 1583 return tj;
aoqi@0 1584 }
aoqi@0 1585
aoqi@0 1586 void Compile::AliasType::Init(int i, const TypePtr* at) {
aoqi@0 1587 _index = i;
aoqi@0 1588 _adr_type = at;
aoqi@0 1589 _field = NULL;
aoqi@0 1590 _element = NULL;
aoqi@0 1591 _is_rewritable = true; // default
aoqi@0 1592 const TypeOopPtr *atoop = (at != NULL) ? at->isa_oopptr() : NULL;
aoqi@0 1593 if (atoop != NULL && atoop->is_known_instance()) {
aoqi@0 1594 const TypeOopPtr *gt = atoop->cast_to_instance_id(TypeOopPtr::InstanceBot);
aoqi@0 1595 _general_index = Compile::current()->get_alias_index(gt);
aoqi@0 1596 } else {
aoqi@0 1597 _general_index = 0;
aoqi@0 1598 }
aoqi@0 1599 }
aoqi@0 1600
aoqi@0 1601 //---------------------------------print_on------------------------------------
aoqi@0 1602 #ifndef PRODUCT
aoqi@0 1603 void Compile::AliasType::print_on(outputStream* st) {
aoqi@0 1604 if (index() < 10)
aoqi@0 1605 st->print("@ <%d> ", index());
aoqi@0 1606 else st->print("@ <%d>", index());
aoqi@0 1607 st->print(is_rewritable() ? " " : " RO");
aoqi@0 1608 int offset = adr_type()->offset();
aoqi@0 1609 if (offset == Type::OffsetBot)
aoqi@0 1610 st->print(" +any");
aoqi@0 1611 else st->print(" +%-3d", offset);
aoqi@0 1612 st->print(" in ");
aoqi@0 1613 adr_type()->dump_on(st);
aoqi@0 1614 const TypeOopPtr* tjp = adr_type()->isa_oopptr();
aoqi@0 1615 if (field() != NULL && tjp) {
aoqi@0 1616 if (tjp->klass() != field()->holder() ||
aoqi@0 1617 tjp->offset() != field()->offset_in_bytes()) {
aoqi@0 1618 st->print(" != ");
aoqi@0 1619 field()->print();
aoqi@0 1620 st->print(" ***");
aoqi@0 1621 }
aoqi@0 1622 }
aoqi@0 1623 }
aoqi@0 1624
aoqi@0 1625 void print_alias_types() {
aoqi@0 1626 Compile* C = Compile::current();
aoqi@0 1627 tty->print_cr("--- Alias types, AliasIdxBot .. %d", C->num_alias_types()-1);
aoqi@0 1628 for (int idx = Compile::AliasIdxBot; idx < C->num_alias_types(); idx++) {
aoqi@0 1629 C->alias_type(idx)->print_on(tty);
aoqi@0 1630 tty->cr();
aoqi@0 1631 }
aoqi@0 1632 }
aoqi@0 1633 #endif
aoqi@0 1634
aoqi@0 1635
aoqi@0 1636 //----------------------------probe_alias_cache--------------------------------
aoqi@0 1637 Compile::AliasCacheEntry* Compile::probe_alias_cache(const TypePtr* adr_type) {
aoqi@0 1638 intptr_t key = (intptr_t) adr_type;
aoqi@0 1639 key ^= key >> logAliasCacheSize;
aoqi@0 1640 return &_alias_cache[key & right_n_bits(logAliasCacheSize)];
aoqi@0 1641 }
aoqi@0 1642
aoqi@0 1643
aoqi@0 1644 //-----------------------------grow_alias_types--------------------------------
aoqi@0 1645 void Compile::grow_alias_types() {
aoqi@0 1646 const int old_ats = _max_alias_types; // how many before?
aoqi@0 1647 const int new_ats = old_ats; // how many more?
aoqi@0 1648 const int grow_ats = old_ats+new_ats; // how many now?
aoqi@0 1649 _max_alias_types = grow_ats;
aoqi@0 1650 _alias_types = REALLOC_ARENA_ARRAY(comp_arena(), AliasType*, _alias_types, old_ats, grow_ats);
aoqi@0 1651 AliasType* ats = NEW_ARENA_ARRAY(comp_arena(), AliasType, new_ats);
aoqi@0 1652 Copy::zero_to_bytes(ats, sizeof(AliasType)*new_ats);
aoqi@0 1653 for (int i = 0; i < new_ats; i++) _alias_types[old_ats+i] = &ats[i];
aoqi@0 1654 }
aoqi@0 1655
aoqi@0 1656
aoqi@0 1657 //--------------------------------find_alias_type------------------------------
aoqi@0 1658 Compile::AliasType* Compile::find_alias_type(const TypePtr* adr_type, bool no_create, ciField* original_field) {
aoqi@0 1659 if (_AliasLevel == 0)
aoqi@0 1660 return alias_type(AliasIdxBot);
aoqi@0 1661
aoqi@0 1662 AliasCacheEntry* ace = probe_alias_cache(adr_type);
aoqi@0 1663 if (ace->_adr_type == adr_type) {
aoqi@0 1664 return alias_type(ace->_index);
aoqi@0 1665 }
aoqi@0 1666
aoqi@0 1667 // Handle special cases.
aoqi@0 1668 if (adr_type == NULL) return alias_type(AliasIdxTop);
aoqi@0 1669 if (adr_type == TypePtr::BOTTOM) return alias_type(AliasIdxBot);
aoqi@0 1670
aoqi@0 1671 // Do it the slow way.
aoqi@0 1672 const TypePtr* flat = flatten_alias_type(adr_type);
aoqi@0 1673
aoqi@0 1674 #ifdef ASSERT
aoqi@0 1675 assert(flat == flatten_alias_type(flat), "idempotent");
aoqi@0 1676 assert(flat != TypePtr::BOTTOM, "cannot alias-analyze an untyped ptr");
aoqi@0 1677 if (flat->isa_oopptr() && !flat->isa_klassptr()) {
aoqi@0 1678 const TypeOopPtr* foop = flat->is_oopptr();
aoqi@0 1679 // Scalarizable allocations have exact klass always.
aoqi@0 1680 bool exact = !foop->klass_is_exact() || foop->is_known_instance();
aoqi@0 1681 const TypePtr* xoop = foop->cast_to_exactness(exact)->is_ptr();
aoqi@0 1682 assert(foop == flatten_alias_type(xoop), "exactness must not affect alias type");
aoqi@0 1683 }
aoqi@0 1684 assert(flat == flatten_alias_type(flat), "exact bit doesn't matter");
aoqi@0 1685 #endif
aoqi@0 1686
aoqi@0 1687 int idx = AliasIdxTop;
aoqi@0 1688 for (int i = 0; i < num_alias_types(); i++) {
aoqi@0 1689 if (alias_type(i)->adr_type() == flat) {
aoqi@0 1690 idx = i;
aoqi@0 1691 break;
aoqi@0 1692 }
aoqi@0 1693 }
aoqi@0 1694
aoqi@0 1695 if (idx == AliasIdxTop) {
aoqi@0 1696 if (no_create) return NULL;
aoqi@0 1697 // Grow the array if necessary.
aoqi@0 1698 if (_num_alias_types == _max_alias_types) grow_alias_types();
aoqi@0 1699 // Add a new alias type.
aoqi@0 1700 idx = _num_alias_types++;
aoqi@0 1701 _alias_types[idx]->Init(idx, flat);
aoqi@0 1702 if (flat == TypeInstPtr::KLASS) alias_type(idx)->set_rewritable(false);
aoqi@0 1703 if (flat == TypeAryPtr::RANGE) alias_type(idx)->set_rewritable(false);
aoqi@0 1704 if (flat->isa_instptr()) {
aoqi@0 1705 if (flat->offset() == java_lang_Class::klass_offset_in_bytes()
aoqi@0 1706 && flat->is_instptr()->klass() == env()->Class_klass())
aoqi@0 1707 alias_type(idx)->set_rewritable(false);
aoqi@0 1708 }
aoqi@0 1709 if (flat->isa_aryptr()) {
aoqi@0 1710 #ifdef ASSERT
aoqi@0 1711 const int header_size_min = arrayOopDesc::base_offset_in_bytes(T_BYTE);
aoqi@0 1712 // (T_BYTE has the weakest alignment and size restrictions...)
aoqi@0 1713 assert(flat->offset() < header_size_min, "array body reference must be OffsetBot");
aoqi@0 1714 #endif
aoqi@0 1715 if (flat->offset() == TypePtr::OffsetBot) {
aoqi@0 1716 alias_type(idx)->set_element(flat->is_aryptr()->elem());
aoqi@0 1717 }
aoqi@0 1718 }
aoqi@0 1719 if (flat->isa_klassptr()) {
aoqi@0 1720 if (flat->offset() == in_bytes(Klass::super_check_offset_offset()))
aoqi@0 1721 alias_type(idx)->set_rewritable(false);
aoqi@0 1722 if (flat->offset() == in_bytes(Klass::modifier_flags_offset()))
aoqi@0 1723 alias_type(idx)->set_rewritable(false);
aoqi@0 1724 if (flat->offset() == in_bytes(Klass::access_flags_offset()))
aoqi@0 1725 alias_type(idx)->set_rewritable(false);
aoqi@0 1726 if (flat->offset() == in_bytes(Klass::java_mirror_offset()))
aoqi@0 1727 alias_type(idx)->set_rewritable(false);
aoqi@0 1728 }
aoqi@0 1729 // %%% (We would like to finalize JavaThread::threadObj_offset(),
aoqi@0 1730 // but the base pointer type is not distinctive enough to identify
aoqi@0 1731 // references into JavaThread.)
aoqi@0 1732
aoqi@0 1733 // Check for final fields.
aoqi@0 1734 const TypeInstPtr* tinst = flat->isa_instptr();
aoqi@0 1735 if (tinst && tinst->offset() >= instanceOopDesc::base_offset_in_bytes()) {
aoqi@0 1736 ciField* field;
aoqi@0 1737 if (tinst->const_oop() != NULL &&
aoqi@0 1738 tinst->klass() == ciEnv::current()->Class_klass() &&
aoqi@0 1739 tinst->offset() >= (tinst->klass()->as_instance_klass()->size_helper() * wordSize)) {
aoqi@0 1740 // static field
aoqi@0 1741 ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
aoqi@0 1742 field = k->get_field_by_offset(tinst->offset(), true);
aoqi@0 1743 } else {
aoqi@0 1744 ciInstanceKlass *k = tinst->klass()->as_instance_klass();
aoqi@0 1745 field = k->get_field_by_offset(tinst->offset(), false);
aoqi@0 1746 }
aoqi@0 1747 assert(field == NULL ||
aoqi@0 1748 original_field == NULL ||
aoqi@0 1749 (field->holder() == original_field->holder() &&
aoqi@0 1750 field->offset() == original_field->offset() &&
aoqi@0 1751 field->is_static() == original_field->is_static()), "wrong field?");
aoqi@0 1752 // Set field() and is_rewritable() attributes.
aoqi@0 1753 if (field != NULL) alias_type(idx)->set_field(field);
aoqi@0 1754 }
aoqi@0 1755 }
aoqi@0 1756
aoqi@0 1757 // Fill the cache for next time.
aoqi@0 1758 ace->_adr_type = adr_type;
aoqi@0 1759 ace->_index = idx;
aoqi@0 1760 assert(alias_type(adr_type) == alias_type(idx), "type must be installed");
aoqi@0 1761
aoqi@0 1762 // Might as well try to fill the cache for the flattened version, too.
aoqi@0 1763 AliasCacheEntry* face = probe_alias_cache(flat);
aoqi@0 1764 if (face->_adr_type == NULL) {
aoqi@0 1765 face->_adr_type = flat;
aoqi@0 1766 face->_index = idx;
aoqi@0 1767 assert(alias_type(flat) == alias_type(idx), "flat type must work too");
aoqi@0 1768 }
aoqi@0 1769
aoqi@0 1770 return alias_type(idx);
aoqi@0 1771 }
aoqi@0 1772
aoqi@0 1773
aoqi@0 1774 Compile::AliasType* Compile::alias_type(ciField* field) {
aoqi@0 1775 const TypeOopPtr* t;
aoqi@0 1776 if (field->is_static())
aoqi@0 1777 t = TypeInstPtr::make(field->holder()->java_mirror());
aoqi@0 1778 else
aoqi@0 1779 t = TypeOopPtr::make_from_klass_raw(field->holder());
aoqi@0 1780 AliasType* atp = alias_type(t->add_offset(field->offset_in_bytes()), field);
aoqi@0 1781 assert((field->is_final() || field->is_stable()) == !atp->is_rewritable(), "must get the rewritable bits correct");
aoqi@0 1782 return atp;
aoqi@0 1783 }
aoqi@0 1784
aoqi@0 1785
aoqi@0 1786 //------------------------------have_alias_type--------------------------------
aoqi@0 1787 bool Compile::have_alias_type(const TypePtr* adr_type) {
aoqi@0 1788 AliasCacheEntry* ace = probe_alias_cache(adr_type);
aoqi@0 1789 if (ace->_adr_type == adr_type) {
aoqi@0 1790 return true;
aoqi@0 1791 }
aoqi@0 1792
aoqi@0 1793 // Handle special cases.
aoqi@0 1794 if (adr_type == NULL) return true;
aoqi@0 1795 if (adr_type == TypePtr::BOTTOM) return true;
aoqi@0 1796
aoqi@0 1797 return find_alias_type(adr_type, true, NULL) != NULL;
aoqi@0 1798 }
aoqi@0 1799
aoqi@0 1800 //-----------------------------must_alias--------------------------------------
aoqi@0 1801 // True if all values of the given address type are in the given alias category.
aoqi@0 1802 bool Compile::must_alias(const TypePtr* adr_type, int alias_idx) {
aoqi@0 1803 if (alias_idx == AliasIdxBot) return true; // the universal category
aoqi@0 1804 if (adr_type == NULL) return true; // NULL serves as TypePtr::TOP
aoqi@0 1805 if (alias_idx == AliasIdxTop) return false; // the empty category
aoqi@0 1806 if (adr_type->base() == Type::AnyPtr) return false; // TypePtr::BOTTOM or its twins
aoqi@0 1807
aoqi@0 1808 // the only remaining possible overlap is identity
aoqi@0 1809 int adr_idx = get_alias_index(adr_type);
aoqi@0 1810 assert(adr_idx != AliasIdxBot && adr_idx != AliasIdxTop, "");
aoqi@0 1811 assert(adr_idx == alias_idx ||
aoqi@0 1812 (alias_type(alias_idx)->adr_type() != TypeOopPtr::BOTTOM
aoqi@0 1813 && adr_type != TypeOopPtr::BOTTOM),
aoqi@0 1814 "should not be testing for overlap with an unsafe pointer");
aoqi@0 1815 return adr_idx == alias_idx;
aoqi@0 1816 }
aoqi@0 1817
aoqi@0 1818 //------------------------------can_alias--------------------------------------
aoqi@0 1819 // True if any values of the given address type are in the given alias category.
aoqi@0 1820 bool Compile::can_alias(const TypePtr* adr_type, int alias_idx) {
aoqi@0 1821 if (alias_idx == AliasIdxTop) return false; // the empty category
aoqi@0 1822 if (adr_type == NULL) return false; // NULL serves as TypePtr::TOP
aoqi@0 1823 if (alias_idx == AliasIdxBot) return true; // the universal category
aoqi@0 1824 if (adr_type->base() == Type::AnyPtr) return true; // TypePtr::BOTTOM or its twins
aoqi@0 1825
aoqi@0 1826 // the only remaining possible overlap is identity
aoqi@0 1827 int adr_idx = get_alias_index(adr_type);
aoqi@0 1828 assert(adr_idx != AliasIdxBot && adr_idx != AliasIdxTop, "");
aoqi@0 1829 return adr_idx == alias_idx;
aoqi@0 1830 }
aoqi@0 1831
aoqi@0 1832
aoqi@0 1833
aoqi@0 1834 //---------------------------pop_warm_call-------------------------------------
aoqi@0 1835 WarmCallInfo* Compile::pop_warm_call() {
aoqi@0 1836 WarmCallInfo* wci = _warm_calls;
aoqi@0 1837 if (wci != NULL) _warm_calls = wci->remove_from(wci);
aoqi@0 1838 return wci;
aoqi@0 1839 }
aoqi@0 1840
aoqi@0 1841 //----------------------------Inline_Warm--------------------------------------
aoqi@0 1842 int Compile::Inline_Warm() {
aoqi@0 1843 // If there is room, try to inline some more warm call sites.
aoqi@0 1844 // %%% Do a graph index compaction pass when we think we're out of space?
aoqi@0 1845 if (!InlineWarmCalls) return 0;
aoqi@0 1846
aoqi@0 1847 int calls_made_hot = 0;
aoqi@0 1848 int room_to_grow = NodeCountInliningCutoff - unique();
aoqi@0 1849 int amount_to_grow = MIN2(room_to_grow, (int)NodeCountInliningStep);
aoqi@0 1850 int amount_grown = 0;
aoqi@0 1851 WarmCallInfo* call;
aoqi@0 1852 while (amount_to_grow > 0 && (call = pop_warm_call()) != NULL) {
aoqi@0 1853 int est_size = (int)call->size();
aoqi@0 1854 if (est_size > (room_to_grow - amount_grown)) {
aoqi@0 1855 // This one won't fit anyway. Get rid of it.
aoqi@0 1856 call->make_cold();
aoqi@0 1857 continue;
aoqi@0 1858 }
aoqi@0 1859 call->make_hot();
aoqi@0 1860 calls_made_hot++;
aoqi@0 1861 amount_grown += est_size;
aoqi@0 1862 amount_to_grow -= est_size;
aoqi@0 1863 }
aoqi@0 1864
aoqi@0 1865 if (calls_made_hot > 0) set_major_progress();
aoqi@0 1866 return calls_made_hot;
aoqi@0 1867 }
aoqi@0 1868
aoqi@0 1869
aoqi@0 1870 //----------------------------Finish_Warm--------------------------------------
aoqi@0 1871 void Compile::Finish_Warm() {
aoqi@0 1872 if (!InlineWarmCalls) return;
aoqi@0 1873 if (failing()) return;
aoqi@0 1874 if (warm_calls() == NULL) return;
aoqi@0 1875
aoqi@0 1876 // Clean up loose ends, if we are out of space for inlining.
aoqi@0 1877 WarmCallInfo* call;
aoqi@0 1878 while ((call = pop_warm_call()) != NULL) {
aoqi@0 1879 call->make_cold();
aoqi@0 1880 }
aoqi@0 1881 }
aoqi@0 1882
aoqi@0 1883 //---------------------cleanup_loop_predicates-----------------------
aoqi@0 1884 // Remove the opaque nodes that protect the predicates so that all unused
aoqi@0 1885 // checks and uncommon_traps will be eliminated from the ideal graph
aoqi@0 1886 void Compile::cleanup_loop_predicates(PhaseIterGVN &igvn) {
aoqi@0 1887 if (predicate_count()==0) return;
aoqi@0 1888 for (int i = predicate_count(); i > 0; i--) {
aoqi@0 1889 Node * n = predicate_opaque1_node(i-1);
aoqi@0 1890 assert(n->Opcode() == Op_Opaque1, "must be");
aoqi@0 1891 igvn.replace_node(n, n->in(1));
aoqi@0 1892 }
aoqi@0 1893 assert(predicate_count()==0, "should be clean!");
aoqi@0 1894 }
aoqi@0 1895
aoqi@0 1896 // StringOpts and late inlining of string methods
aoqi@0 1897 void Compile::inline_string_calls(bool parse_time) {
aoqi@0 1898 {
aoqi@0 1899 // remove useless nodes to make the usage analysis simpler
aoqi@0 1900 ResourceMark rm;
aoqi@0 1901 PhaseRemoveUseless pru(initial_gvn(), for_igvn());
aoqi@0 1902 }
aoqi@0 1903
aoqi@0 1904 {
aoqi@0 1905 ResourceMark rm;
aoqi@0 1906 print_method(PHASE_BEFORE_STRINGOPTS, 3);
aoqi@0 1907 PhaseStringOpts pso(initial_gvn(), for_igvn());
aoqi@0 1908 print_method(PHASE_AFTER_STRINGOPTS, 3);
aoqi@0 1909 }
aoqi@0 1910
aoqi@0 1911 // now inline anything that we skipped the first time around
aoqi@0 1912 if (!parse_time) {
aoqi@0 1913 _late_inlines_pos = _late_inlines.length();
aoqi@0 1914 }
aoqi@0 1915
aoqi@0 1916 while (_string_late_inlines.length() > 0) {
aoqi@0 1917 CallGenerator* cg = _string_late_inlines.pop();
aoqi@0 1918 cg->do_late_inline();
aoqi@0 1919 if (failing()) return;
aoqi@0 1920 }
aoqi@0 1921 _string_late_inlines.trunc_to(0);
aoqi@0 1922 }
aoqi@0 1923
aoqi@0 1924 // Late inlining of boxing methods
aoqi@0 1925 void Compile::inline_boxing_calls(PhaseIterGVN& igvn) {
aoqi@0 1926 if (_boxing_late_inlines.length() > 0) {
aoqi@0 1927 assert(has_boxed_value(), "inconsistent");
aoqi@0 1928
aoqi@0 1929 PhaseGVN* gvn = initial_gvn();
aoqi@0 1930 set_inlining_incrementally(true);
aoqi@0 1931
aoqi@0 1932 assert( igvn._worklist.size() == 0, "should be done with igvn" );
aoqi@0 1933 for_igvn()->clear();
aoqi@0 1934 gvn->replace_with(&igvn);
aoqi@0 1935
roland@7041 1936 _late_inlines_pos = _late_inlines.length();
roland@7041 1937
aoqi@0 1938 while (_boxing_late_inlines.length() > 0) {
aoqi@0 1939 CallGenerator* cg = _boxing_late_inlines.pop();
aoqi@0 1940 cg->do_late_inline();
aoqi@0 1941 if (failing()) return;
aoqi@0 1942 }
aoqi@0 1943 _boxing_late_inlines.trunc_to(0);
aoqi@0 1944
aoqi@0 1945 {
aoqi@0 1946 ResourceMark rm;
aoqi@0 1947 PhaseRemoveUseless pru(gvn, for_igvn());
aoqi@0 1948 }
aoqi@0 1949
aoqi@0 1950 igvn = PhaseIterGVN(gvn);
aoqi@0 1951 igvn.optimize();
aoqi@0 1952
aoqi@0 1953 set_inlining_progress(false);
aoqi@0 1954 set_inlining_incrementally(false);
aoqi@0 1955 }
aoqi@0 1956 }
aoqi@0 1957
aoqi@0 1958 void Compile::inline_incrementally_one(PhaseIterGVN& igvn) {
aoqi@0 1959 assert(IncrementalInline, "incremental inlining should be on");
aoqi@0 1960 PhaseGVN* gvn = initial_gvn();
aoqi@0 1961
aoqi@0 1962 set_inlining_progress(false);
aoqi@0 1963 for_igvn()->clear();
aoqi@0 1964 gvn->replace_with(&igvn);
aoqi@0 1965
aoqi@0 1966 int i = 0;
aoqi@0 1967
aoqi@0 1968 for (; i <_late_inlines.length() && !inlining_progress(); i++) {
aoqi@0 1969 CallGenerator* cg = _late_inlines.at(i);
aoqi@0 1970 _late_inlines_pos = i+1;
aoqi@0 1971 cg->do_late_inline();
aoqi@0 1972 if (failing()) return;
aoqi@0 1973 }
aoqi@0 1974 int j = 0;
aoqi@0 1975 for (; i < _late_inlines.length(); i++, j++) {
aoqi@0 1976 _late_inlines.at_put(j, _late_inlines.at(i));
aoqi@0 1977 }
aoqi@0 1978 _late_inlines.trunc_to(j);
aoqi@0 1979
aoqi@0 1980 {
aoqi@0 1981 ResourceMark rm;
aoqi@0 1982 PhaseRemoveUseless pru(gvn, for_igvn());
aoqi@0 1983 }
aoqi@0 1984
aoqi@0 1985 igvn = PhaseIterGVN(gvn);
aoqi@0 1986 }
aoqi@0 1987
aoqi@0 1988 // Perform incremental inlining until bound on number of live nodes is reached
aoqi@0 1989 void Compile::inline_incrementally(PhaseIterGVN& igvn) {
aoqi@0 1990 PhaseGVN* gvn = initial_gvn();
aoqi@0 1991
aoqi@0 1992 set_inlining_incrementally(true);
aoqi@0 1993 set_inlining_progress(true);
aoqi@0 1994 uint low_live_nodes = 0;
aoqi@0 1995
aoqi@0 1996 while(inlining_progress() && _late_inlines.length() > 0) {
aoqi@0 1997
aoqi@0 1998 if (live_nodes() > (uint)LiveNodeCountInliningCutoff) {
aoqi@0 1999 if (low_live_nodes < (uint)LiveNodeCountInliningCutoff * 8 / 10) {
aoqi@0 2000 // PhaseIdealLoop is expensive so we only try it once we are
roland@7041 2001 // out of live nodes and we only try it again if the previous
roland@7041 2002 // helped got the number of nodes down significantly
aoqi@0 2003 PhaseIdealLoop ideal_loop( igvn, false, true );
aoqi@0 2004 if (failing()) return;
aoqi@0 2005 low_live_nodes = live_nodes();
aoqi@0 2006 _major_progress = true;
aoqi@0 2007 }
aoqi@0 2008
aoqi@0 2009 if (live_nodes() > (uint)LiveNodeCountInliningCutoff) {
aoqi@0 2010 break;
aoqi@0 2011 }
aoqi@0 2012 }
aoqi@0 2013
aoqi@0 2014 inline_incrementally_one(igvn);
aoqi@0 2015
aoqi@0 2016 if (failing()) return;
aoqi@0 2017
aoqi@0 2018 igvn.optimize();
aoqi@0 2019
aoqi@0 2020 if (failing()) return;
aoqi@0 2021 }
aoqi@0 2022
aoqi@0 2023 assert( igvn._worklist.size() == 0, "should be done with igvn" );
aoqi@0 2024
aoqi@0 2025 if (_string_late_inlines.length() > 0) {
aoqi@0 2026 assert(has_stringbuilder(), "inconsistent");
aoqi@0 2027 for_igvn()->clear();
aoqi@0 2028 initial_gvn()->replace_with(&igvn);
aoqi@0 2029
aoqi@0 2030 inline_string_calls(false);
aoqi@0 2031
aoqi@0 2032 if (failing()) return;
aoqi@0 2033
aoqi@0 2034 {
aoqi@0 2035 ResourceMark rm;
aoqi@0 2036 PhaseRemoveUseless pru(initial_gvn(), for_igvn());
aoqi@0 2037 }
aoqi@0 2038
aoqi@0 2039 igvn = PhaseIterGVN(gvn);
aoqi@0 2040
aoqi@0 2041 igvn.optimize();
aoqi@0 2042 }
aoqi@0 2043
aoqi@0 2044 set_inlining_incrementally(false);
aoqi@0 2045 }
aoqi@0 2046
aoqi@0 2047
aoqi@0 2048 //------------------------------Optimize---------------------------------------
aoqi@0 2049 // Given a graph, optimize it.
aoqi@0 2050 void Compile::Optimize() {
aoqi@0 2051 TracePhase t1("optimizer", &_t_optimizer, true);
aoqi@0 2052
aoqi@0 2053 #ifndef PRODUCT
aoqi@0 2054 if (env()->break_at_compile()) {
aoqi@0 2055 BREAKPOINT;
aoqi@0 2056 }
aoqi@0 2057
aoqi@0 2058 #endif
aoqi@0 2059
aoqi@0 2060 ResourceMark rm;
aoqi@0 2061 int loop_opts_cnt;
aoqi@0 2062
aoqi@0 2063 NOT_PRODUCT( verify_graph_edges(); )
aoqi@0 2064
aoqi@0 2065 print_method(PHASE_AFTER_PARSING);
aoqi@0 2066
aoqi@0 2067 {
aoqi@0 2068 // Iterative Global Value Numbering, including ideal transforms
aoqi@0 2069 // Initialize IterGVN with types and values from parse-time GVN
aoqi@0 2070 PhaseIterGVN igvn(initial_gvn());
aoqi@0 2071 {
aoqi@0 2072 NOT_PRODUCT( TracePhase t2("iterGVN", &_t_iterGVN, TimeCompiler); )
aoqi@0 2073 igvn.optimize();
aoqi@0 2074 }
aoqi@0 2075
aoqi@0 2076 print_method(PHASE_ITER_GVN1, 2);
aoqi@0 2077
aoqi@0 2078 if (failing()) return;
aoqi@0 2079
aoqi@0 2080 {
aoqi@0 2081 NOT_PRODUCT( TracePhase t2("incrementalInline", &_t_incrInline, TimeCompiler); )
aoqi@0 2082 inline_incrementally(igvn);
aoqi@0 2083 }
aoqi@0 2084
aoqi@0 2085 print_method(PHASE_INCREMENTAL_INLINE, 2);
aoqi@0 2086
aoqi@0 2087 if (failing()) return;
aoqi@0 2088
aoqi@0 2089 if (eliminate_boxing()) {
aoqi@0 2090 NOT_PRODUCT( TracePhase t2("incrementalInline", &_t_incrInline, TimeCompiler); )
aoqi@0 2091 // Inline valueOf() methods now.
aoqi@0 2092 inline_boxing_calls(igvn);
aoqi@0 2093
roland@7041 2094 if (AlwaysIncrementalInline) {
roland@7041 2095 inline_incrementally(igvn);
roland@7041 2096 }
roland@7041 2097
aoqi@0 2098 print_method(PHASE_INCREMENTAL_BOXING_INLINE, 2);
aoqi@0 2099
aoqi@0 2100 if (failing()) return;
aoqi@0 2101 }
aoqi@0 2102
aoqi@0 2103 // Remove the speculative part of types and clean up the graph from
aoqi@0 2104 // the extra CastPP nodes whose only purpose is to carry them. Do
aoqi@0 2105 // that early so that optimizations are not disrupted by the extra
aoqi@0 2106 // CastPP nodes.
aoqi@0 2107 remove_speculative_types(igvn);
aoqi@0 2108
aoqi@0 2109 // No more new expensive nodes will be added to the list from here
aoqi@0 2110 // so keep only the actual candidates for optimizations.
aoqi@0 2111 cleanup_expensive_nodes(igvn);
aoqi@0 2112
aoqi@0 2113 // Perform escape analysis
aoqi@0 2114 if (_do_escape_analysis && ConnectionGraph::has_candidates(this)) {
aoqi@0 2115 if (has_loops()) {
aoqi@0 2116 // Cleanup graph (remove dead nodes).
aoqi@0 2117 TracePhase t2("idealLoop", &_t_idealLoop, true);
aoqi@0 2118 PhaseIdealLoop ideal_loop( igvn, false, true );
aoqi@0 2119 if (major_progress()) print_method(PHASE_PHASEIDEAL_BEFORE_EA, 2);
aoqi@0 2120 if (failing()) return;
aoqi@0 2121 }
aoqi@0 2122 ConnectionGraph::do_analysis(this, &igvn);
aoqi@0 2123
aoqi@0 2124 if (failing()) return;
aoqi@0 2125
aoqi@0 2126 // Optimize out fields loads from scalar replaceable allocations.
aoqi@0 2127 igvn.optimize();
aoqi@0 2128 print_method(PHASE_ITER_GVN_AFTER_EA, 2);
aoqi@0 2129
aoqi@0 2130 if (failing()) return;
aoqi@0 2131
aoqi@0 2132 if (congraph() != NULL && macro_count() > 0) {
aoqi@0 2133 NOT_PRODUCT( TracePhase t2("macroEliminate", &_t_macroEliminate, TimeCompiler); )
aoqi@0 2134 PhaseMacroExpand mexp(igvn);
aoqi@0 2135 mexp.eliminate_macro_nodes();
aoqi@0 2136 igvn.set_delay_transform(false);
aoqi@0 2137
aoqi@0 2138 igvn.optimize();
aoqi@0 2139 print_method(PHASE_ITER_GVN_AFTER_ELIMINATION, 2);
aoqi@0 2140
aoqi@0 2141 if (failing()) return;
aoqi@0 2142 }
aoqi@0 2143 }
aoqi@0 2144
aoqi@0 2145 // Loop transforms on the ideal graph. Range Check Elimination,
aoqi@0 2146 // peeling, unrolling, etc.
aoqi@0 2147
aoqi@0 2148 // Set loop opts counter
aoqi@0 2149 loop_opts_cnt = num_loop_opts();
aoqi@0 2150 if((loop_opts_cnt > 0) && (has_loops() || has_split_ifs())) {
aoqi@0 2151 {
aoqi@0 2152 TracePhase t2("idealLoop", &_t_idealLoop, true);
aoqi@0 2153 PhaseIdealLoop ideal_loop( igvn, true );
aoqi@0 2154 loop_opts_cnt--;
aoqi@0 2155 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP1, 2);
aoqi@0 2156 if (failing()) return;
aoqi@0 2157 }
aoqi@0 2158 // Loop opts pass if partial peeling occurred in previous pass
aoqi@0 2159 if(PartialPeelLoop && major_progress() && (loop_opts_cnt > 0)) {
aoqi@0 2160 TracePhase t3("idealLoop", &_t_idealLoop, true);
aoqi@0 2161 PhaseIdealLoop ideal_loop( igvn, false );
aoqi@0 2162 loop_opts_cnt--;
aoqi@0 2163 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP2, 2);
aoqi@0 2164 if (failing()) return;
aoqi@0 2165 }
aoqi@0 2166 // Loop opts pass for loop-unrolling before CCP
aoqi@0 2167 if(major_progress() && (loop_opts_cnt > 0)) {
aoqi@0 2168 TracePhase t4("idealLoop", &_t_idealLoop, true);
aoqi@0 2169 PhaseIdealLoop ideal_loop( igvn, false );
aoqi@0 2170 loop_opts_cnt--;
aoqi@0 2171 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP3, 2);
aoqi@0 2172 }
aoqi@0 2173 if (!failing()) {
aoqi@0 2174 // Verify that last round of loop opts produced a valid graph
aoqi@0 2175 NOT_PRODUCT( TracePhase t2("idealLoopVerify", &_t_idealLoopVerify, TimeCompiler); )
aoqi@0 2176 PhaseIdealLoop::verify(igvn);
aoqi@0 2177 }
aoqi@0 2178 }
aoqi@0 2179 if (failing()) return;
aoqi@0 2180
aoqi@0 2181 // Conditional Constant Propagation;
aoqi@0 2182 PhaseCCP ccp( &igvn );
aoqi@0 2183 assert( true, "Break here to ccp.dump_nodes_and_types(_root,999,1)");
aoqi@0 2184 {
aoqi@0 2185 TracePhase t2("ccp", &_t_ccp, true);
aoqi@0 2186 ccp.do_transform();
aoqi@0 2187 }
aoqi@0 2188 print_method(PHASE_CPP1, 2);
aoqi@0 2189
aoqi@0 2190 assert( true, "Break here to ccp.dump_old2new_map()");
aoqi@0 2191
aoqi@0 2192 // Iterative Global Value Numbering, including ideal transforms
aoqi@0 2193 {
aoqi@0 2194 NOT_PRODUCT( TracePhase t2("iterGVN2", &_t_iterGVN2, TimeCompiler); )
aoqi@0 2195 igvn = ccp;
aoqi@0 2196 igvn.optimize();
aoqi@0 2197 }
aoqi@0 2198
aoqi@0 2199 print_method(PHASE_ITER_GVN2, 2);
aoqi@0 2200
aoqi@0 2201 if (failing()) return;
aoqi@0 2202
aoqi@0 2203 // Loop transforms on the ideal graph. Range Check Elimination,
aoqi@0 2204 // peeling, unrolling, etc.
aoqi@0 2205 if(loop_opts_cnt > 0) {
aoqi@0 2206 debug_only( int cnt = 0; );
aoqi@0 2207 while(major_progress() && (loop_opts_cnt > 0)) {
aoqi@0 2208 TracePhase t2("idealLoop", &_t_idealLoop, true);
aoqi@0 2209 assert( cnt++ < 40, "infinite cycle in loop optimization" );
aoqi@0 2210 PhaseIdealLoop ideal_loop( igvn, true);
aoqi@0 2211 loop_opts_cnt--;
aoqi@0 2212 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP_ITERATIONS, 2);
aoqi@0 2213 if (failing()) return;
aoqi@0 2214 }
aoqi@0 2215 }
aoqi@0 2216
aoqi@0 2217 {
aoqi@0 2218 // Verify that all previous optimizations produced a valid graph
aoqi@0 2219 // at least to this point, even if no loop optimizations were done.
aoqi@0 2220 NOT_PRODUCT( TracePhase t2("idealLoopVerify", &_t_idealLoopVerify, TimeCompiler); )
aoqi@0 2221 PhaseIdealLoop::verify(igvn);
aoqi@0 2222 }
aoqi@0 2223
aoqi@0 2224 {
aoqi@0 2225 NOT_PRODUCT( TracePhase t2("macroExpand", &_t_macroExpand, TimeCompiler); )
aoqi@0 2226 PhaseMacroExpand mex(igvn);
aoqi@0 2227 if (mex.expand_macro_nodes()) {
aoqi@0 2228 assert(failing(), "must bail out w/ explicit message");
aoqi@0 2229 return;
aoqi@0 2230 }
aoqi@0 2231 }
aoqi@0 2232
aoqi@0 2233 } // (End scope of igvn; run destructor if necessary for asserts.)
aoqi@0 2234
aoqi@0 2235 dump_inlining();
aoqi@0 2236 // A method with only infinite loops has no edges entering loops from root
aoqi@0 2237 {
aoqi@0 2238 NOT_PRODUCT( TracePhase t2("graphReshape", &_t_graphReshaping, TimeCompiler); )
aoqi@0 2239 if (final_graph_reshaping()) {
aoqi@0 2240 assert(failing(), "must bail out w/ explicit message");
aoqi@0 2241 return;
aoqi@0 2242 }
aoqi@0 2243 }
aoqi@0 2244
aoqi@0 2245 print_method(PHASE_OPTIMIZE_FINISHED, 2);
aoqi@0 2246 }
aoqi@0 2247
aoqi@0 2248
aoqi@0 2249 //------------------------------Code_Gen---------------------------------------
aoqi@0 2250 // Given a graph, generate code for it
aoqi@0 2251 void Compile::Code_Gen() {
aoqi@0 2252 if (failing()) {
aoqi@0 2253 return;
aoqi@0 2254 }
aoqi@0 2255
aoqi@0 2256 // Perform instruction selection. You might think we could reclaim Matcher
aoqi@0 2257 // memory PDQ, but actually the Matcher is used in generating spill code.
aoqi@0 2258 // Internals of the Matcher (including some VectorSets) must remain live
aoqi@0 2259 // for awhile - thus I cannot reclaim Matcher memory lest a VectorSet usage
aoqi@0 2260 // set a bit in reclaimed memory.
aoqi@0 2261
aoqi@0 2262 // In debug mode can dump m._nodes.dump() for mapping of ideal to machine
aoqi@0 2263 // nodes. Mapping is only valid at the root of each matched subtree.
aoqi@0 2264 NOT_PRODUCT( verify_graph_edges(); )
aoqi@0 2265
aoqi@0 2266 Matcher matcher;
aoqi@0 2267 _matcher = &matcher;
aoqi@0 2268 {
aoqi@0 2269 TracePhase t2("matcher", &_t_matcher, true);
aoqi@0 2270 matcher.match();
aoqi@0 2271 }
aoqi@0 2272 // In debug mode can dump m._nodes.dump() for mapping of ideal to machine
aoqi@0 2273 // nodes. Mapping is only valid at the root of each matched subtree.
aoqi@0 2274 NOT_PRODUCT( verify_graph_edges(); )
aoqi@0 2275
aoqi@0 2276 // If you have too many nodes, or if matching has failed, bail out
aoqi@0 2277 check_node_count(0, "out of nodes matching instructions");
aoqi@0 2278 if (failing()) {
aoqi@0 2279 return;
aoqi@0 2280 }
aoqi@0 2281
aoqi@0 2282 // Build a proper-looking CFG
aoqi@0 2283 PhaseCFG cfg(node_arena(), root(), matcher);
aoqi@0 2284 _cfg = &cfg;
aoqi@0 2285 {
aoqi@0 2286 NOT_PRODUCT( TracePhase t2("scheduler", &_t_scheduler, TimeCompiler); )
aoqi@0 2287 bool success = cfg.do_global_code_motion();
aoqi@0 2288 if (!success) {
aoqi@0 2289 return;
aoqi@0 2290 }
aoqi@0 2291
aoqi@0 2292 print_method(PHASE_GLOBAL_CODE_MOTION, 2);
aoqi@0 2293 NOT_PRODUCT( verify_graph_edges(); )
aoqi@0 2294 debug_only( cfg.verify(); )
aoqi@0 2295 }
aoqi@0 2296
aoqi@0 2297 PhaseChaitin regalloc(unique(), cfg, matcher);
aoqi@0 2298 _regalloc = &regalloc;
aoqi@0 2299 {
aoqi@0 2300 TracePhase t2("regalloc", &_t_registerAllocation, true);
aoqi@0 2301 // Perform register allocation. After Chaitin, use-def chains are
aoqi@0 2302 // no longer accurate (at spill code) and so must be ignored.
aoqi@0 2303 // Node->LRG->reg mappings are still accurate.
aoqi@0 2304 _regalloc->Register_Allocate();
aoqi@0 2305
aoqi@0 2306 // Bail out if the allocator builds too many nodes
aoqi@0 2307 if (failing()) {
aoqi@0 2308 return;
aoqi@0 2309 }
aoqi@0 2310 }
aoqi@0 2311
aoqi@0 2312 // Prior to register allocation we kept empty basic blocks in case the
aoqi@0 2313 // the allocator needed a place to spill. After register allocation we
aoqi@0 2314 // are not adding any new instructions. If any basic block is empty, we
aoqi@0 2315 // can now safely remove it.
aoqi@0 2316 {
aoqi@0 2317 NOT_PRODUCT( TracePhase t2("blockOrdering", &_t_blockOrdering, TimeCompiler); )
aoqi@0 2318 cfg.remove_empty_blocks();
aoqi@0 2319 if (do_freq_based_layout()) {
aoqi@0 2320 PhaseBlockLayout layout(cfg);
aoqi@0 2321 } else {
aoqi@0 2322 cfg.set_loop_alignment();
aoqi@0 2323 }
aoqi@0 2324 cfg.fixup_flow();
aoqi@0 2325 }
aoqi@0 2326
aoqi@0 2327 // Apply peephole optimizations
aoqi@0 2328 if( OptoPeephole ) {
aoqi@0 2329 NOT_PRODUCT( TracePhase t2("peephole", &_t_peephole, TimeCompiler); )
aoqi@0 2330 PhasePeephole peep( _regalloc, cfg);
aoqi@0 2331 peep.do_transform();
aoqi@0 2332 }
aoqi@0 2333
aoqi@0 2334 // Do late expand if CPU requires this.
aoqi@0 2335 if (Matcher::require_postalloc_expand) {
aoqi@0 2336 NOT_PRODUCT(TracePhase t2c("postalloc_expand", &_t_postalloc_expand, true));
aoqi@0 2337 cfg.postalloc_expand(_regalloc);
aoqi@0 2338 }
aoqi@0 2339
aoqi@0 2340 // Convert Nodes to instruction bits in a buffer
aoqi@0 2341 {
aoqi@0 2342 // %%%% workspace merge brought two timers together for one job
aoqi@0 2343 TracePhase t2a("output", &_t_output, true);
aoqi@0 2344 NOT_PRODUCT( TraceTime t2b(NULL, &_t_codeGeneration, TimeCompiler, false); )
aoqi@0 2345 Output();
aoqi@0 2346 }
aoqi@0 2347
aoqi@0 2348 print_method(PHASE_FINAL_CODE);
aoqi@0 2349
aoqi@0 2350 // He's dead, Jim.
aoqi@0 2351 _cfg = (PhaseCFG*)0xdeadbeef;
aoqi@0 2352 _regalloc = (PhaseChaitin*)0xdeadbeef;
aoqi@0 2353 }
aoqi@0 2354
aoqi@0 2355
aoqi@0 2356 //------------------------------dump_asm---------------------------------------
aoqi@0 2357 // Dump formatted assembly
aoqi@0 2358 #ifndef PRODUCT
aoqi@0 2359 void Compile::dump_asm(int *pcs, uint pc_limit) {
aoqi@0 2360 bool cut_short = false;
aoqi@0 2361 tty->print_cr("#");
aoqi@0 2362 tty->print("# "); _tf->dump(); tty->cr();
aoqi@0 2363 tty->print_cr("#");
aoqi@0 2364
aoqi@0 2365 // For all blocks
aoqi@0 2366 int pc = 0x0; // Program counter
aoqi@0 2367 char starts_bundle = ' ';
aoqi@0 2368 _regalloc->dump_frame();
aoqi@0 2369
aoqi@0 2370 Node *n = NULL;
aoqi@0 2371 for (uint i = 0; i < _cfg->number_of_blocks(); i++) {
aoqi@0 2372 if (VMThread::should_terminate()) {
aoqi@0 2373 cut_short = true;
aoqi@0 2374 break;
aoqi@0 2375 }
aoqi@0 2376 Block* block = _cfg->get_block(i);
aoqi@0 2377 if (block->is_connector() && !Verbose) {
aoqi@0 2378 continue;
aoqi@0 2379 }
aoqi@0 2380 n = block->head();
aoqi@0 2381 if (pcs && n->_idx < pc_limit) {
aoqi@0 2382 tty->print("%3.3x ", pcs[n->_idx]);
aoqi@0 2383 } else {
aoqi@0 2384 tty->print(" ");
aoqi@0 2385 }
aoqi@0 2386 block->dump_head(_cfg);
aoqi@0 2387 if (block->is_connector()) {
aoqi@0 2388 tty->print_cr(" # Empty connector block");
aoqi@0 2389 } else if (block->num_preds() == 2 && block->pred(1)->is_CatchProj() && block->pred(1)->as_CatchProj()->_con == CatchProjNode::fall_through_index) {
aoqi@0 2390 tty->print_cr(" # Block is sole successor of call");
aoqi@0 2391 }
aoqi@0 2392
aoqi@0 2393 // For all instructions
aoqi@0 2394 Node *delay = NULL;
aoqi@0 2395 for (uint j = 0; j < block->number_of_nodes(); j++) {
aoqi@0 2396 if (VMThread::should_terminate()) {
aoqi@0 2397 cut_short = true;
aoqi@0 2398 break;
aoqi@0 2399 }
aoqi@0 2400 n = block->get_node(j);
aoqi@0 2401 if (valid_bundle_info(n)) {
aoqi@0 2402 Bundle* bundle = node_bundling(n);
aoqi@0 2403 if (bundle->used_in_unconditional_delay()) {
aoqi@0 2404 delay = n;
aoqi@0 2405 continue;
aoqi@0 2406 }
aoqi@0 2407 if (bundle->starts_bundle()) {
aoqi@0 2408 starts_bundle = '+';
aoqi@0 2409 }
aoqi@0 2410 }
aoqi@0 2411
aoqi@0 2412 if (WizardMode) {
aoqi@0 2413 n->dump();
aoqi@0 2414 }
aoqi@0 2415
aoqi@0 2416 if( !n->is_Region() && // Dont print in the Assembly
aoqi@0 2417 !n->is_Phi() && // a few noisely useless nodes
aoqi@0 2418 !n->is_Proj() &&
aoqi@0 2419 !n->is_MachTemp() &&
aoqi@0 2420 !n->is_SafePointScalarObject() &&
aoqi@0 2421 !n->is_Catch() && // Would be nice to print exception table targets
aoqi@0 2422 !n->is_MergeMem() && // Not very interesting
aoqi@0 2423 !n->is_top() && // Debug info table constants
aoqi@0 2424 !(n->is_Con() && !n->is_Mach())// Debug info table constants
aoqi@0 2425 ) {
aoqi@0 2426 if (pcs && n->_idx < pc_limit)
aoqi@0 2427 tty->print("%3.3x", pcs[n->_idx]);
aoqi@0 2428 else
aoqi@0 2429 tty->print(" ");
aoqi@0 2430 tty->print(" %c ", starts_bundle);
aoqi@0 2431 starts_bundle = ' ';
aoqi@0 2432 tty->print("\t");
aoqi@0 2433 n->format(_regalloc, tty);
aoqi@0 2434 tty->cr();
aoqi@0 2435 }
aoqi@0 2436
aoqi@0 2437 // If we have an instruction with a delay slot, and have seen a delay,
aoqi@0 2438 // then back up and print it
aoqi@0 2439 if (valid_bundle_info(n) && node_bundling(n)->use_unconditional_delay()) {
aoqi@0 2440 assert(delay != NULL, "no unconditional delay instruction");
aoqi@0 2441 if (WizardMode) delay->dump();
aoqi@0 2442
aoqi@0 2443 if (node_bundling(delay)->starts_bundle())
aoqi@0 2444 starts_bundle = '+';
aoqi@0 2445 if (pcs && n->_idx < pc_limit)
aoqi@0 2446 tty->print("%3.3x", pcs[n->_idx]);
aoqi@0 2447 else
aoqi@0 2448 tty->print(" ");
aoqi@0 2449 tty->print(" %c ", starts_bundle);
aoqi@0 2450 starts_bundle = ' ';
aoqi@0 2451 tty->print("\t");
aoqi@0 2452 delay->format(_regalloc, tty);
aoqi@0 2453 tty->cr();
aoqi@0 2454 delay = NULL;
aoqi@0 2455 }
aoqi@0 2456
aoqi@0 2457 // Dump the exception table as well
aoqi@0 2458 if( n->is_Catch() && (Verbose || WizardMode) ) {
aoqi@0 2459 // Print the exception table for this offset
aoqi@0 2460 _handler_table.print_subtable_for(pc);
aoqi@0 2461 }
aoqi@0 2462 }
aoqi@0 2463
aoqi@0 2464 if (pcs && n->_idx < pc_limit)
aoqi@0 2465 tty->print_cr("%3.3x", pcs[n->_idx]);
aoqi@0 2466 else
aoqi@0 2467 tty->cr();
aoqi@0 2468
aoqi@0 2469 assert(cut_short || delay == NULL, "no unconditional delay branch");
aoqi@0 2470
aoqi@0 2471 } // End of per-block dump
aoqi@0 2472 tty->cr();
aoqi@0 2473
aoqi@0 2474 if (cut_short) tty->print_cr("*** disassembly is cut short ***");
aoqi@0 2475 }
aoqi@0 2476 #endif
aoqi@0 2477
aoqi@0 2478 //------------------------------Final_Reshape_Counts---------------------------
aoqi@0 2479 // This class defines counters to help identify when a method
aoqi@0 2480 // may/must be executed using hardware with only 24-bit precision.
aoqi@0 2481 struct Final_Reshape_Counts : public StackObj {
aoqi@0 2482 int _call_count; // count non-inlined 'common' calls
aoqi@0 2483 int _float_count; // count float ops requiring 24-bit precision
aoqi@0 2484 int _double_count; // count double ops requiring more precision
aoqi@0 2485 int _java_call_count; // count non-inlined 'java' calls
aoqi@0 2486 int _inner_loop_count; // count loops which need alignment
aoqi@0 2487 VectorSet _visited; // Visitation flags
aoqi@0 2488 Node_List _tests; // Set of IfNodes & PCTableNodes
aoqi@0 2489
aoqi@0 2490 Final_Reshape_Counts() :
aoqi@0 2491 _call_count(0), _float_count(0), _double_count(0),
aoqi@0 2492 _java_call_count(0), _inner_loop_count(0),
aoqi@0 2493 _visited( Thread::current()->resource_area() ) { }
aoqi@0 2494
aoqi@0 2495 void inc_call_count () { _call_count ++; }
aoqi@0 2496 void inc_float_count () { _float_count ++; }
aoqi@0 2497 void inc_double_count() { _double_count++; }
aoqi@0 2498 void inc_java_call_count() { _java_call_count++; }
aoqi@0 2499 void inc_inner_loop_count() { _inner_loop_count++; }
aoqi@0 2500
aoqi@0 2501 int get_call_count () const { return _call_count ; }
aoqi@0 2502 int get_float_count () const { return _float_count ; }
aoqi@0 2503 int get_double_count() const { return _double_count; }
aoqi@0 2504 int get_java_call_count() const { return _java_call_count; }
aoqi@0 2505 int get_inner_loop_count() const { return _inner_loop_count; }
aoqi@0 2506 };
aoqi@0 2507
aoqi@0 2508 #ifdef ASSERT
aoqi@0 2509 static bool oop_offset_is_sane(const TypeInstPtr* tp) {
aoqi@0 2510 ciInstanceKlass *k = tp->klass()->as_instance_klass();
aoqi@0 2511 // Make sure the offset goes inside the instance layout.
aoqi@0 2512 return k->contains_field_offset(tp->offset());
aoqi@0 2513 // Note that OffsetBot and OffsetTop are very negative.
aoqi@0 2514 }
aoqi@0 2515 #endif
aoqi@0 2516
aoqi@0 2517 // Eliminate trivially redundant StoreCMs and accumulate their
aoqi@0 2518 // precedence edges.
aoqi@0 2519 void Compile::eliminate_redundant_card_marks(Node* n) {
aoqi@0 2520 assert(n->Opcode() == Op_StoreCM, "expected StoreCM");
aoqi@0 2521 if (n->in(MemNode::Address)->outcnt() > 1) {
aoqi@0 2522 // There are multiple users of the same address so it might be
aoqi@0 2523 // possible to eliminate some of the StoreCMs
aoqi@0 2524 Node* mem = n->in(MemNode::Memory);
aoqi@0 2525 Node* adr = n->in(MemNode::Address);
aoqi@0 2526 Node* val = n->in(MemNode::ValueIn);
aoqi@0 2527 Node* prev = n;
aoqi@0 2528 bool done = false;
aoqi@0 2529 // Walk the chain of StoreCMs eliminating ones that match. As
aoqi@0 2530 // long as it's a chain of single users then the optimization is
aoqi@0 2531 // safe. Eliminating partially redundant StoreCMs would require
aoqi@0 2532 // cloning copies down the other paths.
aoqi@0 2533 while (mem->Opcode() == Op_StoreCM && mem->outcnt() == 1 && !done) {
aoqi@0 2534 if (adr == mem->in(MemNode::Address) &&
aoqi@0 2535 val == mem->in(MemNode::ValueIn)) {
aoqi@0 2536 // redundant StoreCM
aoqi@0 2537 if (mem->req() > MemNode::OopStore) {
aoqi@0 2538 // Hasn't been processed by this code yet.
aoqi@0 2539 n->add_prec(mem->in(MemNode::OopStore));
aoqi@0 2540 } else {
aoqi@0 2541 // Already converted to precedence edge
aoqi@0 2542 for (uint i = mem->req(); i < mem->len(); i++) {
aoqi@0 2543 // Accumulate any precedence edges
aoqi@0 2544 if (mem->in(i) != NULL) {
aoqi@0 2545 n->add_prec(mem->in(i));
aoqi@0 2546 }
aoqi@0 2547 }
aoqi@0 2548 // Everything above this point has been processed.
aoqi@0 2549 done = true;
aoqi@0 2550 }
aoqi@0 2551 // Eliminate the previous StoreCM
aoqi@0 2552 prev->set_req(MemNode::Memory, mem->in(MemNode::Memory));
aoqi@0 2553 assert(mem->outcnt() == 0, "should be dead");
aoqi@0 2554 mem->disconnect_inputs(NULL, this);
aoqi@0 2555 } else {
aoqi@0 2556 prev = mem;
aoqi@0 2557 }
aoqi@0 2558 mem = prev->in(MemNode::Memory);
aoqi@0 2559 }
aoqi@0 2560 }
aoqi@0 2561 }
aoqi@0 2562
aoqi@0 2563 //------------------------------final_graph_reshaping_impl----------------------
aoqi@0 2564 // Implement items 1-5 from final_graph_reshaping below.
aoqi@0 2565 void Compile::final_graph_reshaping_impl( Node *n, Final_Reshape_Counts &frc) {
aoqi@0 2566
aoqi@0 2567 if ( n->outcnt() == 0 ) return; // dead node
aoqi@0 2568 uint nop = n->Opcode();
aoqi@0 2569
aoqi@0 2570 // Check for 2-input instruction with "last use" on right input.
aoqi@0 2571 // Swap to left input. Implements item (2).
aoqi@0 2572 if( n->req() == 3 && // two-input instruction
aoqi@0 2573 n->in(1)->outcnt() > 1 && // left use is NOT a last use
aoqi@0 2574 (!n->in(1)->is_Phi() || n->in(1)->in(2) != n) && // it is not data loop
aoqi@0 2575 n->in(2)->outcnt() == 1 &&// right use IS a last use
aoqi@0 2576 !n->in(2)->is_Con() ) { // right use is not a constant
aoqi@0 2577 // Check for commutative opcode
aoqi@0 2578 switch( nop ) {
aoqi@0 2579 case Op_AddI: case Op_AddF: case Op_AddD: case Op_AddL:
aoqi@0 2580 case Op_MaxI: case Op_MinI:
aoqi@0 2581 case Op_MulI: case Op_MulF: case Op_MulD: case Op_MulL:
aoqi@0 2582 case Op_AndL: case Op_XorL: case Op_OrL:
aoqi@0 2583 case Op_AndI: case Op_XorI: case Op_OrI: {
aoqi@0 2584 // Move "last use" input to left by swapping inputs
aoqi@0 2585 n->swap_edges(1, 2);
aoqi@0 2586 break;
aoqi@0 2587 }
aoqi@0 2588 default:
aoqi@0 2589 break;
aoqi@0 2590 }
aoqi@0 2591 }
aoqi@0 2592
aoqi@0 2593 #ifdef ASSERT
aoqi@0 2594 if( n->is_Mem() ) {
aoqi@0 2595 int alias_idx = get_alias_index(n->as_Mem()->adr_type());
aoqi@0 2596 assert( n->in(0) != NULL || alias_idx != Compile::AliasIdxRaw ||
aoqi@0 2597 // oop will be recorded in oop map if load crosses safepoint
aoqi@0 2598 n->is_Load() && (n->as_Load()->bottom_type()->isa_oopptr() ||
aoqi@0 2599 LoadNode::is_immutable_value(n->in(MemNode::Address))),
aoqi@0 2600 "raw memory operations should have control edge");
aoqi@0 2601 }
aoqi@0 2602 #endif
aoqi@0 2603 // Count FPU ops and common calls, implements item (3)
aoqi@0 2604 switch( nop ) {
aoqi@0 2605 // Count all float operations that may use FPU
aoqi@0 2606 case Op_AddF:
aoqi@0 2607 case Op_SubF:
aoqi@0 2608 case Op_MulF:
aoqi@0 2609 case Op_DivF:
aoqi@0 2610 case Op_NegF:
aoqi@0 2611 case Op_ModF:
aoqi@0 2612 case Op_ConvI2F:
aoqi@0 2613 case Op_ConF:
aoqi@0 2614 case Op_CmpF:
aoqi@0 2615 case Op_CmpF3:
aoqi@0 2616 // case Op_ConvL2F: // longs are split into 32-bit halves
aoqi@0 2617 frc.inc_float_count();
aoqi@0 2618 break;
aoqi@0 2619
aoqi@0 2620 case Op_ConvF2D:
aoqi@0 2621 case Op_ConvD2F:
aoqi@0 2622 frc.inc_float_count();
aoqi@0 2623 frc.inc_double_count();
aoqi@0 2624 break;
aoqi@0 2625
aoqi@0 2626 // Count all double operations that may use FPU
aoqi@0 2627 case Op_AddD:
aoqi@0 2628 case Op_SubD:
aoqi@0 2629 case Op_MulD:
aoqi@0 2630 case Op_DivD:
aoqi@0 2631 case Op_NegD:
aoqi@0 2632 case Op_ModD:
aoqi@0 2633 case Op_ConvI2D:
aoqi@0 2634 case Op_ConvD2I:
aoqi@0 2635 // case Op_ConvL2D: // handled by leaf call
aoqi@0 2636 // case Op_ConvD2L: // handled by leaf call
aoqi@0 2637 case Op_ConD:
aoqi@0 2638 case Op_CmpD:
aoqi@0 2639 case Op_CmpD3:
aoqi@0 2640 frc.inc_double_count();
aoqi@0 2641 break;
aoqi@0 2642 case Op_Opaque1: // Remove Opaque Nodes before matching
aoqi@0 2643 case Op_Opaque2: // Remove Opaque Nodes before matching
aoqi@0 2644 case Op_Opaque3:
aoqi@0 2645 n->subsume_by(n->in(1), this);
aoqi@0 2646 break;
aoqi@0 2647 case Op_CallStaticJava:
aoqi@0 2648 case Op_CallJava:
aoqi@0 2649 case Op_CallDynamicJava:
aoqi@0 2650 frc.inc_java_call_count(); // Count java call site;
aoqi@0 2651 case Op_CallRuntime:
aoqi@0 2652 case Op_CallLeaf:
aoqi@0 2653 case Op_CallLeafNoFP: {
aoqi@0 2654 assert( n->is_Call(), "" );
aoqi@0 2655 CallNode *call = n->as_Call();
aoqi@0 2656 // Count call sites where the FP mode bit would have to be flipped.
aoqi@0 2657 // Do not count uncommon runtime calls:
aoqi@0 2658 // uncommon_trap, _complete_monitor_locking, _complete_monitor_unlocking,
aoqi@0 2659 // _new_Java, _new_typeArray, _new_objArray, _rethrow_Java, ...
aoqi@0 2660 if( !call->is_CallStaticJava() || !call->as_CallStaticJava()->_name ) {
aoqi@0 2661 frc.inc_call_count(); // Count the call site
aoqi@0 2662 } else { // See if uncommon argument is shared
aoqi@0 2663 Node *n = call->in(TypeFunc::Parms);
aoqi@0 2664 int nop = n->Opcode();
aoqi@0 2665 // Clone shared simple arguments to uncommon calls, item (1).
aoqi@0 2666 if( n->outcnt() > 1 &&
aoqi@0 2667 !n->is_Proj() &&
aoqi@0 2668 nop != Op_CreateEx &&
aoqi@0 2669 nop != Op_CheckCastPP &&
aoqi@0 2670 nop != Op_DecodeN &&
aoqi@0 2671 nop != Op_DecodeNKlass &&
aoqi@0 2672 !n->is_Mem() ) {
aoqi@0 2673 Node *x = n->clone();
aoqi@0 2674 call->set_req( TypeFunc::Parms, x );
aoqi@0 2675 }
aoqi@0 2676 }
aoqi@0 2677 break;
aoqi@0 2678 }
aoqi@0 2679
aoqi@0 2680 case Op_StoreD:
aoqi@0 2681 case Op_LoadD:
aoqi@0 2682 case Op_LoadD_unaligned:
aoqi@0 2683 frc.inc_double_count();
aoqi@0 2684 goto handle_mem;
aoqi@0 2685 case Op_StoreF:
aoqi@0 2686 case Op_LoadF:
aoqi@0 2687 frc.inc_float_count();
aoqi@0 2688 goto handle_mem;
aoqi@0 2689
aoqi@0 2690 case Op_StoreCM:
aoqi@0 2691 {
aoqi@0 2692 // Convert OopStore dependence into precedence edge
aoqi@0 2693 Node* prec = n->in(MemNode::OopStore);
aoqi@0 2694 n->del_req(MemNode::OopStore);
aoqi@0 2695 n->add_prec(prec);
aoqi@0 2696 eliminate_redundant_card_marks(n);
aoqi@0 2697 }
aoqi@0 2698
aoqi@0 2699 // fall through
aoqi@0 2700
aoqi@0 2701 case Op_StoreB:
aoqi@0 2702 case Op_StoreC:
aoqi@0 2703 case Op_StorePConditional:
aoqi@0 2704 case Op_StoreI:
aoqi@0 2705 case Op_StoreL:
aoqi@0 2706 case Op_StoreIConditional:
aoqi@0 2707 case Op_StoreLConditional:
aoqi@0 2708 case Op_CompareAndSwapI:
aoqi@0 2709 case Op_CompareAndSwapL:
aoqi@0 2710 case Op_CompareAndSwapP:
aoqi@0 2711 case Op_CompareAndSwapN:
aoqi@0 2712 case Op_GetAndAddI:
aoqi@0 2713 case Op_GetAndAddL:
aoqi@0 2714 case Op_GetAndSetI:
aoqi@0 2715 case Op_GetAndSetL:
aoqi@0 2716 case Op_GetAndSetP:
aoqi@0 2717 case Op_GetAndSetN:
aoqi@0 2718 case Op_StoreP:
aoqi@0 2719 case Op_StoreN:
aoqi@0 2720 case Op_StoreNKlass:
aoqi@0 2721 case Op_LoadB:
aoqi@0 2722 case Op_LoadUB:
aoqi@0 2723 case Op_LoadUS:
aoqi@0 2724 case Op_LoadI:
aoqi@0 2725 case Op_LoadKlass:
aoqi@0 2726 case Op_LoadNKlass:
aoqi@0 2727 case Op_LoadL:
aoqi@0 2728 case Op_LoadL_unaligned:
aoqi@0 2729 case Op_LoadPLocked:
aoqi@0 2730 case Op_LoadP:
aoqi@0 2731 case Op_LoadN:
aoqi@0 2732 case Op_LoadRange:
aoqi@0 2733 case Op_LoadS: {
aoqi@0 2734 handle_mem:
aoqi@0 2735 #ifdef ASSERT
aoqi@0 2736 if( VerifyOptoOopOffsets ) {
aoqi@0 2737 assert( n->is_Mem(), "" );
aoqi@0 2738 MemNode *mem = (MemNode*)n;
aoqi@0 2739 // Check to see if address types have grounded out somehow.
aoqi@0 2740 const TypeInstPtr *tp = mem->in(MemNode::Address)->bottom_type()->isa_instptr();
aoqi@0 2741 assert( !tp || oop_offset_is_sane(tp), "" );
aoqi@0 2742 }
aoqi@0 2743 #endif
aoqi@0 2744 break;
aoqi@0 2745 }
aoqi@0 2746
aoqi@0 2747 case Op_AddP: { // Assert sane base pointers
aoqi@0 2748 Node *addp = n->in(AddPNode::Address);
aoqi@0 2749 assert( !addp->is_AddP() ||
aoqi@0 2750 addp->in(AddPNode::Base)->is_top() || // Top OK for allocation
aoqi@0 2751 addp->in(AddPNode::Base) == n->in(AddPNode::Base),
aoqi@0 2752 "Base pointers must match" );
aoqi@0 2753 #ifdef _LP64
aoqi@0 2754 if ((UseCompressedOops || UseCompressedClassPointers) &&
aoqi@0 2755 addp->Opcode() == Op_ConP &&
aoqi@0 2756 addp == n->in(AddPNode::Base) &&
aoqi@0 2757 n->in(AddPNode::Offset)->is_Con()) {
aoqi@0 2758 // Use addressing with narrow klass to load with offset on x86.
aoqi@0 2759 // On sparc loading 32-bits constant and decoding it have less
aoqi@0 2760 // instructions (4) then load 64-bits constant (7).
aoqi@0 2761 // Do this transformation here since IGVN will convert ConN back to ConP.
aoqi@0 2762 const Type* t = addp->bottom_type();
aoqi@0 2763 if (t->isa_oopptr() || t->isa_klassptr()) {
aoqi@0 2764 Node* nn = NULL;
aoqi@0 2765
aoqi@0 2766 int op = t->isa_oopptr() ? Op_ConN : Op_ConNKlass;
aoqi@0 2767
aoqi@0 2768 // Look for existing ConN node of the same exact type.
aoqi@0 2769 Node* r = root();
aoqi@0 2770 uint cnt = r->outcnt();
aoqi@0 2771 for (uint i = 0; i < cnt; i++) {
aoqi@0 2772 Node* m = r->raw_out(i);
aoqi@0 2773 if (m!= NULL && m->Opcode() == op &&
aoqi@0 2774 m->bottom_type()->make_ptr() == t) {
aoqi@0 2775 nn = m;
aoqi@0 2776 break;
aoqi@0 2777 }
aoqi@0 2778 }
aoqi@0 2779 if (nn != NULL) {
aoqi@0 2780 // Decode a narrow oop to match address
aoqi@0 2781 // [R12 + narrow_oop_reg<<3 + offset]
aoqi@0 2782 if (t->isa_oopptr()) {
aoqi@0 2783 nn = new (this) DecodeNNode(nn, t);
aoqi@0 2784 } else {
aoqi@0 2785 nn = new (this) DecodeNKlassNode(nn, t);
aoqi@0 2786 }
aoqi@0 2787 n->set_req(AddPNode::Base, nn);
aoqi@0 2788 n->set_req(AddPNode::Address, nn);
aoqi@0 2789 if (addp->outcnt() == 0) {
aoqi@0 2790 addp->disconnect_inputs(NULL, this);
aoqi@0 2791 }
aoqi@0 2792 }
aoqi@0 2793 }
aoqi@0 2794 }
aoqi@0 2795 #endif
aoqi@0 2796 break;
aoqi@0 2797 }
aoqi@0 2798
aoqi@0 2799 #ifdef _LP64
aoqi@0 2800 case Op_CastPP:
aoqi@0 2801 if (n->in(1)->is_DecodeN() && Matcher::gen_narrow_oop_implicit_null_checks()) {
aoqi@0 2802 Node* in1 = n->in(1);
aoqi@0 2803 const Type* t = n->bottom_type();
aoqi@0 2804 Node* new_in1 = in1->clone();
aoqi@0 2805 new_in1->as_DecodeN()->set_type(t);
aoqi@0 2806
aoqi@0 2807 if (!Matcher::narrow_oop_use_complex_address()) {
aoqi@0 2808 //
aoqi@0 2809 // x86, ARM and friends can handle 2 adds in addressing mode
aoqi@0 2810 // and Matcher can fold a DecodeN node into address by using
aoqi@0 2811 // a narrow oop directly and do implicit NULL check in address:
aoqi@0 2812 //
aoqi@0 2813 // [R12 + narrow_oop_reg<<3 + offset]
aoqi@0 2814 // NullCheck narrow_oop_reg
aoqi@0 2815 //
aoqi@0 2816 // On other platforms (Sparc) we have to keep new DecodeN node and
aoqi@0 2817 // use it to do implicit NULL check in address:
aoqi@0 2818 //
aoqi@0 2819 // decode_not_null narrow_oop_reg, base_reg
aoqi@0 2820 // [base_reg + offset]
aoqi@0 2821 // NullCheck base_reg
aoqi@0 2822 //
aoqi@0 2823 // Pin the new DecodeN node to non-null path on these platform (Sparc)
aoqi@0 2824 // to keep the information to which NULL check the new DecodeN node
aoqi@0 2825 // corresponds to use it as value in implicit_null_check().
aoqi@0 2826 //
aoqi@0 2827 new_in1->set_req(0, n->in(0));
aoqi@0 2828 }
aoqi@0 2829
aoqi@0 2830 n->subsume_by(new_in1, this);
aoqi@0 2831 if (in1->outcnt() == 0) {
aoqi@0 2832 in1->disconnect_inputs(NULL, this);
aoqi@0 2833 }
aoqi@0 2834 }
aoqi@0 2835 break;
aoqi@0 2836
aoqi@0 2837 case Op_CmpP:
aoqi@0 2838 // Do this transformation here to preserve CmpPNode::sub() and
aoqi@0 2839 // other TypePtr related Ideal optimizations (for example, ptr nullness).
aoqi@0 2840 if (n->in(1)->is_DecodeNarrowPtr() || n->in(2)->is_DecodeNarrowPtr()) {
aoqi@0 2841 Node* in1 = n->in(1);
aoqi@0 2842 Node* in2 = n->in(2);
aoqi@0 2843 if (!in1->is_DecodeNarrowPtr()) {
aoqi@0 2844 in2 = in1;
aoqi@0 2845 in1 = n->in(2);
aoqi@0 2846 }
aoqi@0 2847 assert(in1->is_DecodeNarrowPtr(), "sanity");
aoqi@0 2848
aoqi@0 2849 Node* new_in2 = NULL;
aoqi@0 2850 if (in2->is_DecodeNarrowPtr()) {
aoqi@0 2851 assert(in2->Opcode() == in1->Opcode(), "must be same node type");
aoqi@0 2852 new_in2 = in2->in(1);
aoqi@0 2853 } else if (in2->Opcode() == Op_ConP) {
aoqi@0 2854 const Type* t = in2->bottom_type();
aoqi@0 2855 if (t == TypePtr::NULL_PTR) {
aoqi@0 2856 assert(in1->is_DecodeN(), "compare klass to null?");
aoqi@0 2857 // Don't convert CmpP null check into CmpN if compressed
aoqi@0 2858 // oops implicit null check is not generated.
aoqi@0 2859 // This will allow to generate normal oop implicit null check.
aoqi@0 2860 if (Matcher::gen_narrow_oop_implicit_null_checks())
aoqi@0 2861 new_in2 = ConNode::make(this, TypeNarrowOop::NULL_PTR);
aoqi@0 2862 //
aoqi@0 2863 // This transformation together with CastPP transformation above
aoqi@0 2864 // will generated code for implicit NULL checks for compressed oops.
aoqi@0 2865 //
aoqi@0 2866 // The original code after Optimize()
aoqi@0 2867 //
aoqi@0 2868 // LoadN memory, narrow_oop_reg
aoqi@0 2869 // decode narrow_oop_reg, base_reg
aoqi@0 2870 // CmpP base_reg, NULL
aoqi@0 2871 // CastPP base_reg // NotNull
aoqi@0 2872 // Load [base_reg + offset], val_reg
aoqi@0 2873 //
aoqi@0 2874 // after these transformations will be
aoqi@0 2875 //
aoqi@0 2876 // LoadN memory, narrow_oop_reg
aoqi@0 2877 // CmpN narrow_oop_reg, NULL
aoqi@0 2878 // decode_not_null narrow_oop_reg, base_reg
aoqi@0 2879 // Load [base_reg + offset], val_reg
aoqi@0 2880 //
aoqi@0 2881 // and the uncommon path (== NULL) will use narrow_oop_reg directly
aoqi@0 2882 // since narrow oops can be used in debug info now (see the code in
aoqi@0 2883 // final_graph_reshaping_walk()).
aoqi@0 2884 //
aoqi@0 2885 // At the end the code will be matched to
aoqi@0 2886 // on x86:
aoqi@0 2887 //
aoqi@0 2888 // Load_narrow_oop memory, narrow_oop_reg
aoqi@0 2889 // Load [R12 + narrow_oop_reg<<3 + offset], val_reg
aoqi@0 2890 // NullCheck narrow_oop_reg
aoqi@0 2891 //
aoqi@0 2892 // and on sparc:
aoqi@0 2893 //
aoqi@0 2894 // Load_narrow_oop memory, narrow_oop_reg
aoqi@0 2895 // decode_not_null narrow_oop_reg, base_reg
aoqi@0 2896 // Load [base_reg + offset], val_reg
aoqi@0 2897 // NullCheck base_reg
aoqi@0 2898 //
aoqi@0 2899 } else if (t->isa_oopptr()) {
aoqi@0 2900 new_in2 = ConNode::make(this, t->make_narrowoop());
aoqi@0 2901 } else if (t->isa_klassptr()) {
aoqi@0 2902 new_in2 = ConNode::make(this, t->make_narrowklass());
aoqi@0 2903 }
aoqi@0 2904 }
aoqi@0 2905 if (new_in2 != NULL) {
aoqi@0 2906 Node* cmpN = new (this) CmpNNode(in1->in(1), new_in2);
aoqi@0 2907 n->subsume_by(cmpN, this);
aoqi@0 2908 if (in1->outcnt() == 0) {
aoqi@0 2909 in1->disconnect_inputs(NULL, this);
aoqi@0 2910 }
aoqi@0 2911 if (in2->outcnt() == 0) {
aoqi@0 2912 in2->disconnect_inputs(NULL, this);
aoqi@0 2913 }
aoqi@0 2914 }
aoqi@0 2915 }
aoqi@0 2916 break;
aoqi@0 2917
aoqi@0 2918 case Op_DecodeN:
aoqi@0 2919 case Op_DecodeNKlass:
aoqi@0 2920 assert(!n->in(1)->is_EncodeNarrowPtr(), "should be optimized out");
aoqi@0 2921 // DecodeN could be pinned when it can't be fold into
aoqi@0 2922 // an address expression, see the code for Op_CastPP above.
aoqi@0 2923 assert(n->in(0) == NULL || (UseCompressedOops && !Matcher::narrow_oop_use_complex_address()), "no control");
aoqi@0 2924 break;
aoqi@0 2925
aoqi@0 2926 case Op_EncodeP:
aoqi@0 2927 case Op_EncodePKlass: {
aoqi@0 2928 Node* in1 = n->in(1);
aoqi@0 2929 if (in1->is_DecodeNarrowPtr()) {
aoqi@0 2930 n->subsume_by(in1->in(1), this);
aoqi@0 2931 } else if (in1->Opcode() == Op_ConP) {
aoqi@0 2932 const Type* t = in1->bottom_type();
aoqi@0 2933 if (t == TypePtr::NULL_PTR) {
aoqi@0 2934 assert(t->isa_oopptr(), "null klass?");
aoqi@0 2935 n->subsume_by(ConNode::make(this, TypeNarrowOop::NULL_PTR), this);
aoqi@0 2936 } else if (t->isa_oopptr()) {
aoqi@0 2937 n->subsume_by(ConNode::make(this, t->make_narrowoop()), this);
aoqi@0 2938 } else if (t->isa_klassptr()) {
aoqi@0 2939 n->subsume_by(ConNode::make(this, t->make_narrowklass()), this);
aoqi@0 2940 }
aoqi@0 2941 }
aoqi@0 2942 if (in1->outcnt() == 0) {
aoqi@0 2943 in1->disconnect_inputs(NULL, this);
aoqi@0 2944 }
aoqi@0 2945 break;
aoqi@0 2946 }
aoqi@0 2947
aoqi@0 2948 case Op_Proj: {
aoqi@0 2949 if (OptimizeStringConcat) {
aoqi@0 2950 ProjNode* p = n->as_Proj();
aoqi@0 2951 if (p->_is_io_use) {
aoqi@0 2952 // Separate projections were used for the exception path which
aoqi@0 2953 // are normally removed by a late inline. If it wasn't inlined
aoqi@0 2954 // then they will hang around and should just be replaced with
aoqi@0 2955 // the original one.
aoqi@0 2956 Node* proj = NULL;
aoqi@0 2957 // Replace with just one
aoqi@0 2958 for (SimpleDUIterator i(p->in(0)); i.has_next(); i.next()) {
aoqi@0 2959 Node *use = i.get();
aoqi@0 2960 if (use->is_Proj() && p != use && use->as_Proj()->_con == p->_con) {
aoqi@0 2961 proj = use;
aoqi@0 2962 break;
aoqi@0 2963 }
aoqi@0 2964 }
aoqi@0 2965 assert(proj != NULL, "must be found");
aoqi@0 2966 p->subsume_by(proj, this);
aoqi@0 2967 }
aoqi@0 2968 }
aoqi@0 2969 break;
aoqi@0 2970 }
aoqi@0 2971
aoqi@0 2972 case Op_Phi:
aoqi@0 2973 if (n->as_Phi()->bottom_type()->isa_narrowoop() || n->as_Phi()->bottom_type()->isa_narrowklass()) {
aoqi@0 2974 // The EncodeP optimization may create Phi with the same edges
aoqi@0 2975 // for all paths. It is not handled well by Register Allocator.
aoqi@0 2976 Node* unique_in = n->in(1);
aoqi@0 2977 assert(unique_in != NULL, "");
aoqi@0 2978 uint cnt = n->req();
aoqi@0 2979 for (uint i = 2; i < cnt; i++) {
aoqi@0 2980 Node* m = n->in(i);
aoqi@0 2981 assert(m != NULL, "");
aoqi@0 2982 if (unique_in != m)
aoqi@0 2983 unique_in = NULL;
aoqi@0 2984 }
aoqi@0 2985 if (unique_in != NULL) {
aoqi@0 2986 n->subsume_by(unique_in, this);
aoqi@0 2987 }
aoqi@0 2988 }
aoqi@0 2989 break;
aoqi@0 2990
aoqi@0 2991 #endif
aoqi@0 2992
aoqi@0 2993 case Op_ModI:
aoqi@0 2994 if (UseDivMod) {
aoqi@0 2995 // Check if a%b and a/b both exist
aoqi@0 2996 Node* d = n->find_similar(Op_DivI);
aoqi@0 2997 if (d) {
aoqi@0 2998 // Replace them with a fused divmod if supported
aoqi@0 2999 if (Matcher::has_match_rule(Op_DivModI)) {
aoqi@0 3000 DivModINode* divmod = DivModINode::make(this, n);
aoqi@0 3001 d->subsume_by(divmod->div_proj(), this);
aoqi@0 3002 n->subsume_by(divmod->mod_proj(), this);
aoqi@0 3003 } else {
aoqi@0 3004 // replace a%b with a-((a/b)*b)
aoqi@0 3005 Node* mult = new (this) MulINode(d, d->in(2));
aoqi@0 3006 Node* sub = new (this) SubINode(d->in(1), mult);
aoqi@0 3007 n->subsume_by(sub, this);
aoqi@0 3008 }
aoqi@0 3009 }
aoqi@0 3010 }
aoqi@0 3011 break;
aoqi@0 3012
aoqi@0 3013 case Op_ModL:
aoqi@0 3014 if (UseDivMod) {
aoqi@0 3015 // Check if a%b and a/b both exist
aoqi@0 3016 Node* d = n->find_similar(Op_DivL);
aoqi@0 3017 if (d) {
aoqi@0 3018 // Replace them with a fused divmod if supported
aoqi@0 3019 if (Matcher::has_match_rule(Op_DivModL)) {
aoqi@0 3020 DivModLNode* divmod = DivModLNode::make(this, n);
aoqi@0 3021 d->subsume_by(divmod->div_proj(), this);
aoqi@0 3022 n->subsume_by(divmod->mod_proj(), this);
aoqi@0 3023 } else {
aoqi@0 3024 // replace a%b with a-((a/b)*b)
aoqi@0 3025 Node* mult = new (this) MulLNode(d, d->in(2));
aoqi@0 3026 Node* sub = new (this) SubLNode(d->in(1), mult);
aoqi@0 3027 n->subsume_by(sub, this);
aoqi@0 3028 }
aoqi@0 3029 }
aoqi@0 3030 }
aoqi@0 3031 break;
aoqi@0 3032
aoqi@0 3033 case Op_LoadVector:
aoqi@0 3034 case Op_StoreVector:
aoqi@0 3035 break;
aoqi@0 3036
aoqi@0 3037 case Op_PackB:
aoqi@0 3038 case Op_PackS:
aoqi@0 3039 case Op_PackI:
aoqi@0 3040 case Op_PackF:
aoqi@0 3041 case Op_PackL:
aoqi@0 3042 case Op_PackD:
aoqi@0 3043 if (n->req()-1 > 2) {
aoqi@0 3044 // Replace many operand PackNodes with a binary tree for matching
aoqi@0 3045 PackNode* p = (PackNode*) n;
aoqi@0 3046 Node* btp = p->binary_tree_pack(this, 1, n->req());
aoqi@0 3047 n->subsume_by(btp, this);
aoqi@0 3048 }
aoqi@0 3049 break;
aoqi@0 3050 case Op_Loop:
aoqi@0 3051 case Op_CountedLoop:
aoqi@0 3052 if (n->as_Loop()->is_inner_loop()) {
aoqi@0 3053 frc.inc_inner_loop_count();
aoqi@0 3054 }
aoqi@0 3055 break;
aoqi@0 3056 case Op_LShiftI:
aoqi@0 3057 case Op_RShiftI:
aoqi@0 3058 case Op_URShiftI:
aoqi@0 3059 case Op_LShiftL:
aoqi@0 3060 case Op_RShiftL:
aoqi@0 3061 case Op_URShiftL:
aoqi@0 3062 if (Matcher::need_masked_shift_count) {
aoqi@0 3063 // The cpu's shift instructions don't restrict the count to the
aoqi@0 3064 // lower 5/6 bits. We need to do the masking ourselves.
aoqi@0 3065 Node* in2 = n->in(2);
aoqi@0 3066 juint mask = (n->bottom_type() == TypeInt::INT) ? (BitsPerInt - 1) : (BitsPerLong - 1);
aoqi@0 3067 const TypeInt* t = in2->find_int_type();
aoqi@0 3068 if (t != NULL && t->is_con()) {
aoqi@0 3069 juint shift = t->get_con();
aoqi@0 3070 if (shift > mask) { // Unsigned cmp
aoqi@0 3071 n->set_req(2, ConNode::make(this, TypeInt::make(shift & mask)));
aoqi@0 3072 }
aoqi@0 3073 } else {
aoqi@0 3074 if (t == NULL || t->_lo < 0 || t->_hi > (int)mask) {
aoqi@0 3075 Node* shift = new (this) AndINode(in2, ConNode::make(this, TypeInt::make(mask)));
aoqi@0 3076 n->set_req(2, shift);
aoqi@0 3077 }
aoqi@0 3078 }
aoqi@0 3079 if (in2->outcnt() == 0) { // Remove dead node
aoqi@0 3080 in2->disconnect_inputs(NULL, this);
aoqi@0 3081 }
aoqi@0 3082 }
aoqi@0 3083 break;
aoqi@0 3084 case Op_MemBarStoreStore:
aoqi@0 3085 case Op_MemBarRelease:
aoqi@0 3086 // Break the link with AllocateNode: it is no longer useful and
aoqi@0 3087 // confuses register allocation.
aoqi@0 3088 if (n->req() > MemBarNode::Precedent) {
aoqi@0 3089 n->set_req(MemBarNode::Precedent, top());
aoqi@0 3090 }
aoqi@0 3091 break;
aoqi@0 3092 default:
aoqi@0 3093 assert( !n->is_Call(), "" );
aoqi@0 3094 assert( !n->is_Mem(), "" );
aoqi@0 3095 break;
aoqi@0 3096 }
aoqi@0 3097
aoqi@0 3098 // Collect CFG split points
aoqi@0 3099 if (n->is_MultiBranch())
aoqi@0 3100 frc._tests.push(n);
aoqi@0 3101 }
aoqi@0 3102
aoqi@0 3103 //------------------------------final_graph_reshaping_walk---------------------
aoqi@0 3104 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
aoqi@0 3105 // requires that the walk visits a node's inputs before visiting the node.
aoqi@0 3106 void Compile::final_graph_reshaping_walk( Node_Stack &nstack, Node *root, Final_Reshape_Counts &frc ) {
aoqi@0 3107 ResourceArea *area = Thread::current()->resource_area();
aoqi@0 3108 Unique_Node_List sfpt(area);
aoqi@0 3109
aoqi@0 3110 frc._visited.set(root->_idx); // first, mark node as visited
aoqi@0 3111 uint cnt = root->req();
aoqi@0 3112 Node *n = root;
aoqi@0 3113 uint i = 0;
aoqi@0 3114 while (true) {
aoqi@0 3115 if (i < cnt) {
aoqi@0 3116 // Place all non-visited non-null inputs onto stack
aoqi@0 3117 Node* m = n->in(i);
aoqi@0 3118 ++i;
aoqi@0 3119 if (m != NULL && !frc._visited.test_set(m->_idx)) {
aoqi@0 3120 if (m->is_SafePoint() && m->as_SafePoint()->jvms() != NULL) {
aoqi@0 3121 // compute worst case interpreter size in case of a deoptimization
aoqi@0 3122 update_interpreter_frame_size(m->as_SafePoint()->jvms()->interpreter_frame_size());
aoqi@0 3123
aoqi@0 3124 sfpt.push(m);
aoqi@0 3125 }
aoqi@0 3126 cnt = m->req();
aoqi@0 3127 nstack.push(n, i); // put on stack parent and next input's index
aoqi@0 3128 n = m;
aoqi@0 3129 i = 0;
aoqi@0 3130 }
aoqi@0 3131 } else {
aoqi@0 3132 // Now do post-visit work
aoqi@0 3133 final_graph_reshaping_impl( n, frc );
aoqi@0 3134 if (nstack.is_empty())
aoqi@0 3135 break; // finished
aoqi@0 3136 n = nstack.node(); // Get node from stack
aoqi@0 3137 cnt = n->req();
aoqi@0 3138 i = nstack.index();
aoqi@0 3139 nstack.pop(); // Shift to the next node on stack
aoqi@0 3140 }
aoqi@0 3141 }
aoqi@0 3142
aoqi@0 3143 // Skip next transformation if compressed oops are not used.
aoqi@0 3144 if ((UseCompressedOops && !Matcher::gen_narrow_oop_implicit_null_checks()) ||
aoqi@0 3145 (!UseCompressedOops && !UseCompressedClassPointers))
aoqi@0 3146 return;
aoqi@0 3147
aoqi@0 3148 // Go over safepoints nodes to skip DecodeN/DecodeNKlass nodes for debug edges.
aoqi@0 3149 // It could be done for an uncommon traps or any safepoints/calls
aoqi@0 3150 // if the DecodeN/DecodeNKlass node is referenced only in a debug info.
aoqi@0 3151 while (sfpt.size() > 0) {
aoqi@0 3152 n = sfpt.pop();
aoqi@0 3153 JVMState *jvms = n->as_SafePoint()->jvms();
aoqi@0 3154 assert(jvms != NULL, "sanity");
aoqi@0 3155 int start = jvms->debug_start();
aoqi@0 3156 int end = n->req();
aoqi@0 3157 bool is_uncommon = (n->is_CallStaticJava() &&
aoqi@0 3158 n->as_CallStaticJava()->uncommon_trap_request() != 0);
aoqi@0 3159 for (int j = start; j < end; j++) {
aoqi@0 3160 Node* in = n->in(j);
aoqi@0 3161 if (in->is_DecodeNarrowPtr()) {
aoqi@0 3162 bool safe_to_skip = true;
aoqi@0 3163 if (!is_uncommon ) {
aoqi@0 3164 // Is it safe to skip?
aoqi@0 3165 for (uint i = 0; i < in->outcnt(); i++) {
aoqi@0 3166 Node* u = in->raw_out(i);
aoqi@0 3167 if (!u->is_SafePoint() ||
aoqi@0 3168 u->is_Call() && u->as_Call()->has_non_debug_use(n)) {
aoqi@0 3169 safe_to_skip = false;
aoqi@0 3170 }
aoqi@0 3171 }
aoqi@0 3172 }
aoqi@0 3173 if (safe_to_skip) {
aoqi@0 3174 n->set_req(j, in->in(1));
aoqi@0 3175 }
aoqi@0 3176 if (in->outcnt() == 0) {
aoqi@0 3177 in->disconnect_inputs(NULL, this);
aoqi@0 3178 }
aoqi@0 3179 }
aoqi@0 3180 }
aoqi@0 3181 }
aoqi@0 3182 }
aoqi@0 3183
aoqi@0 3184 //------------------------------final_graph_reshaping--------------------------
aoqi@0 3185 // Final Graph Reshaping.
aoqi@0 3186 //
aoqi@0 3187 // (1) Clone simple inputs to uncommon calls, so they can be scheduled late
aoqi@0 3188 // and not commoned up and forced early. Must come after regular
aoqi@0 3189 // optimizations to avoid GVN undoing the cloning. Clone constant
aoqi@0 3190 // inputs to Loop Phis; these will be split by the allocator anyways.
aoqi@0 3191 // Remove Opaque nodes.
aoqi@0 3192 // (2) Move last-uses by commutative operations to the left input to encourage
aoqi@0 3193 // Intel update-in-place two-address operations and better register usage
aoqi@0 3194 // on RISCs. Must come after regular optimizations to avoid GVN Ideal
aoqi@0 3195 // calls canonicalizing them back.
aoqi@0 3196 // (3) Count the number of double-precision FP ops, single-precision FP ops
aoqi@0 3197 // and call sites. On Intel, we can get correct rounding either by
aoqi@0 3198 // forcing singles to memory (requires extra stores and loads after each
aoqi@0 3199 // FP bytecode) or we can set a rounding mode bit (requires setting and
aoqi@0 3200 // clearing the mode bit around call sites). The mode bit is only used
aoqi@0 3201 // if the relative frequency of single FP ops to calls is low enough.
aoqi@0 3202 // This is a key transform for SPEC mpeg_audio.
aoqi@0 3203 // (4) Detect infinite loops; blobs of code reachable from above but not
aoqi@0 3204 // below. Several of the Code_Gen algorithms fail on such code shapes,
aoqi@0 3205 // so we simply bail out. Happens a lot in ZKM.jar, but also happens
aoqi@0 3206 // from time to time in other codes (such as -Xcomp finalizer loops, etc).
aoqi@0 3207 // Detection is by looking for IfNodes where only 1 projection is
aoqi@0 3208 // reachable from below or CatchNodes missing some targets.
aoqi@0 3209 // (5) Assert for insane oop offsets in debug mode.
aoqi@0 3210
aoqi@0 3211 bool Compile::final_graph_reshaping() {
aoqi@0 3212 // an infinite loop may have been eliminated by the optimizer,
aoqi@0 3213 // in which case the graph will be empty.
aoqi@0 3214 if (root()->req() == 1) {
aoqi@0 3215 record_method_not_compilable("trivial infinite loop");
aoqi@0 3216 return true;
aoqi@0 3217 }
aoqi@0 3218
aoqi@0 3219 // Expensive nodes have their control input set to prevent the GVN
aoqi@0 3220 // from freely commoning them. There's no GVN beyond this point so
aoqi@0 3221 // no need to keep the control input. We want the expensive nodes to
aoqi@0 3222 // be freely moved to the least frequent code path by gcm.
aoqi@0 3223 assert(OptimizeExpensiveOps || expensive_count() == 0, "optimization off but list non empty?");
aoqi@0 3224 for (int i = 0; i < expensive_count(); i++) {
aoqi@0 3225 _expensive_nodes->at(i)->set_req(0, NULL);
aoqi@0 3226 }
aoqi@0 3227
aoqi@0 3228 Final_Reshape_Counts frc;
aoqi@0 3229
aoqi@0 3230 // Visit everybody reachable!
aoqi@0 3231 // Allocate stack of size C->unique()/2 to avoid frequent realloc
aoqi@0 3232 Node_Stack nstack(unique() >> 1);
aoqi@0 3233 final_graph_reshaping_walk(nstack, root(), frc);
aoqi@0 3234
aoqi@0 3235 // Check for unreachable (from below) code (i.e., infinite loops).
aoqi@0 3236 for( uint i = 0; i < frc._tests.size(); i++ ) {
aoqi@0 3237 MultiBranchNode *n = frc._tests[i]->as_MultiBranch();
aoqi@0 3238 // Get number of CFG targets.
aoqi@0 3239 // Note that PCTables include exception targets after calls.
aoqi@0 3240 uint required_outcnt = n->required_outcnt();
aoqi@0 3241 if (n->outcnt() != required_outcnt) {
aoqi@0 3242 // Check for a few special cases. Rethrow Nodes never take the
aoqi@0 3243 // 'fall-thru' path, so expected kids is 1 less.
aoqi@0 3244 if (n->is_PCTable() && n->in(0) && n->in(0)->in(0)) {
aoqi@0 3245 if (n->in(0)->in(0)->is_Call()) {
aoqi@0 3246 CallNode *call = n->in(0)->in(0)->as_Call();
aoqi@0 3247 if (call->entry_point() == OptoRuntime::rethrow_stub()) {
aoqi@0 3248 required_outcnt--; // Rethrow always has 1 less kid
aoqi@0 3249 } else if (call->req() > TypeFunc::Parms &&
aoqi@0 3250 call->is_CallDynamicJava()) {
aoqi@0 3251 // Check for null receiver. In such case, the optimizer has
aoqi@0 3252 // detected that the virtual call will always result in a null
aoqi@0 3253 // pointer exception. The fall-through projection of this CatchNode
aoqi@0 3254 // will not be populated.
aoqi@0 3255 Node *arg0 = call->in(TypeFunc::Parms);
aoqi@0 3256 if (arg0->is_Type() &&
aoqi@0 3257 arg0->as_Type()->type()->higher_equal(TypePtr::NULL_PTR)) {
aoqi@0 3258 required_outcnt--;
aoqi@0 3259 }
aoqi@0 3260 } else if (call->entry_point() == OptoRuntime::new_array_Java() &&
aoqi@0 3261 call->req() > TypeFunc::Parms+1 &&
aoqi@0 3262 call->is_CallStaticJava()) {
aoqi@0 3263 // Check for negative array length. In such case, the optimizer has
aoqi@0 3264 // detected that the allocation attempt will always result in an
aoqi@0 3265 // exception. There is no fall-through projection of this CatchNode .
aoqi@0 3266 Node *arg1 = call->in(TypeFunc::Parms+1);
aoqi@0 3267 if (arg1->is_Type() &&
aoqi@0 3268 arg1->as_Type()->type()->join(TypeInt::POS)->empty()) {
aoqi@0 3269 required_outcnt--;
aoqi@0 3270 }
aoqi@0 3271 }
aoqi@0 3272 }
aoqi@0 3273 }
aoqi@0 3274 // Recheck with a better notion of 'required_outcnt'
aoqi@0 3275 if (n->outcnt() != required_outcnt) {
aoqi@0 3276 record_method_not_compilable("malformed control flow");
aoqi@0 3277 return true; // Not all targets reachable!
aoqi@0 3278 }
aoqi@0 3279 }
aoqi@0 3280 // Check that I actually visited all kids. Unreached kids
aoqi@0 3281 // must be infinite loops.
aoqi@0 3282 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++)
aoqi@0 3283 if (!frc._visited.test(n->fast_out(j)->_idx)) {
aoqi@0 3284 record_method_not_compilable("infinite loop");
aoqi@0 3285 return true; // Found unvisited kid; must be unreach
aoqi@0 3286 }
aoqi@0 3287 }
aoqi@0 3288
aoqi@0 3289 // If original bytecodes contained a mixture of floats and doubles
aoqi@0 3290 // check if the optimizer has made it homogenous, item (3).
aoqi@0 3291 if( Use24BitFPMode && Use24BitFP && UseSSE == 0 &&
aoqi@0 3292 frc.get_float_count() > 32 &&
aoqi@0 3293 frc.get_double_count() == 0 &&
aoqi@0 3294 (10 * frc.get_call_count() < frc.get_float_count()) ) {
aoqi@0 3295 set_24_bit_selection_and_mode( false, true );
aoqi@0 3296 }
aoqi@0 3297
aoqi@0 3298 set_java_calls(frc.get_java_call_count());
aoqi@0 3299 set_inner_loops(frc.get_inner_loop_count());
aoqi@0 3300
aoqi@0 3301 // No infinite loops, no reason to bail out.
aoqi@0 3302 return false;
aoqi@0 3303 }
aoqi@0 3304
aoqi@0 3305 //-----------------------------too_many_traps----------------------------------
aoqi@0 3306 // Report if there are too many traps at the current method and bci.
aoqi@0 3307 // Return true if there was a trap, and/or PerMethodTrapLimit is exceeded.
aoqi@0 3308 bool Compile::too_many_traps(ciMethod* method,
aoqi@0 3309 int bci,
aoqi@0 3310 Deoptimization::DeoptReason reason) {
aoqi@0 3311 ciMethodData* md = method->method_data();
aoqi@0 3312 if (md->is_empty()) {
aoqi@0 3313 // Assume the trap has not occurred, or that it occurred only
aoqi@0 3314 // because of a transient condition during start-up in the interpreter.
aoqi@0 3315 return false;
aoqi@0 3316 }
aoqi@0 3317 ciMethod* m = Deoptimization::reason_is_speculate(reason) ? this->method() : NULL;
aoqi@0 3318 if (md->has_trap_at(bci, m, reason) != 0) {
aoqi@0 3319 // Assume PerBytecodeTrapLimit==0, for a more conservative heuristic.
aoqi@0 3320 // Also, if there are multiple reasons, or if there is no per-BCI record,
aoqi@0 3321 // assume the worst.
aoqi@0 3322 if (log())
aoqi@0 3323 log()->elem("observe trap='%s' count='%d'",
aoqi@0 3324 Deoptimization::trap_reason_name(reason),
aoqi@0 3325 md->trap_count(reason));
aoqi@0 3326 return true;
aoqi@0 3327 } else {
aoqi@0 3328 // Ignore method/bci and see if there have been too many globally.
aoqi@0 3329 return too_many_traps(reason, md);
aoqi@0 3330 }
aoqi@0 3331 }
aoqi@0 3332
aoqi@0 3333 // Less-accurate variant which does not require a method and bci.
aoqi@0 3334 bool Compile::too_many_traps(Deoptimization::DeoptReason reason,
aoqi@0 3335 ciMethodData* logmd) {
aoqi@0 3336 if (trap_count(reason) >= Deoptimization::per_method_trap_limit(reason)) {
aoqi@0 3337 // Too many traps globally.
aoqi@0 3338 // Note that we use cumulative trap_count, not just md->trap_count.
aoqi@0 3339 if (log()) {
aoqi@0 3340 int mcount = (logmd == NULL)? -1: (int)logmd->trap_count(reason);
aoqi@0 3341 log()->elem("observe trap='%s' count='0' mcount='%d' ccount='%d'",
aoqi@0 3342 Deoptimization::trap_reason_name(reason),
aoqi@0 3343 mcount, trap_count(reason));
aoqi@0 3344 }
aoqi@0 3345 return true;
aoqi@0 3346 } else {
aoqi@0 3347 // The coast is clear.
aoqi@0 3348 return false;
aoqi@0 3349 }
aoqi@0 3350 }
aoqi@0 3351
aoqi@0 3352 //--------------------------too_many_recompiles--------------------------------
aoqi@0 3353 // Report if there are too many recompiles at the current method and bci.
aoqi@0 3354 // Consults PerBytecodeRecompilationCutoff and PerMethodRecompilationCutoff.
aoqi@0 3355 // Is not eager to return true, since this will cause the compiler to use
aoqi@0 3356 // Action_none for a trap point, to avoid too many recompilations.
aoqi@0 3357 bool Compile::too_many_recompiles(ciMethod* method,
aoqi@0 3358 int bci,
aoqi@0 3359 Deoptimization::DeoptReason reason) {
aoqi@0 3360 ciMethodData* md = method->method_data();
aoqi@0 3361 if (md->is_empty()) {
aoqi@0 3362 // Assume the trap has not occurred, or that it occurred only
aoqi@0 3363 // because of a transient condition during start-up in the interpreter.
aoqi@0 3364 return false;
aoqi@0 3365 }
aoqi@0 3366 // Pick a cutoff point well within PerBytecodeRecompilationCutoff.
aoqi@0 3367 uint bc_cutoff = (uint) PerBytecodeRecompilationCutoff / 8;
aoqi@0 3368 uint m_cutoff = (uint) PerMethodRecompilationCutoff / 2 + 1; // not zero
aoqi@0 3369 Deoptimization::DeoptReason per_bc_reason
aoqi@0 3370 = Deoptimization::reason_recorded_per_bytecode_if_any(reason);
aoqi@0 3371 ciMethod* m = Deoptimization::reason_is_speculate(reason) ? this->method() : NULL;
aoqi@0 3372 if ((per_bc_reason == Deoptimization::Reason_none
aoqi@0 3373 || md->has_trap_at(bci, m, reason) != 0)
aoqi@0 3374 // The trap frequency measure we care about is the recompile count:
aoqi@0 3375 && md->trap_recompiled_at(bci, m)
aoqi@0 3376 && md->overflow_recompile_count() >= bc_cutoff) {
aoqi@0 3377 // Do not emit a trap here if it has already caused recompilations.
aoqi@0 3378 // Also, if there are multiple reasons, or if there is no per-BCI record,
aoqi@0 3379 // assume the worst.
aoqi@0 3380 if (log())
aoqi@0 3381 log()->elem("observe trap='%s recompiled' count='%d' recompiles2='%d'",
aoqi@0 3382 Deoptimization::trap_reason_name(reason),
aoqi@0 3383 md->trap_count(reason),
aoqi@0 3384 md->overflow_recompile_count());
aoqi@0 3385 return true;
aoqi@0 3386 } else if (trap_count(reason) != 0
aoqi@0 3387 && decompile_count() >= m_cutoff) {
aoqi@0 3388 // Too many recompiles globally, and we have seen this sort of trap.
aoqi@0 3389 // Use cumulative decompile_count, not just md->decompile_count.
aoqi@0 3390 if (log())
aoqi@0 3391 log()->elem("observe trap='%s' count='%d' mcount='%d' decompiles='%d' mdecompiles='%d'",
aoqi@0 3392 Deoptimization::trap_reason_name(reason),
aoqi@0 3393 md->trap_count(reason), trap_count(reason),
aoqi@0 3394 md->decompile_count(), decompile_count());
aoqi@0 3395 return true;
aoqi@0 3396 } else {
aoqi@0 3397 // The coast is clear.
aoqi@0 3398 return false;
aoqi@0 3399 }
aoqi@0 3400 }
aoqi@0 3401
aoqi@0 3402 // Compute when not to trap. Used by matching trap based nodes and
aoqi@0 3403 // NullCheck optimization.
aoqi@0 3404 void Compile::set_allowed_deopt_reasons() {
aoqi@0 3405 _allowed_reasons = 0;
aoqi@0 3406 if (is_method_compilation()) {
aoqi@0 3407 for (int rs = (int)Deoptimization::Reason_none+1; rs < Compile::trapHistLength; rs++) {
aoqi@0 3408 assert(rs < BitsPerInt, "recode bit map");
aoqi@0 3409 if (!too_many_traps((Deoptimization::DeoptReason) rs)) {
aoqi@0 3410 _allowed_reasons |= nth_bit(rs);
aoqi@0 3411 }
aoqi@0 3412 }
aoqi@0 3413 }
aoqi@0 3414 }
aoqi@0 3415
aoqi@0 3416 #ifndef PRODUCT
aoqi@0 3417 //------------------------------verify_graph_edges---------------------------
aoqi@0 3418 // Walk the Graph and verify that there is a one-to-one correspondence
aoqi@0 3419 // between Use-Def edges and Def-Use edges in the graph.
aoqi@0 3420 void Compile::verify_graph_edges(bool no_dead_code) {
aoqi@0 3421 if (VerifyGraphEdges) {
aoqi@0 3422 ResourceArea *area = Thread::current()->resource_area();
aoqi@0 3423 Unique_Node_List visited(area);
aoqi@0 3424 // Call recursive graph walk to check edges
aoqi@0 3425 _root->verify_edges(visited);
aoqi@0 3426 if (no_dead_code) {
aoqi@0 3427 // Now make sure that no visited node is used by an unvisited node.
aoqi@0 3428 bool dead_nodes = 0;
aoqi@0 3429 Unique_Node_List checked(area);
aoqi@0 3430 while (visited.size() > 0) {
aoqi@0 3431 Node* n = visited.pop();
aoqi@0 3432 checked.push(n);
aoqi@0 3433 for (uint i = 0; i < n->outcnt(); i++) {
aoqi@0 3434 Node* use = n->raw_out(i);
aoqi@0 3435 if (checked.member(use)) continue; // already checked
aoqi@0 3436 if (visited.member(use)) continue; // already in the graph
aoqi@0 3437 if (use->is_Con()) continue; // a dead ConNode is OK
aoqi@0 3438 // At this point, we have found a dead node which is DU-reachable.
aoqi@0 3439 if (dead_nodes++ == 0)
aoqi@0 3440 tty->print_cr("*** Dead nodes reachable via DU edges:");
aoqi@0 3441 use->dump(2);
aoqi@0 3442 tty->print_cr("---");
aoqi@0 3443 checked.push(use); // No repeats; pretend it is now checked.
aoqi@0 3444 }
aoqi@0 3445 }
aoqi@0 3446 assert(dead_nodes == 0, "using nodes must be reachable from root");
aoqi@0 3447 }
aoqi@0 3448 }
aoqi@0 3449 }
aoqi@0 3450
aoqi@0 3451 // Verify GC barriers consistency
aoqi@0 3452 // Currently supported:
aoqi@0 3453 // - G1 pre-barriers (see GraphKit::g1_write_barrier_pre())
aoqi@0 3454 void Compile::verify_barriers() {
aoqi@0 3455 if (UseG1GC) {
aoqi@0 3456 // Verify G1 pre-barriers
aoqi@0 3457 const int marking_offset = in_bytes(JavaThread::satb_mark_queue_offset() + PtrQueue::byte_offset_of_active());
aoqi@0 3458
aoqi@0 3459 ResourceArea *area = Thread::current()->resource_area();
aoqi@0 3460 Unique_Node_List visited(area);
aoqi@0 3461 Node_List worklist(area);
aoqi@0 3462 // We're going to walk control flow backwards starting from the Root
aoqi@0 3463 worklist.push(_root);
aoqi@0 3464 while (worklist.size() > 0) {
aoqi@0 3465 Node* x = worklist.pop();
aoqi@0 3466 if (x == NULL || x == top()) continue;
aoqi@0 3467 if (visited.member(x)) {
aoqi@0 3468 continue;
aoqi@0 3469 } else {
aoqi@0 3470 visited.push(x);
aoqi@0 3471 }
aoqi@0 3472
aoqi@0 3473 if (x->is_Region()) {
aoqi@0 3474 for (uint i = 1; i < x->req(); i++) {
aoqi@0 3475 worklist.push(x->in(i));
aoqi@0 3476 }
aoqi@0 3477 } else {
aoqi@0 3478 worklist.push(x->in(0));
aoqi@0 3479 // We are looking for the pattern:
aoqi@0 3480 // /->ThreadLocal
aoqi@0 3481 // If->Bool->CmpI->LoadB->AddP->ConL(marking_offset)
aoqi@0 3482 // \->ConI(0)
aoqi@0 3483 // We want to verify that the If and the LoadB have the same control
aoqi@0 3484 // See GraphKit::g1_write_barrier_pre()
aoqi@0 3485 if (x->is_If()) {
aoqi@0 3486 IfNode *iff = x->as_If();
aoqi@0 3487 if (iff->in(1)->is_Bool() && iff->in(1)->in(1)->is_Cmp()) {
aoqi@0 3488 CmpNode *cmp = iff->in(1)->in(1)->as_Cmp();
aoqi@0 3489 if (cmp->Opcode() == Op_CmpI && cmp->in(2)->is_Con() && cmp->in(2)->bottom_type()->is_int()->get_con() == 0
aoqi@0 3490 && cmp->in(1)->is_Load()) {
aoqi@0 3491 LoadNode* load = cmp->in(1)->as_Load();
aoqi@0 3492 if (load->Opcode() == Op_LoadB && load->in(2)->is_AddP() && load->in(2)->in(2)->Opcode() == Op_ThreadLocal
aoqi@0 3493 && load->in(2)->in(3)->is_Con()
aoqi@0 3494 && load->in(2)->in(3)->bottom_type()->is_intptr_t()->get_con() == marking_offset) {
aoqi@0 3495
aoqi@0 3496 Node* if_ctrl = iff->in(0);
aoqi@0 3497 Node* load_ctrl = load->in(0);
aoqi@0 3498
aoqi@0 3499 if (if_ctrl != load_ctrl) {
aoqi@0 3500 // Skip possible CProj->NeverBranch in infinite loops
aoqi@0 3501 if ((if_ctrl->is_Proj() && if_ctrl->Opcode() == Op_CProj)
aoqi@0 3502 && (if_ctrl->in(0)->is_MultiBranch() && if_ctrl->in(0)->Opcode() == Op_NeverBranch)) {
aoqi@0 3503 if_ctrl = if_ctrl->in(0)->in(0);
aoqi@0 3504 }
aoqi@0 3505 }
aoqi@0 3506 assert(load_ctrl != NULL && if_ctrl == load_ctrl, "controls must match");
aoqi@0 3507 }
aoqi@0 3508 }
aoqi@0 3509 }
aoqi@0 3510 }
aoqi@0 3511 }
aoqi@0 3512 }
aoqi@0 3513 }
aoqi@0 3514 }
aoqi@0 3515
aoqi@0 3516 #endif
aoqi@0 3517
aoqi@0 3518 // The Compile object keeps track of failure reasons separately from the ciEnv.
aoqi@0 3519 // This is required because there is not quite a 1-1 relation between the
aoqi@0 3520 // ciEnv and its compilation task and the Compile object. Note that one
aoqi@0 3521 // ciEnv might use two Compile objects, if C2Compiler::compile_method decides
aoqi@0 3522 // to backtrack and retry without subsuming loads. Other than this backtracking
aoqi@0 3523 // behavior, the Compile's failure reason is quietly copied up to the ciEnv
aoqi@0 3524 // by the logic in C2Compiler.
aoqi@0 3525 void Compile::record_failure(const char* reason) {
aoqi@0 3526 if (log() != NULL) {
aoqi@0 3527 log()->elem("failure reason='%s' phase='compile'", reason);
aoqi@0 3528 }
aoqi@0 3529 if (_failure_reason == NULL) {
aoqi@0 3530 // Record the first failure reason.
aoqi@0 3531 _failure_reason = reason;
aoqi@0 3532 }
aoqi@0 3533
aoqi@0 3534 EventCompilerFailure event;
aoqi@0 3535 if (event.should_commit()) {
aoqi@0 3536 event.set_compileID(Compile::compile_id());
aoqi@0 3537 event.set_failure(reason);
aoqi@0 3538 event.commit();
aoqi@0 3539 }
aoqi@0 3540
aoqi@0 3541 if (!C->failure_reason_is(C2Compiler::retry_no_subsuming_loads())) {
aoqi@0 3542 C->print_method(PHASE_FAILURE);
aoqi@0 3543 }
aoqi@0 3544 _root = NULL; // flush the graph, too
aoqi@0 3545 }
aoqi@0 3546
aoqi@0 3547 Compile::TracePhase::TracePhase(const char* name, elapsedTimer* accumulator, bool dolog)
aoqi@0 3548 : TraceTime(NULL, accumulator, false NOT_PRODUCT( || TimeCompiler ), false),
aoqi@0 3549 _phase_name(name), _dolog(dolog)
aoqi@0 3550 {
aoqi@0 3551 if (dolog) {
aoqi@0 3552 C = Compile::current();
aoqi@0 3553 _log = C->log();
aoqi@0 3554 } else {
aoqi@0 3555 C = NULL;
aoqi@0 3556 _log = NULL;
aoqi@0 3557 }
aoqi@0 3558 if (_log != NULL) {
aoqi@0 3559 _log->begin_head("phase name='%s' nodes='%d' live='%d'", _phase_name, C->unique(), C->live_nodes());
aoqi@0 3560 _log->stamp();
aoqi@0 3561 _log->end_head();
aoqi@0 3562 }
aoqi@0 3563 }
aoqi@0 3564
aoqi@0 3565 Compile::TracePhase::~TracePhase() {
aoqi@0 3566
aoqi@0 3567 C = Compile::current();
aoqi@0 3568 if (_dolog) {
aoqi@0 3569 _log = C->log();
aoqi@0 3570 } else {
aoqi@0 3571 _log = NULL;
aoqi@0 3572 }
aoqi@0 3573
aoqi@0 3574 #ifdef ASSERT
aoqi@0 3575 if (PrintIdealNodeCount) {
aoqi@0 3576 tty->print_cr("phase name='%s' nodes='%d' live='%d' live_graph_walk='%d'",
aoqi@0 3577 _phase_name, C->unique(), C->live_nodes(), C->count_live_nodes_by_graph_walk());
aoqi@0 3578 }
aoqi@0 3579
aoqi@0 3580 if (VerifyIdealNodeCount) {
aoqi@0 3581 Compile::current()->print_missing_nodes();
aoqi@0 3582 }
aoqi@0 3583 #endif
aoqi@0 3584
aoqi@0 3585 if (_log != NULL) {
aoqi@0 3586 _log->done("phase name='%s' nodes='%d' live='%d'", _phase_name, C->unique(), C->live_nodes());
aoqi@0 3587 }
aoqi@0 3588 }
aoqi@0 3589
aoqi@0 3590 //=============================================================================
aoqi@0 3591 // Two Constant's are equal when the type and the value are equal.
aoqi@0 3592 bool Compile::Constant::operator==(const Constant& other) {
aoqi@0 3593 if (type() != other.type() ) return false;
aoqi@0 3594 if (can_be_reused() != other.can_be_reused()) return false;
aoqi@0 3595 // For floating point values we compare the bit pattern.
aoqi@0 3596 switch (type()) {
aoqi@0 3597 case T_FLOAT: return (_v._value.i == other._v._value.i);
aoqi@0 3598 case T_LONG:
aoqi@0 3599 case T_DOUBLE: return (_v._value.j == other._v._value.j);
aoqi@0 3600 case T_OBJECT:
aoqi@0 3601 case T_ADDRESS: return (_v._value.l == other._v._value.l);
aoqi@0 3602 case T_VOID: return (_v._value.l == other._v._value.l); // jump-table entries
aoqi@0 3603 case T_METADATA: return (_v._metadata == other._v._metadata);
aoqi@0 3604 default: ShouldNotReachHere();
aoqi@0 3605 }
aoqi@0 3606 return false;
aoqi@0 3607 }
aoqi@0 3608
aoqi@0 3609 static int type_to_size_in_bytes(BasicType t) {
aoqi@0 3610 switch (t) {
aoqi@0 3611 case T_LONG: return sizeof(jlong );
aoqi@0 3612 case T_FLOAT: return sizeof(jfloat );
aoqi@0 3613 case T_DOUBLE: return sizeof(jdouble);
aoqi@0 3614 case T_METADATA: return sizeof(Metadata*);
aoqi@0 3615 // We use T_VOID as marker for jump-table entries (labels) which
aoqi@0 3616 // need an internal word relocation.
aoqi@0 3617 case T_VOID:
aoqi@0 3618 case T_ADDRESS:
aoqi@0 3619 case T_OBJECT: return sizeof(jobject);
aoqi@0 3620 }
aoqi@0 3621
aoqi@0 3622 ShouldNotReachHere();
aoqi@0 3623 return -1;
aoqi@0 3624 }
aoqi@0 3625
aoqi@0 3626 int Compile::ConstantTable::qsort_comparator(Constant* a, Constant* b) {
aoqi@0 3627 // sort descending
aoqi@0 3628 if (a->freq() > b->freq()) return -1;
aoqi@0 3629 if (a->freq() < b->freq()) return 1;
aoqi@0 3630 return 0;
aoqi@0 3631 }
aoqi@0 3632
aoqi@0 3633 void Compile::ConstantTable::calculate_offsets_and_size() {
aoqi@0 3634 // First, sort the array by frequencies.
aoqi@0 3635 _constants.sort(qsort_comparator);
aoqi@0 3636
aoqi@0 3637 #ifdef ASSERT
aoqi@0 3638 // Make sure all jump-table entries were sorted to the end of the
aoqi@0 3639 // array (they have a negative frequency).
aoqi@0 3640 bool found_void = false;
aoqi@0 3641 for (int i = 0; i < _constants.length(); i++) {
aoqi@0 3642 Constant con = _constants.at(i);
aoqi@0 3643 if (con.type() == T_VOID)
aoqi@0 3644 found_void = true; // jump-tables
aoqi@0 3645 else
aoqi@0 3646 assert(!found_void, "wrong sorting");
aoqi@0 3647 }
aoqi@0 3648 #endif
aoqi@0 3649
aoqi@0 3650 int offset = 0;
aoqi@0 3651 for (int i = 0; i < _constants.length(); i++) {
aoqi@0 3652 Constant* con = _constants.adr_at(i);
aoqi@0 3653
aoqi@0 3654 // Align offset for type.
aoqi@0 3655 int typesize = type_to_size_in_bytes(con->type());
aoqi@0 3656 offset = align_size_up(offset, typesize);
aoqi@0 3657 con->set_offset(offset); // set constant's offset
aoqi@0 3658
aoqi@0 3659 if (con->type() == T_VOID) {
aoqi@0 3660 MachConstantNode* n = (MachConstantNode*) con->get_jobject();
aoqi@0 3661 offset = offset + typesize * n->outcnt(); // expand jump-table
aoqi@0 3662 } else {
aoqi@0 3663 offset = offset + typesize;
aoqi@0 3664 }
aoqi@0 3665 }
aoqi@0 3666
aoqi@0 3667 // Align size up to the next section start (which is insts; see
aoqi@0 3668 // CodeBuffer::align_at_start).
aoqi@0 3669 assert(_size == -1, "already set?");
aoqi@0 3670 _size = align_size_up(offset, CodeEntryAlignment);
aoqi@0 3671 }
aoqi@0 3672
aoqi@0 3673 void Compile::ConstantTable::emit(CodeBuffer& cb) {
aoqi@0 3674 MacroAssembler _masm(&cb);
aoqi@0 3675 for (int i = 0; i < _constants.length(); i++) {
aoqi@0 3676 Constant con = _constants.at(i);
aoqi@0 3677 address constant_addr;
aoqi@0 3678 switch (con.type()) {
aoqi@0 3679 case T_LONG: constant_addr = _masm.long_constant( con.get_jlong() ); break;
aoqi@0 3680 case T_FLOAT: constant_addr = _masm.float_constant( con.get_jfloat() ); break;
aoqi@0 3681 case T_DOUBLE: constant_addr = _masm.double_constant(con.get_jdouble()); break;
aoqi@0 3682 case T_OBJECT: {
aoqi@0 3683 jobject obj = con.get_jobject();
aoqi@0 3684 int oop_index = _masm.oop_recorder()->find_index(obj);
aoqi@0 3685 constant_addr = _masm.address_constant((address) obj, oop_Relocation::spec(oop_index));
aoqi@0 3686 break;
aoqi@0 3687 }
aoqi@0 3688 case T_ADDRESS: {
aoqi@0 3689 address addr = (address) con.get_jobject();
aoqi@0 3690 constant_addr = _masm.address_constant(addr);
aoqi@0 3691 break;
aoqi@0 3692 }
aoqi@0 3693 // We use T_VOID as marker for jump-table entries (labels) which
aoqi@0 3694 // need an internal word relocation.
aoqi@0 3695 case T_VOID: {
aoqi@0 3696 MachConstantNode* n = (MachConstantNode*) con.get_jobject();
aoqi@0 3697 // Fill the jump-table with a dummy word. The real value is
aoqi@0 3698 // filled in later in fill_jump_table.
aoqi@0 3699 address dummy = (address) n;
aoqi@0 3700 constant_addr = _masm.address_constant(dummy);
aoqi@0 3701 // Expand jump-table
aoqi@0 3702 for (uint i = 1; i < n->outcnt(); i++) {
aoqi@0 3703 address temp_addr = _masm.address_constant(dummy + i);
aoqi@0 3704 assert(temp_addr, "consts section too small");
aoqi@0 3705 }
aoqi@0 3706 break;
aoqi@0 3707 }
aoqi@0 3708 case T_METADATA: {
aoqi@0 3709 Metadata* obj = con.get_metadata();
aoqi@0 3710 int metadata_index = _masm.oop_recorder()->find_index(obj);
aoqi@0 3711 constant_addr = _masm.address_constant((address) obj, metadata_Relocation::spec(metadata_index));
aoqi@0 3712 break;
aoqi@0 3713 }
aoqi@0 3714 default: ShouldNotReachHere();
aoqi@0 3715 }
aoqi@0 3716 assert(constant_addr, "consts section too small");
aoqi@0 3717 assert((constant_addr - _masm.code()->consts()->start()) == con.offset(),
aoqi@0 3718 err_msg_res("must be: %d == %d", (int) (constant_addr - _masm.code()->consts()->start()), (int)(con.offset())));
aoqi@0 3719 }
aoqi@0 3720 }
aoqi@0 3721
aoqi@0 3722 int Compile::ConstantTable::find_offset(Constant& con) const {
aoqi@0 3723 int idx = _constants.find(con);
aoqi@0 3724 assert(idx != -1, "constant must be in constant table");
aoqi@0 3725 int offset = _constants.at(idx).offset();
aoqi@0 3726 assert(offset != -1, "constant table not emitted yet?");
aoqi@0 3727 return offset;
aoqi@0 3728 }
aoqi@0 3729
aoqi@0 3730 void Compile::ConstantTable::add(Constant& con) {
aoqi@0 3731 if (con.can_be_reused()) {
aoqi@0 3732 int idx = _constants.find(con);
aoqi@0 3733 if (idx != -1 && _constants.at(idx).can_be_reused()) {
aoqi@0 3734 _constants.adr_at(idx)->inc_freq(con.freq()); // increase the frequency by the current value
aoqi@0 3735 return;
aoqi@0 3736 }
aoqi@0 3737 }
aoqi@0 3738 (void) _constants.append(con);
aoqi@0 3739 }
aoqi@0 3740
aoqi@0 3741 Compile::Constant Compile::ConstantTable::add(MachConstantNode* n, BasicType type, jvalue value) {
aoqi@0 3742 Block* b = Compile::current()->cfg()->get_block_for_node(n);
aoqi@0 3743 Constant con(type, value, b->_freq);
aoqi@0 3744 add(con);
aoqi@0 3745 return con;
aoqi@0 3746 }
aoqi@0 3747
aoqi@0 3748 Compile::Constant Compile::ConstantTable::add(Metadata* metadata) {
aoqi@0 3749 Constant con(metadata);
aoqi@0 3750 add(con);
aoqi@0 3751 return con;
aoqi@0 3752 }
aoqi@0 3753
aoqi@0 3754 Compile::Constant Compile::ConstantTable::add(MachConstantNode* n, MachOper* oper) {
aoqi@0 3755 jvalue value;
aoqi@0 3756 BasicType type = oper->type()->basic_type();
aoqi@0 3757 switch (type) {
aoqi@0 3758 case T_LONG: value.j = oper->constantL(); break;
aoqi@0 3759 case T_FLOAT: value.f = oper->constantF(); break;
aoqi@0 3760 case T_DOUBLE: value.d = oper->constantD(); break;
aoqi@0 3761 case T_OBJECT:
aoqi@0 3762 case T_ADDRESS: value.l = (jobject) oper->constant(); break;
aoqi@0 3763 case T_METADATA: return add((Metadata*)oper->constant()); break;
aoqi@0 3764 default: guarantee(false, err_msg_res("unhandled type: %s", type2name(type)));
aoqi@0 3765 }
aoqi@0 3766 return add(n, type, value);
aoqi@0 3767 }
aoqi@0 3768
aoqi@0 3769 Compile::Constant Compile::ConstantTable::add_jump_table(MachConstantNode* n) {
aoqi@0 3770 jvalue value;
aoqi@0 3771 // We can use the node pointer here to identify the right jump-table
aoqi@0 3772 // as this method is called from Compile::Fill_buffer right before
aoqi@0 3773 // the MachNodes are emitted and the jump-table is filled (means the
aoqi@0 3774 // MachNode pointers do not change anymore).
aoqi@0 3775 value.l = (jobject) n;
aoqi@0 3776 Constant con(T_VOID, value, next_jump_table_freq(), false); // Labels of a jump-table cannot be reused.
aoqi@0 3777 add(con);
aoqi@0 3778 return con;
aoqi@0 3779 }
aoqi@0 3780
aoqi@0 3781 void Compile::ConstantTable::fill_jump_table(CodeBuffer& cb, MachConstantNode* n, GrowableArray<Label*> labels) const {
aoqi@0 3782 // If called from Compile::scratch_emit_size do nothing.
aoqi@0 3783 if (Compile::current()->in_scratch_emit_size()) return;
aoqi@0 3784
aoqi@0 3785 assert(labels.is_nonempty(), "must be");
aoqi@0 3786 assert((uint) labels.length() == n->outcnt(), err_msg_res("must be equal: %d == %d", labels.length(), n->outcnt()));
aoqi@0 3787
aoqi@0 3788 // Since MachConstantNode::constant_offset() also contains
aoqi@0 3789 // table_base_offset() we need to subtract the table_base_offset()
aoqi@0 3790 // to get the plain offset into the constant table.
aoqi@0 3791 int offset = n->constant_offset() - table_base_offset();
aoqi@0 3792
aoqi@0 3793 MacroAssembler _masm(&cb);
aoqi@0 3794 address* jump_table_base = (address*) (_masm.code()->consts()->start() + offset);
aoqi@0 3795
aoqi@0 3796 for (uint i = 0; i < n->outcnt(); i++) {
aoqi@0 3797 address* constant_addr = &jump_table_base[i];
aoqi@0 3798 assert(*constant_addr == (((address) n) + i), err_msg_res("all jump-table entries must contain adjusted node pointer: " INTPTR_FORMAT " == " INTPTR_FORMAT, p2i(*constant_addr), p2i(((address) n) + i)));
aoqi@0 3799 *constant_addr = cb.consts()->target(*labels.at(i), (address) constant_addr);
aoqi@0 3800 cb.consts()->relocate((address) constant_addr, relocInfo::internal_word_type);
aoqi@0 3801 }
aoqi@0 3802 }
aoqi@0 3803
aoqi@0 3804 void Compile::dump_inlining() {
aoqi@0 3805 if (print_inlining() || print_intrinsics()) {
aoqi@0 3806 // Print inlining message for candidates that we couldn't inline
aoqi@0 3807 // for lack of space or non constant receiver
aoqi@0 3808 for (int i = 0; i < _late_inlines.length(); i++) {
aoqi@0 3809 CallGenerator* cg = _late_inlines.at(i);
aoqi@0 3810 cg->print_inlining_late("live nodes > LiveNodeCountInliningCutoff");
aoqi@0 3811 }
aoqi@0 3812 Unique_Node_List useful;
aoqi@0 3813 useful.push(root());
aoqi@0 3814 for (uint next = 0; next < useful.size(); ++next) {
aoqi@0 3815 Node* n = useful.at(next);
aoqi@0 3816 if (n->is_Call() && n->as_Call()->generator() != NULL && n->as_Call()->generator()->call_node() == n) {
aoqi@0 3817 CallNode* call = n->as_Call();
aoqi@0 3818 CallGenerator* cg = call->generator();
aoqi@0 3819 cg->print_inlining_late("receiver not constant");
aoqi@0 3820 }
aoqi@0 3821 uint max = n->len();
aoqi@0 3822 for ( uint i = 0; i < max; ++i ) {
aoqi@0 3823 Node *m = n->in(i);
aoqi@0 3824 if ( m == NULL ) continue;
aoqi@0 3825 useful.push(m);
aoqi@0 3826 }
aoqi@0 3827 }
aoqi@0 3828 for (int i = 0; i < _print_inlining_list->length(); i++) {
aoqi@0 3829 tty->print("%s", _print_inlining_list->adr_at(i)->ss()->as_string());
aoqi@0 3830 }
aoqi@0 3831 }
aoqi@0 3832 }
aoqi@0 3833
aoqi@0 3834 // Dump inlining replay data to the stream.
aoqi@0 3835 // Don't change thread state and acquire any locks.
aoqi@0 3836 void Compile::dump_inline_data(outputStream* out) {
aoqi@0 3837 InlineTree* inl_tree = ilt();
aoqi@0 3838 if (inl_tree != NULL) {
aoqi@0 3839 out->print(" inline %d", inl_tree->count());
aoqi@0 3840 inl_tree->dump_replay_data(out);
aoqi@0 3841 }
aoqi@0 3842 }
aoqi@0 3843
aoqi@0 3844 int Compile::cmp_expensive_nodes(Node* n1, Node* n2) {
aoqi@0 3845 if (n1->Opcode() < n2->Opcode()) return -1;
aoqi@0 3846 else if (n1->Opcode() > n2->Opcode()) return 1;
aoqi@0 3847
aoqi@0 3848 assert(n1->req() == n2->req(), err_msg_res("can't compare %s nodes: n1->req() = %d, n2->req() = %d", NodeClassNames[n1->Opcode()], n1->req(), n2->req()));
aoqi@0 3849 for (uint i = 1; i < n1->req(); i++) {
aoqi@0 3850 if (n1->in(i) < n2->in(i)) return -1;
aoqi@0 3851 else if (n1->in(i) > n2->in(i)) return 1;
aoqi@0 3852 }
aoqi@0 3853
aoqi@0 3854 return 0;
aoqi@0 3855 }
aoqi@0 3856
aoqi@0 3857 int Compile::cmp_expensive_nodes(Node** n1p, Node** n2p) {
aoqi@0 3858 Node* n1 = *n1p;
aoqi@0 3859 Node* n2 = *n2p;
aoqi@0 3860
aoqi@0 3861 return cmp_expensive_nodes(n1, n2);
aoqi@0 3862 }
aoqi@0 3863
aoqi@0 3864 void Compile::sort_expensive_nodes() {
aoqi@0 3865 if (!expensive_nodes_sorted()) {
aoqi@0 3866 _expensive_nodes->sort(cmp_expensive_nodes);
aoqi@0 3867 }
aoqi@0 3868 }
aoqi@0 3869
aoqi@0 3870 bool Compile::expensive_nodes_sorted() const {
aoqi@0 3871 for (int i = 1; i < _expensive_nodes->length(); i++) {
aoqi@0 3872 if (cmp_expensive_nodes(_expensive_nodes->adr_at(i), _expensive_nodes->adr_at(i-1)) < 0) {
aoqi@0 3873 return false;
aoqi@0 3874 }
aoqi@0 3875 }
aoqi@0 3876 return true;
aoqi@0 3877 }
aoqi@0 3878
aoqi@0 3879 bool Compile::should_optimize_expensive_nodes(PhaseIterGVN &igvn) {
aoqi@0 3880 if (_expensive_nodes->length() == 0) {
aoqi@0 3881 return false;
aoqi@0 3882 }
aoqi@0 3883
aoqi@0 3884 assert(OptimizeExpensiveOps, "optimization off?");
aoqi@0 3885
aoqi@0 3886 // Take this opportunity to remove dead nodes from the list
aoqi@0 3887 int j = 0;
aoqi@0 3888 for (int i = 0; i < _expensive_nodes->length(); i++) {
aoqi@0 3889 Node* n = _expensive_nodes->at(i);
aoqi@0 3890 if (!n->is_unreachable(igvn)) {
aoqi@0 3891 assert(n->is_expensive(), "should be expensive");
aoqi@0 3892 _expensive_nodes->at_put(j, n);
aoqi@0 3893 j++;
aoqi@0 3894 }
aoqi@0 3895 }
aoqi@0 3896 _expensive_nodes->trunc_to(j);
aoqi@0 3897
aoqi@0 3898 // Then sort the list so that similar nodes are next to each other
aoqi@0 3899 // and check for at least two nodes of identical kind with same data
aoqi@0 3900 // inputs.
aoqi@0 3901 sort_expensive_nodes();
aoqi@0 3902
aoqi@0 3903 for (int i = 0; i < _expensive_nodes->length()-1; i++) {
aoqi@0 3904 if (cmp_expensive_nodes(_expensive_nodes->adr_at(i), _expensive_nodes->adr_at(i+1)) == 0) {
aoqi@0 3905 return true;
aoqi@0 3906 }
aoqi@0 3907 }
aoqi@0 3908
aoqi@0 3909 return false;
aoqi@0 3910 }
aoqi@0 3911
aoqi@0 3912 void Compile::cleanup_expensive_nodes(PhaseIterGVN &igvn) {
aoqi@0 3913 if (_expensive_nodes->length() == 0) {
aoqi@0 3914 return;
aoqi@0 3915 }
aoqi@0 3916
aoqi@0 3917 assert(OptimizeExpensiveOps, "optimization off?");
aoqi@0 3918
aoqi@0 3919 // Sort to bring similar nodes next to each other and clear the
aoqi@0 3920 // control input of nodes for which there's only a single copy.
aoqi@0 3921 sort_expensive_nodes();
aoqi@0 3922
aoqi@0 3923 int j = 0;
aoqi@0 3924 int identical = 0;
aoqi@0 3925 int i = 0;
aoqi@0 3926 for (; i < _expensive_nodes->length()-1; i++) {
aoqi@0 3927 assert(j <= i, "can't write beyond current index");
aoqi@0 3928 if (_expensive_nodes->at(i)->Opcode() == _expensive_nodes->at(i+1)->Opcode()) {
aoqi@0 3929 identical++;
aoqi@0 3930 _expensive_nodes->at_put(j++, _expensive_nodes->at(i));
aoqi@0 3931 continue;
aoqi@0 3932 }
aoqi@0 3933 if (identical > 0) {
aoqi@0 3934 _expensive_nodes->at_put(j++, _expensive_nodes->at(i));
aoqi@0 3935 identical = 0;
aoqi@0 3936 } else {
aoqi@0 3937 Node* n = _expensive_nodes->at(i);
aoqi@0 3938 igvn.hash_delete(n);
aoqi@0 3939 n->set_req(0, NULL);
aoqi@0 3940 igvn.hash_insert(n);
aoqi@0 3941 }
aoqi@0 3942 }
aoqi@0 3943 if (identical > 0) {
aoqi@0 3944 _expensive_nodes->at_put(j++, _expensive_nodes->at(i));
aoqi@0 3945 } else if (_expensive_nodes->length() >= 1) {
aoqi@0 3946 Node* n = _expensive_nodes->at(i);
aoqi@0 3947 igvn.hash_delete(n);
aoqi@0 3948 n->set_req(0, NULL);
aoqi@0 3949 igvn.hash_insert(n);
aoqi@0 3950 }
aoqi@0 3951 _expensive_nodes->trunc_to(j);
aoqi@0 3952 }
aoqi@0 3953
aoqi@0 3954 void Compile::add_expensive_node(Node * n) {
aoqi@0 3955 assert(!_expensive_nodes->contains(n), "duplicate entry in expensive list");
aoqi@0 3956 assert(n->is_expensive(), "expensive nodes with non-null control here only");
aoqi@0 3957 assert(!n->is_CFG() && !n->is_Mem(), "no cfg or memory nodes here");
aoqi@0 3958 if (OptimizeExpensiveOps) {
aoqi@0 3959 _expensive_nodes->append(n);
aoqi@0 3960 } else {
aoqi@0 3961 // Clear control input and let IGVN optimize expensive nodes if
aoqi@0 3962 // OptimizeExpensiveOps is off.
aoqi@0 3963 n->set_req(0, NULL);
aoqi@0 3964 }
aoqi@0 3965 }
aoqi@0 3966
aoqi@0 3967 /**
aoqi@0 3968 * Remove the speculative part of types and clean up the graph
aoqi@0 3969 */
aoqi@0 3970 void Compile::remove_speculative_types(PhaseIterGVN &igvn) {
aoqi@0 3971 if (UseTypeSpeculation) {
aoqi@0 3972 Unique_Node_List worklist;
aoqi@0 3973 worklist.push(root());
aoqi@0 3974 int modified = 0;
aoqi@0 3975 // Go over all type nodes that carry a speculative type, drop the
aoqi@0 3976 // speculative part of the type and enqueue the node for an igvn
aoqi@0 3977 // which may optimize it out.
aoqi@0 3978 for (uint next = 0; next < worklist.size(); ++next) {
aoqi@0 3979 Node *n = worklist.at(next);
aoqi@0 3980 if (n->is_Type()) {
aoqi@0 3981 TypeNode* tn = n->as_Type();
aoqi@0 3982 const Type* t = tn->type();
aoqi@0 3983 const Type* t_no_spec = t->remove_speculative();
aoqi@0 3984 if (t_no_spec != t) {
aoqi@0 3985 bool in_hash = igvn.hash_delete(n);
aoqi@0 3986 assert(in_hash, "node should be in igvn hash table");
aoqi@0 3987 tn->set_type(t_no_spec);
aoqi@0 3988 igvn.hash_insert(n);
aoqi@0 3989 igvn._worklist.push(n); // give it a chance to go away
aoqi@0 3990 modified++;
aoqi@0 3991 }
aoqi@0 3992 }
aoqi@0 3993 uint max = n->len();
aoqi@0 3994 for( uint i = 0; i < max; ++i ) {
aoqi@0 3995 Node *m = n->in(i);
aoqi@0 3996 if (not_a_node(m)) continue;
aoqi@0 3997 worklist.push(m);
aoqi@0 3998 }
aoqi@0 3999 }
aoqi@0 4000 // Drop the speculative part of all types in the igvn's type table
aoqi@0 4001 igvn.remove_speculative_types();
aoqi@0 4002 if (modified > 0) {
aoqi@0 4003 igvn.optimize();
aoqi@0 4004 }
aoqi@0 4005 #ifdef ASSERT
aoqi@0 4006 // Verify that after the IGVN is over no speculative type has resurfaced
aoqi@0 4007 worklist.clear();
aoqi@0 4008 worklist.push(root());
aoqi@0 4009 for (uint next = 0; next < worklist.size(); ++next) {
aoqi@0 4010 Node *n = worklist.at(next);
aoqi@0 4011 const Type* t = igvn.type_or_null(n);
aoqi@0 4012 assert((t == NULL) || (t == t->remove_speculative()), "no more speculative types");
aoqi@0 4013 if (n->is_Type()) {
aoqi@0 4014 t = n->as_Type()->type();
aoqi@0 4015 assert(t == t->remove_speculative(), "no more speculative types");
aoqi@0 4016 }
aoqi@0 4017 uint max = n->len();
aoqi@0 4018 for( uint i = 0; i < max; ++i ) {
aoqi@0 4019 Node *m = n->in(i);
aoqi@0 4020 if (not_a_node(m)) continue;
aoqi@0 4021 worklist.push(m);
aoqi@0 4022 }
aoqi@0 4023 }
aoqi@0 4024 igvn.check_no_speculative_types();
aoqi@0 4025 #endif
aoqi@0 4026 }
aoqi@0 4027 }
aoqi@0 4028
aoqi@0 4029 // Auxiliary method to support randomized stressing/fuzzing.
aoqi@0 4030 //
aoqi@0 4031 // This method can be called the arbitrary number of times, with current count
aoqi@0 4032 // as the argument. The logic allows selecting a single candidate from the
aoqi@0 4033 // running list of candidates as follows:
aoqi@0 4034 // int count = 0;
aoqi@0 4035 // Cand* selected = null;
aoqi@0 4036 // while(cand = cand->next()) {
aoqi@0 4037 // if (randomized_select(++count)) {
aoqi@0 4038 // selected = cand;
aoqi@0 4039 // }
aoqi@0 4040 // }
aoqi@0 4041 //
aoqi@0 4042 // Including count equalizes the chances any candidate is "selected".
aoqi@0 4043 // This is useful when we don't have the complete list of candidates to choose
aoqi@0 4044 // from uniformly. In this case, we need to adjust the randomicity of the
aoqi@0 4045 // selection, or else we will end up biasing the selection towards the latter
aoqi@0 4046 // candidates.
aoqi@0 4047 //
aoqi@0 4048 // Quick back-envelope calculation shows that for the list of n candidates
aoqi@0 4049 // the equal probability for the candidate to persist as "best" can be
aoqi@0 4050 // achieved by replacing it with "next" k-th candidate with the probability
aoqi@0 4051 // of 1/k. It can be easily shown that by the end of the run, the
aoqi@0 4052 // probability for any candidate is converged to 1/n, thus giving the
aoqi@0 4053 // uniform distribution among all the candidates.
aoqi@0 4054 //
aoqi@0 4055 // We don't care about the domain size as long as (RANDOMIZED_DOMAIN / count) is large.
aoqi@0 4056 #define RANDOMIZED_DOMAIN_POW 29
aoqi@0 4057 #define RANDOMIZED_DOMAIN (1 << RANDOMIZED_DOMAIN_POW)
aoqi@0 4058 #define RANDOMIZED_DOMAIN_MASK ((1 << (RANDOMIZED_DOMAIN_POW + 1)) - 1)
aoqi@0 4059 bool Compile::randomized_select(int count) {
aoqi@0 4060 assert(count > 0, "only positive");
aoqi@0 4061 return (os::random() & RANDOMIZED_DOMAIN_MASK) < (RANDOMIZED_DOMAIN / count);
aoqi@0 4062 }

mercurial