src/share/vm/opto/compile.cpp

Tue, 10 Mar 2020 10:46:35 +0100

author
mdoerr
date
Tue, 10 Mar 2020 10:46:35 +0100
changeset 9912
97d09139b360
parent 9858
b985cbb00e68
child 9931
fd44df5e3bc3
child 9957
d2ec2776ad0c
permissions
-rw-r--r--

8146612: C2: Precedence edges specification violated
Reviewed-by: kvn

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

mercurial