src/share/vm/opto/compile.cpp

Wed, 14 Oct 2020 17:44:48 +0800

author
aoqi
date
Wed, 14 Oct 2020 17:44:48 +0800
changeset 9931
fd44df5e3bc3
parent 9572
624a0741915c
parent 9858
b985cbb00e68
child 10015
eb7ce841ccec
permissions
-rw-r--r--

Merge

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

mercurial