annotate src/share/vm/c1/c1_GraphBuilder.cpp @ 470:ad8c8ca4ab0f

6785258: Update copyright year Summary: Update copyright for files that have been modified starting July 2008 to Dec 2008 Reviewed-by: katleman, ohair, tbell
author xdono
date Mon, 15 Dec 2008 16:55:11 -0800
parents 3a86a8dcf27c
children be93aad57795
rev   line source
duke@0 1 /*
xdono@470 2 * Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 #include "incls/_precompiled.incl"
duke@0 26 #include "incls/_c1_GraphBuilder.cpp.incl"
duke@0 27
duke@0 28 class BlockListBuilder VALUE_OBJ_CLASS_SPEC {
duke@0 29 private:
duke@0 30 Compilation* _compilation;
duke@0 31 IRScope* _scope;
duke@0 32
duke@0 33 BlockList _blocks; // internal list of all blocks
duke@0 34 BlockList* _bci2block; // mapping from bci to blocks for GraphBuilder
duke@0 35
duke@0 36 // fields used by mark_loops
duke@0 37 BitMap _active; // for iteration of control flow graph
duke@0 38 BitMap _visited; // for iteration of control flow graph
duke@0 39 intArray _loop_map; // caches the information if a block is contained in a loop
duke@0 40 int _next_loop_index; // next free loop number
duke@0 41 int _next_block_number; // for reverse postorder numbering of blocks
duke@0 42
duke@0 43 // accessors
duke@0 44 Compilation* compilation() const { return _compilation; }
duke@0 45 IRScope* scope() const { return _scope; }
duke@0 46 ciMethod* method() const { return scope()->method(); }
duke@0 47 XHandlers* xhandlers() const { return scope()->xhandlers(); }
duke@0 48
duke@0 49 // unified bailout support
duke@0 50 void bailout(const char* msg) const { compilation()->bailout(msg); }
duke@0 51 bool bailed_out() const { return compilation()->bailed_out(); }
duke@0 52
duke@0 53 // helper functions
duke@0 54 BlockBegin* make_block_at(int bci, BlockBegin* predecessor);
duke@0 55 void handle_exceptions(BlockBegin* current, int cur_bci);
duke@0 56 void handle_jsr(BlockBegin* current, int sr_bci, int next_bci);
duke@0 57 void store_one(BlockBegin* current, int local);
duke@0 58 void store_two(BlockBegin* current, int local);
duke@0 59 void set_entries(int osr_bci);
duke@0 60 void set_leaders();
duke@0 61
duke@0 62 void make_loop_header(BlockBegin* block);
duke@0 63 void mark_loops();
duke@0 64 int mark_loops(BlockBegin* b, bool in_subroutine);
duke@0 65
duke@0 66 // debugging
duke@0 67 #ifndef PRODUCT
duke@0 68 void print();
duke@0 69 #endif
duke@0 70
duke@0 71 public:
duke@0 72 // creation
duke@0 73 BlockListBuilder(Compilation* compilation, IRScope* scope, int osr_bci);
duke@0 74
duke@0 75 // accessors for GraphBuilder
duke@0 76 BlockList* bci2block() const { return _bci2block; }
duke@0 77 };
duke@0 78
duke@0 79
duke@0 80 // Implementation of BlockListBuilder
duke@0 81
duke@0 82 BlockListBuilder::BlockListBuilder(Compilation* compilation, IRScope* scope, int osr_bci)
duke@0 83 : _compilation(compilation)
duke@0 84 , _scope(scope)
duke@0 85 , _blocks(16)
duke@0 86 , _bci2block(new BlockList(scope->method()->code_size(), NULL))
duke@0 87 , _next_block_number(0)
duke@0 88 , _active() // size not known yet
duke@0 89 , _visited() // size not known yet
duke@0 90 , _next_loop_index(0)
duke@0 91 , _loop_map() // size not known yet
duke@0 92 {
duke@0 93 set_entries(osr_bci);
duke@0 94 set_leaders();
duke@0 95 CHECK_BAILOUT();
duke@0 96
duke@0 97 mark_loops();
duke@0 98 NOT_PRODUCT(if (PrintInitialBlockList) print());
duke@0 99
duke@0 100 #ifndef PRODUCT
duke@0 101 if (PrintCFGToFile) {
duke@0 102 stringStream title;
duke@0 103 title.print("BlockListBuilder ");
duke@0 104 scope->method()->print_name(&title);
duke@0 105 CFGPrinter::print_cfg(_bci2block, title.as_string(), false, false);
duke@0 106 }
duke@0 107 #endif
duke@0 108 }
duke@0 109
duke@0 110
duke@0 111 void BlockListBuilder::set_entries(int osr_bci) {
duke@0 112 // generate start blocks
duke@0 113 BlockBegin* std_entry = make_block_at(0, NULL);
duke@0 114 if (scope()->caller() == NULL) {
duke@0 115 std_entry->set(BlockBegin::std_entry_flag);
duke@0 116 }
duke@0 117 if (osr_bci != -1) {
duke@0 118 BlockBegin* osr_entry = make_block_at(osr_bci, NULL);
duke@0 119 osr_entry->set(BlockBegin::osr_entry_flag);
duke@0 120 }
duke@0 121
duke@0 122 // generate exception entry blocks
duke@0 123 XHandlers* list = xhandlers();
duke@0 124 const int n = list->length();
duke@0 125 for (int i = 0; i < n; i++) {
duke@0 126 XHandler* h = list->handler_at(i);
duke@0 127 BlockBegin* entry = make_block_at(h->handler_bci(), NULL);
duke@0 128 entry->set(BlockBegin::exception_entry_flag);
duke@0 129 h->set_entry_block(entry);
duke@0 130 }
duke@0 131 }
duke@0 132
duke@0 133
duke@0 134 BlockBegin* BlockListBuilder::make_block_at(int cur_bci, BlockBegin* predecessor) {
duke@0 135 assert(method()->bci_block_start().at(cur_bci), "wrong block starts of MethodLivenessAnalyzer");
duke@0 136
duke@0 137 BlockBegin* block = _bci2block->at(cur_bci);
duke@0 138 if (block == NULL) {
duke@0 139 block = new BlockBegin(cur_bci);
duke@0 140 block->init_stores_to_locals(method()->max_locals());
duke@0 141 _bci2block->at_put(cur_bci, block);
duke@0 142 _blocks.append(block);
duke@0 143
duke@0 144 assert(predecessor == NULL || predecessor->bci() < cur_bci, "targets for backward branches must already exist");
duke@0 145 }
duke@0 146
duke@0 147 if (predecessor != NULL) {
duke@0 148 if (block->is_set(BlockBegin::exception_entry_flag)) {
duke@0 149 BAILOUT_("Exception handler can be reached by both normal and exceptional control flow", block);
duke@0 150 }
duke@0 151
duke@0 152 predecessor->add_successor(block);
duke@0 153 block->increment_total_preds();
duke@0 154 }
duke@0 155
duke@0 156 return block;
duke@0 157 }
duke@0 158
duke@0 159
duke@0 160 inline void BlockListBuilder::store_one(BlockBegin* current, int local) {
duke@0 161 current->stores_to_locals().set_bit(local);
duke@0 162 }
duke@0 163 inline void BlockListBuilder::store_two(BlockBegin* current, int local) {
duke@0 164 store_one(current, local);
duke@0 165 store_one(current, local + 1);
duke@0 166 }
duke@0 167
duke@0 168
duke@0 169 void BlockListBuilder::handle_exceptions(BlockBegin* current, int cur_bci) {
duke@0 170 // Draws edges from a block to its exception handlers
duke@0 171 XHandlers* list = xhandlers();
duke@0 172 const int n = list->length();
duke@0 173
duke@0 174 for (int i = 0; i < n; i++) {
duke@0 175 XHandler* h = list->handler_at(i);
duke@0 176
duke@0 177 if (h->covers(cur_bci)) {
duke@0 178 BlockBegin* entry = h->entry_block();
duke@0 179 assert(entry != NULL && entry == _bci2block->at(h->handler_bci()), "entry must be set");
duke@0 180 assert(entry->is_set(BlockBegin::exception_entry_flag), "flag must be set");
duke@0 181
duke@0 182 // add each exception handler only once
duke@0 183 if (!current->is_successor(entry)) {
duke@0 184 current->add_successor(entry);
duke@0 185 entry->increment_total_preds();
duke@0 186 }
duke@0 187
duke@0 188 // stop when reaching catchall
duke@0 189 if (h->catch_type() == 0) break;
duke@0 190 }
duke@0 191 }
duke@0 192 }
duke@0 193
duke@0 194 void BlockListBuilder::handle_jsr(BlockBegin* current, int sr_bci, int next_bci) {
duke@0 195 // start a new block after jsr-bytecode and link this block into cfg
duke@0 196 make_block_at(next_bci, current);
duke@0 197
duke@0 198 // start a new block at the subroutine entry at mark it with special flag
duke@0 199 BlockBegin* sr_block = make_block_at(sr_bci, current);
duke@0 200 if (!sr_block->is_set(BlockBegin::subroutine_entry_flag)) {
duke@0 201 sr_block->set(BlockBegin::subroutine_entry_flag);
duke@0 202 }
duke@0 203 }
duke@0 204
duke@0 205
duke@0 206 void BlockListBuilder::set_leaders() {
duke@0 207 bool has_xhandlers = xhandlers()->has_handlers();
duke@0 208 BlockBegin* current = NULL;
duke@0 209
duke@0 210 // The information which bci starts a new block simplifies the analysis
duke@0 211 // Without it, backward branches could jump to a bci where no block was created
duke@0 212 // during bytecode iteration. This would require the creation of a new block at the
duke@0 213 // branch target and a modification of the successor lists.
duke@0 214 BitMap bci_block_start = method()->bci_block_start();
duke@0 215
duke@0 216 ciBytecodeStream s(method());
duke@0 217 while (s.next() != ciBytecodeStream::EOBC()) {
duke@0 218 int cur_bci = s.cur_bci();
duke@0 219
duke@0 220 if (bci_block_start.at(cur_bci)) {
duke@0 221 current = make_block_at(cur_bci, current);
duke@0 222 }
duke@0 223 assert(current != NULL, "must have current block");
duke@0 224
duke@0 225 if (has_xhandlers && GraphBuilder::can_trap(method(), s.cur_bc())) {
duke@0 226 handle_exceptions(current, cur_bci);
duke@0 227 }
duke@0 228
duke@0 229 switch (s.cur_bc()) {
duke@0 230 // track stores to local variables for selective creation of phi functions
duke@0 231 case Bytecodes::_iinc: store_one(current, s.get_index()); break;
duke@0 232 case Bytecodes::_istore: store_one(current, s.get_index()); break;
duke@0 233 case Bytecodes::_lstore: store_two(current, s.get_index()); break;
duke@0 234 case Bytecodes::_fstore: store_one(current, s.get_index()); break;
duke@0 235 case Bytecodes::_dstore: store_two(current, s.get_index()); break;
duke@0 236 case Bytecodes::_astore: store_one(current, s.get_index()); break;
duke@0 237 case Bytecodes::_istore_0: store_one(current, 0); break;
duke@0 238 case Bytecodes::_istore_1: store_one(current, 1); break;
duke@0 239 case Bytecodes::_istore_2: store_one(current, 2); break;
duke@0 240 case Bytecodes::_istore_3: store_one(current, 3); break;
duke@0 241 case Bytecodes::_lstore_0: store_two(current, 0); break;
duke@0 242 case Bytecodes::_lstore_1: store_two(current, 1); break;
duke@0 243 case Bytecodes::_lstore_2: store_two(current, 2); break;
duke@0 244 case Bytecodes::_lstore_3: store_two(current, 3); break;
duke@0 245 case Bytecodes::_fstore_0: store_one(current, 0); break;
duke@0 246 case Bytecodes::_fstore_1: store_one(current, 1); break;
duke@0 247 case Bytecodes::_fstore_2: store_one(current, 2); break;
duke@0 248 case Bytecodes::_fstore_3: store_one(current, 3); break;
duke@0 249 case Bytecodes::_dstore_0: store_two(current, 0); break;
duke@0 250 case Bytecodes::_dstore_1: store_two(current, 1); break;
duke@0 251 case Bytecodes::_dstore_2: store_two(current, 2); break;
duke@0 252 case Bytecodes::_dstore_3: store_two(current, 3); break;
duke@0 253 case Bytecodes::_astore_0: store_one(current, 0); break;
duke@0 254 case Bytecodes::_astore_1: store_one(current, 1); break;
duke@0 255 case Bytecodes::_astore_2: store_one(current, 2); break;
duke@0 256 case Bytecodes::_astore_3: store_one(current, 3); break;
duke@0 257
duke@0 258 // track bytecodes that affect the control flow
duke@0 259 case Bytecodes::_athrow: // fall through
duke@0 260 case Bytecodes::_ret: // fall through
duke@0 261 case Bytecodes::_ireturn: // fall through
duke@0 262 case Bytecodes::_lreturn: // fall through
duke@0 263 case Bytecodes::_freturn: // fall through
duke@0 264 case Bytecodes::_dreturn: // fall through
duke@0 265 case Bytecodes::_areturn: // fall through
duke@0 266 case Bytecodes::_return:
duke@0 267 current = NULL;
duke@0 268 break;
duke@0 269
duke@0 270 case Bytecodes::_ifeq: // fall through
duke@0 271 case Bytecodes::_ifne: // fall through
duke@0 272 case Bytecodes::_iflt: // fall through
duke@0 273 case Bytecodes::_ifge: // fall through
duke@0 274 case Bytecodes::_ifgt: // fall through
duke@0 275 case Bytecodes::_ifle: // fall through
duke@0 276 case Bytecodes::_if_icmpeq: // fall through
duke@0 277 case Bytecodes::_if_icmpne: // fall through
duke@0 278 case Bytecodes::_if_icmplt: // fall through
duke@0 279 case Bytecodes::_if_icmpge: // fall through
duke@0 280 case Bytecodes::_if_icmpgt: // fall through
duke@0 281 case Bytecodes::_if_icmple: // fall through
duke@0 282 case Bytecodes::_if_acmpeq: // fall through
duke@0 283 case Bytecodes::_if_acmpne: // fall through
duke@0 284 case Bytecodes::_ifnull: // fall through
duke@0 285 case Bytecodes::_ifnonnull:
duke@0 286 make_block_at(s.next_bci(), current);
duke@0 287 make_block_at(s.get_dest(), current);
duke@0 288 current = NULL;
duke@0 289 break;
duke@0 290
duke@0 291 case Bytecodes::_goto:
duke@0 292 make_block_at(s.get_dest(), current);
duke@0 293 current = NULL;
duke@0 294 break;
duke@0 295
duke@0 296 case Bytecodes::_goto_w:
duke@0 297 make_block_at(s.get_far_dest(), current);
duke@0 298 current = NULL;
duke@0 299 break;
duke@0 300
duke@0 301 case Bytecodes::_jsr:
duke@0 302 handle_jsr(current, s.get_dest(), s.next_bci());
duke@0 303 current = NULL;
duke@0 304 break;
duke@0 305
duke@0 306 case Bytecodes::_jsr_w:
duke@0 307 handle_jsr(current, s.get_far_dest(), s.next_bci());
duke@0 308 current = NULL;
duke@0 309 break;
duke@0 310
duke@0 311 case Bytecodes::_tableswitch: {
duke@0 312 // set block for each case
duke@0 313 Bytecode_tableswitch *switch_ = Bytecode_tableswitch_at(s.cur_bcp());
duke@0 314 int l = switch_->length();
duke@0 315 for (int i = 0; i < l; i++) {
duke@0 316 make_block_at(cur_bci + switch_->dest_offset_at(i), current);
duke@0 317 }
duke@0 318 make_block_at(cur_bci + switch_->default_offset(), current);
duke@0 319 current = NULL;
duke@0 320 break;
duke@0 321 }
duke@0 322
duke@0 323 case Bytecodes::_lookupswitch: {
duke@0 324 // set block for each case
duke@0 325 Bytecode_lookupswitch *switch_ = Bytecode_lookupswitch_at(s.cur_bcp());
duke@0 326 int l = switch_->number_of_pairs();
duke@0 327 for (int i = 0; i < l; i++) {
duke@0 328 make_block_at(cur_bci + switch_->pair_at(i)->offset(), current);
duke@0 329 }
duke@0 330 make_block_at(cur_bci + switch_->default_offset(), current);
duke@0 331 current = NULL;
duke@0 332 break;
duke@0 333 }
duke@0 334 }
duke@0 335 }
duke@0 336 }
duke@0 337
duke@0 338
duke@0 339 void BlockListBuilder::mark_loops() {
duke@0 340 ResourceMark rm;
duke@0 341
duke@0 342 _active = BitMap(BlockBegin::number_of_blocks()); _active.clear();
duke@0 343 _visited = BitMap(BlockBegin::number_of_blocks()); _visited.clear();
duke@0 344 _loop_map = intArray(BlockBegin::number_of_blocks(), 0);
duke@0 345 _next_loop_index = 0;
duke@0 346 _next_block_number = _blocks.length();
duke@0 347
duke@0 348 // recursively iterate the control flow graph
duke@0 349 mark_loops(_bci2block->at(0), false);
duke@0 350 assert(_next_block_number >= 0, "invalid block numbers");
duke@0 351 }
duke@0 352
duke@0 353 void BlockListBuilder::make_loop_header(BlockBegin* block) {
duke@0 354 if (block->is_set(BlockBegin::exception_entry_flag)) {
duke@0 355 // exception edges may look like loops but don't mark them as such
duke@0 356 // since it screws up block ordering.
duke@0 357 return;
duke@0 358 }
duke@0 359 if (!block->is_set(BlockBegin::parser_loop_header_flag)) {
duke@0 360 block->set(BlockBegin::parser_loop_header_flag);
duke@0 361
duke@0 362 assert(_loop_map.at(block->block_id()) == 0, "must not be set yet");
duke@0 363 assert(0 <= _next_loop_index && _next_loop_index < BitsPerInt, "_next_loop_index is used as a bit-index in integer");
duke@0 364 _loop_map.at_put(block->block_id(), 1 << _next_loop_index);
duke@0 365 if (_next_loop_index < 31) _next_loop_index++;
duke@0 366 } else {
duke@0 367 // block already marked as loop header
duke@0 368 assert(is_power_of_2(_loop_map.at(block->block_id())), "exactly one bit must be set");
duke@0 369 }
duke@0 370 }
duke@0 371
duke@0 372 int BlockListBuilder::mark_loops(BlockBegin* block, bool in_subroutine) {
duke@0 373 int block_id = block->block_id();
duke@0 374
duke@0 375 if (_visited.at(block_id)) {
duke@0 376 if (_active.at(block_id)) {
duke@0 377 // reached block via backward branch
duke@0 378 make_loop_header(block);
duke@0 379 }
duke@0 380 // return cached loop information for this block
duke@0 381 return _loop_map.at(block_id);
duke@0 382 }
duke@0 383
duke@0 384 if (block->is_set(BlockBegin::subroutine_entry_flag)) {
duke@0 385 in_subroutine = true;
duke@0 386 }
duke@0 387
duke@0 388 // set active and visited bits before successors are processed
duke@0 389 _visited.set_bit(block_id);
duke@0 390 _active.set_bit(block_id);
duke@0 391
duke@0 392 intptr_t loop_state = 0;
duke@0 393 for (int i = block->number_of_sux() - 1; i >= 0; i--) {
duke@0 394 // recursively process all successors
duke@0 395 loop_state |= mark_loops(block->sux_at(i), in_subroutine);
duke@0 396 }
duke@0 397
duke@0 398 // clear active-bit after all successors are processed
duke@0 399 _active.clear_bit(block_id);
duke@0 400
duke@0 401 // reverse-post-order numbering of all blocks
duke@0 402 block->set_depth_first_number(_next_block_number);
duke@0 403 _next_block_number--;
duke@0 404
duke@0 405 if (loop_state != 0 || in_subroutine ) {
duke@0 406 // block is contained at least in one loop, so phi functions are necessary
duke@0 407 // phi functions are also necessary for all locals stored in a subroutine
duke@0 408 scope()->requires_phi_function().set_union(block->stores_to_locals());
duke@0 409 }
duke@0 410
duke@0 411 if (block->is_set(BlockBegin::parser_loop_header_flag)) {
duke@0 412 int header_loop_state = _loop_map.at(block_id);
duke@0 413 assert(is_power_of_2((unsigned)header_loop_state), "exactly one bit must be set");
duke@0 414
duke@0 415 // If the highest bit is set (i.e. when integer value is negative), the method
duke@0 416 // has 32 or more loops. This bit is never cleared because it is used for multiple loops
duke@0 417 if (header_loop_state >= 0) {
duke@0 418 clear_bits(loop_state, header_loop_state);
duke@0 419 }
duke@0 420 }
duke@0 421
duke@0 422 // cache and return loop information for this block
duke@0 423 _loop_map.at_put(block_id, loop_state);
duke@0 424 return loop_state;
duke@0 425 }
duke@0 426
duke@0 427
duke@0 428 #ifndef PRODUCT
duke@0 429
duke@0 430 int compare_depth_first(BlockBegin** a, BlockBegin** b) {
duke@0 431 return (*a)->depth_first_number() - (*b)->depth_first_number();
duke@0 432 }
duke@0 433
duke@0 434 void BlockListBuilder::print() {
duke@0 435 tty->print("----- initial block list of BlockListBuilder for method ");
duke@0 436 method()->print_short_name();
duke@0 437 tty->cr();
duke@0 438
duke@0 439 // better readability if blocks are sorted in processing order
duke@0 440 _blocks.sort(compare_depth_first);
duke@0 441
duke@0 442 for (int i = 0; i < _blocks.length(); i++) {
duke@0 443 BlockBegin* cur = _blocks.at(i);
duke@0 444 tty->print("%4d: B%-4d bci: %-4d preds: %-4d ", cur->depth_first_number(), cur->block_id(), cur->bci(), cur->total_preds());
duke@0 445
duke@0 446 tty->print(cur->is_set(BlockBegin::std_entry_flag) ? " std" : " ");
duke@0 447 tty->print(cur->is_set(BlockBegin::osr_entry_flag) ? " osr" : " ");
duke@0 448 tty->print(cur->is_set(BlockBegin::exception_entry_flag) ? " ex" : " ");
duke@0 449 tty->print(cur->is_set(BlockBegin::subroutine_entry_flag) ? " sr" : " ");
duke@0 450 tty->print(cur->is_set(BlockBegin::parser_loop_header_flag) ? " lh" : " ");
duke@0 451
duke@0 452 if (cur->number_of_sux() > 0) {
duke@0 453 tty->print(" sux: ");
duke@0 454 for (int j = 0; j < cur->number_of_sux(); j++) {
duke@0 455 BlockBegin* sux = cur->sux_at(j);
duke@0 456 tty->print("B%d ", sux->block_id());
duke@0 457 }
duke@0 458 }
duke@0 459 tty->cr();
duke@0 460 }
duke@0 461 }
duke@0 462
duke@0 463 #endif
duke@0 464
duke@0 465
duke@0 466 // A simple growable array of Values indexed by ciFields
duke@0 467 class FieldBuffer: public CompilationResourceObj {
duke@0 468 private:
duke@0 469 GrowableArray<Value> _values;
duke@0 470
duke@0 471 public:
duke@0 472 FieldBuffer() {}
duke@0 473
duke@0 474 void kill() {
duke@0 475 _values.trunc_to(0);
duke@0 476 }
duke@0 477
duke@0 478 Value at(ciField* field) {
duke@0 479 assert(field->holder()->is_loaded(), "must be a loaded field");
duke@0 480 int offset = field->offset();
duke@0 481 if (offset < _values.length()) {
duke@0 482 return _values.at(offset);
duke@0 483 } else {
duke@0 484 return NULL;
duke@0 485 }
duke@0 486 }
duke@0 487
duke@0 488 void at_put(ciField* field, Value value) {
duke@0 489 assert(field->holder()->is_loaded(), "must be a loaded field");
duke@0 490 int offset = field->offset();
duke@0 491 _values.at_put_grow(offset, value, NULL);
duke@0 492 }
duke@0 493
duke@0 494 };
duke@0 495
duke@0 496
duke@0 497 // MemoryBuffer is fairly simple model of the current state of memory.
duke@0 498 // It partitions memory into several pieces. The first piece is
duke@0 499 // generic memory where little is known about the owner of the memory.
duke@0 500 // This is conceptually represented by the tuple <O, F, V> which says
duke@0 501 // that the field F of object O has value V. This is flattened so
duke@0 502 // that F is represented by the offset of the field and the parallel
duke@0 503 // arrays _objects and _values are used for O and V. Loads of O.F can
duke@0 504 // simply use V. Newly allocated objects are kept in a separate list
duke@0 505 // along with a parallel array for each object which represents the
duke@0 506 // current value of its fields. Stores of the default value to fields
duke@0 507 // which have never been stored to before are eliminated since they
duke@0 508 // are redundant. Once newly allocated objects are stored into
duke@0 509 // another object or they are passed out of the current compile they
duke@0 510 // are treated like generic memory.
duke@0 511
duke@0 512 class MemoryBuffer: public CompilationResourceObj {
duke@0 513 private:
duke@0 514 FieldBuffer _values;
duke@0 515 GrowableArray<Value> _objects;
duke@0 516 GrowableArray<Value> _newobjects;
duke@0 517 GrowableArray<FieldBuffer*> _fields;
duke@0 518
duke@0 519 public:
duke@0 520 MemoryBuffer() {}
duke@0 521
duke@0 522 StoreField* store(StoreField* st) {
duke@0 523 if (!EliminateFieldAccess) {
duke@0 524 return st;
duke@0 525 }
duke@0 526
duke@0 527 Value object = st->obj();
duke@0 528 Value value = st->value();
duke@0 529 ciField* field = st->field();
duke@0 530 if (field->holder()->is_loaded()) {
duke@0 531 int offset = field->offset();
duke@0 532 int index = _newobjects.find(object);
duke@0 533 if (index != -1) {
duke@0 534 // newly allocated object with no other stores performed on this field
duke@0 535 FieldBuffer* buf = _fields.at(index);
duke@0 536 if (buf->at(field) == NULL && is_default_value(value)) {
duke@0 537 #ifndef PRODUCT
duke@0 538 if (PrintIRDuringConstruction && Verbose) {
duke@0 539 tty->print_cr("Eliminated store for object %d:", index);
duke@0 540 st->print_line();
duke@0 541 }
duke@0 542 #endif
duke@0 543 return NULL;
duke@0 544 } else {
duke@0 545 buf->at_put(field, value);
duke@0 546 }
duke@0 547 } else {
duke@0 548 _objects.at_put_grow(offset, object, NULL);
duke@0 549 _values.at_put(field, value);
duke@0 550 }
duke@0 551
duke@0 552 store_value(value);
duke@0 553 } else {
duke@0 554 // if we held onto field names we could alias based on names but
duke@0 555 // we don't know what's being stored to so kill it all.
duke@0 556 kill();
duke@0 557 }
duke@0 558 return st;
duke@0 559 }
duke@0 560
duke@0 561
duke@0 562 // return true if this value correspond to the default value of a field.
duke@0 563 bool is_default_value(Value value) {
duke@0 564 Constant* con = value->as_Constant();
duke@0 565 if (con) {
duke@0 566 switch (con->type()->tag()) {
duke@0 567 case intTag: return con->type()->as_IntConstant()->value() == 0;
duke@0 568 case longTag: return con->type()->as_LongConstant()->value() == 0;
duke@0 569 case floatTag: return jint_cast(con->type()->as_FloatConstant()->value()) == 0;
duke@0 570 case doubleTag: return jlong_cast(con->type()->as_DoubleConstant()->value()) == jlong_cast(0);
duke@0 571 case objectTag: return con->type() == objectNull;
duke@0 572 default: ShouldNotReachHere();
duke@0 573 }
duke@0 574 }
duke@0 575 return false;
duke@0 576 }
duke@0 577
duke@0 578
duke@0 579 // return either the actual value of a load or the load itself
duke@0 580 Value load(LoadField* load) {
duke@0 581 if (!EliminateFieldAccess) {
duke@0 582 return load;
duke@0 583 }
duke@0 584
duke@0 585 if (RoundFPResults && UseSSE < 2 && load->type()->is_float_kind()) {
duke@0 586 // can't skip load since value might get rounded as a side effect
duke@0 587 return load;
duke@0 588 }
duke@0 589
duke@0 590 ciField* field = load->field();
duke@0 591 Value object = load->obj();
duke@0 592 if (field->holder()->is_loaded() && !field->is_volatile()) {
duke@0 593 int offset = field->offset();
duke@0 594 Value result = NULL;
duke@0 595 int index = _newobjects.find(object);
duke@0 596 if (index != -1) {
duke@0 597 result = _fields.at(index)->at(field);
duke@0 598 } else if (_objects.at_grow(offset, NULL) == object) {
duke@0 599 result = _values.at(field);
duke@0 600 }
duke@0 601 if (result != NULL) {
duke@0 602 #ifndef PRODUCT
duke@0 603 if (PrintIRDuringConstruction && Verbose) {
duke@0 604 tty->print_cr("Eliminated load: ");
duke@0 605 load->print_line();
duke@0 606 }
duke@0 607 #endif
duke@0 608 assert(result->type()->tag() == load->type()->tag(), "wrong types");
duke@0 609 return result;
duke@0 610 }
duke@0 611 }
duke@0 612 return load;
duke@0 613 }
duke@0 614
duke@0 615 // Record this newly allocated object
duke@0 616 void new_instance(NewInstance* object) {
duke@0 617 int index = _newobjects.length();
duke@0 618 _newobjects.append(object);
duke@0 619 if (_fields.at_grow(index, NULL) == NULL) {
duke@0 620 _fields.at_put(index, new FieldBuffer());
duke@0 621 } else {
duke@0 622 _fields.at(index)->kill();
duke@0 623 }
duke@0 624 }
duke@0 625
duke@0 626 void store_value(Value value) {
duke@0 627 int index = _newobjects.find(value);
duke@0 628 if (index != -1) {
duke@0 629 // stored a newly allocated object into another object.
duke@0 630 // Assume we've lost track of it as separate slice of memory.
duke@0 631 // We could do better by keeping track of whether individual
duke@0 632 // fields could alias each other.
duke@0 633 _newobjects.remove_at(index);
duke@0 634 // pull out the field info and store it at the end up the list
duke@0 635 // of field info list to be reused later.
duke@0 636 _fields.append(_fields.at(index));
duke@0 637 _fields.remove_at(index);
duke@0 638 }
duke@0 639 }
duke@0 640
duke@0 641 void kill() {
duke@0 642 _newobjects.trunc_to(0);
duke@0 643 _objects.trunc_to(0);
duke@0 644 _values.kill();
duke@0 645 }
duke@0 646 };
duke@0 647
duke@0 648
duke@0 649 // Implementation of GraphBuilder's ScopeData
duke@0 650
duke@0 651 GraphBuilder::ScopeData::ScopeData(ScopeData* parent)
duke@0 652 : _parent(parent)
duke@0 653 , _bci2block(NULL)
duke@0 654 , _scope(NULL)
duke@0 655 , _has_handler(false)
duke@0 656 , _stream(NULL)
duke@0 657 , _work_list(NULL)
duke@0 658 , _parsing_jsr(false)
duke@0 659 , _jsr_xhandlers(NULL)
duke@0 660 , _caller_stack_size(-1)
duke@0 661 , _continuation(NULL)
duke@0 662 , _continuation_state(NULL)
duke@0 663 , _num_returns(0)
duke@0 664 , _cleanup_block(NULL)
duke@0 665 , _cleanup_return_prev(NULL)
duke@0 666 , _cleanup_state(NULL)
duke@0 667 {
duke@0 668 if (parent != NULL) {
duke@0 669 _max_inline_size = (intx) ((float) NestedInliningSizeRatio * (float) parent->max_inline_size() / 100.0f);
duke@0 670 } else {
duke@0 671 _max_inline_size = MaxInlineSize;
duke@0 672 }
duke@0 673 if (_max_inline_size < MaxTrivialSize) {
duke@0 674 _max_inline_size = MaxTrivialSize;
duke@0 675 }
duke@0 676 }
duke@0 677
duke@0 678
duke@0 679 void GraphBuilder::kill_all() {
duke@0 680 if (UseLocalValueNumbering) {
duke@0 681 vmap()->kill_all();
duke@0 682 }
duke@0 683 _memory->kill();
duke@0 684 }
duke@0 685
duke@0 686
duke@0 687 BlockBegin* GraphBuilder::ScopeData::block_at(int bci) {
duke@0 688 if (parsing_jsr()) {
duke@0 689 // It is necessary to clone all blocks associated with a
duke@0 690 // subroutine, including those for exception handlers in the scope
duke@0 691 // of the method containing the jsr (because those exception
duke@0 692 // handlers may contain ret instructions in some cases).
duke@0 693 BlockBegin* block = bci2block()->at(bci);
duke@0 694 if (block != NULL && block == parent()->bci2block()->at(bci)) {
duke@0 695 BlockBegin* new_block = new BlockBegin(block->bci());
duke@0 696 #ifndef PRODUCT
duke@0 697 if (PrintInitialBlockList) {
duke@0 698 tty->print_cr("CFG: cloned block %d (bci %d) as block %d for jsr",
duke@0 699 block->block_id(), block->bci(), new_block->block_id());
duke@0 700 }
duke@0 701 #endif
duke@0 702 // copy data from cloned blocked
duke@0 703 new_block->set_depth_first_number(block->depth_first_number());
duke@0 704 if (block->is_set(BlockBegin::parser_loop_header_flag)) new_block->set(BlockBegin::parser_loop_header_flag);
duke@0 705 // Preserve certain flags for assertion checking
duke@0 706 if (block->is_set(BlockBegin::subroutine_entry_flag)) new_block->set(BlockBegin::subroutine_entry_flag);
duke@0 707 if (block->is_set(BlockBegin::exception_entry_flag)) new_block->set(BlockBegin::exception_entry_flag);
duke@0 708
duke@0 709 // copy was_visited_flag to allow early detection of bailouts
duke@0 710 // if a block that is used in a jsr has already been visited before,
duke@0 711 // it is shared between the normal control flow and a subroutine
duke@0 712 // BlockBegin::try_merge returns false when the flag is set, this leads
duke@0 713 // to a compilation bailout
duke@0 714 if (block->is_set(BlockBegin::was_visited_flag)) new_block->set(BlockBegin::was_visited_flag);
duke@0 715
duke@0 716 bci2block()->at_put(bci, new_block);
duke@0 717 block = new_block;
duke@0 718 }
duke@0 719 return block;
duke@0 720 } else {
duke@0 721 return bci2block()->at(bci);
duke@0 722 }
duke@0 723 }
duke@0 724
duke@0 725
duke@0 726 XHandlers* GraphBuilder::ScopeData::xhandlers() const {
duke@0 727 if (_jsr_xhandlers == NULL) {
duke@0 728 assert(!parsing_jsr(), "");
duke@0 729 return scope()->xhandlers();
duke@0 730 }
duke@0 731 assert(parsing_jsr(), "");
duke@0 732 return _jsr_xhandlers;
duke@0 733 }
duke@0 734
duke@0 735
duke@0 736 void GraphBuilder::ScopeData::set_scope(IRScope* scope) {
duke@0 737 _scope = scope;
duke@0 738 bool parent_has_handler = false;
duke@0 739 if (parent() != NULL) {
duke@0 740 parent_has_handler = parent()->has_handler();
duke@0 741 }
duke@0 742 _has_handler = parent_has_handler || scope->xhandlers()->has_handlers();
duke@0 743 }
duke@0 744
duke@0 745
duke@0 746 void GraphBuilder::ScopeData::set_inline_cleanup_info(BlockBegin* block,
duke@0 747 Instruction* return_prev,
duke@0 748 ValueStack* return_state) {
duke@0 749 _cleanup_block = block;
duke@0 750 _cleanup_return_prev = return_prev;
duke@0 751 _cleanup_state = return_state;
duke@0 752 }
duke@0 753
duke@0 754
duke@0 755 void GraphBuilder::ScopeData::add_to_work_list(BlockBegin* block) {
duke@0 756 if (_work_list == NULL) {
duke@0 757 _work_list = new BlockList();
duke@0 758 }
duke@0 759
duke@0 760 if (!block->is_set(BlockBegin::is_on_work_list_flag)) {
duke@0 761 // Do not start parsing the continuation block while in a
duke@0 762 // sub-scope
duke@0 763 if (parsing_jsr()) {
duke@0 764 if (block == jsr_continuation()) {
duke@0 765 return;
duke@0 766 }
duke@0 767 } else {
duke@0 768 if (block == continuation()) {
duke@0 769 return;
duke@0 770 }
duke@0 771 }
duke@0 772 block->set(BlockBegin::is_on_work_list_flag);
duke@0 773 _work_list->push(block);
duke@0 774
duke@0 775 sort_top_into_worklist(_work_list, block);
duke@0 776 }
duke@0 777 }
duke@0 778
duke@0 779
duke@0 780 void GraphBuilder::sort_top_into_worklist(BlockList* worklist, BlockBegin* top) {
duke@0 781 assert(worklist->top() == top, "");
duke@0 782 // sort block descending into work list
duke@0 783 const int dfn = top->depth_first_number();
duke@0 784 assert(dfn != -1, "unknown depth first number");
duke@0 785 int i = worklist->length()-2;
duke@0 786 while (i >= 0) {
duke@0 787 BlockBegin* b = worklist->at(i);
duke@0 788 if (b->depth_first_number() < dfn) {
duke@0 789 worklist->at_put(i+1, b);
duke@0 790 } else {
duke@0 791 break;
duke@0 792 }
duke@0 793 i --;
duke@0 794 }
duke@0 795 if (i >= -1) worklist->at_put(i + 1, top);
duke@0 796 }
duke@0 797
duke@0 798 int GraphBuilder::ScopeData::caller_stack_size() const {
duke@0 799 ValueStack* state = scope()->caller_state();
duke@0 800 if (state == NULL) {
duke@0 801 return 0;
duke@0 802 }
duke@0 803 return state->stack_size();
duke@0 804 }
duke@0 805
duke@0 806
duke@0 807 BlockBegin* GraphBuilder::ScopeData::remove_from_work_list() {
duke@0 808 if (is_work_list_empty()) {
duke@0 809 return NULL;
duke@0 810 }
duke@0 811 return _work_list->pop();
duke@0 812 }
duke@0 813
duke@0 814
duke@0 815 bool GraphBuilder::ScopeData::is_work_list_empty() const {
duke@0 816 return (_work_list == NULL || _work_list->length() == 0);
duke@0 817 }
duke@0 818
duke@0 819
duke@0 820 void GraphBuilder::ScopeData::setup_jsr_xhandlers() {
duke@0 821 assert(parsing_jsr(), "");
duke@0 822 // clone all the exception handlers from the scope
duke@0 823 XHandlers* handlers = new XHandlers(scope()->xhandlers());
duke@0 824 const int n = handlers->length();
duke@0 825 for (int i = 0; i < n; i++) {
duke@0 826 // The XHandlers need to be adjusted to dispatch to the cloned
duke@0 827 // handler block instead of the default one but the synthetic
duke@0 828 // unlocker needs to be handled specially. The synthetic unlocker
duke@0 829 // should be left alone since there can be only one and all code
duke@0 830 // should dispatch to the same one.
duke@0 831 XHandler* h = handlers->handler_at(i);
duke@0 832 if (h->handler_bci() != SynchronizationEntryBCI) {
duke@0 833 h->set_entry_block(block_at(h->handler_bci()));
duke@0 834 } else {
duke@0 835 assert(h->entry_block()->is_set(BlockBegin::default_exception_handler_flag),
duke@0 836 "should be the synthetic unlock block");
duke@0 837 }
duke@0 838 }
duke@0 839 _jsr_xhandlers = handlers;
duke@0 840 }
duke@0 841
duke@0 842
duke@0 843 int GraphBuilder::ScopeData::num_returns() {
duke@0 844 if (parsing_jsr()) {
duke@0 845 return parent()->num_returns();
duke@0 846 }
duke@0 847 return _num_returns;
duke@0 848 }
duke@0 849
duke@0 850
duke@0 851 void GraphBuilder::ScopeData::incr_num_returns() {
duke@0 852 if (parsing_jsr()) {
duke@0 853 parent()->incr_num_returns();
duke@0 854 } else {
duke@0 855 ++_num_returns;
duke@0 856 }
duke@0 857 }
duke@0 858
duke@0 859
duke@0 860 // Implementation of GraphBuilder
duke@0 861
duke@0 862 #define INLINE_BAILOUT(msg) { inline_bailout(msg); return false; }
duke@0 863
duke@0 864
duke@0 865 void GraphBuilder::load_constant() {
duke@0 866 ciConstant con = stream()->get_constant();
duke@0 867 if (con.basic_type() == T_ILLEGAL) {
duke@0 868 BAILOUT("could not resolve a constant");
duke@0 869 } else {
duke@0 870 ValueType* t = illegalType;
duke@0 871 ValueStack* patch_state = NULL;
duke@0 872 switch (con.basic_type()) {
duke@0 873 case T_BOOLEAN: t = new IntConstant (con.as_boolean()); break;
duke@0 874 case T_BYTE : t = new IntConstant (con.as_byte ()); break;
duke@0 875 case T_CHAR : t = new IntConstant (con.as_char ()); break;
duke@0 876 case T_SHORT : t = new IntConstant (con.as_short ()); break;
duke@0 877 case T_INT : t = new IntConstant (con.as_int ()); break;
duke@0 878 case T_LONG : t = new LongConstant (con.as_long ()); break;
duke@0 879 case T_FLOAT : t = new FloatConstant (con.as_float ()); break;
duke@0 880 case T_DOUBLE : t = new DoubleConstant (con.as_double ()); break;
duke@0 881 case T_ARRAY : t = new ArrayConstant (con.as_object ()->as_array ()); break;
duke@0 882 case T_OBJECT :
duke@0 883 {
duke@0 884 ciObject* obj = con.as_object();
duke@0 885 if (obj->is_klass()) {
duke@0 886 ciKlass* klass = obj->as_klass();
duke@0 887 if (!klass->is_loaded() || PatchALot) {
duke@0 888 patch_state = state()->copy();
duke@0 889 t = new ObjectConstant(obj);
duke@0 890 } else {
duke@0 891 t = new InstanceConstant(klass->java_mirror());
duke@0 892 }
duke@0 893 } else {
duke@0 894 t = new InstanceConstant(obj->as_instance());
duke@0 895 }
duke@0 896 break;
duke@0 897 }
duke@0 898 default : ShouldNotReachHere();
duke@0 899 }
duke@0 900 Value x;
duke@0 901 if (patch_state != NULL) {
duke@0 902 x = new Constant(t, patch_state);
duke@0 903 } else {
duke@0 904 x = new Constant(t);
duke@0 905 }
duke@0 906 push(t, append(x));
duke@0 907 }
duke@0 908 }
duke@0 909
duke@0 910
duke@0 911 void GraphBuilder::load_local(ValueType* type, int index) {
duke@0 912 Value x = state()->load_local(index);
duke@0 913 push(type, x);
duke@0 914 }
duke@0 915
duke@0 916
duke@0 917 void GraphBuilder::store_local(ValueType* type, int index) {
duke@0 918 Value x = pop(type);
duke@0 919 store_local(state(), x, type, index);
duke@0 920 }
duke@0 921
duke@0 922
duke@0 923 void GraphBuilder::store_local(ValueStack* state, Value x, ValueType* type, int index) {
duke@0 924 if (parsing_jsr()) {
duke@0 925 // We need to do additional tracking of the location of the return
duke@0 926 // address for jsrs since we don't handle arbitrary jsr/ret
duke@0 927 // constructs. Here we are figuring out in which circumstances we
duke@0 928 // need to bail out.
duke@0 929 if (x->type()->is_address()) {
duke@0 930 scope_data()->set_jsr_return_address_local(index);
duke@0 931
duke@0 932 // Also check parent jsrs (if any) at this time to see whether
duke@0 933 // they are using this local. We don't handle skipping over a
duke@0 934 // ret.
duke@0 935 for (ScopeData* cur_scope_data = scope_data()->parent();
duke@0 936 cur_scope_data != NULL && cur_scope_data->parsing_jsr() && cur_scope_data->scope() == scope();
duke@0 937 cur_scope_data = cur_scope_data->parent()) {
duke@0 938 if (cur_scope_data->jsr_return_address_local() == index) {
duke@0 939 BAILOUT("subroutine overwrites return address from previous subroutine");
duke@0 940 }
duke@0 941 }
duke@0 942 } else if (index == scope_data()->jsr_return_address_local()) {
duke@0 943 scope_data()->set_jsr_return_address_local(-1);
duke@0 944 }
duke@0 945 }
duke@0 946
duke@0 947 state->store_local(index, round_fp(x));
duke@0 948 }
duke@0 949
duke@0 950
duke@0 951 void GraphBuilder::load_indexed(BasicType type) {
duke@0 952 Value index = ipop();
duke@0 953 Value array = apop();
duke@0 954 Value length = NULL;
duke@0 955 if (CSEArrayLength ||
duke@0 956 (array->as_AccessField() && array->as_AccessField()->field()->is_constant()) ||
duke@0 957 (array->as_NewArray() && array->as_NewArray()->length() && array->as_NewArray()->length()->type()->is_constant())) {
duke@0 958 length = append(new ArrayLength(array, lock_stack()));
duke@0 959 }
duke@0 960 push(as_ValueType(type), append(new LoadIndexed(array, index, length, type, lock_stack())));
duke@0 961 }
duke@0 962
duke@0 963
duke@0 964 void GraphBuilder::store_indexed(BasicType type) {
duke@0 965 Value value = pop(as_ValueType(type));
duke@0 966 Value index = ipop();
duke@0 967 Value array = apop();
duke@0 968 Value length = NULL;
duke@0 969 if (CSEArrayLength ||
duke@0 970 (array->as_AccessField() && array->as_AccessField()->field()->is_constant()) ||
duke@0 971 (array->as_NewArray() && array->as_NewArray()->length() && array->as_NewArray()->length()->type()->is_constant())) {
duke@0 972 length = append(new ArrayLength(array, lock_stack()));
duke@0 973 }
duke@0 974 StoreIndexed* result = new StoreIndexed(array, index, length, type, value, lock_stack());
duke@0 975 append(result);
never@459 976 _memory->store_value(value);
duke@0 977 }
duke@0 978
duke@0 979
duke@0 980 void GraphBuilder::stack_op(Bytecodes::Code code) {
duke@0 981 switch (code) {
duke@0 982 case Bytecodes::_pop:
duke@0 983 { state()->raw_pop();
duke@0 984 }
duke@0 985 break;
duke@0 986 case Bytecodes::_pop2:
duke@0 987 { state()->raw_pop();
duke@0 988 state()->raw_pop();
duke@0 989 }
duke@0 990 break;
duke@0 991 case Bytecodes::_dup:
duke@0 992 { Value w = state()->raw_pop();
duke@0 993 state()->raw_push(w);
duke@0 994 state()->raw_push(w);
duke@0 995 }
duke@0 996 break;
duke@0 997 case Bytecodes::_dup_x1:
duke@0 998 { Value w1 = state()->raw_pop();
duke@0 999 Value w2 = state()->raw_pop();
duke@0 1000 state()->raw_push(w1);
duke@0 1001 state()->raw_push(w2);
duke@0 1002 state()->raw_push(w1);
duke@0 1003 }
duke@0 1004 break;
duke@0 1005 case Bytecodes::_dup_x2:
duke@0 1006 { Value w1 = state()->raw_pop();
duke@0 1007 Value w2 = state()->raw_pop();
duke@0 1008 Value w3 = state()->raw_pop();
duke@0 1009 state()->raw_push(w1);
duke@0 1010 state()->raw_push(w3);
duke@0 1011 state()->raw_push(w2);
duke@0 1012 state()->raw_push(w1);
duke@0 1013 }
duke@0 1014 break;
duke@0 1015 case Bytecodes::_dup2:
duke@0 1016 { Value w1 = state()->raw_pop();
duke@0 1017 Value w2 = state()->raw_pop();
duke@0 1018 state()->raw_push(w2);
duke@0 1019 state()->raw_push(w1);
duke@0 1020 state()->raw_push(w2);
duke@0 1021 state()->raw_push(w1);
duke@0 1022 }
duke@0 1023 break;
duke@0 1024 case Bytecodes::_dup2_x1:
duke@0 1025 { Value w1 = state()->raw_pop();
duke@0 1026 Value w2 = state()->raw_pop();
duke@0 1027 Value w3 = state()->raw_pop();
duke@0 1028 state()->raw_push(w2);
duke@0 1029 state()->raw_push(w1);
duke@0 1030 state()->raw_push(w3);
duke@0 1031 state()->raw_push(w2);
duke@0 1032 state()->raw_push(w1);
duke@0 1033 }
duke@0 1034 break;
duke@0 1035 case Bytecodes::_dup2_x2:
duke@0 1036 { Value w1 = state()->raw_pop();
duke@0 1037 Value w2 = state()->raw_pop();
duke@0 1038 Value w3 = state()->raw_pop();
duke@0 1039 Value w4 = state()->raw_pop();
duke@0 1040 state()->raw_push(w2);
duke@0 1041 state()->raw_push(w1);
duke@0 1042 state()->raw_push(w4);
duke@0 1043 state()->raw_push(w3);
duke@0 1044 state()->raw_push(w2);
duke@0 1045 state()->raw_push(w1);
duke@0 1046 }
duke@0 1047 break;
duke@0 1048 case Bytecodes::_swap:
duke@0 1049 { Value w1 = state()->raw_pop();
duke@0 1050 Value w2 = state()->raw_pop();
duke@0 1051 state()->raw_push(w1);
duke@0 1052 state()->raw_push(w2);
duke@0 1053 }
duke@0 1054 break;
duke@0 1055 default:
duke@0 1056 ShouldNotReachHere();
duke@0 1057 break;
duke@0 1058 }
duke@0 1059 }
duke@0 1060
duke@0 1061
duke@0 1062 void GraphBuilder::arithmetic_op(ValueType* type, Bytecodes::Code code, ValueStack* stack) {
duke@0 1063 Value y = pop(type);
duke@0 1064 Value x = pop(type);
duke@0 1065 // NOTE: strictfp can be queried from current method since we don't
duke@0 1066 // inline methods with differing strictfp bits
duke@0 1067 Value res = new ArithmeticOp(code, x, y, method()->is_strict(), stack);
duke@0 1068 // Note: currently single-precision floating-point rounding on Intel is handled at the LIRGenerator level
duke@0 1069 res = append(res);
duke@0 1070 if (method()->is_strict()) {
duke@0 1071 res = round_fp(res);
duke@0 1072 }
duke@0 1073 push(type, res);
duke@0 1074 }
duke@0 1075
duke@0 1076
duke@0 1077 void GraphBuilder::negate_op(ValueType* type) {
duke@0 1078 push(type, append(new NegateOp(pop(type))));
duke@0 1079 }
duke@0 1080
duke@0 1081
duke@0 1082 void GraphBuilder::shift_op(ValueType* type, Bytecodes::Code code) {
duke@0 1083 Value s = ipop();
duke@0 1084 Value x = pop(type);
duke@0 1085 // try to simplify
duke@0 1086 // Note: This code should go into the canonicalizer as soon as it can
duke@0 1087 // can handle canonicalized forms that contain more than one node.
duke@0 1088 if (CanonicalizeNodes && code == Bytecodes::_iushr) {
duke@0 1089 // pattern: x >>> s
duke@0 1090 IntConstant* s1 = s->type()->as_IntConstant();
duke@0 1091 if (s1 != NULL) {
duke@0 1092 // pattern: x >>> s1, with s1 constant
duke@0 1093 ShiftOp* l = x->as_ShiftOp();
duke@0 1094 if (l != NULL && l->op() == Bytecodes::_ishl) {
duke@0 1095 // pattern: (a << b) >>> s1
duke@0 1096 IntConstant* s0 = l->y()->type()->as_IntConstant();
duke@0 1097 if (s0 != NULL) {
duke@0 1098 // pattern: (a << s0) >>> s1
duke@0 1099 const int s0c = s0->value() & 0x1F; // only the low 5 bits are significant for shifts
duke@0 1100 const int s1c = s1->value() & 0x1F; // only the low 5 bits are significant for shifts
duke@0 1101 if (s0c == s1c) {
duke@0 1102 if (s0c == 0) {
duke@0 1103 // pattern: (a << 0) >>> 0 => simplify to: a
duke@0 1104 ipush(l->x());
duke@0 1105 } else {
duke@0 1106 // pattern: (a << s0c) >>> s0c => simplify to: a & m, with m constant
duke@0 1107 assert(0 < s0c && s0c < BitsPerInt, "adjust code below to handle corner cases");
duke@0 1108 const int m = (1 << (BitsPerInt - s0c)) - 1;
duke@0 1109 Value s = append(new Constant(new IntConstant(m)));
duke@0 1110 ipush(append(new LogicOp(Bytecodes::_iand, l->x(), s)));
duke@0 1111 }
duke@0 1112 return;
duke@0 1113 }
duke@0 1114 }
duke@0 1115 }
duke@0 1116 }
duke@0 1117 }
duke@0 1118 // could not simplify
duke@0 1119 push(type, append(new ShiftOp(code, x, s)));
duke@0 1120 }
duke@0 1121
duke@0 1122
duke@0 1123 void GraphBuilder::logic_op(ValueType* type, Bytecodes::Code code) {
duke@0 1124 Value y = pop(type);
duke@0 1125 Value x = pop(type);
duke@0 1126 push(type, append(new LogicOp(code, x, y)));
duke@0 1127 }
duke@0 1128
duke@0 1129
duke@0 1130 void GraphBuilder::compare_op(ValueType* type, Bytecodes::Code code) {
duke@0 1131 ValueStack* state_before = state()->copy();
duke@0 1132 Value y = pop(type);
duke@0 1133 Value x = pop(type);
duke@0 1134 ipush(append(new CompareOp(code, x, y, state_before)));
duke@0 1135 }
duke@0 1136
duke@0 1137
duke@0 1138 void GraphBuilder::convert(Bytecodes::Code op, BasicType from, BasicType to) {
duke@0 1139 push(as_ValueType(to), append(new Convert(op, pop(as_ValueType(from)), as_ValueType(to))));
duke@0 1140 }
duke@0 1141
duke@0 1142
duke@0 1143 void GraphBuilder::increment() {
duke@0 1144 int index = stream()->get_index();
duke@0 1145 int delta = stream()->is_wide() ? (signed short)Bytes::get_Java_u2(stream()->cur_bcp() + 4) : (signed char)(stream()->cur_bcp()[2]);
duke@0 1146 load_local(intType, index);
duke@0 1147 ipush(append(new Constant(new IntConstant(delta))));
duke@0 1148 arithmetic_op(intType, Bytecodes::_iadd);
duke@0 1149 store_local(intType, index);
duke@0 1150 }
duke@0 1151
duke@0 1152
duke@0 1153 void GraphBuilder::_goto(int from_bci, int to_bci) {
duke@0 1154 profile_bci(from_bci);
duke@0 1155 append(new Goto(block_at(to_bci), to_bci <= from_bci));
duke@0 1156 }
duke@0 1157
duke@0 1158
duke@0 1159 void GraphBuilder::if_node(Value x, If::Condition cond, Value y, ValueStack* state_before) {
duke@0 1160 BlockBegin* tsux = block_at(stream()->get_dest());
duke@0 1161 BlockBegin* fsux = block_at(stream()->next_bci());
duke@0 1162 bool is_bb = tsux->bci() < stream()->cur_bci() || fsux->bci() < stream()->cur_bci();
duke@0 1163 If* if_node = append(new If(x, cond, false, y, tsux, fsux, is_bb ? state_before : NULL, is_bb))->as_If();
duke@0 1164 if (profile_branches() && (if_node != NULL)) {
duke@0 1165 if_node->set_profiled_method(method());
duke@0 1166 if_node->set_profiled_bci(bci());
duke@0 1167 if_node->set_should_profile(true);
duke@0 1168 }
duke@0 1169 }
duke@0 1170
duke@0 1171
duke@0 1172 void GraphBuilder::if_zero(ValueType* type, If::Condition cond) {
duke@0 1173 Value y = append(new Constant(intZero));
duke@0 1174 ValueStack* state_before = state()->copy();
duke@0 1175 Value x = ipop();
duke@0 1176 if_node(x, cond, y, state_before);
duke@0 1177 }
duke@0 1178
duke@0 1179
duke@0 1180 void GraphBuilder::if_null(ValueType* type, If::Condition cond) {
duke@0 1181 Value y = append(new Constant(objectNull));
duke@0 1182 ValueStack* state_before = state()->copy();
duke@0 1183 Value x = apop();
duke@0 1184 if_node(x, cond, y, state_before);
duke@0 1185 }
duke@0 1186
duke@0 1187
duke@0 1188 void GraphBuilder::if_same(ValueType* type, If::Condition cond) {
duke@0 1189 ValueStack* state_before = state()->copy();
duke@0 1190 Value y = pop(type);
duke@0 1191 Value x = pop(type);
duke@0 1192 if_node(x, cond, y, state_before);
duke@0 1193 }
duke@0 1194
duke@0 1195
duke@0 1196 void GraphBuilder::jsr(int dest) {
duke@0 1197 // We only handle well-formed jsrs (those which are "block-structured").
duke@0 1198 // If the bytecodes are strange (jumping out of a jsr block) then we
duke@0 1199 // might end up trying to re-parse a block containing a jsr which
duke@0 1200 // has already been activated. Watch for this case and bail out.
duke@0 1201 for (ScopeData* cur_scope_data = scope_data();
duke@0 1202 cur_scope_data != NULL && cur_scope_data->parsing_jsr() && cur_scope_data->scope() == scope();
duke@0 1203 cur_scope_data = cur_scope_data->parent()) {
duke@0 1204 if (cur_scope_data->jsr_entry_bci() == dest) {
duke@0 1205 BAILOUT("too-complicated jsr/ret structure");
duke@0 1206 }
duke@0 1207 }
duke@0 1208
duke@0 1209 push(addressType, append(new Constant(new AddressConstant(next_bci()))));
duke@0 1210 if (!try_inline_jsr(dest)) {
duke@0 1211 return; // bailed out while parsing and inlining subroutine
duke@0 1212 }
duke@0 1213 }
duke@0 1214
duke@0 1215
duke@0 1216 void GraphBuilder::ret(int local_index) {
duke@0 1217 if (!parsing_jsr()) BAILOUT("ret encountered while not parsing subroutine");
duke@0 1218
duke@0 1219 if (local_index != scope_data()->jsr_return_address_local()) {
duke@0 1220 BAILOUT("can not handle complicated jsr/ret constructs");
duke@0 1221 }
duke@0 1222
duke@0 1223 // Rets simply become (NON-SAFEPOINT) gotos to the jsr continuation
duke@0 1224 append(new Goto(scope_data()->jsr_continuation(), false));
duke@0 1225 }
duke@0 1226
duke@0 1227
duke@0 1228 void GraphBuilder::table_switch() {
duke@0 1229 Bytecode_tableswitch* switch_ = Bytecode_tableswitch_at(method()->code() + bci());
duke@0 1230 const int l = switch_->length();
duke@0 1231 if (CanonicalizeNodes && l == 1) {
duke@0 1232 // total of 2 successors => use If instead of switch
duke@0 1233 // Note: This code should go into the canonicalizer as soon as it can
duke@0 1234 // can handle canonicalized forms that contain more than one node.
duke@0 1235 Value key = append(new Constant(new IntConstant(switch_->low_key())));
duke@0 1236 BlockBegin* tsux = block_at(bci() + switch_->dest_offset_at(0));
duke@0 1237 BlockBegin* fsux = block_at(bci() + switch_->default_offset());
duke@0 1238 bool is_bb = tsux->bci() < bci() || fsux->bci() < bci();
duke@0 1239 ValueStack* state_before = is_bb ? state() : NULL;
duke@0 1240 append(new If(ipop(), If::eql, true, key, tsux, fsux, state_before, is_bb));
duke@0 1241 } else {
duke@0 1242 // collect successors
duke@0 1243 BlockList* sux = new BlockList(l + 1, NULL);
duke@0 1244 int i;
duke@0 1245 bool has_bb = false;
duke@0 1246 for (i = 0; i < l; i++) {
duke@0 1247 sux->at_put(i, block_at(bci() + switch_->dest_offset_at(i)));
duke@0 1248 if (switch_->dest_offset_at(i) < 0) has_bb = true;
duke@0 1249 }
duke@0 1250 // add default successor
duke@0 1251 sux->at_put(i, block_at(bci() + switch_->default_offset()));
duke@0 1252 ValueStack* state_before = has_bb ? state() : NULL;
duke@0 1253 append(new TableSwitch(ipop(), sux, switch_->low_key(), state_before, has_bb));
duke@0 1254 }
duke@0 1255 }
duke@0 1256
duke@0 1257
duke@0 1258 void GraphBuilder::lookup_switch() {
duke@0 1259 Bytecode_lookupswitch* switch_ = Bytecode_lookupswitch_at(method()->code() + bci());
duke@0 1260 const int l = switch_->number_of_pairs();
duke@0 1261 if (CanonicalizeNodes && l == 1) {
duke@0 1262 // total of 2 successors => use If instead of switch
duke@0 1263 // Note: This code should go into the canonicalizer as soon as it can
duke@0 1264 // can handle canonicalized forms that contain more than one node.
duke@0 1265 // simplify to If
duke@0 1266 LookupswitchPair* pair = switch_->pair_at(0);
duke@0 1267 Value key = append(new Constant(new IntConstant(pair->match())));
duke@0 1268 BlockBegin* tsux = block_at(bci() + pair->offset());
duke@0 1269 BlockBegin* fsux = block_at(bci() + switch_->default_offset());
duke@0 1270 bool is_bb = tsux->bci() < bci() || fsux->bci() < bci();
duke@0 1271 ValueStack* state_before = is_bb ? state() : NULL;
duke@0 1272 append(new If(ipop(), If::eql, true, key, tsux, fsux, state_before, is_bb));
duke@0 1273 } else {
duke@0 1274 // collect successors & keys
duke@0 1275 BlockList* sux = new BlockList(l + 1, NULL);
duke@0 1276 intArray* keys = new intArray(l, 0);
duke@0 1277 int i;
duke@0 1278 bool has_bb = false;
duke@0 1279 for (i = 0; i < l; i++) {
duke@0 1280 LookupswitchPair* pair = switch_->pair_at(i);
duke@0 1281 if (pair->offset() < 0) has_bb = true;
duke@0 1282 sux->at_put(i, block_at(bci() + pair->offset()));
duke@0 1283 keys->at_put(i, pair->match());
duke@0 1284 }
duke@0 1285 // add default successor
duke@0 1286 sux->at_put(i, block_at(bci() + switch_->default_offset()));
duke@0 1287 ValueStack* state_before = has_bb ? state() : NULL;
duke@0 1288 append(new LookupSwitch(ipop(), sux, keys, state_before, has_bb));
duke@0 1289 }
duke@0 1290 }
duke@0 1291
duke@0 1292 void GraphBuilder::call_register_finalizer() {
duke@0 1293 // If the receiver requires finalization then emit code to perform
duke@0 1294 // the registration on return.
duke@0 1295
duke@0 1296 // Gather some type information about the receiver
duke@0 1297 Value receiver = state()->load_local(0);
duke@0 1298 assert(receiver != NULL, "must have a receiver");
duke@0 1299 ciType* declared_type = receiver->declared_type();
duke@0 1300 ciType* exact_type = receiver->exact_type();
duke@0 1301 if (exact_type == NULL &&
duke@0 1302 receiver->as_Local() &&
duke@0 1303 receiver->as_Local()->java_index() == 0) {
duke@0 1304 ciInstanceKlass* ik = compilation()->method()->holder();
duke@0 1305 if (ik->is_final()) {
duke@0 1306 exact_type = ik;
duke@0 1307 } else if (UseCHA && !(ik->has_subklass() || ik->is_interface())) {
duke@0 1308 // test class is leaf class
duke@0 1309 compilation()->dependency_recorder()->assert_leaf_type(ik);
duke@0 1310 exact_type = ik;
duke@0 1311 } else {
duke@0 1312 declared_type = ik;
duke@0 1313 }
duke@0 1314 }
duke@0 1315
duke@0 1316 // see if we know statically that registration isn't required
duke@0 1317 bool needs_check = true;
duke@0 1318 if (exact_type != NULL) {
duke@0 1319 needs_check = exact_type->as_instance_klass()->has_finalizer();
duke@0 1320 } else if (declared_type != NULL) {
duke@0 1321 ciInstanceKlass* ik = declared_type->as_instance_klass();
duke@0 1322 if (!Dependencies::has_finalizable_subclass(ik)) {
duke@0 1323 compilation()->dependency_recorder()->assert_has_no_finalizable_subclasses(ik);
duke@0 1324 needs_check = false;
duke@0 1325 }
duke@0 1326 }
duke@0 1327
duke@0 1328 if (needs_check) {
duke@0 1329 // Perform the registration of finalizable objects.
duke@0 1330 load_local(objectType, 0);
duke@0 1331 append_split(new Intrinsic(voidType, vmIntrinsics::_Object_init,
duke@0 1332 state()->pop_arguments(1),
duke@0 1333 true, lock_stack(), true));
duke@0 1334 }
duke@0 1335 }
duke@0 1336
duke@0 1337
duke@0 1338 void GraphBuilder::method_return(Value x) {
duke@0 1339 if (RegisterFinalizersAtInit &&
duke@0 1340 method()->intrinsic_id() == vmIntrinsics::_Object_init) {
duke@0 1341 call_register_finalizer();
duke@0 1342 }
duke@0 1343
duke@0 1344 // Check to see whether we are inlining. If so, Return
duke@0 1345 // instructions become Gotos to the continuation point.
duke@0 1346 if (continuation() != NULL) {
duke@0 1347 assert(!method()->is_synchronized() || InlineSynchronizedMethods, "can not inline synchronized methods yet");
duke@0 1348
duke@0 1349 // If the inlined method is synchronized, the monitor must be
duke@0 1350 // released before we jump to the continuation block.
duke@0 1351 if (method()->is_synchronized()) {
duke@0 1352 int i = state()->caller_state()->locks_size();
duke@0 1353 assert(state()->locks_size() == i + 1, "receiver must be locked here");
duke@0 1354 monitorexit(state()->lock_at(i), SynchronizationEntryBCI);
duke@0 1355 }
duke@0 1356
duke@0 1357 state()->truncate_stack(caller_stack_size());
duke@0 1358 if (x != NULL) {
duke@0 1359 state()->push(x->type(), x);
duke@0 1360 }
duke@0 1361 Goto* goto_callee = new Goto(continuation(), false);
duke@0 1362
duke@0 1363 // See whether this is the first return; if so, store off some
duke@0 1364 // of the state for later examination
duke@0 1365 if (num_returns() == 0) {
duke@0 1366 set_inline_cleanup_info(_block, _last, state());
duke@0 1367 }
duke@0 1368
duke@0 1369 // State at end of inlined method is the state of the caller
duke@0 1370 // without the method parameters on stack, including the
duke@0 1371 // return value, if any, of the inlined method on operand stack.
duke@0 1372 set_state(scope_data()->continuation_state()->copy());
duke@0 1373 if (x) {
duke@0 1374 state()->push(x->type(), x);
duke@0 1375 }
duke@0 1376
duke@0 1377 // The current bci() is in the wrong scope, so use the bci() of
duke@0 1378 // the continuation point.
duke@0 1379 append_with_bci(goto_callee, scope_data()->continuation()->bci());
duke@0 1380 incr_num_returns();
duke@0 1381
duke@0 1382 return;
duke@0 1383 }
duke@0 1384
duke@0 1385 state()->truncate_stack(0);
duke@0 1386 if (method()->is_synchronized()) {
duke@0 1387 // perform the unlocking before exiting the method
duke@0 1388 Value receiver;
duke@0 1389 if (!method()->is_static()) {
duke@0 1390 receiver = _initial_state->local_at(0);
duke@0 1391 } else {
duke@0 1392 receiver = append(new Constant(new ClassConstant(method()->holder())));
duke@0 1393 }
duke@0 1394 append_split(new MonitorExit(receiver, state()->unlock()));
duke@0 1395 }
duke@0 1396
duke@0 1397 append(new Return(x));
duke@0 1398 }
duke@0 1399
duke@0 1400
duke@0 1401 void GraphBuilder::access_field(Bytecodes::Code code) {
duke@0 1402 bool will_link;
duke@0 1403 ciField* field = stream()->get_field(will_link);
duke@0 1404 ciInstanceKlass* holder = field->holder();
duke@0 1405 BasicType field_type = field->type()->basic_type();
duke@0 1406 ValueType* type = as_ValueType(field_type);
duke@0 1407 // call will_link again to determine if the field is valid.
duke@0 1408 const bool is_loaded = holder->is_loaded() &&
duke@0 1409 field->will_link(method()->holder(), code);
duke@0 1410 const bool is_initialized = is_loaded && holder->is_initialized();
duke@0 1411
duke@0 1412 ValueStack* state_copy = NULL;
duke@0 1413 if (!is_initialized || PatchALot) {
duke@0 1414 // save state before instruction for debug info when
duke@0 1415 // deoptimization happens during patching
duke@0 1416 state_copy = state()->copy();
duke@0 1417 }
duke@0 1418
duke@0 1419 Value obj = NULL;
duke@0 1420 if (code == Bytecodes::_getstatic || code == Bytecodes::_putstatic) {
duke@0 1421 // commoning of class constants should only occur if the class is
duke@0 1422 // fully initialized and resolved in this constant pool. The will_link test
duke@0 1423 // above essentially checks if this class is resolved in this constant pool
duke@0 1424 // so, the is_initialized flag should be suffiect.
duke@0 1425 if (state_copy != NULL) {
duke@0 1426 // build a patching constant
duke@0 1427 obj = new Constant(new ClassConstant(holder), state_copy);
duke@0 1428 } else {
duke@0 1429 obj = new Constant(new ClassConstant(holder));
duke@0 1430 }
duke@0 1431 }
duke@0 1432
duke@0 1433
duke@0 1434 const int offset = is_loaded ? field->offset() : -1;
duke@0 1435 switch (code) {
duke@0 1436 case Bytecodes::_getstatic: {
duke@0 1437 // check for compile-time constants, i.e., initialized static final fields
duke@0 1438 Instruction* constant = NULL;
duke@0 1439 if (field->is_constant() && !PatchALot) {
duke@0 1440 ciConstant field_val = field->constant_value();
duke@0 1441 BasicType field_type = field_val.basic_type();
duke@0 1442 switch (field_type) {
duke@0 1443 case T_ARRAY:
duke@0 1444 case T_OBJECT:
duke@0 1445 if (field_val.as_object()->has_encoding()) {
duke@0 1446 constant = new Constant(as_ValueType(field_val));
duke@0 1447 }
duke@0 1448 break;
duke@0 1449
duke@0 1450 default:
duke@0 1451 constant = new Constant(as_ValueType(field_val));
duke@0 1452 }
duke@0 1453 }
duke@0 1454 if (constant != NULL) {
duke@0 1455 push(type, append(constant));
duke@0 1456 state_copy = NULL; // Not a potential deoptimization point (see set_state_before logic below)
duke@0 1457 } else {
duke@0 1458 push(type, append(new LoadField(append(obj), offset, field, true,
duke@0 1459 lock_stack(), state_copy, is_loaded, is_initialized)));
duke@0 1460 }
duke@0 1461 break;
duke@0 1462 }
duke@0 1463 case Bytecodes::_putstatic:
duke@0 1464 { Value val = pop(type);
duke@0 1465 append(new StoreField(append(obj), offset, field, val, true, lock_stack(), state_copy, is_loaded, is_initialized));
duke@0 1466 }
duke@0 1467 break;
duke@0 1468 case Bytecodes::_getfield :
duke@0 1469 {
duke@0 1470 LoadField* load = new LoadField(apop(), offset, field, false, lock_stack(), state_copy, is_loaded, true);
duke@0 1471 Value replacement = is_loaded ? _memory->load(load) : load;
duke@0 1472 if (replacement != load) {
duke@0 1473 assert(replacement->bci() != -99 || replacement->as_Phi() || replacement->as_Local(),
duke@0 1474 "should already by linked");
duke@0 1475 push(type, replacement);
duke@0 1476 } else {
duke@0 1477 push(type, append(load));
duke@0 1478 }
duke@0 1479 break;
duke@0 1480 }
duke@0 1481
duke@0 1482 case Bytecodes::_putfield :
duke@0 1483 { Value val = pop(type);
duke@0 1484 StoreField* store = new StoreField(apop(), offset, field, val, false, lock_stack(), state_copy, is_loaded, true);
duke@0 1485 if (is_loaded) store = _memory->store(store);
duke@0 1486 if (store != NULL) {
duke@0 1487 append(store);
duke@0 1488 }
duke@0 1489 }
duke@0 1490 break;
duke@0 1491 default :
duke@0 1492 ShouldNotReachHere();
duke@0 1493 break;
duke@0 1494 }
duke@0 1495 }
duke@0 1496
duke@0 1497
duke@0 1498 Dependencies* GraphBuilder::dependency_recorder() const {
duke@0 1499 assert(DeoptC1, "need debug information");
duke@0 1500 compilation()->set_needs_debug_information(true);
duke@0 1501 return compilation()->dependency_recorder();
duke@0 1502 }
duke@0 1503
duke@0 1504
duke@0 1505 void GraphBuilder::invoke(Bytecodes::Code code) {
duke@0 1506 bool will_link;
duke@0 1507 ciMethod* target = stream()->get_method(will_link);
duke@0 1508 // we have to make sure the argument size (incl. the receiver)
duke@0 1509 // is correct for compilation (the call would fail later during
duke@0 1510 // linkage anyway) - was bug (gri 7/28/99)
duke@0 1511 if (target->is_loaded() && target->is_static() != (code == Bytecodes::_invokestatic)) BAILOUT("will cause link error");
duke@0 1512 ciInstanceKlass* klass = target->holder();
duke@0 1513
duke@0 1514 // check if CHA possible: if so, change the code to invoke_special
duke@0 1515 ciInstanceKlass* calling_klass = method()->holder();
duke@0 1516 ciKlass* holder = stream()->get_declared_method_holder();
duke@0 1517 ciInstanceKlass* callee_holder = ciEnv::get_instance_klass_for_declared_method_holder(holder);
duke@0 1518 ciInstanceKlass* actual_recv = callee_holder;
duke@0 1519
duke@0 1520 // some methods are obviously bindable without any type checks so
duke@0 1521 // convert them directly to an invokespecial.
duke@0 1522 if (target->is_loaded() && !target->is_abstract() &&
duke@0 1523 target->can_be_statically_bound() && code == Bytecodes::_invokevirtual) {
duke@0 1524 code = Bytecodes::_invokespecial;
duke@0 1525 }
duke@0 1526
duke@0 1527 // NEEDS_CLEANUP
duke@0 1528 // I've added the target-is_loaded() test below but I don't really understand
duke@0 1529 // how klass->is_loaded() can be true and yet target->is_loaded() is false.
duke@0 1530 // this happened while running the JCK invokevirtual tests under doit. TKR
duke@0 1531 ciMethod* cha_monomorphic_target = NULL;
duke@0 1532 ciMethod* exact_target = NULL;
duke@0 1533 if (UseCHA && DeoptC1 && klass->is_loaded() && target->is_loaded()) {
duke@0 1534 Value receiver = NULL;
duke@0 1535 ciInstanceKlass* receiver_klass = NULL;
duke@0 1536 bool type_is_exact = false;
duke@0 1537 // try to find a precise receiver type
duke@0 1538 if (will_link && !target->is_static()) {
duke@0 1539 int index = state()->stack_size() - (target->arg_size_no_receiver() + 1);
duke@0 1540 receiver = state()->stack_at(index);
duke@0 1541 ciType* type = receiver->exact_type();
duke@0 1542 if (type != NULL && type->is_loaded() &&
duke@0 1543 type->is_instance_klass() && !type->as_instance_klass()->is_interface()) {
duke@0 1544 receiver_klass = (ciInstanceKlass*) type;
duke@0 1545 type_is_exact = true;
duke@0 1546 }
duke@0 1547 if (type == NULL) {
duke@0 1548 type = receiver->declared_type();
duke@0 1549 if (type != NULL && type->is_loaded() &&
duke@0 1550 type->is_instance_klass() && !type->as_instance_klass()->is_interface()) {
duke@0 1551 receiver_klass = (ciInstanceKlass*) type;
duke@0 1552 if (receiver_klass->is_leaf_type() && !receiver_klass->is_final()) {
duke@0 1553 // Insert a dependency on this type since
duke@0 1554 // find_monomorphic_target may assume it's already done.
duke@0 1555 dependency_recorder()->assert_leaf_type(receiver_klass);
duke@0 1556 type_is_exact = true;
duke@0 1557 }
duke@0 1558 }
duke@0 1559 }
duke@0 1560 }
duke@0 1561 if (receiver_klass != NULL && type_is_exact &&
duke@0 1562 receiver_klass->is_loaded() && code != Bytecodes::_invokespecial) {
duke@0 1563 // If we have the exact receiver type we can bind directly to
duke@0 1564 // the method to call.
duke@0 1565 exact_target = target->resolve_invoke(calling_klass, receiver_klass);
duke@0 1566 if (exact_target != NULL) {
duke@0 1567 target = exact_target;
duke@0 1568 code = Bytecodes::_invokespecial;
duke@0 1569 }
duke@0 1570 }
duke@0 1571 if (receiver_klass != NULL &&
duke@0 1572 receiver_klass->is_subtype_of(actual_recv) &&
duke@0 1573 actual_recv->is_initialized()) {
duke@0 1574 actual_recv = receiver_klass;
duke@0 1575 }
duke@0 1576
duke@0 1577 if ((code == Bytecodes::_invokevirtual && callee_holder->is_initialized()) ||
duke@0 1578 (code == Bytecodes::_invokeinterface && callee_holder->is_initialized() && !actual_recv->is_interface())) {
duke@0 1579 // Use CHA on the receiver to select a more precise method.
duke@0 1580 cha_monomorphic_target = target->find_monomorphic_target(calling_klass, callee_holder, actual_recv);
duke@0 1581 } else if (code == Bytecodes::_invokeinterface && callee_holder->is_loaded() && receiver != NULL) {
duke@0 1582 // if there is only one implementor of this interface then we
duke@0 1583 // may be able bind this invoke directly to the implementing
duke@0 1584 // klass but we need both a dependence on the single interface
duke@0 1585 // and on the method we bind to. Additionally since all we know
duke@0 1586 // about the receiver type is the it's supposed to implement the
duke@0 1587 // interface we have to insert a check that it's the class we
duke@0 1588 // expect. Interface types are not checked by the verifier so
duke@0 1589 // they are roughly equivalent to Object.
duke@0 1590 ciInstanceKlass* singleton = NULL;
duke@0 1591 if (target->holder()->nof_implementors() == 1) {
duke@0 1592 singleton = target->holder()->implementor(0);
duke@0 1593 }
duke@0 1594 if (singleton) {
duke@0 1595 cha_monomorphic_target = target->find_monomorphic_target(calling_klass, target->holder(), singleton);
duke@0 1596 if (cha_monomorphic_target != NULL) {
duke@0 1597 // If CHA is able to bind this invoke then update the class
duke@0 1598 // to match that class, otherwise klass will refer to the
duke@0 1599 // interface.
duke@0 1600 klass = cha_monomorphic_target->holder();
duke@0 1601 actual_recv = target->holder();
duke@0 1602
duke@0 1603 // insert a check it's really the expected class.
duke@0 1604 CheckCast* c = new CheckCast(klass, receiver, NULL);
duke@0 1605 c->set_incompatible_class_change_check();
duke@0 1606 c->set_direct_compare(klass->is_final());
duke@0 1607 append_split(c);
duke@0 1608 }
duke@0 1609 }
duke@0 1610 }
duke@0 1611 }
duke@0 1612
duke@0 1613 if (cha_monomorphic_target != NULL) {
duke@0 1614 if (cha_monomorphic_target->is_abstract()) {
duke@0 1615 // Do not optimize for abstract methods
duke@0 1616 cha_monomorphic_target = NULL;
duke@0 1617 }
duke@0 1618 }
duke@0 1619
duke@0 1620 if (cha_monomorphic_target != NULL) {
duke@0 1621 if (!(target->is_final_method())) {
duke@0 1622 // If we inlined because CHA revealed only a single target method,
duke@0 1623 // then we are dependent on that target method not getting overridden
duke@0 1624 // by dynamic class loading. Be sure to test the "static" receiver
duke@0 1625 // dest_method here, as opposed to the actual receiver, which may
duke@0 1626 // falsely lead us to believe that the receiver is final or private.
duke@0 1627 dependency_recorder()->assert_unique_concrete_method(actual_recv, cha_monomorphic_target);
duke@0 1628 }
duke@0 1629 code = Bytecodes::_invokespecial;
duke@0 1630 }
duke@0 1631 // check if we could do inlining
duke@0 1632 if (!PatchALot && Inline && klass->is_loaded() &&
duke@0 1633 (klass->is_initialized() || klass->is_interface() && target->holder()->is_initialized())
duke@0 1634 && target->will_link(klass, callee_holder, code)) {
duke@0 1635 // callee is known => check if we have static binding
duke@0 1636 assert(target->is_loaded(), "callee must be known");
duke@0 1637 if (code == Bytecodes::_invokestatic
duke@0 1638 || code == Bytecodes::_invokespecial
duke@0 1639 || code == Bytecodes::_invokevirtual && target->is_final_method()
duke@0 1640 ) {
duke@0 1641 // static binding => check if callee is ok
duke@0 1642 ciMethod* inline_target = (cha_monomorphic_target != NULL)
duke@0 1643 ? cha_monomorphic_target
duke@0 1644 : target;
duke@0 1645 bool res = try_inline(inline_target, (cha_monomorphic_target != NULL) || (exact_target != NULL));
duke@0 1646 CHECK_BAILOUT();
duke@0 1647
duke@0 1648 #ifndef PRODUCT
duke@0 1649 // printing
duke@0 1650 if (PrintInlining && !res) {
duke@0 1651 // if it was successfully inlined, then it was already printed.
duke@0 1652 print_inline_result(inline_target, res);
duke@0 1653 }
duke@0 1654 #endif
duke@0 1655 clear_inline_bailout();
duke@0 1656 if (res) {
duke@0 1657 // Register dependence if JVMTI has either breakpoint
duke@0 1658 // setting or hotswapping of methods capabilities since they may
duke@0 1659 // cause deoptimization.
duke@0 1660 if (JvmtiExport::can_hotswap_or_post_breakpoint()) {
duke@0 1661 dependency_recorder()->assert_evol_method(inline_target);
duke@0 1662 }
duke@0 1663 return;
duke@0 1664 }
duke@0 1665 }
duke@0 1666 }
duke@0 1667 // If we attempted an inline which did not succeed because of a
duke@0 1668 // bailout during construction of the callee graph, the entire
duke@0 1669 // compilation has to be aborted. This is fairly rare and currently
duke@0 1670 // seems to only occur for jasm-generated classes which contain
duke@0 1671 // jsr/ret pairs which are not associated with finally clauses and
duke@0 1672 // do not have exception handlers in the containing method, and are
duke@0 1673 // therefore not caught early enough to abort the inlining without
duke@0 1674 // corrupting the graph. (We currently bail out with a non-empty
duke@0 1675 // stack at a ret in these situations.)
duke@0 1676 CHECK_BAILOUT();
duke@0 1677
duke@0 1678 // inlining not successful => standard invoke
duke@0 1679 bool is_static = code == Bytecodes::_invokestatic;
duke@0 1680 ValueType* result_type = as_ValueType(target->return_type());
duke@0 1681 Values* args = state()->pop_arguments(target->arg_size_no_receiver());
duke@0 1682 Value recv = is_static ? NULL : apop();
duke@0 1683 bool is_loaded = target->is_loaded();
duke@0 1684 int vtable_index = methodOopDesc::invalid_vtable_index;
duke@0 1685
duke@0 1686 #ifdef SPARC
duke@0 1687 // Currently only supported on Sparc.
duke@0 1688 // The UseInlineCaches only controls dispatch to invokevirtuals for
duke@0 1689 // loaded classes which we weren't able to statically bind.
duke@0 1690 if (!UseInlineCaches && is_loaded && code == Bytecodes::_invokevirtual
duke@0 1691 && !target->can_be_statically_bound()) {
duke@0 1692 // Find a vtable index if one is available
duke@0 1693 vtable_index = target->resolve_vtable_index(calling_klass, callee_holder);
duke@0 1694 }
duke@0 1695 #endif
duke@0 1696
duke@0 1697 if (recv != NULL &&
duke@0 1698 (code == Bytecodes::_invokespecial ||
duke@0 1699 !is_loaded || target->is_final() ||
duke@0 1700 profile_calls())) {
duke@0 1701 // invokespecial always needs a NULL check. invokevirtual where
duke@0 1702 // the target is final or where it's not known that whether the
duke@0 1703 // target is final requires a NULL check. Otherwise normal
duke@0 1704 // invokevirtual will perform the null check during the lookup
duke@0 1705 // logic or the unverified entry point. Profiling of calls
duke@0 1706 // requires that the null check is performed in all cases.
duke@0 1707 null_check(recv);
duke@0 1708 }
duke@0 1709
duke@0 1710 if (profile_calls()) {
duke@0 1711 assert(cha_monomorphic_target == NULL || exact_target == NULL, "both can not be set");
duke@0 1712 ciKlass* target_klass = NULL;
duke@0 1713 if (cha_monomorphic_target != NULL) {
duke@0 1714 target_klass = cha_monomorphic_target->holder();
duke@0 1715 } else if (exact_target != NULL) {
duke@0 1716 target_klass = exact_target->holder();
duke@0 1717 }
duke@0 1718 profile_call(recv, target_klass);
duke@0 1719 }
duke@0 1720
duke@0 1721 Invoke* result = new Invoke(code, result_type, recv, args, vtable_index, target);
duke@0 1722 // push result
duke@0 1723 append_split(result);
duke@0 1724
duke@0 1725 if (result_type != voidType) {
duke@0 1726 if (method()->is_strict()) {
duke@0 1727 push(result_type, round_fp(result));
duke@0 1728 } else {
duke@0 1729 push(result_type, result);
duke@0 1730 }
duke@0 1731 }
duke@0 1732 }
duke@0 1733
duke@0 1734
duke@0 1735 void GraphBuilder::new_instance(int klass_index) {
duke@0 1736 bool will_link;
duke@0 1737 ciKlass* klass = stream()->get_klass(will_link);
duke@0 1738 assert(klass->is_instance_klass(), "must be an instance klass");
duke@0 1739 NewInstance* new_instance = new NewInstance(klass->as_instance_klass());
duke@0 1740 _memory->new_instance(new_instance);
duke@0 1741 apush(append_split(new_instance));
duke@0 1742 }
duke@0 1743
duke@0 1744
duke@0 1745 void GraphBuilder::new_type_array() {
duke@0 1746 apush(append_split(new NewTypeArray(ipop(), (BasicType)stream()->get_index())));
duke@0 1747 }
duke@0 1748
duke@0 1749
duke@0 1750 void GraphBuilder::new_object_array() {
duke@0 1751 bool will_link;
duke@0 1752 ciKlass* klass = stream()->get_klass(will_link);
duke@0 1753 ValueStack* state_before = !klass->is_loaded() || PatchALot ? state()->copy() : NULL;
duke@0 1754 NewArray* n = new NewObjectArray(klass, ipop(), state_before);
duke@0 1755 apush(append_split(n));
duke@0 1756 }
duke@0 1757
duke@0 1758
duke@0 1759 bool GraphBuilder::direct_compare(ciKlass* k) {
duke@0 1760 if (k->is_loaded() && k->is_instance_klass() && !UseSlowPath) {
duke@0 1761 ciInstanceKlass* ik = k->as_instance_klass();
duke@0 1762 if (ik->is_final()) {
duke@0 1763 return true;
duke@0 1764 } else {
duke@0 1765 if (DeoptC1 && UseCHA && !(ik->has_subklass() || ik->is_interface())) {
duke@0 1766 // test class is leaf class
duke@0 1767 dependency_recorder()->assert_leaf_type(ik);
duke@0 1768 return true;
duke@0 1769 }
duke@0 1770 }
duke@0 1771 }
duke@0 1772 return false;
duke@0 1773 }
duke@0 1774
duke@0 1775
duke@0 1776 void GraphBuilder::check_cast(int klass_index) {
duke@0 1777 bool will_link;
duke@0 1778 ciKlass* klass = stream()->get_klass(will_link);
duke@0 1779 ValueStack* state_before = !klass->is_loaded() || PatchALot ? state()->copy() : NULL;
duke@0 1780 CheckCast* c = new CheckCast(klass, apop(), state_before);
duke@0 1781 apush(append_split(c));
duke@0 1782 c->set_direct_compare(direct_compare(klass));
duke@0 1783 if (profile_checkcasts()) {
duke@0 1784 c->set_profiled_method(method());
duke@0 1785 c->set_profiled_bci(bci());
duke@0 1786 c->set_should_profile(true);
duke@0 1787 }
duke@0 1788 }
duke@0 1789
duke@0 1790
duke@0 1791 void GraphBuilder::instance_of(int klass_index) {
duke@0 1792 bool will_link;
duke@0 1793 ciKlass* klass = stream()->get_klass(will_link);
duke@0 1794 ValueStack* state_before = !klass->is_loaded() || PatchALot ? state()->copy() : NULL;
duke@0 1795 InstanceOf* i = new InstanceOf(klass, apop(), state_before);
duke@0 1796 ipush(append_split(i));
duke@0 1797 i->set_direct_compare(direct_compare(klass));
duke@0 1798 }
duke@0 1799
duke@0 1800
duke@0 1801 void GraphBuilder::monitorenter(Value x, int bci) {
duke@0 1802 // save state before locking in case of deoptimization after a NullPointerException
duke@0 1803 ValueStack* lock_stack_before = lock_stack();
duke@0 1804 append_with_bci(new MonitorEnter(x, state()->lock(scope(), x), lock_stack_before), bci);
duke@0 1805 kill_all();
duke@0 1806 }
duke@0 1807
duke@0 1808
duke@0 1809 void GraphBuilder::monitorexit(Value x, int bci) {
duke@0 1810 // Note: the comment below is only relevant for the case where we do
duke@0 1811 // not deoptimize due to asynchronous exceptions (!(DeoptC1 &&
duke@0 1812 // DeoptOnAsyncException), which is not used anymore)
duke@0 1813
duke@0 1814 // Note: Potentially, the monitor state in an exception handler
duke@0 1815 // can be wrong due to wrong 'initialization' of the handler
duke@0 1816 // via a wrong asynchronous exception path. This can happen,
duke@0 1817 // if the exception handler range for asynchronous exceptions
duke@0 1818 // is too long (see also java bug 4327029, and comment in
duke@0 1819 // GraphBuilder::handle_exception()). This may cause 'under-
duke@0 1820 // flow' of the monitor stack => bailout instead.
duke@0 1821 if (state()->locks_size() < 1) BAILOUT("monitor stack underflow");
duke@0 1822 append_with_bci(new MonitorExit(x, state()->unlock()), bci);
duke@0 1823 kill_all();
duke@0 1824 }
duke@0 1825
duke@0 1826
duke@0 1827 void GraphBuilder::new_multi_array(int dimensions) {
duke@0 1828 bool will_link;
duke@0 1829 ciKlass* klass = stream()->get_klass(will_link);
duke@0 1830 ValueStack* state_before = !klass->is_loaded() || PatchALot ? state()->copy() : NULL;
duke@0 1831
duke@0 1832 Values* dims = new Values(dimensions, NULL);
duke@0 1833 // fill in all dimensions
duke@0 1834 int i = dimensions;
duke@0 1835 while (i-- > 0) dims->at_put(i, ipop());
duke@0 1836 // create array
duke@0 1837 NewArray* n = new NewMultiArray(klass, dims, state_before);
duke@0 1838 apush(append_split(n));
duke@0 1839 }
duke@0 1840
duke@0 1841
duke@0 1842 void GraphBuilder::throw_op(int bci) {
duke@0 1843 // We require that the debug info for a Throw be the "state before"
duke@0 1844 // the Throw (i.e., exception oop is still on TOS)
duke@0 1845 ValueStack* state_before = state()->copy();
duke@0 1846 Throw* t = new Throw(apop(), state_before);
duke@0 1847 append_with_bci(t, bci);
duke@0 1848 }
duke@0 1849
duke@0 1850
duke@0 1851 Value GraphBuilder::round_fp(Value fp_value) {
duke@0 1852 // no rounding needed if SSE2 is used
duke@0 1853 if (RoundFPResults && UseSSE < 2) {
duke@0 1854 // Must currently insert rounding node for doubleword values that
duke@0 1855 // are results of expressions (i.e., not loads from memory or
duke@0 1856 // constants)
duke@0 1857 if (fp_value->type()->tag() == doubleTag &&
duke@0 1858 fp_value->as_Constant() == NULL &&
duke@0 1859 fp_value->as_Local() == NULL && // method parameters need no rounding
duke@0 1860 fp_value->as_RoundFP() == NULL) {
duke@0 1861 return append(new RoundFP(fp_value));
duke@0 1862 }
duke@0 1863 }
duke@0 1864 return fp_value;
duke@0 1865 }
duke@0 1866
duke@0 1867
duke@0 1868 Instruction* GraphBuilder::append_with_bci(Instruction* instr, int bci) {
duke@0 1869 Canonicalizer canon(instr, bci);
duke@0 1870 Instruction* i1 = canon.canonical();
duke@0 1871 if (i1->bci() != -99) {
duke@0 1872 // Canonicalizer returned an instruction which was already
duke@0 1873 // appended so simply return it.
duke@0 1874 return i1;
duke@0 1875 } else if (UseLocalValueNumbering) {
duke@0 1876 // Lookup the instruction in the ValueMap and add it to the map if
duke@0 1877 // it's not found.
duke@0 1878 Instruction* i2 = vmap()->find_insert(i1);
duke@0 1879 if (i2 != i1) {
duke@0 1880 // found an entry in the value map, so just return it.
duke@0 1881 assert(i2->bci() != -1, "should already be linked");
duke@0 1882 return i2;
duke@0 1883 }
never@459 1884 ValueNumberingEffects vne(vmap());
never@459 1885 i1->visit(&vne);
duke@0 1886 }
duke@0 1887
duke@0 1888 if (i1->as_Phi() == NULL && i1->as_Local() == NULL) {
duke@0 1889 // i1 was not eliminated => append it
duke@0 1890 assert(i1->next() == NULL, "shouldn't already be linked");
duke@0 1891 _last = _last->set_next(i1, canon.bci());
duke@0 1892 if (++_instruction_count >= InstructionCountCutoff
duke@0 1893 && !bailed_out()) {
duke@0 1894 // set the bailout state but complete normal processing. We
duke@0 1895 // might do a little more work before noticing the bailout so we
duke@0 1896 // want processing to continue normally until it's noticed.
duke@0 1897 bailout("Method and/or inlining is too large");
duke@0 1898 }
duke@0 1899
duke@0 1900 #ifndef PRODUCT
duke@0 1901 if (PrintIRDuringConstruction) {
duke@0 1902 InstructionPrinter ip;
duke@0 1903 ip.print_line(i1);
duke@0 1904 if (Verbose) {
duke@0 1905 state()->print();
duke@0 1906 }
duke@0 1907 }
duke@0 1908 #endif
duke@0 1909 assert(_last == i1, "adjust code below");
duke@0 1910 StateSplit* s = i1->as_StateSplit();
duke@0 1911 if (s != NULL && i1->as_BlockEnd() == NULL) {
duke@0 1912 if (EliminateFieldAccess) {
never@459 1913 Intrinsic* intrinsic = s->as_Intrinsic();
duke@0 1914 if (s->as_Invoke() != NULL || (intrinsic && !intrinsic->preserves_state())) {
duke@0 1915 _memory->kill();
duke@0 1916 }
duke@0 1917 }
duke@0 1918 s->set_state(state()->copy());
duke@0 1919 }
duke@0 1920 // set up exception handlers for this instruction if necessary
duke@0 1921 if (i1->can_trap()) {
duke@0 1922 assert(exception_state() != NULL || !has_handler(), "must have setup exception state");
duke@0 1923 i1->set_exception_handlers(handle_exception(bci));
duke@0 1924 }
duke@0 1925 }
duke@0 1926 return i1;
duke@0 1927 }
duke@0 1928
duke@0 1929
duke@0 1930 Instruction* GraphBuilder::append(Instruction* instr) {
duke@0 1931 assert(instr->as_StateSplit() == NULL || instr->as_BlockEnd() != NULL, "wrong append used");
duke@0 1932 return append_with_bci(instr, bci());
duke@0 1933 }
duke@0 1934
duke@0 1935
duke@0 1936 Instruction* GraphBuilder::append_split(StateSplit* instr) {
duke@0 1937 return append_with_bci(instr, bci());
duke@0 1938 }
duke@0 1939
duke@0 1940
duke@0 1941 void GraphBuilder::null_check(Value value) {
duke@0 1942 if (value->as_NewArray() != NULL || value->as_NewInstance() != NULL) {
duke@0 1943 return;
duke@0 1944 } else {
duke@0 1945 Constant* con = value->as_Constant();
duke@0 1946 if (con) {
duke@0 1947 ObjectType* c = con->type()->as_ObjectType();
duke@0 1948 if (c && c->is_loaded()) {
duke@0 1949 ObjectConstant* oc = c->as_ObjectConstant();
duke@0 1950 if (!oc || !oc->value()->is_null_object()) {
duke@0 1951 return;
duke@0 1952 }
duke@0 1953 }
duke@0 1954 }
duke@0 1955 }
duke@0 1956 append(new NullCheck(value, lock_stack()));
duke@0 1957 }
duke@0 1958
duke@0 1959
duke@0 1960
duke@0 1961 XHandlers* GraphBuilder::handle_exception(int cur_bci) {
duke@0 1962 // fast path if it is guaranteed that no exception handlers are present
duke@0 1963 if (!has_handler()) {
duke@0 1964 // TODO: check if return NULL is possible (avoids empty lists)
duke@0 1965 return new XHandlers();
duke@0 1966 }
duke@0 1967
duke@0 1968 XHandlers* exception_handlers = new XHandlers();
duke@0 1969 ScopeData* cur_scope_data = scope_data();
duke@0 1970 ValueStack* s = exception_state();
duke@0 1971 int scope_count = 0;
duke@0 1972
duke@0 1973 assert(s != NULL, "exception state must be set");
duke@0 1974 do {
duke@0 1975 assert(cur_scope_data->scope() == s->scope(), "scopes do not match");
duke@0 1976 assert(cur_bci == SynchronizationEntryBCI || cur_bci == cur_scope_data->stream()->cur_bci(), "invalid bci");
duke@0 1977
duke@0 1978 // join with all potential exception handlers
duke@0 1979 XHandlers* list = cur_scope_data->xhandlers();
duke@0 1980 const int n = list->length();
duke@0 1981 for (int i = 0; i < n; i++) {
duke@0 1982 XHandler* h = list->handler_at(i);
duke@0 1983 if (h->covers(cur_bci)) {
duke@0 1984 // h is a potential exception handler => join it
duke@0 1985 compilation()->set_has_exception_handlers(true);
duke@0 1986
duke@0 1987 BlockBegin* entry = h->entry_block();
duke@0 1988 if (entry == block()) {
duke@0 1989 // It's acceptable for an exception handler to cover itself
duke@0 1990 // but we don't handle that in the parser currently. It's
duke@0 1991 // very rare so we bailout instead of trying to handle it.
duke@0 1992 BAILOUT_("exception handler covers itself", exception_handlers);
duke@0 1993 }
duke@0 1994 assert(entry->bci() == h->handler_bci(), "must match");
duke@0 1995 assert(entry->bci() == -1 || entry == cur_scope_data->block_at(entry->bci()), "blocks must correspond");
duke@0 1996
duke@0 1997 // previously this was a BAILOUT, but this is not necessary
duke@0 1998 // now because asynchronous exceptions are not handled this way.
duke@0 1999 assert(entry->state() == NULL || s->locks_size() == entry->state()->locks_size(), "locks do not match");
duke@0 2000
duke@0 2001 // xhandler start with an empty expression stack
duke@0 2002 s->truncate_stack(cur_scope_data->caller_stack_size());
duke@0 2003
duke@0 2004 // Note: Usually this join must work. However, very
duke@0 2005 // complicated jsr-ret structures where we don't ret from
duke@0 2006 // the subroutine can cause the objects on the monitor
duke@0 2007 // stacks to not match because blocks can be parsed twice.
duke@0 2008 // The only test case we've seen so far which exhibits this
duke@0 2009 // problem is caught by the infinite recursion test in
duke@0 2010 // GraphBuilder::jsr() if the join doesn't work.
duke@0 2011 if (!entry->try_merge(s)) {
duke@0 2012 BAILOUT_("error while joining with exception handler, prob. due to complicated jsr/rets", exception_handlers);
duke@0 2013 }
duke@0 2014
duke@0 2015 // add current state for correct handling of phi functions at begin of xhandler
duke@0 2016 int phi_operand = entry->add_exception_state(s);
duke@0 2017
duke@0 2018 // add entry to the list of xhandlers of this block
duke@0 2019 _block->add_exception_handler(entry);
duke@0 2020
duke@0 2021 // add back-edge from xhandler entry to this block
duke@0 2022 if (!entry->is_predecessor(_block)) {
duke@0 2023 entry->add_predecessor(_block);
duke@0 2024 }
duke@0 2025
duke@0 2026 // clone XHandler because phi_operand and scope_count can not be shared
duke@0 2027 XHandler* new_xhandler = new XHandler(h);
duke@0 2028 new_xhandler->set_phi_operand(phi_operand);
duke@0 2029 new_xhandler->set_scope_count(scope_count);
duke@0 2030 exception_handlers->append(new_xhandler);
duke@0 2031
duke@0 2032 // fill in exception handler subgraph lazily
duke@0 2033 assert(!entry->is_set(BlockBegin::was_visited_flag), "entry must not be visited yet");
duke@0 2034 cur_scope_data->add_to_work_list(entry);
duke@0 2035
duke@0 2036 // stop when reaching catchall
duke@0 2037 if (h->catch_type() == 0) {
duke@0 2038 return exception_handlers;
duke@0 2039 }
duke@0 2040 }
duke@0 2041 }
duke@0 2042
duke@0 2043 // Set up iteration for next time.
duke@0 2044 // If parsing a jsr, do not grab exception handlers from the
duke@0 2045 // parent scopes for this method (already got them, and they
duke@0 2046 // needed to be cloned)
duke@0 2047 if (cur_scope_data->parsing_jsr()) {
duke@0 2048 IRScope* tmp_scope = cur_scope_data->scope();
duke@0 2049 while (cur_scope_data->parent() != NULL &&
duke@0 2050 cur_scope_data->parent()->scope() == tmp_scope) {
duke@0 2051 cur_scope_data = cur_scope_data->parent();
duke@0 2052 }
duke@0 2053 }
duke@0 2054 if (cur_scope_data != NULL) {
duke@0 2055 if (cur_scope_data->parent() != NULL) {
duke@0 2056 // must use pop_scope instead of caller_state to preserve all monitors
duke@0 2057 s = s->pop_scope();
duke@0 2058 }
duke@0 2059 cur_bci = cur_scope_data->scope()->caller_bci();
duke@0 2060 cur_scope_data = cur_scope_data->parent();
duke@0 2061 scope_count++;
duke@0 2062 }
duke@0 2063 } while (cur_scope_data != NULL);
duke@0 2064
duke@0 2065 return exception_handlers;
duke@0 2066 }
duke@0 2067
duke@0 2068
duke@0 2069 // Helper class for simplifying Phis.
duke@0 2070 class PhiSimplifier : public BlockClosure {
duke@0 2071 private:
duke@0 2072 bool _has_substitutions;
duke@0 2073 Value simplify(Value v);
duke@0 2074
duke@0 2075 public:
duke@0 2076 PhiSimplifier(BlockBegin* start) : _has_substitutions(false) {
duke@0 2077 start->iterate_preorder(this);
duke@0 2078 if (_has_substitutions) {
duke@0 2079 SubstitutionResolver sr(start);
duke@0 2080 }
duke@0 2081 }
duke@0 2082 void block_do(BlockBegin* b);
duke@0 2083 bool has_substitutions() const { return _has_substitutions; }
duke@0 2084 };
duke@0 2085
duke@0 2086
duke@0 2087 Value PhiSimplifier::simplify(Value v) {
duke@0 2088 Phi* phi = v->as_Phi();
duke@0 2089
duke@0 2090 if (phi == NULL) {
duke@0 2091 // no phi function
duke@0 2092 return v;
duke@0 2093 } else if (v->has_subst()) {
duke@0 2094 // already substituted; subst can be phi itself -> simplify
duke@0 2095 return simplify(v->subst());
duke@0 2096 } else if (phi->is_set(Phi::cannot_simplify)) {
duke@0 2097 // already tried to simplify phi before
duke@0 2098 return phi;
duke@0 2099 } else if (phi->is_set(Phi::visited)) {
duke@0 2100 // break cycles in phi functions
duke@0 2101 return phi;
duke@0 2102 } else if (phi->type()->is_illegal()) {
duke@0 2103 // illegal phi functions are ignored anyway
duke@0 2104 return phi;
duke@0 2105
duke@0 2106 } else {
duke@0 2107 // mark phi function as processed to break cycles in phi functions
duke@0 2108 phi->set(Phi::visited);
duke@0 2109
duke@0 2110 // simplify x = [y, x] and x = [y, y] to y
duke@0 2111 Value subst = NULL;
duke@0 2112 int opd_count = phi->operand_count();
duke@0 2113 for (int i = 0; i < opd_count; i++) {
duke@0 2114 Value opd = phi->operand_at(i);
duke@0 2115 assert(opd != NULL, "Operand must exist!");
duke@0 2116
duke@0 2117 if (opd->type()->is_illegal()) {
duke@0 2118 // if one operand is illegal, the entire phi function is illegal
duke@0 2119 phi->make_illegal();
duke@0 2120 phi->clear(Phi::visited);
duke@0 2121 return phi;
duke@0 2122 }
duke@0 2123
duke@0 2124 Value new_opd = simplify(opd);
duke@0 2125 assert(new_opd != NULL, "Simplified operand must exist!");
duke@0 2126
duke@0 2127 if (new_opd != phi && new_opd != subst) {
duke@0 2128 if (subst == NULL) {
duke@0 2129 subst = new_opd;
duke@0 2130 } else {
duke@0 2131 // no simplification possible
duke@0 2132 phi->set(Phi::cannot_simplify);
duke@0 2133 phi->clear(Phi::visited);
duke@0 2134 return phi;
duke@0 2135 }
duke@0 2136 }
duke@0 2137 }
duke@0 2138
duke@0 2139 // sucessfully simplified phi function
duke@0 2140 assert(subst != NULL, "illegal phi function");
duke@0 2141 _has_substitutions = true;
duke@0 2142 phi->clear(Phi::visited);
duke@0 2143 phi->set_subst(subst);
duke@0 2144
duke@0 2145 #ifndef PRODUCT
duke@0 2146 if (PrintPhiFunctions) {
duke@0 2147 tty->print_cr("simplified phi function %c%d to %c%d (Block B%d)", phi->type()->tchar(), phi->id(), subst->type()->tchar(), subst->id(), phi->block()->block_id());
duke@0 2148 }
duke@0 2149 #endif
duke@0 2150
duke@0 2151 return subst;
duke@0 2152 }
duke@0 2153 }
duke@0 2154
duke@0 2155
duke@0 2156 void PhiSimplifier::block_do(BlockBegin* b) {
duke@0 2157 for_each_phi_fun(b, phi,
duke@0 2158 simplify(phi);
duke@0 2159 );
duke@0 2160
duke@0 2161 #ifdef ASSERT
duke@0 2162 for_each_phi_fun(b, phi,
duke@0 2163 assert(phi->operand_count() != 1 || phi->subst() != phi, "missed trivial simplification");
duke@0 2164 );
duke@0 2165
duke@0 2166 ValueStack* state = b->state()->caller_state();
duke@0 2167 int index;
duke@0 2168 Value value;
duke@0 2169 for_each_state(state) {
duke@0 2170 for_each_local_value(state, index, value) {
duke@0 2171 Phi* phi = value->as_Phi();
duke@0 2172 assert(phi == NULL || phi->block() != b, "must not have phi function to simplify in caller state");
duke@0 2173 }
duke@0 2174 }
duke@0 2175 #endif
duke@0 2176 }
duke@0 2177
duke@0 2178 // This method is called after all blocks are filled with HIR instructions
duke@0 2179 // It eliminates all Phi functions of the form x = [y, y] and x = [y, x]
duke@0 2180 void GraphBuilder::eliminate_redundant_phis(BlockBegin* start) {
duke@0 2181 PhiSimplifier simplifier(start);
duke@0 2182 }
duke@0 2183
duke@0 2184
duke@0 2185 void GraphBuilder::connect_to_end(BlockBegin* beg) {
duke@0 2186 // setup iteration
duke@0 2187 kill_all();
duke@0 2188 _block = beg;
duke@0 2189 _state = beg->state()->copy();
duke@0 2190 _last = beg;
duke@0 2191 iterate_bytecodes_for_block(beg->bci());
duke@0 2192 }
duke@0 2193
duke@0 2194
duke@0 2195 BlockEnd* GraphBuilder::iterate_bytecodes_for_block(int bci) {
duke@0 2196 #ifndef PRODUCT
duke@0 2197 if (PrintIRDuringConstruction) {
duke@0 2198 tty->cr();
duke@0 2199 InstructionPrinter ip;
duke@0 2200 ip.print_instr(_block); tty->cr();
duke@0 2201 ip.print_stack(_block->state()); tty->cr();
duke@0 2202 ip.print_inline_level(_block);
duke@0 2203 ip.print_head();
duke@0 2204 tty->print_cr("locals size: %d stack size: %d", state()->locals_size(), state()->stack_size());
duke@0 2205 }
duke@0 2206 #endif
duke@0 2207 _skip_block = false;
duke@0 2208 assert(state() != NULL, "ValueStack missing!");
duke@0 2209 ciBytecodeStream s(method());
duke@0 2210 s.reset_to_bci(bci);
duke@0 2211 int prev_bci = bci;
duke@0 2212 scope_data()->set_stream(&s);
duke@0 2213 // iterate
duke@0 2214 Bytecodes::Code code = Bytecodes::_illegal;
duke@0 2215 bool push_exception = false;
duke@0 2216
duke@0 2217 if (block()->is_set(BlockBegin::exception_entry_flag) && block()->next() == NULL) {
duke@0 2218 // first thing in the exception entry block should be the exception object.
duke@0 2219 push_exception = true;
duke@0 2220 }
duke@0 2221
duke@0 2222 while (!bailed_out() && last()->as_BlockEnd() == NULL &&
duke@0 2223 (code = stream()->next()) != ciBytecodeStream::EOBC() &&
duke@0 2224 (block_at(s.cur_bci()) == NULL || block_at(s.cur_bci()) == block())) {
duke@0 2225
duke@0 2226 if (has_handler() && can_trap(method(), code)) {
duke@0 2227 // copy the state because it is modified before handle_exception is called
duke@0 2228 set_exception_state(state()->copy());
duke@0 2229 } else {
duke@0 2230 // handle_exception is not called for this bytecode
duke@0 2231 set_exception_state(NULL);
duke@0 2232 }
duke@0 2233
duke@0 2234 // Check for active jsr during OSR compilation
duke@0 2235 if (compilation()->is_osr_compile()
duke@0 2236 && scope()->is_top_scope()
duke@0 2237 && parsing_jsr()
duke@0 2238 && s.cur_bci() == compilation()->osr_bci()) {
duke@0 2239 bailout("OSR not supported while a jsr is active");
duke@0 2240 }
duke@0 2241
duke@0 2242 if (push_exception) {
duke@0 2243 apush(append(new ExceptionObject()));
duke@0 2244 push_exception = false;
duke@0 2245 }
duke@0 2246
duke@0 2247 // handle bytecode
duke@0 2248 switch (code) {
duke@0 2249 case Bytecodes::_nop : /* nothing to do */ break;
duke@0 2250 case Bytecodes::_aconst_null : apush(append(new Constant(objectNull ))); break;
duke@0 2251 case Bytecodes::_iconst_m1 : ipush(append(new Constant(new IntConstant (-1)))); break;
duke@0 2252 case Bytecodes::_iconst_0 : ipush(append(new Constant(intZero ))); break;
duke@0 2253 case Bytecodes::_iconst_1 : ipush(append(new Constant(intOne ))); break;
duke@0 2254 case Bytecodes::_iconst_2 : ipush(append(new Constant(new IntConstant ( 2)))); break;
duke@0 2255 case Bytecodes::_iconst_3 : ipush(append(new Constant(new IntConstant ( 3)))); break;
duke@0 2256 case Bytecodes::_iconst_4 : ipush(append(new Constant(new IntConstant ( 4)))); break;
duke@0 2257 case Bytecodes::_iconst_5 : ipush(append(new Constant(new IntConstant ( 5)))); break;
duke@0 2258 case Bytecodes::_lconst_0 : lpush(append(new Constant(new LongConstant ( 0)))); break;
duke@0 2259 case Bytecodes::_lconst_1 : lpush(append(new Constant(new LongConstant ( 1)))); break;
duke@0 2260 case Bytecodes::_fconst_0 : fpush(append(new Constant(new FloatConstant ( 0)))); break;
duke@0 2261 case Bytecodes::_fconst_1 : fpush(append(new Constant(new FloatConstant ( 1)))); break;
duke@0 2262 case Bytecodes::_fconst_2 : fpush(append(new Constant(new FloatConstant ( 2)))); break;
duke@0 2263 case Bytecodes::_dconst_0 : dpush(append(new Constant(new DoubleConstant( 0)))); break;
duke@0 2264 case Bytecodes::_dconst_1 : dpush(append(new Constant(new DoubleConstant( 1)))); break;
duke@0 2265 case Bytecodes::_bipush : ipush(append(new Constant(new IntConstant(((signed char*)s.cur_bcp())[1])))); break;
duke@0 2266 case Bytecodes::_sipush : ipush(append(new Constant(new IntConstant((short)Bytes::get_Java_u2(s.cur_bcp()+1))))); break;
duke@0 2267 case Bytecodes::_ldc : // fall through
duke@0 2268 case Bytecodes::_ldc_w : // fall through
duke@0 2269 case Bytecodes::_ldc2_w : load_constant(); break;
duke@0 2270 case Bytecodes::_iload : load_local(intType , s.get_index()); break;
duke@0 2271 case Bytecodes::_lload : load_local(longType , s.get_index()); break;
duke@0 2272 case Bytecodes::_fload : load_local(floatType , s.get_index()); break;
duke@0 2273 case Bytecodes::_dload : load_local(doubleType , s.get_index()); break;
duke@0 2274 case Bytecodes::_aload : load_local(instanceType, s.get_index()); break;
duke@0 2275 case Bytecodes::_iload_0 : load_local(intType , 0); break;
duke@0 2276 case Bytecodes::_iload_1 : load_local(intType , 1); break;
duke@0 2277 case Bytecodes::_iload_2 : load_local(intType , 2); break;
duke@0 2278 case Bytecodes::_iload_3 : load_local(intType , 3); break;
duke@0 2279 case Bytecodes::_lload_0 : load_local(longType , 0); break;
duke@0 2280 case Bytecodes::_lload_1 : load_local(longType , 1); break;
duke@0 2281 case Bytecodes::_lload_2 : load_local(longType , 2); break;
duke@0 2282 case Bytecodes::_lload_3 : load_local(longType , 3); break;
duke@0 2283 case Bytecodes::_fload_0 : load_local(floatType , 0); break;
duke@0 2284 case Bytecodes::_fload_1 : load_local(floatType , 1); break;
duke@0 2285 case Bytecodes::_fload_2 : load_local(floatType , 2); break;
duke@0 2286 case Bytecodes::_fload_3 : load_local(floatType , 3); break;
duke@0 2287 case Bytecodes::_dload_0 : load_local(doubleType, 0); break;
duke@0 2288 case Bytecodes::_dload_1 : load_local(doubleType, 1); break;
duke@0 2289 case Bytecodes::_dload_2 : load_local(doubleType, 2); break;
duke@0 2290 case Bytecodes::_dload_3 : load_local(doubleType, 3); break;
duke@0 2291 case Bytecodes::_aload_0 : load_local(objectType, 0); break;
duke@0 2292 case Bytecodes::_aload_1 : load_local(objectType, 1); break;
duke@0 2293 case Bytecodes::_aload_2 : load_local(objectType, 2); break;
duke@0 2294 case Bytecodes::_aload_3 : load_local(objectType, 3); break;
duke@0 2295 case Bytecodes::_iaload : load_indexed(T_INT ); break;
duke@0 2296 case Bytecodes::_laload : load_indexed(T_LONG ); break;
duke@0 2297 case Bytecodes::_faload : load_indexed(T_FLOAT ); break;
duke@0 2298 case Bytecodes::_daload : load_indexed(T_DOUBLE); break;
duke@0 2299 case Bytecodes::_aaload : load_indexed(T_OBJECT); break;
duke@0 2300 case Bytecodes::_baload : load_indexed(T_BYTE ); break;
duke@0 2301 case Bytecodes::_caload : load_indexed(T_CHAR ); break;
duke@0 2302 case Bytecodes::_saload : load_indexed(T_SHORT ); break;
duke@0 2303 case Bytecodes::_istore : store_local(intType , s.get_index()); break;
duke@0 2304 case Bytecodes::_lstore : store_local(longType , s.get_index()); break;
duke@0 2305 case Bytecodes::_fstore : store_local(floatType , s.get_index()); break;
duke@0 2306 case Bytecodes::_dstore : store_local(doubleType, s.get_index()); break;
duke@0 2307 case Bytecodes::_astore : store_local(objectType, s.get_index()); break;
duke@0 2308 case Bytecodes::_istore_0 : store_local(intType , 0); break;
duke@0 2309 case Bytecodes::_istore_1 : store_local(intType , 1); break;
duke@0 2310 case Bytecodes::_istore_2 : store_local(intType , 2); break;
duke@0 2311 case Bytecodes::_istore_3 : store_local(intType , 3); break;
duke@0 2312 case Bytecodes::_lstore_0 : store_local(longType , 0); break;
duke@0 2313 case Bytecodes::_lstore_1 : store_local(longType , 1); break;
duke@0 2314 case Bytecodes::_lstore_2 : store_local(longType , 2); break;
duke@0 2315 case Bytecodes::_lstore_3 : store_local(longType , 3); break;
duke@0 2316 case Bytecodes::_fstore_0 : store_local(floatType , 0); break;
duke@0 2317 case Bytecodes::_fstore_1 : store_local(floatType , 1); break;
duke@0 2318 case Bytecodes::_fstore_2 : store_local(floatType , 2); break;
duke@0 2319 case Bytecodes::_fstore_3 : store_local(floatType , 3); break;
duke@0 2320 case Bytecodes::_dstore_0 : store_local(doubleType, 0); break;
duke@0 2321 case Bytecodes::_dstore_1 : store_local(doubleType, 1); break;
duke@0 2322 case Bytecodes::_dstore_2 : store_local(doubleType, 2); break;
duke@0 2323 case Bytecodes::_dstore_3 : store_local(doubleType, 3); break;
duke@0 2324 case Bytecodes::_astore_0 : store_local(objectType, 0); break;
duke@0 2325 case Bytecodes::_astore_1 : store_local(objectType, 1); break;
duke@0 2326 case Bytecodes::_astore_2 : store_local(objectType, 2); break;
duke@0 2327 case Bytecodes::_astore_3 : store_local(objectType, 3); break;
duke@0 2328 case Bytecodes::_iastore : store_indexed(T_INT ); break;
duke@0 2329 case Bytecodes::_lastore : store_indexed(T_LONG ); break;
duke@0 2330 case Bytecodes::_fastore : store_indexed(T_FLOAT ); break;
duke@0 2331 case Bytecodes::_dastore : store_indexed(T_DOUBLE); break;
duke@0 2332 case Bytecodes::_aastore : store_indexed(T_OBJECT); break;
duke@0 2333 case Bytecodes::_bastore : store_indexed(T_BYTE ); break;
duke@0 2334 case Bytecodes::_castore : store_indexed(T_CHAR ); break;
duke@0 2335 case Bytecodes::_sastore : store_indexed(T_SHORT ); break;
duke@0 2336 case Bytecodes::_pop : // fall through
duke@0 2337 case Bytecodes::_pop2 : // fall through
duke@0 2338 case Bytecodes::_dup : // fall through
duke@0 2339 case Bytecodes::_dup_x1 : // fall through
duke@0 2340 case Bytecodes::_dup_x2 : // fall through
duke@0 2341 case Bytecodes::_dup2 : // fall through
duke@0 2342 case Bytecodes::_dup2_x1 : // fall through
duke@0 2343 case Bytecodes::_dup2_x2 : // fall through
duke@0 2344 case Bytecodes::_swap : stack_op(code); break;
duke@0 2345 case Bytecodes::_iadd : arithmetic_op(intType , code); break;
duke@0 2346 case Bytecodes::_ladd : arithmetic_op(longType , code); break;
duke@0 2347 case Bytecodes::_fadd : arithmetic_op(floatType , code); break;
duke@0 2348 case Bytecodes::_dadd : arithmetic_op(doubleType, code); break;
duke@0 2349 case Bytecodes::_isub : arithmetic_op(intType , code); break;
duke@0 2350 case Bytecodes::_lsub : arithmetic_op(longType , code); break;
duke@0 2351 case Bytecodes::_fsub : arithmetic_op(floatType , code); break;
duke@0 2352 case Bytecodes::_dsub : arithmetic_op(doubleType, code); break;
duke@0 2353 case Bytecodes::_imul : arithmetic_op(intType , code); break;
duke@0 2354 case Bytecodes::_lmul : arithmetic_op(longType , code); break;
duke@0 2355 case Bytecodes::_fmul : arithmetic_op(floatType , code); break;
duke@0 2356 case Bytecodes::_dmul : arithmetic_op(doubleType, code); break;
duke@0 2357 case Bytecodes::_idiv : arithmetic_op(intType , code, lock_stack()); break;
duke@0 2358 case Bytecodes::_ldiv : arithmetic_op(longType , code, lock_stack()); break;
duke@0 2359 case Bytecodes::_fdiv : arithmetic_op(floatType , code); break;
duke@0 2360 case Bytecodes::_ddiv : arithmetic_op(doubleType, code); break;
duke@0 2361 case Bytecodes::_irem : arithmetic_op(intType , code, lock_stack()); break;
duke@0 2362 case Bytecodes::_lrem : arithmetic_op(longType , code, lock_stack()); break;
duke@0 2363 case Bytecodes::_frem : arithmetic_op(floatType , code); break;
duke@0 2364 case Bytecodes::_drem : arithmetic_op(doubleType, code); break;
duke@0 2365 case Bytecodes::_ineg : negate_op(intType ); break;
duke@0 2366 case Bytecodes::_lneg : negate_op(longType ); break;
duke@0 2367 case Bytecodes::_fneg : negate_op(floatType ); break;
duke@0 2368 case Bytecodes::_dneg : negate_op(doubleType); break;
duke@0 2369 case Bytecodes::_ishl : shift_op(intType , code); break;
duke@0 2370 case Bytecodes::_lshl : shift_op(longType, code); break;
duke@0 2371 case Bytecodes::_ishr : shift_op(intType , code); break;
duke@0 2372 case Bytecodes::_lshr : shift_op(longType, code); break;
duke@0 2373 case Bytecodes::_iushr : shift_op(intType , code); break;
duke@0 2374 case Bytecodes::_lushr : shift_op(longType, code); break;
duke@0 2375 case Bytecodes::_iand : logic_op(intType , code); break;
duke@0 2376 case Bytecodes::_land : logic_op(longType, code); break;
duke@0 2377 case Bytecodes::_ior : logic_op(intType , code); break;
duke@0 2378 case Bytecodes::_lor : logic_op(longType, code); break;
duke@0 2379 case Bytecodes::_ixor : logic_op(intType , code); break;
duke@0 2380 case Bytecodes::_lxor : logic_op(longType, code); break;
duke@0 2381 case Bytecodes::_iinc : increment(); break;
duke@0 2382 case Bytecodes::_i2l : convert(code, T_INT , T_LONG ); break;
duke@0 2383 case Bytecodes::_i2f : convert(code, T_INT , T_FLOAT ); break;
duke@0 2384 case Bytecodes::_i2d : convert(code, T_INT , T_DOUBLE); break;
duke@0 2385 case Bytecodes::_l2i : convert(code, T_LONG , T_INT ); break;
duke@0 2386 case Bytecodes::_l2f : convert(code, T_LONG , T_FLOAT ); break;
duke@0 2387 case Bytecodes::_l2d : convert(code, T_LONG , T_DOUBLE); break;
duke@0 2388 case Bytecodes::_f2i : convert(code, T_FLOAT , T_INT ); break;
duke@0 2389 case Bytecodes::_f2l : convert(code, T_FLOAT , T_LONG ); break;
duke@0 2390 case Bytecodes::_f2d : convert(code, T_FLOAT , T_DOUBLE); break;
duke@0 2391 case Bytecodes::_d2i : convert(code, T_DOUBLE, T_INT ); break;
duke@0 2392 case Bytecodes::_d2l : convert(code, T_DOUBLE, T_LONG ); break;
duke@0 2393 case Bytecodes::_d2f : convert(code, T_DOUBLE, T_FLOAT ); break;
duke@0 2394 case Bytecodes::_i2b : convert(code, T_INT , T_BYTE ); break;
duke@0 2395 case Bytecodes::_i2c : convert(code, T_INT , T_CHAR ); break;
duke@0 2396 case Bytecodes::_i2s : convert(code, T_INT , T_SHORT ); break;
duke@0 2397 case Bytecodes::_lcmp : compare_op(longType , code); break;
duke@0 2398 case Bytecodes::_fcmpl : compare_op(floatType , code); break;
duke@0 2399 case Bytecodes::_fcmpg : compare_op(floatType , code); break;
duke@0 2400 case Bytecodes::_dcmpl : compare_op(doubleType, code); break;
duke@0 2401 case Bytecodes::_dcmpg : compare_op(doubleType, code); break;
duke@0 2402 case Bytecodes::_ifeq : if_zero(intType , If::eql); break;
duke@0 2403 case Bytecodes::_ifne : if_zero(intType , If::neq); break;
duke@0 2404 case Bytecodes::_iflt : if_zero(intType , If::lss); break;
duke@0 2405 case Bytecodes::_ifge : if_zero(intType , If::geq); break;
duke@0 2406 case Bytecodes::_ifgt : if_zero(intType , If::gtr); break;
duke@0 2407 case Bytecodes::_ifle : if_zero(intType , If::leq); break;
duke@0 2408 case Bytecodes::_if_icmpeq : if_same(intType , If::eql); break;
duke@0 2409 case Bytecodes::_if_icmpne : if_same(intType , If::neq); break;
duke@0 2410 case Bytecodes::_if_icmplt : if_same(intType , If::lss); break;
duke@0 2411 case Bytecodes::_if_icmpge : if_same(intType , If::geq); break;
duke@0 2412 case Bytecodes::_if_icmpgt : if_same(intType , If::gtr); break;
duke@0 2413 case Bytecodes::_if_icmple : if_same(intType , If::leq); break;
duke@0 2414 case Bytecodes::_if_acmpeq : if_same(objectType, If::eql); break;
duke@0 2415 case Bytecodes::_if_acmpne : if_same(objectType, If::neq); break;
duke@0 2416 case Bytecodes::_goto : _goto(s.cur_bci(), s.get_dest()); break;
duke@0 2417 case Bytecodes::_jsr : jsr(s.get_dest()); break;
duke@0 2418 case Bytecodes::_ret : ret(s.get_index()); break;
duke@0 2419 case Bytecodes::_tableswitch : table_switch(); break;
duke@0 2420 case Bytecodes::_lookupswitch : lookup_switch(); break;
duke@0 2421 case Bytecodes::_ireturn : method_return(ipop()); break;
duke@0 2422 case Bytecodes::_lreturn : method_return(lpop()); break;
duke@0 2423 case Bytecodes::_freturn : method_return(fpop()); break;
duke@0 2424 case Bytecodes::_dreturn : method_return(dpop()); break;
duke@0 2425 case Bytecodes::_areturn : method_return(apop()); break;
duke@0 2426 case Bytecodes::_return : method_return(NULL ); break;
duke@0 2427 case Bytecodes::_getstatic : // fall through
duke@0 2428 case Bytecodes::_putstatic : // fall through
duke@0 2429 case Bytecodes::_getfield : // fall through
duke@0 2430 case Bytecodes::_putfield : access_field(code); break;
duke@0 2431 case Bytecodes::_invokevirtual : // fall through
duke@0 2432 case Bytecodes::_invokespecial : // fall through
duke@0 2433 case Bytecodes::_invokestatic : // fall through
duke@0 2434 case Bytecodes::_invokeinterface: invoke(code); break;
duke@0 2435 case Bytecodes::_xxxunusedxxx : ShouldNotReachHere(); break;
duke@0 2436 case Bytecodes::_new : new_instance(s.get_index_big()); break;
duke@0 2437 case Bytecodes::_newarray : new_type_array(); break;
duke@0 2438 case Bytecodes::_anewarray : new_object_array(); break;
duke@0 2439 case Bytecodes::_arraylength : ipush(append(new ArrayLength(apop(), lock_stack()))); break;
duke@0 2440 case Bytecodes::_athrow : throw_op(s.cur_bci()); break;
duke@0 2441 case Bytecodes::_checkcast : check_cast(s.get_index_big()); break;
duke@0 2442 case Bytecodes::_instanceof : instance_of(s.get_index_big()); break;
duke@0 2443 // Note: we do not have special handling for the monitorenter bytecode if DeoptC1 && DeoptOnAsyncException
duke@0 2444 case Bytecodes::_monitorenter : monitorenter(apop(), s.cur_bci()); break;
duke@0 2445 case Bytecodes::_monitorexit : monitorexit (apop(), s.cur_bci()); break;
duke@0 2446 case Bytecodes::_wide : ShouldNotReachHere(); break;
duke@0 2447 case Bytecodes::_multianewarray : new_multi_array(s.cur_bcp()[3]); break;
duke@0 2448 case Bytecodes::_ifnull : if_null(objectType, If::eql); break;
duke@0 2449 case Bytecodes::_ifnonnull : if_null(objectType, If::neq); break;
duke@0 2450 case Bytecodes::_goto_w : _goto(s.cur_bci(), s.get_far_dest()); break;
duke@0 2451 case Bytecodes::_jsr_w : jsr(s.get_far_dest()); break;
duke@0 2452 case Bytecodes::_breakpoint : BAILOUT_("concurrent setting of breakpoint", NULL);
duke@0 2453 default : ShouldNotReachHere(); break;
duke@0 2454 }
duke@0 2455 // save current bci to setup Goto at the end
duke@0 2456 prev_bci = s.cur_bci();
duke@0 2457 }
duke@0 2458 CHECK_BAILOUT_(NULL);
duke@0 2459 // stop processing of this block (see try_inline_full)
duke@0 2460 if (_skip_block) {
duke@0 2461 _skip_block = false;
duke@0 2462 assert(_last && _last->as_BlockEnd(), "");
duke@0 2463 return _last->as_BlockEnd();
duke@0 2464 }
duke@0 2465 // if there are any, check if last instruction is a BlockEnd instruction
duke@0 2466 BlockEnd* end = last()->as_BlockEnd();
duke@0 2467 if (end == NULL) {
duke@0 2468 // all blocks must end with a BlockEnd instruction => add a Goto
duke@0 2469 end = new Goto(block_at(s.cur_bci()), false);
duke@0 2470 _last = _last->set_next(end, prev_bci);
duke@0 2471 }
duke@0 2472 assert(end == last()->as_BlockEnd(), "inconsistency");
duke@0 2473
duke@0 2474 // if the method terminates, we don't need the stack anymore
duke@0 2475 if (end->as_Return() != NULL) {
duke@0 2476 state()->clear_stack();
duke@0 2477 } else if (end->as_Throw() != NULL) {
duke@0 2478 // May have exception handler in caller scopes
duke@0 2479 state()->truncate_stack(scope()->lock_stack_size());
duke@0 2480 }
duke@0 2481
duke@0 2482 // connect to begin & set state
duke@0 2483 // NOTE that inlining may have changed the block we are parsing
duke@0 2484 block()->set_end(end);
duke@0 2485 end->set_state(state());
duke@0 2486 // propagate state
duke@0 2487 for (int i = end->number_of_sux() - 1; i >= 0; i--) {
duke@0 2488 BlockBegin* sux = end->sux_at(i);
duke@0 2489 assert(sux->is_predecessor(block()), "predecessor missing");
duke@0 2490 // be careful, bailout if bytecodes are strange
duke@0 2491 if (!sux->try_merge(state())) BAILOUT_("block join failed", NULL);
duke@0 2492 scope_data()->add_to_work_list(end->sux_at(i));
duke@0 2493 }
duke@0 2494
duke@0 2495 scope_data()->set_stream(NULL);
duke@0 2496
duke@0 2497 // done
duke@0 2498 return end;
duke@0 2499 }
duke@0 2500
duke@0 2501
duke@0 2502 void GraphBuilder::iterate_all_blocks(bool start_in_current_block_for_inlining) {
duke@0 2503 do {
duke@0 2504 if (start_in_current_block_for_inlining && !bailed_out()) {
duke@0 2505 iterate_bytecodes_for_block(0);
duke@0 2506 start_in_current_block_for_inlining = false;
duke@0 2507 } else {
duke@0 2508 BlockBegin* b;
duke@0 2509 while ((b = scope_data()->remove_from_work_list()) != NULL) {
duke@0 2510 if (!b->is_set(BlockBegin::was_visited_flag)) {
duke@0 2511 if (b->is_set(BlockBegin::osr_entry_flag)) {
duke@0 2512 // we're about to parse the osr entry block, so make sure
duke@0 2513 // we setup the OSR edge leading into this block so that
duke@0 2514 // Phis get setup correctly.
duke@0 2515 setup_osr_entry_block();
duke@0 2516 // this is no longer the osr entry block, so clear it.
duke@0 2517 b->clear(BlockBegin::osr_entry_flag);
duke@0 2518 }
duke@0 2519 b->set(BlockBegin::was_visited_flag);
duke@0 2520 connect_to_end(b);
duke@0 2521 }
duke@0 2522 }
duke@0 2523 }
duke@0 2524 } while (!bailed_out() && !scope_data()->is_work_list_empty());
duke@0 2525 }
duke@0 2526
duke@0 2527
duke@0 2528 bool GraphBuilder::_is_initialized = false;
duke@0 2529 bool GraphBuilder::_can_trap [Bytecodes::number_of_java_codes];
duke@0 2530 bool GraphBuilder::_is_async[Bytecodes::number_of_java_codes];
duke@0 2531
duke@0 2532 void GraphBuilder::initialize() {
duke@0 2533 // make sure initialization happens only once (need a
duke@0 2534 // lock here, if we allow the compiler to be re-entrant)
duke@0 2535 if (is_initialized()) return;
duke@0 2536 _is_initialized = true;
duke@0 2537
duke@0 2538 // the following bytecodes are assumed to potentially
duke@0 2539 // throw exceptions in compiled code - note that e.g.
duke@0 2540 // monitorexit & the return bytecodes do not throw
duke@0 2541 // exceptions since monitor pairing proved that they
duke@0 2542 // succeed (if monitor pairing succeeded)
duke@0 2543 Bytecodes::Code can_trap_list[] =
duke@0 2544 { Bytecodes::_ldc
duke@0 2545 , Bytecodes::_ldc_w
duke@0 2546 , Bytecodes::_ldc2_w
duke@0 2547 , Bytecodes::_iaload
duke@0 2548 , Bytecodes::_laload
duke@0 2549 , Bytecodes::_faload
duke@0 2550 , Bytecodes::_daload
duke@0 2551 , Bytecodes::_aaload
duke@0 2552 , Bytecodes::_baload
duke@0 2553 , Bytecodes::_caload
duke@0 2554 , Bytecodes::_saload
duke@0 2555 , Bytecodes::_iastore
duke@0 2556 , Bytecodes::_lastore
duke@0 2557 , Bytecodes::_fastore
duke@0 2558 , Bytecodes::_dastore
duke@0 2559 , Bytecodes::_aastore
duke@0 2560 , Bytecodes::_bastore
duke@0 2561 , Bytecodes::_castore
duke@0 2562 , Bytecodes::_sastore
duke@0 2563 , Bytecodes::_idiv
duke@0 2564 , Bytecodes::_ldiv
duke@0 2565 , Bytecodes::_irem
duke@0 2566 , Bytecodes::_lrem
duke@0 2567 , Bytecodes::_getstatic
duke@0 2568 , Bytecodes::_putstatic
duke@0 2569 , Bytecodes::_getfield
duke@0 2570 , Bytecodes::_putfield
duke@0 2571 , Bytecodes::_invokevirtual
duke@0 2572 , Bytecodes::_invokespecial
duke@0 2573 , Bytecodes::_invokestatic
duke@0 2574 , Bytecodes::_invokeinterface
duke@0 2575 , Bytecodes::_new
duke@0 2576 , Bytecodes::_newarray
duke@0 2577 , Bytecodes::_anewarray
duke@0 2578 , Bytecodes::_arraylength
duke@0 2579 , Bytecodes::_athrow
duke@0 2580 , Bytecodes::_checkcast
duke@0 2581 , Bytecodes::_instanceof
duke@0 2582 , Bytecodes::_monitorenter
duke@0 2583 , Bytecodes::_multianewarray
duke@0 2584 };
duke@0 2585
duke@0 2586 // the following bytecodes are assumed to potentially
duke@0 2587 // throw asynchronous exceptions in compiled code due
duke@0 2588 // to safepoints (note: these entries could be merged
duke@0 2589 // with the can_trap_list - however, we need to know
duke@0 2590 // which ones are asynchronous for now - see also the
duke@0 2591 // comment in GraphBuilder::handle_exception)
duke@0 2592 Bytecodes::Code is_async_list[] =
duke@0 2593 { Bytecodes::_ifeq
duke@0 2594 , Bytecodes::_ifne
duke@0 2595 , Bytecodes::_iflt
duke@0 2596 , Bytecodes::_ifge
duke@0 2597 , Bytecodes::_ifgt
duke@0 2598 , Bytecodes::_ifle
duke@0 2599 , Bytecodes::_if_icmpeq
duke@0 2600 , Bytecodes::_if_icmpne
duke@0 2601 , Bytecodes::_if_icmplt
duke@0 2602 , Bytecodes::_if_icmpge
duke@0 2603 , Bytecodes::_if_icmpgt
duke@0 2604 , Bytecodes::_if_icmple
duke@0 2605 , Bytecodes::_if_acmpeq
duke@0 2606 , Bytecodes::_if_acmpne
duke@0 2607 , Bytecodes::_goto
duke@0 2608 , Bytecodes::_jsr
duke@0 2609 , Bytecodes::_ret
duke@0 2610 , Bytecodes::_tableswitch
duke@0 2611 , Bytecodes::_lookupswitch
duke@0 2612 , Bytecodes::_ireturn
duke@0 2613 , Bytecodes::_lreturn
duke@0 2614 , Bytecodes::_freturn
duke@0 2615 , Bytecodes::_dreturn
duke@0 2616 , Bytecodes::_areturn
duke@0 2617 , Bytecodes::_return
duke@0 2618 , Bytecodes::_ifnull
duke@0 2619 , Bytecodes::_ifnonnull
duke@0 2620 , Bytecodes::_goto_w
duke@0 2621 , Bytecodes::_jsr_w
duke@0 2622 };
duke@0 2623
duke@0 2624 // inititialize trap tables
duke@0 2625 for (int i = 0; i < Bytecodes::number_of_java_codes; i++) {
duke@0 2626 _can_trap[i] = false;
duke@0 2627 _is_async[i] = false;
duke@0 2628 }
duke@0 2629 // set standard trap info
duke@0 2630 for (uint j = 0; j < ARRAY_SIZE(can_trap_list); j++) {
duke@0 2631 _can_trap[can_trap_list[j]] = true;
duke@0 2632 }
duke@0 2633
duke@0 2634 // We now deoptimize if an asynchronous exception is thrown. This
duke@0 2635 // considerably cleans up corner case issues related to javac's
duke@0 2636 // incorrect exception handler ranges for async exceptions and
duke@0 2637 // allows us to precisely analyze the types of exceptions from
duke@0 2638 // certain bytecodes.
duke@0 2639 if (!(DeoptC1 && DeoptOnAsyncException)) {
duke@0 2640 // set asynchronous trap info
duke@0 2641 for (uint k = 0; k < ARRAY_SIZE(is_async_list); k++) {
duke@0 2642 assert(!_can_trap[is_async_list[k]], "can_trap_list and is_async_list should be disjoint");
duke@0 2643 _can_trap[is_async_list[k]] = true;
duke@0 2644 _is_async[is_async_list[k]] = true;
duke@0 2645 }
duke@0 2646 }
duke@0 2647 }
duke@0 2648
duke@0 2649
duke@0 2650 BlockBegin* GraphBuilder::header_block(BlockBegin* entry, BlockBegin::Flag f, ValueStack* state) {
duke@0 2651 assert(entry->is_set(f), "entry/flag mismatch");
duke@0 2652 // create header block
duke@0 2653 BlockBegin* h = new BlockBegin(entry->bci());
duke@0 2654 h->set_depth_first_number(0);
duke@0 2655
duke@0 2656 Value l = h;
duke@0 2657 if (profile_branches()) {
duke@0 2658 // Increment the invocation count on entry to the method. We
duke@0 2659 // can't use profile_invocation here because append isn't setup to
duke@0 2660 // work properly at this point. The instruction have to be
duke@0 2661 // appended to the instruction stream by hand.
duke@0 2662 Value m = new Constant(new ObjectConstant(compilation()->method()));
duke@0 2663 h->set_next(m, 0);
duke@0 2664 Value p = new ProfileCounter(m, methodOopDesc::interpreter_invocation_counter_offset_in_bytes(), 1);
duke@0 2665 m->set_next(p, 0);
duke@0 2666 l = p;
duke@0 2667 }
duke@0 2668
duke@0 2669 BlockEnd* g = new Goto(entry, false);
duke@0 2670 l->set_next(g, entry->bci());
duke@0 2671 h->set_end(g);
duke@0 2672 h->set(f);
duke@0 2673 // setup header block end state
duke@0 2674 ValueStack* s = state->copy(); // can use copy since stack is empty (=> no phis)
duke@0 2675 assert(s->stack_is_empty(), "must have empty stack at entry point");
duke@0 2676 g->set_state(s);
duke@0 2677 return h;
duke@0 2678 }
duke@0 2679
duke@0 2680
duke@0 2681
duke@0 2682 BlockBegin* GraphBuilder::setup_start_block(int osr_bci, BlockBegin* std_entry, BlockBegin* osr_entry, ValueStack* state) {
duke@0 2683 BlockBegin* start = new BlockBegin(0);
duke@0 2684
duke@0 2685 // This code eliminates the empty start block at the beginning of
duke@0 2686 // each method. Previously, each method started with the
duke@0 2687 // start-block created below, and this block was followed by the
duke@0 2688 // header block that was always empty. This header block is only
duke@0 2689 // necesary if std_entry is also a backward branch target because
duke@0 2690 // then phi functions may be necessary in the header block. It's
duke@0 2691 // also necessary when profiling so that there's a single block that
duke@0 2692 // can increment the interpreter_invocation_count.
duke@0 2693 BlockBegin* new_header_block;
duke@0 2694 if (std_entry->number_of_preds() == 0 && !profile_branches()) {
duke@0 2695 new_header_block = std_entry;
duke@0 2696 } else {
duke@0 2697 new_header_block = header_block(std_entry, BlockBegin::std_entry_flag, state);
duke@0 2698 }
duke@0 2699
duke@0 2700 // setup start block (root for the IR graph)
duke@0 2701 Base* base =
duke@0 2702 new Base(
duke@0 2703 new_header_block,
duke@0 2704 osr_entry
duke@0 2705 );
duke@0 2706 start->set_next(base, 0);
duke@0 2707 start->set_end(base);
duke@0 2708 // create & setup state for start block
duke@0 2709 start->set_state(state->copy());
duke@0 2710 base->set_state(state->copy());
duke@0 2711
duke@0 2712 if (base->std_entry()->state() == NULL) {
duke@0 2713 // setup states for header blocks
duke@0 2714 base->std_entry()->merge(state);
duke@0 2715 }
duke@0 2716
duke@0 2717 assert(base->std_entry()->state() != NULL, "");
duke@0 2718 return start;
duke@0 2719 }
duke@0 2720
duke@0 2721
duke@0 2722 void GraphBuilder::setup_osr_entry_block() {
duke@0 2723 assert(compilation()->is_osr_compile(), "only for osrs");
duke@0 2724
duke@0 2725 int osr_bci = compilation()->osr_bci();
duke@0 2726 ciBytecodeStream s(method());
duke@0 2727 s.reset_to_bci(osr_bci);
duke@0 2728 s.next();
duke@0 2729 scope_data()->set_stream(&s);
duke@0 2730
duke@0 2731 // create a new block to be the osr setup code
duke@0 2732 _osr_entry = new BlockBegin(osr_bci);
duke@0 2733 _osr_entry->set(BlockBegin::osr_entry_flag);
duke@0 2734 _osr_entry->set_depth_first_number(0);
duke@0 2735 BlockBegin* target = bci2block()->at(osr_bci);
duke@0 2736 assert(target != NULL && target->is_set(BlockBegin::osr_entry_flag), "must be there");
duke@0 2737 // the osr entry has no values for locals
duke@0 2738 ValueStack* state = target->state()->copy();
duke@0 2739 _osr_entry->set_state(state);
duke@0 2740
duke@0 2741 kill_all();
duke@0 2742 _block = _osr_entry;
duke@0 2743 _state = _osr_entry->state()->copy();
duke@0 2744 _last = _osr_entry;
duke@0 2745 Value e = append(new OsrEntry());
duke@0 2746 e->set_needs_null_check(false);
duke@0 2747
duke@0 2748 // OSR buffer is
duke@0 2749 //
duke@0 2750 // locals[nlocals-1..0]
duke@0 2751 // monitors[number_of_locks-1..0]
duke@0 2752 //
duke@0 2753 // locals is a direct copy of the interpreter frame so in the osr buffer
duke@0 2754 // so first slot in the local array is the last local from the interpreter
duke@0 2755 // and last slot is local[0] (receiver) from the interpreter
duke@0 2756 //
duke@0 2757 // Similarly with locks. The first lock slot in the osr buffer is the nth lock
duke@0 2758 // from the interpreter frame, the nth lock slot in the osr buffer is 0th lock
duke@0 2759 // in the interpreter frame (the method lock if a sync method)
duke@0 2760
duke@0 2761 // Initialize monitors in the compiled activation.
duke@0 2762
duke@0 2763 int index;
duke@0 2764 Value local;
duke@0 2765
duke@0 2766 // find all the locals that the interpreter thinks contain live oops
duke@0 2767 const BitMap live_oops = method()->live_local_oops_at_bci(osr_bci);
duke@0 2768
duke@0 2769 // compute the offset into the locals so that we can treat the buffer
duke@0 2770 // as if the locals were still in the interpreter frame
duke@0 2771 int locals_offset = BytesPerWord * (method()->max_locals() - 1);
duke@0 2772 for_each_local_value(state, index, local) {
duke@0 2773 int offset = locals_offset - (index + local->type()->size() - 1) * BytesPerWord;
duke@0 2774 Value get;
duke@0 2775 if (local->type()->is_object_kind() && !live_oops.at(index)) {
duke@0 2776 // The interpreter thinks this local is dead but the compiler
duke@0 2777 // doesn't so pretend that the interpreter passed in null.
duke@0 2778 get = append(new Constant(objectNull));
duke@0 2779 } else {
duke@0 2780 get = append(new UnsafeGetRaw(as_BasicType(local->type()), e,
duke@0 2781 append(new Constant(new IntConstant(offset))),
duke@0 2782 0,
duke@0 2783 true));
duke@0 2784 }
duke@0 2785 _state->store_local(index, get);
duke@0 2786 }
duke@0 2787
duke@0 2788 // the storage for the OSR buffer is freed manually in the LIRGenerator.
duke@0 2789
duke@0 2790 assert(state->caller_state() == NULL, "should be top scope");
duke@0 2791 state->clear_locals();
duke@0 2792 Goto* g = new Goto(target, false);
duke@0 2793 g->set_state(_state->copy());
duke@0 2794 append(g);
duke@0 2795 _osr_entry->set_end(g);
duke@0 2796 target->merge(_osr_entry->end()->state());
duke@0 2797
duke@0 2798 scope_data()->set_stream(NULL);
duke@0 2799 }
duke@0 2800
duke@0 2801
duke@0 2802 ValueStack* GraphBuilder::state_at_entry() {
duke@0 2803 ValueStack* state = new ValueStack(scope(), method()->max_locals(), method()->max_stack());
duke@0 2804
duke@0 2805 // Set up locals for receiver
duke@0 2806 int idx = 0;
duke@0 2807 if (!method()->is_static()) {
duke@0 2808 // we should always see the receiver
duke@0 2809 state->store_local(idx, new Local(objectType, idx));
duke@0 2810 idx = 1;
duke@0 2811 }
duke@0 2812
duke@0 2813 // Set up locals for incoming arguments
duke@0 2814 ciSignature* sig = method()->signature();
duke@0 2815 for (int i = 0; i < sig->count(); i++) {
duke@0 2816 ciType* type = sig->type_at(i);
duke@0 2817 BasicType basic_type = type->basic_type();
duke@0 2818 // don't allow T_ARRAY to propagate into locals types
duke@0 2819 if (basic_type == T_ARRAY) basic_type = T_OBJECT;
duke@0 2820 ValueType* vt = as_ValueType(basic_type);
duke@0 2821 state->store_local(idx, new Local(vt, idx));
duke@0 2822 idx += type->size();
duke@0 2823 }
duke@0 2824
duke@0 2825 // lock synchronized method
duke@0 2826 if (method()->is_synchronized()) {
duke@0 2827 state->lock(scope(), NULL);
duke@0 2828 }
duke@0 2829
duke@0 2830 return state;
duke@0 2831 }
duke@0 2832
duke@0 2833
duke@0 2834 GraphBuilder::GraphBuilder(Compilation* compilation, IRScope* scope)
duke@0 2835 : _scope_data(NULL)
duke@0 2836 , _exception_state(NULL)
duke@0 2837 , _instruction_count(0)
duke@0 2838 , _osr_entry(NULL)
duke@0 2839 , _memory(new MemoryBuffer())
duke@0 2840 , _compilation(compilation)
duke@0 2841 , _inline_bailout_msg(NULL)
duke@0 2842 {
duke@0 2843 int osr_bci = compilation->osr_bci();
duke@0 2844
duke@0 2845 // determine entry points and bci2block mapping
duke@0 2846 BlockListBuilder blm(compilation, scope, osr_bci);
duke@0 2847 CHECK_BAILOUT();
duke@0 2848
duke@0 2849 BlockList* bci2block = blm.bci2block();
duke@0 2850 BlockBegin* start_block = bci2block->at(0);
duke@0 2851
duke@0 2852 assert(is_initialized(), "GraphBuilder must have been initialized");
duke@0 2853 push_root_scope(scope, bci2block, start_block);
duke@0 2854
duke@0 2855 // setup state for std entry
duke@0 2856 _initial_state = state_at_entry();
duke@0 2857 start_block->merge(_initial_state);
duke@0 2858
duke@0 2859 BlockBegin* sync_handler = NULL;
duke@0 2860 if (method()->is_synchronized() || DTraceMethodProbes) {
duke@0 2861 // setup an exception handler to do the unlocking and/or notification
duke@0 2862 sync_handler = new BlockBegin(-1);
duke@0 2863 sync_handler->set(BlockBegin::exception_entry_flag);
duke@0 2864 sync_handler->set(BlockBegin::is_on_work_list_flag);
duke@0 2865 sync_handler->set(BlockBegin::default_exception_handler_flag);
duke@0 2866
duke@0 2867 ciExceptionHandler* desc = new ciExceptionHandler(method()->holder(), 0, method()->code_size(), -1, 0);
duke@0 2868 XHandler* h = new XHandler(desc);
duke@0 2869 h->set_entry_block(sync_handler);
duke@0 2870 scope_data()->xhandlers()->append(h);
duke@0 2871 scope_data()->set_has_handler();
duke@0 2872 }
duke@0 2873
duke@0 2874 // complete graph
duke@0 2875 _vmap = new ValueMap();
duke@0 2876 scope->compute_lock_stack_size();
duke@0 2877 switch (scope->method()->intrinsic_id()) {
duke@0 2878 case vmIntrinsics::_dabs : // fall through
duke@0 2879 case vmIntrinsics::_dsqrt : // fall through
duke@0 2880 case vmIntrinsics::_dsin : // fall through
duke@0 2881 case vmIntrinsics::_dcos : // fall through
duke@0 2882 case vmIntrinsics::_dtan : // fall through
duke@0 2883 case vmIntrinsics::_dlog : // fall through
duke@0 2884 case vmIntrinsics::_dlog10 : // fall through
duke@0 2885 {
duke@0 2886 // Compiles where the root method is an intrinsic need a special
duke@0 2887 // compilation environment because the bytecodes for the method
duke@0 2888 // shouldn't be parsed during the compilation, only the special
duke@0 2889 // Intrinsic node should be emitted. If this isn't done the the
duke@0 2890 // code for the inlined version will be different than the root
duke@0 2891 // compiled version which could lead to monotonicity problems on
duke@0 2892 // intel.
duke@0 2893
duke@0 2894 // Set up a stream so that appending instructions works properly.
duke@0 2895 ciBytecodeStream s(scope->method());
duke@0 2896 s.reset_to_bci(0);
duke@0 2897 scope_data()->set_stream(&s);
duke@0 2898 s.next();
duke@0 2899
duke@0 2900 // setup the initial block state
duke@0 2901 _block = start_block;
duke@0 2902 _state = start_block->state()->copy();
duke@0 2903 _last = start_block;
duke@0 2904 load_local(doubleType, 0);
duke@0 2905
duke@0 2906 // Emit the intrinsic node.
duke@0 2907 bool result = try_inline_intrinsics(scope->method());
duke@0 2908 if (!result) BAILOUT("failed to inline intrinsic");
duke@0 2909 method_return(dpop());
duke@0 2910
duke@0 2911 // connect the begin and end blocks and we're all done.
duke@0 2912 BlockEnd* end = last()->as_BlockEnd();
duke@0 2913 block()->set_end(end);
duke@0 2914 end->set_state(state());
duke@0 2915 break;
duke@0 2916 }
duke@0 2917 default:
duke@0 2918 scope_data()->add_to_work_list(start_block);
duke@0 2919 iterate_all_blocks();
duke@0 2920 break;
duke@0 2921 }
duke@0 2922 CHECK_BAILOUT();
duke@0 2923
duke@0 2924 if (sync_handler && sync_handler->state() != NULL) {
duke@0 2925 Value lock = NULL;
duke@0 2926 if (method()->is_synchronized()) {
duke@0 2927 lock = method()->is_static() ? new Constant(new InstanceConstant(method()->holder()->java_mirror())) :
duke@0 2928 _initial_state->local_at(0);
duke@0 2929
duke@0 2930 sync_handler->state()->unlock();
duke@0 2931 sync_handler->state()->lock(scope, lock);
duke@0 2932
duke@0 2933 }
duke@0 2934 fill_sync_handler(lock, sync_handler, true);
duke@0 2935 }
duke@0 2936
duke@0 2937 _start = setup_start_block(osr_bci, start_block, _osr_entry, _initial_state);
duke@0 2938
duke@0 2939 eliminate_redundant_phis(_start);
duke@0 2940
duke@0 2941 NOT_PRODUCT(if (PrintValueNumbering && Verbose) print_stats());
duke@0 2942 // for osr compile, bailout if some requirements are not fulfilled
duke@0 2943 if (osr_bci != -1) {
duke@0 2944 BlockBegin* osr_block = blm.bci2block()->at(osr_bci);
duke@0 2945 assert(osr_block->is_set(BlockBegin::was_visited_flag),"osr entry must have been visited for osr compile");
duke@0 2946
duke@0 2947 // check if osr entry point has empty stack - we cannot handle non-empty stacks at osr entry points
duke@0 2948 if (!osr_block->state()->stack_is_empty()) {
duke@0 2949 BAILOUT("stack not empty at OSR entry point");
duke@0 2950 }
duke@0 2951 }
duke@0 2952 #ifndef PRODUCT
duke@0 2953 if (PrintCompilation && Verbose) tty->print_cr("Created %d Instructions", _instruction_count);
duke@0 2954 #endif
duke@0 2955 }
duke@0 2956
duke@0 2957
duke@0 2958 ValueStack* GraphBuilder::lock_stack() {
duke@0 2959 // return a new ValueStack representing just the current lock stack
duke@0 2960 // (for debug info at safepoints in exception throwing or handling)
duke@0 2961 ValueStack* new_stack = state()->copy_locks();
duke@0 2962 return new_stack;
duke@0 2963 }
duke@0 2964
duke@0 2965
duke@0 2966 int GraphBuilder::recursive_inline_level(ciMethod* cur_callee) const {
duke@0 2967 int recur_level = 0;
duke@0 2968 for (IRScope* s = scope(); s != NULL; s = s->caller()) {
duke@0 2969 if (s->method() == cur_callee) {
duke@0 2970 ++recur_level;
duke@0 2971 }
duke@0 2972 }
duke@0 2973 return recur_level;
duke@0 2974 }
duke@0 2975
duke@0 2976
duke@0 2977 bool GraphBuilder::try_inline(ciMethod* callee, bool holder_known) {
duke@0 2978 // Clear out any existing inline bailout condition
duke@0 2979 clear_inline_bailout();
duke@0 2980
duke@0 2981 if (callee->should_exclude()) {
duke@0 2982 // callee is excluded
duke@0 2983 INLINE_BAILOUT("excluded by CompilerOracle")
duke@0 2984 } else if (!callee->can_be_compiled()) {
duke@0 2985 // callee is not compilable (prob. has breakpoints)
duke@0 2986 INLINE_BAILOUT("not compilable")
duke@0 2987 } else if (callee->intrinsic_id() != vmIntrinsics::_none && try_inline_intrinsics(callee)) {
duke@0 2988 // intrinsics can be native or not
duke@0 2989 return true;
duke@0 2990 } else if (callee->is_native()) {
duke@0 2991 // non-intrinsic natives cannot be inlined
duke@0 2992 INLINE_BAILOUT("non-intrinsic native")
duke@0 2993 } else if (callee->is_abstract()) {
duke@0 2994 INLINE_BAILOUT("abstract")
duke@0 2995 } else {
duke@0 2996 return try_inline_full(callee, holder_known);
duke@0 2997 }
duke@0 2998 }
duke@0 2999
duke@0 3000
duke@0 3001 bool GraphBuilder::try_inline_intrinsics(ciMethod* callee) {
duke@0 3002 if (!InlineNatives ) INLINE_BAILOUT("intrinsic method inlining disabled");
duke@0 3003 if (callee->is_synchronized()) INLINE_BAILOUT("intrinsic method is synchronized");
duke@0 3004 // callee seems like a good candidate
duke@0 3005 // determine id
duke@0 3006 bool preserves_state = false;
duke@0 3007 bool cantrap = true;
duke@0 3008 vmIntrinsics::ID id = callee->intrinsic_id();
duke@0 3009 switch (id) {
duke@0 3010 case vmIntrinsics::_arraycopy :
duke@0 3011 if (!InlineArrayCopy) return false;
duke@0 3012 break;
duke@0 3013
duke@0 3014 case vmIntrinsics::_currentTimeMillis:
duke@0 3015 case vmIntrinsics::_nanoTime:
duke@0 3016 preserves_state = true;
duke@0 3017 cantrap = false;
duke@0 3018 break;
duke@0 3019
duke@0 3020 case vmIntrinsics::_floatToRawIntBits :
duke@0 3021 case vmIntrinsics::_intBitsToFloat :
duke@0 3022 case vmIntrinsics::_doubleToRawLongBits :
duke@0 3023 case vmIntrinsics::_longBitsToDouble :
duke@0 3024 if (!InlineMathNatives) return false;
duke@0 3025 preserves_state = true;
duke@0 3026 cantrap = false;
duke@0 3027 break;
duke@0 3028
duke@0 3029 case vmIntrinsics::_getClass :
duke@0 3030 if (!InlineClassNatives) return false;
duke@0 3031 preserves_state = true;
duke@0 3032 break;
duke@0 3033
duke@0 3034 case vmIntrinsics::_currentThread :
duke@0 3035 if (!InlineThreadNatives) return false;
duke@0 3036 preserves_state = true;
duke@0 3037 cantrap = false;
duke@0 3038 break;
duke@0 3039
duke@0 3040 case vmIntrinsics::_dabs : // fall through
duke@0 3041 case vmIntrinsics::_dsqrt : // fall through
duke@0 3042 case vmIntrinsics::_dsin : // fall through
duke@0 3043 case vmIntrinsics::_dcos : // fall through
duke@0 3044 case vmIntrinsics::_dtan : // fall through
duke@0 3045 case vmIntrinsics::_dlog : // fall through
duke@0 3046 case vmIntrinsics::_dlog10 : // fall through
duke@0 3047 if (!InlineMathNatives) return false;
duke@0 3048 cantrap = false;
duke@0 3049 preserves_state = true;
duke@0 3050 break;
duke@0 3051
duke@0 3052 // sun/misc/AtomicLong.attemptUpdate
duke@0 3053 case vmIntrinsics::_attemptUpdate :
duke@0 3054 if (!VM_Version::supports_cx8()) return false;
duke@0 3055 if (!InlineAtomicLong) return false;
duke@0 3056 preserves_state = true;
duke@0 3057 break;
duke@0 3058
duke@0 3059 // Use special nodes for Unsafe instructions so we can more easily
duke@0 3060 // perform an address-mode optimization on the raw variants
duke@0 3061 case vmIntrinsics::_getObject : return append_unsafe_get_obj(callee, T_OBJECT, false);
duke@0 3062 case vmIntrinsics::_getBoolean: return append_unsafe_get_obj(callee, T_BOOLEAN, false);
duke@0 3063 case vmIntrinsics::_getByte : return append_unsafe_get_obj(callee, T_BYTE, false);
duke@0 3064 case vmIntrinsics::_getShort : return append_unsafe_get_obj(callee, T_SHORT, false);
duke@0 3065 case vmIntrinsics::_getChar : return append_unsafe_get_obj(callee, T_CHAR, false);
duke@0 3066 case vmIntrinsics::_getInt : return append_unsafe_get_obj(callee, T_INT, false);
duke@0 3067 case vmIntrinsics::_getLong : return append_unsafe_get_obj(callee, T_LONG, false);
duke@0 3068 case vmIntrinsics::_getFloat : return append_unsafe_get_obj(callee, T_FLOAT, false);
duke@0 3069 case vmIntrinsics::_getDouble : return append_unsafe_get_obj(callee, T_DOUBLE, false);
duke@0 3070
duke@0 3071 case vmIntrinsics::_putObject : return append_unsafe_put_obj(callee, T_OBJECT, false);
duke@0 3072 case vmIntrinsics::_putBoolean: return append_unsafe_put_obj(callee, T_BOOLEAN, false);
duke@0 3073 case vmIntrinsics::_putByte : return append_unsafe_put_obj(callee, T_BYTE, false);
duke@0 3074 case vmIntrinsics::_putShort : return append_unsafe_put_obj(callee, T_SHORT, false);
duke@0 3075 case vmIntrinsics::_putChar : return append_unsafe_put_obj(callee, T_CHAR, false);
duke@0 3076 case vmIntrinsics::_putInt : return append_unsafe_put_obj(callee, T_INT, false);
duke@0 3077 case vmIntrinsics::_putLong : return append_unsafe_put_obj(callee, T_LONG, false);
duke@0 3078 case vmIntrinsics::_putFloat : return append_unsafe_put_obj(callee, T_FLOAT, false);
duke@0 3079 case vmIntrinsics::_putDouble : return append_unsafe_put_obj(callee, T_DOUBLE, false);
duke@0 3080
duke@0 3081 case vmIntrinsics::_getObjectVolatile : return append_unsafe_get_obj(callee, T_OBJECT, true);
duke@0 3082 case vmIntrinsics::_getBooleanVolatile: return append_unsafe_get_obj(callee, T_BOOLEAN, true);
duke@0 3083 case vmIntrinsics::_getByteVolatile : return append_unsafe_get_obj(callee, T_BYTE, true);
duke@0 3084 case vmIntrinsics::_getShortVolatile : return append_unsafe_get_obj(callee, T_SHORT, true);
duke@0 3085 case vmIntrinsics::_getCharVolatile : return append_unsafe_get_obj(callee, T_CHAR, true);
duke@0 3086 case vmIntrinsics::_getIntVolatile : return append_unsafe_get_obj(callee, T_INT, true);
duke@0 3087 case vmIntrinsics::_getLongVolatile : return append_unsafe_get_obj(callee, T_LONG, true);
duke@0 3088 case vmIntrinsics::_getFloatVolatile : return append_unsafe_get_obj(callee, T_FLOAT, true);
duke@0 3089 case vmIntrinsics::_getDoubleVolatile : return append_unsafe_get_obj(callee, T_DOUBLE, true);
duke@0 3090
duke@0 3091 case vmIntrinsics::_putObjectVolatile : return append_unsafe_put_obj(callee, T_OBJECT, true);
duke@0 3092 case vmIntrinsics::_putBooleanVolatile: return append_unsafe_put_obj(callee, T_BOOLEAN, true);
duke@0 3093 case vmIntrinsics::_putByteVolatile : return append_unsafe_put_obj(callee, T_BYTE, true);
duke@0 3094 case vmIntrinsics::_putShortVolatile : return append_unsafe_put_obj(callee, T_SHORT, true);
duke@0 3095 case vmIntrinsics::_putCharVolatile : return append_unsafe_put_obj(callee, T_CHAR, true);
duke@0 3096 case vmIntrinsics::_putIntVolatile : return append_unsafe_put_obj(callee, T_INT, true);
duke@0 3097 case vmIntrinsics::_putLongVolatile : return append_unsafe_put_obj(callee, T_LONG, true);
duke@0 3098 case vmIntrinsics::_putFloatVolatile : return append_unsafe_put_obj(callee, T_FLOAT, true);
duke@0 3099 case vmIntrinsics::_putDoubleVolatile : return append_unsafe_put_obj(callee, T_DOUBLE, true);
duke@0 3100
duke@0 3101 case vmIntrinsics::_getByte_raw : return append_unsafe_get_raw(callee, T_BYTE);
duke@0 3102 case vmIntrinsics::_getShort_raw : return append_unsafe_get_raw(callee, T_SHORT);
duke@0 3103 case vmIntrinsics::_getChar_raw : return append_unsafe_get_raw(callee, T_CHAR);
duke@0 3104 case vmIntrinsics::_getInt_raw : return append_unsafe_get_raw(callee, T_INT);
duke@0 3105 case vmIntrinsics::_getLong_raw : return append_unsafe_get_raw(callee, T_LONG);
duke@0 3106 case vmIntrinsics::_getFloat_raw : return append_unsafe_get_raw(callee, T_FLOAT);
duke@0 3107 case vmIntrinsics::_getDouble_raw : return append_unsafe_get_raw(callee, T_DOUBLE);
duke@0 3108
duke@0 3109 case vmIntrinsics::_putByte_raw : return append_unsafe_put_raw(callee, T_BYTE);
duke@0 3110 case vmIntrinsics::_putShort_raw : return append_unsafe_put_raw(callee, T_SHORT);
duke@0 3111 case vmIntrinsics::_putChar_raw : return append_unsafe_put_raw(callee, T_CHAR);
duke@0 3112 case vmIntrinsics::_putInt_raw : return append_unsafe_put_raw(callee, T_INT);
duke@0 3113 case vmIntrinsics::_putLong_raw : return append_unsafe_put_raw(callee, T_LONG);
duke@0 3114 case vmIntrinsics::_putFloat_raw : return append_unsafe_put_raw(callee, T_FLOAT);
duke@0 3115 case vmIntrinsics::_putDouble_raw : return append_unsafe_put_raw(callee, T_DOUBLE);
duke@0 3116
duke@0 3117 case vmIntrinsics::_prefetchRead : return append_unsafe_prefetch(callee, false, false);
duke@0 3118 case vmIntrinsics::_prefetchWrite : return append_unsafe_prefetch(callee, false, true);
duke@0 3119 case vmIntrinsics::_prefetchReadStatic : return append_unsafe_prefetch(callee, true, false);
duke@0 3120 case vmIntrinsics::_prefetchWriteStatic : return append_unsafe_prefetch(callee, true, true);
duke@0 3121
duke@0 3122 case vmIntrinsics::_checkIndex :
duke@0 3123 if (!InlineNIOCheckIndex) return false;
duke@0 3124 preserves_state = true;
duke@0 3125 break;
duke@0 3126 case vmIntrinsics::_putOrderedObject : return append_unsafe_put_obj(callee, T_OBJECT, true);
duke@0 3127 case vmIntrinsics::_putOrderedInt : return append_unsafe_put_obj(callee, T_INT, true);
duke@0 3128 case vmIntrinsics::_putOrderedLong : return append_unsafe_put_obj(callee, T_LONG, true);
duke@0 3129
duke@0 3130 case vmIntrinsics::_compareAndSwapLong:
duke@0 3131 if (!VM_Version::supports_cx8()) return false;
duke@0 3132 // fall through
duke@0 3133 case vmIntrinsics::_compareAndSwapInt:
duke@0 3134 case vmIntrinsics::_compareAndSwapObject:
duke@0 3135 append_unsafe_CAS(callee);
duke@0 3136 return true;
duke@0 3137
duke@0 3138 default : return false; // do not inline
duke@0 3139 }
duke@0 3140 // create intrinsic node
duke@0 3141 const bool has_receiver = !callee->is_static();
duke@0 3142 ValueType* result_type = as_ValueType(callee->return_type());
duke@0 3143
duke@0 3144 Values* args = state()->pop_arguments(callee->arg_size());
duke@0 3145 ValueStack* locks = lock_stack();
duke@0 3146 if (profile_calls()) {
duke@0 3147 // Don't profile in the special case where the root method
duke@0 3148 // is the intrinsic
duke@0 3149 if (callee != method()) {
duke@0 3150 Value recv = NULL;
duke@0 3151 if (has_receiver) {
duke@0 3152 recv = args->at(0);
duke@0 3153 null_check(recv);
duke@0 3154 }
duke@0 3155 profile_call(recv, NULL);
duke@0 3156 }
duke@0 3157 }
duke@0 3158
duke@0 3159 Intrinsic* result = new Intrinsic(result_type, id, args, has_receiver, lock_stack(),
duke@0 3160 preserves_state, cantrap);
duke@0 3161 // append instruction & push result
duke@0 3162 Value value = append_split(result);
duke@0 3163 if (result_type != voidType) push(result_type, value);
duke@0 3164
duke@0 3165 #ifndef PRODUCT
duke@0 3166 // printing
duke@0 3167 if (PrintInlining) {
duke@0 3168 print_inline_result(callee, true);
duke@0 3169 }
duke@0 3170 #endif
duke@0 3171
duke@0 3172 // done
duke@0 3173 return true;
duke@0 3174 }
duke@0 3175
duke@0 3176
duke@0 3177 bool GraphBuilder::try_inline_jsr(int jsr_dest_bci) {
duke@0 3178 // Introduce a new callee continuation point - all Ret instructions
duke@0 3179 // will be replaced with Gotos to this point.
duke@0 3180 BlockBegin* cont = block_at(next_bci());
duke@0 3181 assert(cont != NULL, "continuation must exist (BlockListBuilder starts a new block after a jsr");
duke@0 3182
duke@0 3183 // Note: can not assign state to continuation yet, as we have to
duke@0 3184 // pick up the state from the Ret instructions.
duke@0 3185
duke@0 3186 // Push callee scope
duke@0 3187 push_scope_for_jsr(cont, jsr_dest_bci);
duke@0 3188
duke@0 3189 // Temporarily set up bytecode stream so we can append instructions
duke@0 3190 // (only using the bci of this stream)
duke@0 3191 scope_data()->set_stream(scope_data()->parent()->stream());
duke@0 3192
duke@0 3193 BlockBegin* jsr_start_block = block_at(jsr_dest_bci);
duke@0 3194 assert(jsr_start_block != NULL, "jsr start block must exist");
duke@0 3195 assert(!jsr_start_block->is_set(BlockBegin::was_visited_flag), "should not have visited jsr yet");
duke@0 3196 Goto* goto_sub = new Goto(jsr_start_block, false);
duke@0 3197 goto_sub->set_state(state());
duke@0 3198 // Must copy state to avoid wrong sharing when parsing bytecodes
duke@0 3199 assert(jsr_start_block->state() == NULL, "should have fresh jsr starting block");
duke@0 3200 jsr_start_block->set_state(state()->copy());
duke@0 3201 append(goto_sub);
duke@0 3202 _block->set_end(goto_sub);
duke@0 3203 _last = _block = jsr_start_block;
duke@0 3204
duke@0 3205 // Clear out bytecode stream
duke@0 3206 scope_data()->set_stream(NULL);
duke@0 3207
duke@0 3208 scope_data()->add_to_work_list(jsr_start_block);
duke@0 3209
duke@0 3210 // Ready to resume parsing in subroutine
duke@0 3211 iterate_all_blocks();
duke@0 3212
duke@0 3213 // If we bailed out during parsing, return immediately (this is bad news)
duke@0 3214 CHECK_BAILOUT_(false);
duke@0 3215
duke@0 3216 // Detect whether the continuation can actually be reached. If not,
duke@0 3217 // it has not had state set by the join() operations in
duke@0 3218 // iterate_bytecodes_for_block()/ret() and we should not touch the
duke@0 3219 // iteration state. The calling activation of
duke@0 3220 // iterate_bytecodes_for_block will then complete normally.
duke@0 3221 if (cont->state() != NULL) {
duke@0 3222 if (!cont->is_set(BlockBegin::was_visited_flag)) {
duke@0 3223 // add continuation to work list instead of parsing it immediately
duke@0 3224 scope_data()->parent()->add_to_work_list(cont);
duke@0 3225 }
duke@0 3226 }
duke@0 3227
duke@0 3228 assert(jsr_continuation() == cont, "continuation must not have changed");
duke@0 3229 assert(!jsr_continuation()->is_set(BlockBegin::was_visited_flag) ||
duke@0 3230 jsr_continuation()->is_set(BlockBegin::parser_loop_header_flag),
duke@0 3231 "continuation can only be visited in case of backward branches");
duke@0 3232 assert(_last && _last->as_BlockEnd(), "block must have end");
duke@0 3233
duke@0 3234 // continuation is in work list, so end iteration of current block
duke@0 3235 _skip_block = true;
duke@0 3236 pop_scope_for_jsr();
duke@0 3237
duke@0 3238 return true;
duke@0 3239 }
duke@0 3240
duke@0 3241
duke@0 3242 // Inline the entry of a synchronized method as a monitor enter and
duke@0 3243 // register the exception handler which releases the monitor if an
duke@0 3244 // exception is thrown within the callee. Note that the monitor enter
duke@0 3245 // cannot throw an exception itself, because the receiver is
duke@0 3246 // guaranteed to be non-null by the explicit null check at the
duke@0 3247 // beginning of inlining.
duke@0 3248 void GraphBuilder::inline_sync_entry(Value lock, BlockBegin* sync_handler) {
duke@0 3249 assert(lock != NULL && sync_handler != NULL, "lock or handler missing");
duke@0 3250
duke@0 3251 set_exception_state(state()->copy());
duke@0 3252 monitorenter(lock, SynchronizationEntryBCI);
duke@0 3253 assert(_last->as_MonitorEnter() != NULL, "monitor enter expected");
duke@0 3254 _last->set_needs_null_check(false);
duke@0 3255
duke@0 3256 sync_handler->set(BlockBegin::exception_entry_flag);
duke@0 3257 sync_handler->set(BlockBegin::is_on_work_list_flag);
duke@0 3258
duke@0 3259 ciExceptionHandler* desc = new ciExceptionHandler(method()->holder(), 0, method()->code_size(), -1, 0);
duke@0 3260 XHandler* h = new XHandler(desc);
duke@0 3261 h->set_entry_block(sync_handler);
duke@0 3262 scope_data()->xhandlers()->append(h);
duke@0 3263 scope_data()->set_has_handler();
duke@0 3264 }
duke@0 3265
duke@0 3266
duke@0 3267 // If an exception is thrown and not handled within an inlined
duke@0 3268 // synchronized method, the monitor must be released before the
duke@0 3269 // exception is rethrown in the outer scope. Generate the appropriate
duke@0 3270 // instructions here.
duke@0 3271 void GraphBuilder::fill_sync_handler(Value lock, BlockBegin* sync_handler, bool default_handler) {
duke@0 3272 BlockBegin* orig_block = _block;
duke@0 3273 ValueStack* orig_state = _state;
duke@0 3274 Instruction* orig_last = _last;
duke@0 3275 _last = _block = sync_handler;
duke@0 3276 _state = sync_handler->state()->copy();
duke@0 3277
duke@0 3278 assert(sync_handler != NULL, "handler missing");
duke@0 3279 assert(!sync_handler->is_set(BlockBegin::was_visited_flag), "is visited here");
duke@0 3280
duke@0 3281 assert(lock != NULL || default_handler, "lock or handler missing");
duke@0 3282
duke@0 3283 XHandler* h = scope_data()->xhandlers()->remove_last();
duke@0 3284 assert(h->entry_block() == sync_handler, "corrupt list of handlers");
duke@0 3285
duke@0 3286 block()->set(BlockBegin::was_visited_flag);
duke@0 3287 Value exception = append_with_bci(new ExceptionObject(), SynchronizationEntryBCI);
duke@0 3288 assert(exception->is_pinned(), "must be");
duke@0 3289
duke@0 3290 int bci = SynchronizationEntryBCI;
duke@0 3291 if (lock) {
duke@0 3292 assert(state()->locks_size() > 0 && state()->lock_at(state()->locks_size() - 1) == lock, "lock is missing");
duke@0 3293 if (lock->bci() == -99) {
duke@0 3294 lock = append_with_bci(lock, -1);
duke@0 3295 }
duke@0 3296
duke@0 3297 // exit the monitor in the context of the synchronized method
duke@0 3298 monitorexit(lock, SynchronizationEntryBCI);
duke@0 3299
duke@0 3300 // exit the context of the synchronized method
duke@0 3301 if (!default_handler) {
duke@0 3302 pop_scope();
duke@0 3303 _state = _state->copy();
duke@0 3304 bci = _state->scope()->caller_bci();
duke@0 3305 _state = _state->pop_scope()->copy();
duke@0 3306 }
duke@0 3307 }
duke@0 3308
duke@0 3309 // perform the throw as if at the the call site
duke@0 3310 apush(exception);
duke@0 3311
duke@0 3312 set_exception_state(state()->copy());
duke@0 3313 throw_op(bci);
duke@0 3314
duke@0 3315 BlockEnd* end = last()->as_BlockEnd();
duke@0 3316 block()->set_end(end);
duke@0 3317 end->set_state(state());
duke@0 3318
duke@0 3319 _block = orig_block;
duke@0 3320 _state = orig_state;
duke@0 3321 _last = orig_last;
duke@0 3322 }
duke@0 3323
duke@0 3324
duke@0 3325 bool GraphBuilder::try_inline_full(ciMethod* callee, bool holder_known) {
duke@0 3326 assert(!callee->is_native(), "callee must not be native");
duke@0 3327
duke@0 3328 // first perform tests of things it's not possible to inline
duke@0 3329 if (callee->has_exception_handlers() &&
duke@0 3330 !InlineMethodsWithExceptionHandlers) INLINE_BAILOUT("callee has exception handlers");
duke@0 3331 if (callee->is_synchronized() &&
duke@0 3332 !InlineSynchronizedMethods ) INLINE_BAILOUT("callee is synchronized");
duke@0 3333 if (!callee->holder()->is_initialized()) INLINE_BAILOUT("callee's klass not initialized yet");
duke@0 3334 if (!callee->has_balanced_monitors()) INLINE_BAILOUT("callee's monitors do not match");
duke@0 3335
duke@0 3336 // Proper inlining of methods with jsrs requires a little more work.
duke@0 3337 if (callee->has_jsrs() ) INLINE_BAILOUT("jsrs not handled properly by inliner yet");
duke@0 3338
duke@0 3339 // now perform tests that are based on flag settings
duke@0 3340 if (inline_level() > MaxInlineLevel ) INLINE_BAILOUT("too-deep inlining");
duke@0 3341 if (recursive_inline_level(callee) > MaxRecursiveInlineLevel) INLINE_BAILOUT("too-deep recursive inlining");
duke@0 3342 if (callee->code_size() > max_inline_size() ) INLINE_BAILOUT("callee is too large");
duke@0 3343
duke@0 3344 // don't inline throwable methods unless the inlining tree is rooted in a throwable class
duke@0 3345 if (callee->name() == ciSymbol::object_initializer_name() &&
duke@0 3346 callee->holder()->is_subclass_of(ciEnv::current()->Throwable_klass())) {
duke@0 3347 // Throwable constructor call
duke@0 3348 IRScope* top = scope();
duke@0 3349 while (top->caller() != NULL) {
duke@0 3350 top = top->caller();
duke@0 3351 }
duke@0 3352 if (!top->method()->holder()->is_subclass_of(ciEnv::current()->Throwable_klass())) {
duke@0 3353 INLINE_BAILOUT("don't inline Throwable constructors");
duke@0 3354 }
duke@0 3355 }
duke@0 3356
duke@0 3357 // When SSE2 is used on intel, then no special handling is needed
duke@0 3358 // for strictfp because the enum-constant is fixed at compile time,
duke@0 3359 // the check for UseSSE2 is needed here
duke@0 3360 if (strict_fp_requires_explicit_rounding && UseSSE < 2 && method()->is_strict() != callee->is_strict()) {
duke@0 3361 INLINE_BAILOUT("caller and callee have different strict fp requirements");
duke@0 3362 }
duke@0 3363
duke@0 3364 if (compilation()->env()->num_inlined_bytecodes() > DesiredMethodLimit) {
duke@0 3365 INLINE_BAILOUT("total inlining greater than DesiredMethodLimit");
duke@0 3366 }
duke@0 3367
duke@0 3368 #ifndef PRODUCT
duke@0 3369 // printing
duke@0 3370 if (PrintInlining) {
duke@0 3371 print_inline_result(callee, true);
duke@0 3372 }
duke@0 3373 #endif
duke@0 3374
duke@0 3375 // NOTE: Bailouts from this point on, which occur at the
duke@0 3376 // GraphBuilder level, do not cause bailout just of the inlining but
duke@0 3377 // in fact of the entire compilation.
duke@0 3378
duke@0 3379 BlockBegin* orig_block = block();
duke@0 3380
duke@0 3381 const int args_base = state()->stack_size() - callee->arg_size();
duke@0 3382 assert(args_base >= 0, "stack underflow during inlining");
duke@0 3383
duke@0 3384 // Insert null check if necessary
duke@0 3385 Value recv = NULL;
duke@0 3386 if (code() != Bytecodes::_invokestatic) {
duke@0 3387 // note: null check must happen even if first instruction of callee does
duke@0 3388 // an implicit null check since the callee is in a different scope
duke@0 3389 // and we must make sure exception handling does the right thing
duke@0 3390 assert(!callee->is_static(), "callee must not be static");
duke@0 3391 assert(callee->arg_size() > 0, "must have at least a receiver");
duke@0 3392 recv = state()->stack_at(args_base);
duke@0 3393 null_check(recv);
duke@0 3394 }
duke@0 3395
duke@0 3396 if (profile_inlined_calls()) {
duke@0 3397 profile_call(recv, holder_known ? callee->holder() : NULL);
duke@0 3398 }
duke@0 3399
duke@0 3400 profile_invocation(callee);
duke@0 3401
duke@0 3402 // Introduce a new callee continuation point - if the callee has
duke@0 3403 // more than one return instruction or the return does not allow
duke@0 3404 // fall-through of control flow, all return instructions of the
duke@0 3405 // callee will need to be replaced by Goto's pointing to this
duke@0 3406 // continuation point.
duke@0 3407 BlockBegin* cont = block_at(next_bci());
duke@0 3408 bool continuation_existed = true;
duke@0 3409 if (cont == NULL) {
duke@0 3410 cont = new BlockBegin(next_bci());
duke@0 3411 // low number so that continuation gets parsed as early as possible
duke@0 3412 cont->set_depth_first_number(0);
duke@0 3413 #ifndef PRODUCT
duke@0 3414 if (PrintInitialBlockList) {
duke@0 3415 tty->print_cr("CFG: created block %d (bci %d) as continuation for inline at bci %d",
duke@0 3416 cont->block_id(), cont->bci(), bci());
duke@0 3417 }
duke@0 3418 #endif
duke@0 3419 continuation_existed = false;
duke@0 3420 }
duke@0 3421 // Record number of predecessors of continuation block before
duke@0 3422 // inlining, to detect if inlined method has edges to its
duke@0 3423 // continuation after inlining.
duke@0 3424 int continuation_preds = cont->number_of_preds();
duke@0 3425
duke@0 3426 // Push callee scope
duke@0 3427 push_scope(callee, cont);
duke@0 3428
duke@0 3429 // the BlockListBuilder for the callee could have bailed out
duke@0 3430 CHECK_BAILOUT_(false);
duke@0 3431
duke@0 3432 // Temporarily set up bytecode stream so we can append instructions
duke@0 3433 // (only using the bci of this stream)
duke@0 3434 scope_data()->set_stream(scope_data()->parent()->stream());
duke@0 3435
duke@0 3436 // Pass parameters into callee state: add assignments
duke@0 3437 // note: this will also ensure that all arguments are computed before being passed
duke@0 3438 ValueStack* callee_state = state();
duke@0 3439 ValueStack* caller_state = scope()->caller_state();
duke@0 3440 { int i = args_base;
duke@0 3441 while (i < caller_state->stack_size()) {
duke@0 3442 const int par_no = i - args_base;
duke@0 3443 Value arg = caller_state->stack_at_inc(i);
duke@0 3444 // NOTE: take base() of arg->type() to avoid problems storing
duke@0 3445 // constants
duke@0 3446 store_local(callee_state, arg, arg->type()->base(), par_no);
duke@0 3447 }
duke@0 3448 }
duke@0 3449
duke@0 3450 // Remove args from stack.
duke@0 3451 // Note that we preserve locals state in case we can use it later
duke@0 3452 // (see use of pop_scope() below)
duke@0 3453 caller_state->truncate_stack(args_base);
duke@0 3454 callee_state->truncate_stack(args_base);
duke@0 3455
duke@0 3456 // Setup state that is used at returns form the inlined method.
duke@0 3457 // This is essentially the state of the continuation block,
duke@0 3458 // but without the return value on stack, if any, this will
duke@0 3459 // be pushed at the return instruction (see method_return).
duke@0 3460 scope_data()->set_continuation_state(caller_state->copy());
duke@0 3461
duke@0 3462 // Compute lock stack size for callee scope now that args have been passed
duke@0 3463 scope()->compute_lock_stack_size();
duke@0 3464
duke@0 3465 Value lock;
duke@0 3466 BlockBegin* sync_handler;
duke@0 3467
duke@0 3468 // Inline the locking of the receiver if the callee is synchronized
duke@0 3469 if (callee->is_synchronized()) {
duke@0 3470 lock = callee->is_static() ? append(new Constant(new InstanceConstant(callee->holder()->java_mirror())))
duke@0 3471 : state()->local_at(0);
duke@0 3472 sync_handler = new BlockBegin(-1);
duke@0 3473 inline_sync_entry(lock, sync_handler);
duke@0 3474
duke@0 3475 // recompute the lock stack size
duke@0 3476 scope()->compute_lock_stack_size();
duke@0 3477 }
duke@0 3478
duke@0 3479
duke@0 3480 BlockBegin* callee_start_block = block_at(0);
duke@0 3481 if (callee_start_block != NULL) {
duke@0 3482 assert(callee_start_block->is_set(BlockBegin::parser_loop_header_flag), "must be loop header");
duke@0 3483 Goto* goto_callee = new Goto(callee_start_block, false);
duke@0 3484 goto_callee->set_state(state());
duke@0 3485 // The state for this goto is in the scope of the callee, so use
duke@0 3486 // the entry bci for the callee instead of the call site bci.
duke@0 3487 append_with_bci(goto_callee, 0);
duke@0 3488 _block->set_end(goto_callee);
duke@0 3489 callee_start_block->merge(callee_state);
duke@0 3490
duke@0 3491 _last = _block = callee_start_block;
duke@0 3492
duke@0 3493 scope_data()->add_to_work_list(callee_start_block);
duke@0 3494 }
duke@0 3495
duke@0 3496 // Clear out bytecode stream
duke@0 3497 scope_data()->set_stream(NULL);
duke@0 3498
duke@0 3499 // Ready to resume parsing in callee (either in the same block we
duke@0 3500 // were in before or in the callee's start block)
duke@0 3501 iterate_all_blocks(callee_start_block == NULL);
duke@0 3502
duke@0 3503 // If we bailed out during parsing, return immediately (this is bad news)
duke@0 3504 if (bailed_out()) return false;
duke@0 3505
duke@0 3506 // iterate_all_blocks theoretically traverses in random order; in
duke@0 3507 // practice, we have only traversed the continuation if we are
duke@0 3508 // inlining into a subroutine
duke@0 3509 assert(continuation_existed ||
duke@0 3510 !continuation()->is_set(BlockBegin::was_visited_flag),
duke@0 3511 "continuation should not have been parsed yet if we created it");
duke@0 3512
duke@0 3513 // If we bailed out during parsing, return immediately (this is bad news)
duke@0 3514 CHECK_BAILOUT_(false);
duke@0 3515
duke@0 3516 // At this point we are almost ready to return and resume parsing of
duke@0 3517 // the caller back in the GraphBuilder. The only thing we want to do
duke@0 3518 // first is an optimization: during parsing of the callee we
duke@0 3519 // generated at least one Goto to the continuation block. If we
duke@0 3520 // generated exactly one, and if the inlined method spanned exactly
duke@0 3521 // one block (and we didn't have to Goto its entry), then we snip
duke@0 3522 // off the Goto to the continuation, allowing control to fall
duke@0 3523 // through back into the caller block and effectively performing
duke@0 3524 // block merging. This allows load elimination and CSE to take place
duke@0 3525 // across multiple callee scopes if they are relatively simple, and
duke@0 3526 // is currently essential to making inlining profitable.
duke@0 3527 if ( num_returns() == 1
duke@0 3528 && block() == orig_block
duke@0 3529 && block() == inline_cleanup_block()) {
duke@0 3530 _last = inline_cleanup_return_prev();
duke@0 3531 _state = inline_cleanup_state()->pop_scope();
duke@0 3532 } else if (continuation_preds == cont->number_of_preds()) {
duke@0 3533 // Inlining caused that the instructions after the invoke in the
duke@0 3534 // caller are not reachable any more. So skip filling this block
duke@0 3535 // with instructions!
duke@0 3536 assert (cont == continuation(), "");
duke@0 3537 assert(_last && _last->as_BlockEnd(), "");
duke@0 3538 _skip_block = true;
duke@0 3539 } else {
duke@0 3540 // Resume parsing in continuation block unless it was already parsed.
duke@0 3541 // Note that if we don't change _last here, iteration in
duke@0 3542 // iterate_bytecodes_for_block will stop when we return.
duke@0 3543 if (!continuation()->is_set(BlockBegin::was_visited_flag)) {
duke@0 3544 // add continuation to work list instead of parsing it immediately
duke@0 3545 assert(_last && _last->as_BlockEnd(), "");
duke@0 3546 scope_data()->parent()->add_to_work_list(continuation());
duke@0 3547 _skip_block = true;
duke@0 3548 }
duke@0 3549 }
duke@0 3550
duke@0 3551 // Fill the exception handler for synchronized methods with instructions
duke@0 3552 if (callee->is_synchronized() && sync_handler->state() != NULL) {
duke@0 3553 fill_sync_handler(lock, sync_handler);
duke@0 3554 } else {
duke@0 3555 pop_scope();
duke@0 3556 }
duke@0 3557
duke@0 3558 compilation()->notice_inlined_method(callee);
duke@0 3559
duke@0 3560 return true;
duke@0 3561 }
duke@0 3562
duke@0 3563
duke@0 3564 void GraphBuilder::inline_bailout(const char* msg) {
duke@0 3565 assert(msg != NULL, "inline bailout msg must exist");
duke@0 3566 _inline_bailout_msg = msg;
duke@0 3567 }
duke@0 3568
duke@0 3569
duke@0 3570 void GraphBuilder::clear_inline_bailout() {
duke@0 3571 _inline_bailout_msg = NULL;
duke@0 3572 }
duke@0 3573
duke@0 3574
duke@0 3575 void GraphBuilder::push_root_scope(IRScope* scope, BlockList* bci2block, BlockBegin* start) {
duke@0 3576 ScopeData* data = new ScopeData(NULL);
duke@0 3577 data->set_scope(scope);
duke@0 3578 data->set_bci2block(bci2block);
duke@0 3579 _scope_data = data;
duke@0 3580 _block = start;
duke@0 3581 }
duke@0 3582
duke@0 3583
duke@0 3584 void GraphBuilder::push_scope(ciMethod* callee, BlockBegin* continuation) {
duke@0 3585 IRScope* callee_scope = new IRScope(compilation(), scope(), bci(), callee, -1, false);
duke@0 3586 scope()->add_callee(callee_scope);
duke@0 3587
duke@0 3588 BlockListBuilder blb(compilation(), callee_scope, -1);
duke@0 3589 CHECK_BAILOUT();
duke@0 3590
duke@0 3591 if (!blb.bci2block()->at(0)->is_set(BlockBegin::parser_loop_header_flag)) {
duke@0 3592 // this scope can be inlined directly into the caller so remove
duke@0 3593 // the block at bci 0.
duke@0 3594 blb.bci2block()->at_put(0, NULL);
duke@0 3595 }
duke@0 3596
duke@0 3597 callee_scope->set_caller_state(state());
duke@0 3598 set_state(state()->push_scope(callee_scope));
duke@0 3599
duke@0 3600 ScopeData* data = new ScopeData(scope_data());
duke@0 3601 data->set_scope(callee_scope);
duke@0 3602 data->set_bci2block(blb.bci2block());
duke@0 3603 data->set_continuation(continuation);
duke@0 3604 _scope_data = data;