c1_GraphBuilder.cpp revision 1472:c18cbe5936b8
1163953Srrs/*
2185694Srrs * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
3235828Stuexen * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4235828Stuexen *
5163953Srrs * This code is free software; you can redistribute it and/or modify it
6163953Srrs * under the terms of the GNU General Public License version 2 only, as
7163953Srrs * published by the Free Software Foundation.
8163953Srrs *
9163953Srrs * This code is distributed in the hope that it will be useful, but WITHOUT
10228653Stuexen * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11163953Srrs * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12163953Srrs * version 2 for more details (a copy is included in the LICENSE file that
13163953Srrs * accompanied this code).
14228653Stuexen *
15163953Srrs * You should have received a copy of the GNU General Public License version
16163953Srrs * 2 along with this work; if not, write to the Free Software Foundation,
17163953Srrs * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18163953Srrs *
19163953Srrs * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20163953Srrs * or visit www.oracle.com if you need additional information or have any
21163953Srrs * questions.
22163953Srrs *
23163953Srrs */
24163953Srrs
25163953Srrs#include "incls/_precompiled.incl"
26163953Srrs#include "incls/_c1_GraphBuilder.cpp.incl"
27163953Srrs
28163953Srrsclass BlockListBuilder VALUE_OBJ_CLASS_SPEC {
29163953Srrs private:
30163953Srrs  Compilation* _compilation;
31163953Srrs  IRScope*     _scope;
32163953Srrs
33163953Srrs  BlockList    _blocks;                // internal list of all blocks
34163953Srrs  BlockList*   _bci2block;             // mapping from bci to blocks for GraphBuilder
35235828Stuexen
36166086Srrs  // fields used by mark_loops
37163953Srrs  BitMap       _active;                // for iteration of control flow graph
38163953Srrs  BitMap       _visited;               // for iteration of control flow graph
39163953Srrs  intArray     _loop_map;              // caches the information if a block is contained in a loop
40163953Srrs  int          _next_loop_index;       // next free loop number
41238475Stuexen  int          _next_block_number;     // for reverse postorder numbering of blocks
42275567Stuexen
43167695Srrs  // accessors
44167598Srrs  Compilation*  compilation() const              { return _compilation; }
45163953Srrs  IRScope*      scope() const                    { return _scope; }
46163953Srrs  ciMethod*     method() const                   { return scope()->method(); }
47163953Srrs  XHandlers*    xhandlers() const                { return scope()->xhandlers(); }
48163953Srrs
49163953Srrs  // unified bailout support
50163953Srrs  void          bailout(const char* msg) const   { compilation()->bailout(msg); }
51163953Srrs  bool          bailed_out() const               { return compilation()->bailed_out(); }
52170091Srrs
53185694Srrs  // helper functions
54164085Srrs  BlockBegin* make_block_at(int bci, BlockBegin* predecessor);
55163953Srrs  void handle_exceptions(BlockBegin* current, int cur_bci);
56163953Srrs  void handle_jsr(BlockBegin* current, int sr_bci, int next_bci);
57297208Stuexen  void store_one(BlockBegin* current, int local);
58297208Stuexen  void store_two(BlockBegin* current, int local);
59170091Srrs  void set_entries(int osr_bci);
60163953Srrs  void set_leaders();
61163953Srrs
62163953Srrs  void make_loop_header(BlockBegin* block);
63163953Srrs  void mark_loops();
64163953Srrs  int  mark_loops(BlockBegin* b, bool in_subroutine);
65179783Srrs
66179783Srrs  // debugging
67163953Srrs#ifndef PRODUCT
68179783Srrs  void print();
69163953Srrs#endif
70163953Srrs
71163953Srrs public:
72163953Srrs  // creation
73163953Srrs  BlockListBuilder(Compilation* compilation, IRScope* scope, int osr_bci);
74179783Srrs
75170056Srrs  // accessors for GraphBuilder
76163953Srrs  BlockList*    bci2block() const                { return _bci2block; }
77163953Srrs};
78163953Srrs
79163953Srrs
80163953Srrs// Implementation of BlockListBuilder
81179783Srrs
82179783SrrsBlockListBuilder::BlockListBuilder(Compilation* compilation, IRScope* scope, int osr_bci)
83179783Srrs : _compilation(compilation)
84179783Srrs , _scope(scope)
85179783Srrs , _blocks(16)
86179783Srrs , _bci2block(new BlockList(scope->method()->code_size(), NULL))
87179783Srrs , _next_block_number(0)
88179783Srrs , _active()         // size not known yet
89179783Srrs , _visited()        // size not known yet
90163953Srrs , _next_loop_index(0)
91163953Srrs , _loop_map() // size not known yet
92301114Sbz{
93301114Sbz  set_entries(osr_bci);
94301114Sbz  set_leaders();
95179783Srrs  CHECK_BAILOUT();
96179783Srrs
97179783Srrs  mark_loops();
98303956Stuexen  NOT_PRODUCT(if (PrintInitialBlockList) print());
99301114Sbz
100301114Sbz#ifndef PRODUCT
101163953Srrs  if (PrintCFGToFile) {
102179157Srrs    stringStream title;
103228653Stuexen    title.print("BlockListBuilder ");
104163953Srrs    scope->method()->print_name(&title);
105163953Srrs    CFGPrinter::print_cfg(_bci2block, title.as_string(), false, false);
106197257Stuexen  }
107163953Srrs#endif
108163953Srrs}
109163953Srrs
110163953Srrs
111197257Stuexenvoid BlockListBuilder::set_entries(int osr_bci) {
112197257Stuexen  // generate start blocks
113197257Stuexen  BlockBegin* std_entry = make_block_at(0, NULL);
114197257Stuexen  if (scope()->caller() == NULL) {
115163953Srrs    std_entry->set(BlockBegin::std_entry_flag);
116197257Stuexen  }
117163953Srrs  if (osr_bci != -1) {
118163953Srrs    BlockBegin* osr_entry = make_block_at(osr_bci, NULL);
119163953Srrs    osr_entry->set(BlockBegin::osr_entry_flag);
120163953Srrs  }
121197257Stuexen
122163953Srrs  // generate exception entry blocks
123163953Srrs  XHandlers* list = xhandlers();
124163953Srrs  const int n = list->length();
125163953Srrs  for (int i = 0; i < n; i++) {
126163953Srrs    XHandler* h = list->handler_at(i);
127190689Srrs    BlockBegin* entry = make_block_at(h->handler_bci(), NULL);
128190689Srrs    entry->set(BlockBegin::exception_entry_flag);
129190689Srrs    h->set_entry_block(entry);
130280404Stuexen  }
131163953Srrs}
132280404Stuexen
133280404Stuexen
134280404StuexenBlockBegin* BlockListBuilder::make_block_at(int cur_bci, BlockBegin* predecessor) {
135280404Stuexen  assert(method()->bci_block_start().at(cur_bci), "wrong block starts of MethodLivenessAnalyzer");
136280404Stuexen
137297312Stuexen  BlockBegin* block = _bci2block->at(cur_bci);
138280404Stuexen  if (block == NULL) {
139280404Stuexen    block = new BlockBegin(cur_bci);
140280404Stuexen    block->init_stores_to_locals(method()->max_locals());
141280404Stuexen    _bci2block->at_put(cur_bci, block);
142163953Srrs    _blocks.append(block);
143163953Srrs
144163953Srrs    assert(predecessor == NULL || predecessor->bci() < cur_bci, "targets for backward branches must already exist");
145163953Srrs  }
146163953Srrs
147221249Stuexen  if (predecessor != NULL) {
148298132Stuexen    if (block->is_set(BlockBegin::exception_entry_flag)) {
149297880Stuexen      BAILOUT_("Exception handler can be reached by both normal and exceptional control flow", block);
150163953Srrs    }
151163953Srrs
152297880Stuexen    predecessor->add_successor(block);
153297880Stuexen    block->increment_total_preds();
154297880Stuexen  }
155297880Stuexen
156163953Srrs  return block;
157237565Stuexen}
158172090Srrs
159172090Srrs
160172090Srrsinline void BlockListBuilder::store_one(BlockBegin* current, int local) {
161297880Stuexen  current->stores_to_locals().set_bit(local);
162172091Srrs}
163297880Stuexeninline void BlockListBuilder::store_two(BlockBegin* current, int local) {
164172091Srrs  store_one(current, local);
165172091Srrs  store_one(current, local + 1);
166172091Srrs}
167172091Srrs
168297880Stuexen
169297880Stuexenvoid BlockListBuilder::handle_exceptions(BlockBegin* current, int cur_bci) {
170297880Stuexen  // Draws edges from a block to its exception handlers
171297880Stuexen  XHandlers* list = xhandlers();
172297880Stuexen  const int n = list->length();
173297880Stuexen
174297880Stuexen  for (int i = 0; i < n; i++) {
175297880Stuexen    XHandler* h = list->handler_at(i);
176298132Stuexen
177172091Srrs    if (h->covers(cur_bci)) {
178297880Stuexen      BlockBegin* entry = h->entry_block();
179172091Srrs      assert(entry != NULL && entry == _bci2block->at(h->handler_bci()), "entry must be set");
180224641Stuexen      assert(entry->is_set(BlockBegin::exception_entry_flag), "flag must be set");
181172091Srrs
182235414Stuexen      // add each exception handler only once
183172091Srrs      if (!current->is_successor(entry)) {
184172091Srrs        current->add_successor(entry);
185172091Srrs        entry->increment_total_preds();
186297880Stuexen      }
187297880Stuexen
188298132Stuexen      // stop when reaching catchall
189235403Stuexen      if (h->catch_type() == 0) break;
190237565Stuexen    }
191172091Srrs  }
192172091Srrs}
193172091Srrs
194172091Srrsvoid BlockListBuilder::handle_jsr(BlockBegin* current, int sr_bci, int next_bci) {
195172091Srrs  // start a new block after jsr-bytecode and link this block into cfg
196172091Srrs  make_block_at(next_bci, current);
197172090Srrs
198283650Stuexen  // start a new block at the subroutine entry at mark it with special flag
199283650Stuexen  BlockBegin* sr_block = make_block_at(sr_bci, current);
200237565Stuexen  if (!sr_block->is_set(BlockBegin::subroutine_entry_flag)) {
201172091Srrs    sr_block->set(BlockBegin::subroutine_entry_flag);
202172091Srrs  }
203172090Srrs}
204172091Srrs
205297880Stuexen
206297880Stuexenvoid BlockListBuilder::set_leaders() {
207297880Stuexen  bool has_xhandlers = xhandlers()->has_handlers();
208297880Stuexen  BlockBegin* current = NULL;
209297880Stuexen
210297880Stuexen  // The information which bci starts a new block simplifies the analysis
211297880Stuexen  // Without it, backward branches could jump to a bci where no block was created
212297880Stuexen  // during bytecode iteration. This would require the creation of a new block at the
213297880Stuexen  // branch target and a modification of the successor lists.
214297880Stuexen  BitMap bci_block_start = method()->bci_block_start();
215297880Stuexen
216297880Stuexen  ciBytecodeStream s(method());
217297880Stuexen  while (s.next() != ciBytecodeStream::EOBC()) {
218297880Stuexen    int cur_bci = s.cur_bci();
219297880Stuexen
220297880Stuexen    if (bci_block_start.at(cur_bci)) {
221297880Stuexen      current = make_block_at(cur_bci, current);
222297880Stuexen    }
223297880Stuexen    assert(current != NULL, "must have current block");
224297880Stuexen
225297880Stuexen    if (has_xhandlers && GraphBuilder::can_trap(method(), s.cur_bc())) {
226297880Stuexen      handle_exceptions(current, cur_bci);
227297880Stuexen    }
228297880Stuexen
229297880Stuexen    switch (s.cur_bc()) {
230297880Stuexen      // track stores to local variables for selective creation of phi functions
231297880Stuexen      case Bytecodes::_iinc:     store_one(current, s.get_index()); break;
232297880Stuexen      case Bytecodes::_istore:   store_one(current, s.get_index()); break;
233297880Stuexen      case Bytecodes::_lstore:   store_two(current, s.get_index()); break;
234297880Stuexen      case Bytecodes::_fstore:   store_one(current, s.get_index()); break;
235297880Stuexen      case Bytecodes::_dstore:   store_two(current, s.get_index()); break;
236297880Stuexen      case Bytecodes::_astore:   store_one(current, s.get_index()); break;
237297880Stuexen      case Bytecodes::_istore_0: store_one(current, 0); break;
238297880Stuexen      case Bytecodes::_istore_1: store_one(current, 1); break;
239163953Srrs      case Bytecodes::_istore_2: store_one(current, 2); break;
240172091Srrs      case Bytecodes::_istore_3: store_one(current, 3); break;
241163953Srrs      case Bytecodes::_lstore_0: store_two(current, 0); break;
242163953Srrs      case Bytecodes::_lstore_1: store_two(current, 1); break;
243163953Srrs      case Bytecodes::_lstore_2: store_two(current, 2); break;
244163953Srrs      case Bytecodes::_lstore_3: store_two(current, 3); break;
245297855Stuexen      case Bytecodes::_fstore_0: store_one(current, 0); break;
246163953Srrs      case Bytecodes::_fstore_1: store_one(current, 1); break;
247298223Stuexen      case Bytecodes::_fstore_2: store_one(current, 2); break;
248298223Stuexen      case Bytecodes::_fstore_3: store_one(current, 3); break;
249163953Srrs      case Bytecodes::_dstore_0: store_two(current, 0); break;
250297990Stuexen      case Bytecodes::_dstore_1: store_two(current, 1); break;
251297990Stuexen      case Bytecodes::_dstore_2: store_two(current, 2); break;
252297990Stuexen      case Bytecodes::_dstore_3: store_two(current, 3); break;
253297990Stuexen      case Bytecodes::_astore_0: store_one(current, 0); break;
254297990Stuexen      case Bytecodes::_astore_1: store_one(current, 1); break;
255298132Stuexen      case Bytecodes::_astore_2: store_one(current, 2); break;
256163953Srrs      case Bytecodes::_astore_3: store_one(current, 3); break;
257163953Srrs
258163953Srrs      // track bytecodes that affect the control flow
259163953Srrs      case Bytecodes::_athrow:  // fall through
260163953Srrs      case Bytecodes::_ret:     // fall through
261163953Srrs      case Bytecodes::_ireturn: // fall through
262297990Stuexen      case Bytecodes::_lreturn: // fall through
263163953Srrs      case Bytecodes::_freturn: // fall through
264163953Srrs      case Bytecodes::_dreturn: // fall through
265163953Srrs      case Bytecodes::_areturn: // fall through
266297990Stuexen      case Bytecodes::_return:
267297990Stuexen        current = NULL;
268297990Stuexen        break;
269297990Stuexen
270297990Stuexen      case Bytecodes::_ifeq:      // fall through
271297990Stuexen      case Bytecodes::_ifne:      // fall through
272298132Stuexen      case Bytecodes::_iflt:      // fall through
273298132Stuexen      case Bytecodes::_ifge:      // fall through
274298132Stuexen      case Bytecodes::_ifgt:      // fall through
275298132Stuexen      case Bytecodes::_ifle:      // fall through
276298132Stuexen      case Bytecodes::_if_icmpeq: // fall through
277298132Stuexen      case Bytecodes::_if_icmpne: // fall through
278298132Stuexen      case Bytecodes::_if_icmplt: // fall through
279298132Stuexen      case Bytecodes::_if_icmpge: // fall through
280298132Stuexen      case Bytecodes::_if_icmpgt: // fall through
281298132Stuexen      case Bytecodes::_if_icmple: // fall through
282163953Srrs      case Bytecodes::_if_acmpeq: // fall through
283298132Stuexen      case Bytecodes::_if_acmpne: // fall through
284298132Stuexen      case Bytecodes::_ifnull:    // fall through
285298132Stuexen      case Bytecodes::_ifnonnull:
286163953Srrs        make_block_at(s.next_bci(), current);
287297990Stuexen        make_block_at(s.get_dest(), current);
288297990Stuexen        current = NULL;
289298132Stuexen        break;
290298132Stuexen
291297990Stuexen      case Bytecodes::_goto:
292297990Stuexen        make_block_at(s.get_dest(), current);
293297855Stuexen        current = NULL;
294297880Stuexen        break;
295302138Stuexen
296297855Stuexen      case Bytecodes::_goto_w:
297297855Stuexen        make_block_at(s.get_far_dest(), current);
298297855Stuexen        current = NULL;
299297855Stuexen        break;
300297855Stuexen
301297855Stuexen      case Bytecodes::_jsr:
302297855Stuexen        handle_jsr(current, s.get_dest(), s.next_bci());
303297855Stuexen        current = NULL;
304297880Stuexen        break;
305297855Stuexen
306297855Stuexen      case Bytecodes::_jsr_w:
307297855Stuexen        handle_jsr(current, s.get_far_dest(), s.next_bci());
308297855Stuexen        current = NULL;
309297990Stuexen        break;
310297990Stuexen
311297990Stuexen      case Bytecodes::_tableswitch: {
312297990Stuexen        // set block for each case
313297990Stuexen        Bytecode_tableswitch *switch_ = Bytecode_tableswitch_at(s.cur_bcp());
314297990Stuexen        int l = switch_->length();
315297990Stuexen        for (int i = 0; i < l; i++) {
316297990Stuexen          make_block_at(cur_bci + switch_->dest_offset_at(i), current);
317297990Stuexen        }
318297990Stuexen        make_block_at(cur_bci + switch_->default_offset(), current);
319297990Stuexen        current = NULL;
320297990Stuexen        break;
321297990Stuexen      }
322297990Stuexen
323297990Stuexen      case Bytecodes::_lookupswitch: {
324297990Stuexen        // set block for each case
325297990Stuexen        Bytecode_lookupswitch *switch_ = Bytecode_lookupswitch_at(s.cur_bcp());
326297990Stuexen        int l = switch_->number_of_pairs();
327297855Stuexen        for (int i = 0; i < l; i++) {
328297880Stuexen          make_block_at(cur_bci + switch_->pair_at(i)->offset(), current);
329297990Stuexen        }
330297990Stuexen        make_block_at(cur_bci + switch_->default_offset(), current);
331297990Stuexen        current = NULL;
332297990Stuexen        break;
333163953Srrs      }
334163953Srrs    }
335163953Srrs  }
336163953Srrs}
337163953Srrs
338163953Srrs
339163953Srrsvoid BlockListBuilder::mark_loops() {
340209029Srrs  ResourceMark rm;
341209029Srrs
342209029Srrs  _active = BitMap(BlockBegin::number_of_blocks());         _active.clear();
343163953Srrs  _visited = BitMap(BlockBegin::number_of_blocks());        _visited.clear();
344163953Srrs  _loop_map = intArray(BlockBegin::number_of_blocks(), 0);
345163953Srrs  _next_loop_index = 0;
346163953Srrs  _next_block_number = _blocks.length();
347163953Srrs
348221249Stuexen  // recursively iterate the control flow graph
349221249Stuexen  mark_loops(_bci2block->at(0), false);
350163953Srrs  assert(_next_block_number >= 0, "invalid block numbers");
351163953Srrs}
352163953Srrs
353164085Srrsvoid BlockListBuilder::make_loop_header(BlockBegin* block) {
354163953Srrs  if (block->is_set(BlockBegin::exception_entry_flag)) {
355163953Srrs    // exception edges may look like loops but don't mark them as such
356163953Srrs    // since it screws up block ordering.
357163953Srrs    return;
358164085Srrs  }
359167598Srrs  if (!block->is_set(BlockBegin::parser_loop_header_flag)) {
360163953Srrs    block->set(BlockBegin::parser_loop_header_flag);
361168299Srrs
362167598Srrs    assert(_loop_map.at(block->block_id()) == 0, "must not be set yet");
363168299Srrs    assert(0 <= _next_loop_index && _next_loop_index < BitsPerInt, "_next_loop_index is used as a bit-index in integer");
364170587Srwatson    _loop_map.at_put(block->block_id(), 1 << _next_loop_index);
365170587Srwatson    if (_next_loop_index < 31) _next_loop_index++;
366163953Srrs  } else {
367163953Srrs    // block already marked as loop header
368164039Srwatson    assert(is_power_of_2((unsigned int)_loop_map.at(block->block_id())), "exactly one bit must be set");
369163953Srrs  }
370163953Srrs}
371163953Srrs
372163953Srrsint BlockListBuilder::mark_loops(BlockBegin* block, bool in_subroutine) {
373237715Stuexen  int block_id = block->block_id();
374237715Stuexen
375167598Srrs  if (_visited.at(block_id)) {
376163953Srrs    if (_active.at(block_id)) {
377163953Srrs      // reached block via backward branch
378163953Srrs      make_loop_header(block);
379163953Srrs    }
380163953Srrs    // return cached loop information for this block
381164085Srrs    return _loop_map.at(block_id);
382163953Srrs  }
383171943Srrs
384163953Srrs  if (block->is_set(BlockBegin::subroutine_entry_flag)) {
385163953Srrs    in_subroutine = true;
386163953Srrs  }
387163953Srrs
388164085Srrs  // set active and visited bits before successors are processed
389164085Srrs  _visited.set_bit(block_id);
390164085Srrs  _active.set_bit(block_id);
391164085Srrs
392164085Srrs  intptr_t loop_state = 0;
393164085Srrs  for (int i = block->number_of_sux() - 1; i >= 0; i--) {
394164085Srrs    // recursively process all successors
395164085Srrs    loop_state |= mark_loops(block->sux_at(i), in_subroutine);
396164085Srrs  }
397164085Srrs
398164085Srrs  // clear active-bit after all successors are processed
399164085Srrs  _active.clear_bit(block_id);
400164085Srrs
401164085Srrs  // reverse-post-order numbering of all blocks
402164085Srrs  block->set_depth_first_number(_next_block_number);
403164085Srrs  _next_block_number--;
404163953Srrs
405163953Srrs  if (loop_state != 0 || in_subroutine ) {
406163953Srrs    // block is contained at least in one loop, so phi functions are necessary
407163953Srrs    // phi functions are also necessary for all locals stored in a subroutine
408163953Srrs    scope()->requires_phi_function().set_union(block->stores_to_locals());
409163953Srrs  }
410163953Srrs
411163953Srrs  if (block->is_set(BlockBegin::parser_loop_header_flag)) {
412221249Stuexen    int header_loop_state = _loop_map.at(block_id);
413163953Srrs    assert(is_power_of_2((unsigned)header_loop_state), "exactly one bit must be set");
414163953Srrs
415163953Srrs    // If the highest bit is set (i.e. when integer value is negative), the method
416163953Srrs    // has 32 or more loops. This bit is never cleared because it is used for multiple loops
417163953Srrs    if (header_loop_state >= 0) {
418163953Srrs      clear_bits(loop_state, header_loop_state);
419163953Srrs    }
420233005Stuexen  }
421163953Srrs
422171943Srrs  // cache and return loop information for this block
423163953Srrs  _loop_map.at_put(block_id, loop_state);
424163953Srrs  return loop_state;
425163953Srrs}
426163953Srrs
427163953Srrs
428163953Srrs#ifndef PRODUCT
429163953Srrs
430163953Srrsint compare_depth_first(BlockBegin** a, BlockBegin** b) {
431163953Srrs  return (*a)->depth_first_number() - (*b)->depth_first_number();
432163953Srrs}
433169380Srrs
434169380Srrsvoid BlockListBuilder::print() {
435163953Srrs  tty->print("----- initial block list of BlockListBuilder for method ");
436167695Srrs  method()->print_short_name();
437163953Srrs  tty->cr();
438163953Srrs
439163953Srrs  // better readability if blocks are sorted in processing order
440163953Srrs  _blocks.sort(compare_depth_first);
441167695Srrs
442167695Srrs  for (int i = 0; i < _blocks.length(); i++) {
443167695Srrs    BlockBegin* cur = _blocks.at(i);
444163953Srrs    tty->print("%4d: B%-4d bci: %-4d  preds: %-4d ", cur->depth_first_number(), cur->block_id(), cur->bci(), cur->total_preds());
445163953Srrs
446163953Srrs    tty->print(cur->is_set(BlockBegin::std_entry_flag)               ? " std" : "    ");
447163953Srrs    tty->print(cur->is_set(BlockBegin::osr_entry_flag)               ? " osr" : "    ");
448163953Srrs    tty->print(cur->is_set(BlockBegin::exception_entry_flag)         ? " ex" : "   ");
449163953Srrs    tty->print(cur->is_set(BlockBegin::subroutine_entry_flag)        ? " sr" : "   ");
450163953Srrs    tty->print(cur->is_set(BlockBegin::parser_loop_header_flag)      ? " lh" : "   ");
451163953Srrs
452163953Srrs    if (cur->number_of_sux() > 0) {
453163953Srrs      tty->print("    sux: ");
454163953Srrs      for (int j = 0; j < cur->number_of_sux(); j++) {
455163953Srrs        BlockBegin* sux = cur->sux_at(j);
456228653Stuexen        tty->print("B%d ", sux->block_id());
457163953Srrs      }
458163953Srrs    }
459163953Srrs    tty->cr();
460166086Srrs  }
461170205Srrs}
462185694Srrs
463163953Srrs#endif
464298066Spfg
465171943Srrs
466228907Stuexen// A simple growable array of Values indexed by ciFields
467163953Srrsclass FieldBuffer: public CompilationResourceObj {
468184030Srrs private:
469184030Srrs  GrowableArray<Value> _values;
470184030Srrs
471228907Stuexen public:
472184030Srrs  FieldBuffer() {}
473163953Srrs
474170205Srrs  void kill() {
475163953Srrs    _values.trunc_to(0);
476228907Stuexen  }
477163953Srrs
478163953Srrs  Value at(ciField* field) {
479163953Srrs    assert(field->holder()->is_loaded(), "must be a loaded field");
480163953Srrs    int offset = field->offset();
481163953Srrs    if (offset < _values.length()) {
482163953Srrs      return _values.at(offset);
483197288Srrs    } else {
484163953Srrs      return NULL;
485228907Stuexen    }
486163953Srrs  }
487163953Srrs
488163953Srrs  void at_put(ciField* field, Value value) {
489163953Srrs    assert(field->holder()->is_loaded(), "must be a loaded field");
490163953Srrs    int offset = field->offset();
491244730Stuexen    _values.at_put_grow(offset, value, NULL);
492163953Srrs  }
493163953Srrs
494233005Stuexen};
495171943Srrs
496228907Stuexen
497171943Srrs// MemoryBuffer is fairly simple model of the current state of memory.
498244730Stuexen// It partitions memory into several pieces.  The first piece is
499244730Stuexen// generic memory where little is known about the owner of the memory.
500244730Stuexen// This is conceptually represented by the tuple <O, F, V> which says
501244730Stuexen// that the field F of object O has value V.  This is flattened so
502244730Stuexen// that F is represented by the offset of the field and the parallel
503244730Stuexen// arrays _objects and _values are used for O and V.  Loads of O.F can
504244730Stuexen// simply use V.  Newly allocated objects are kept in a separate list
505244730Stuexen// along with a parallel array for each object which represents the
506163953Srrs// current value of its fields.  Stores of the default value to fields
507163953Srrs// which have never been stored to before are eliminated since they
508221249Stuexen// are redundant.  Once newly allocated objects are stored into
509171990Srrs// another object or they are passed out of the current compile they
510163953Srrs// are treated like generic memory.
511163953Srrs
512163953Srrsclass MemoryBuffer: public CompilationResourceObj {
513163953Srrs private:
514163953Srrs  FieldBuffer                 _values;
515163953Srrs  GrowableArray<Value>        _objects;
516233005Stuexen  GrowableArray<Value>        _newobjects;
517163953Srrs  GrowableArray<FieldBuffer*> _fields;
518163953Srrs
519163953Srrs public:
520163953Srrs  MemoryBuffer() {}
521163953Srrs
522163953Srrs  StoreField* store(StoreField* st) {
523163953Srrs    if (!EliminateFieldAccess) {
524163953Srrs      return st;
525163953Srrs    }
526163953Srrs
527163953Srrs    Value object = st->obj();
528163953Srrs    Value value = st->value();
529163953Srrs    ciField* field = st->field();
530275427Stuexen    if (field->holder()->is_loaded()) {
531163953Srrs      int offset = field->offset();
532163953Srrs      int index = _newobjects.find(object);
533163953Srrs      if (index != -1) {
534169380Srrs        // newly allocated object with no other stores performed on this field
535169380Srrs        FieldBuffer* buf = _fields.at(index);
536163953Srrs        if (buf->at(field) == NULL && is_default_value(value)) {
537163953Srrs#ifndef PRODUCT
538163953Srrs          if (PrintIRDuringConstruction && Verbose) {
539163953Srrs            tty->print_cr("Eliminated store for object %d:", index);
540169380Srrs            st->print_line();
541169380Srrs          }
542163953Srrs#endif
543163953Srrs          return NULL;
544163953Srrs        } else {
545163953Srrs          buf->at_put(field, value);
546163953Srrs        }
547163953Srrs      } else {
548167695Srrs        _objects.at_put_grow(offset, object, NULL);
549163953Srrs        _values.at_put(field, value);
550163953Srrs      }
551163953Srrs
552163953Srrs      store_value(value);
553167695Srrs    } else {
554167695Srrs      // if we held onto field names we could alias based on names but
555167695Srrs      // we don't know what's being stored to so kill it all.
556163953Srrs      kill();
557163953Srrs    }
558163953Srrs    return st;
559163953Srrs  }
560163953Srrs
561163953Srrs
562163953Srrs  // return true if this value correspond to the default value of a field.
563163953Srrs  bool is_default_value(Value value) {
564163953Srrs    Constant* con = value->as_Constant();
565163953Srrs    if (con) {
566163953Srrs      switch (con->type()->tag()) {
567163953Srrs        case intTag:    return con->type()->as_IntConstant()->value() == 0;
568163953Srrs        case longTag:   return con->type()->as_LongConstant()->value() == 0;
569163953Srrs        case floatTag:  return jint_cast(con->type()->as_FloatConstant()->value()) == 0;
570163953Srrs        case doubleTag: return jlong_cast(con->type()->as_DoubleConstant()->value()) == jlong_cast(0);
571163953Srrs        case objectTag: return con->type() == objectNull;
572163953Srrs        default:  ShouldNotReachHere();
573163953Srrs      }
574163953Srrs    }
575163953Srrs    return false;
576163953Srrs  }
577163953Srrs
578163953Srrs
579163953Srrs  // return either the actual value of a load or the load itself
580163953Srrs  Value load(LoadField* load) {
581233005Stuexen    if (!EliminateFieldAccess) {
582163953Srrs      return load;
583163953Srrs    }
584163953Srrs
585163953Srrs    if (RoundFPResults && UseSSE < 2 && load->type()->is_float_kind()) {
586171943Srrs      // can't skip load since value might get rounded as a side effect
587163953Srrs      return load;
588228907Stuexen    }
589163953Srrs
590163953Srrs    ciField* field = load->field();
591163953Srrs    Value object   = load->obj();
592163953Srrs    if (field->holder()->is_loaded() && !field->is_volatile()) {
593228907Stuexen      int offset = field->offset();
594163953Srrs      Value result = NULL;
595163953Srrs      int index = _newobjects.find(object);
596171943Srrs      if (index != -1) {
597163953Srrs        result = _fields.at(index)->at(field);
598163953Srrs      } else if (_objects.at_grow(offset, NULL) == object) {
599163953Srrs        result = _values.at(field);
600163953Srrs      }
601163953Srrs      if (result != NULL) {
602163953Srrs#ifndef PRODUCT
603163953Srrs        if (PrintIRDuringConstruction && Verbose) {
604163953Srrs          tty->print_cr("Eliminated load: ");
605163953Srrs          load->print_line();
606163953Srrs        }
607163953Srrs#endif
608171943Srrs        assert(result->type()->tag() == load->type()->tag(), "wrong types");
609163953Srrs        return result;
610163953Srrs      }
611163953Srrs    }
612163953Srrs    return load;
613163953Srrs  }
614163953Srrs
615223132Stuexen  // Record this newly allocated object
616163953Srrs  void new_instance(NewInstance* object) {
617163953Srrs    int index = _newobjects.length();
618163953Srrs    _newobjects.append(object);
619163953Srrs    if (_fields.at_grow(index, NULL) == NULL) {
620163953Srrs      _fields.at_put(index, new FieldBuffer());
621163953Srrs    } else {
622169420Srrs      _fields.at(index)->kill();
623163953Srrs    }
624163953Srrs  }
625163953Srrs
626163953Srrs  void store_value(Value value) {
627163953Srrs    int index = _newobjects.find(value);
628163953Srrs    if (index != -1) {
629163953Srrs      // stored a newly allocated object into another object.
630165647Srrs      // Assume we've lost track of it as separate slice of memory.
631163953Srrs      // We could do better by keeping track of whether individual
632163953Srrs      // fields could alias each other.
633163953Srrs      _newobjects.remove_at(index);
634163953Srrs      // pull out the field info and store it at the end up the list
635163953Srrs      // of field info list to be reused later.
636163953Srrs      _fields.append(_fields.at(index));
637163953Srrs      _fields.remove_at(index);
638163953Srrs    }
639163953Srrs  }
640163953Srrs
641163953Srrs  void kill() {
642163953Srrs    _newobjects.trunc_to(0);
643163953Srrs    _objects.trunc_to(0);
644163953Srrs    _values.kill();
645163953Srrs  }
646163953Srrs};
647163953Srrs
648163953Srrs
649163953Srrs// Implementation of GraphBuilder's ScopeData
650163953Srrs
651163953SrrsGraphBuilder::ScopeData::ScopeData(ScopeData* parent)
652163953Srrs  : _parent(parent)
653163953Srrs  , _bci2block(NULL)
654163953Srrs  , _scope(NULL)
655163953Srrs  , _has_handler(false)
656163953Srrs  , _stream(NULL)
657163953Srrs  , _work_list(NULL)
658171990Srrs  , _parsing_jsr(false)
659163953Srrs  , _jsr_xhandlers(NULL)
660163953Srrs  , _caller_stack_size(-1)
661163953Srrs  , _continuation(NULL)
662163953Srrs  , _continuation_state(NULL)
663163953Srrs  , _num_returns(0)
664163953Srrs  , _cleanup_block(NULL)
665171943Srrs  , _cleanup_return_prev(NULL)
666163953Srrs  , _cleanup_state(NULL)
667163953Srrs{
668163953Srrs  if (parent != NULL) {
669171745Srrs    _max_inline_size = (intx) ((float) NestedInliningSizeRatio * (float) parent->max_inline_size() / 100.0f);
670171745Srrs  } else {
671199437Stuexen    _max_inline_size = MaxInlineSize;
672163953Srrs  }
673163953Srrs  if (_max_inline_size < MaxTrivialSize) {
674163953Srrs    _max_inline_size = MaxTrivialSize;
675163953Srrs  }
676163953Srrs}
677163953Srrs
678163953Srrs
679163953Srrsvoid GraphBuilder::kill_all() {
680163953Srrs  if (UseLocalValueNumbering) {
681163953Srrs    vmap()->kill_all();
682171943Srrs  }
683163953Srrs  _memory->kill();
684163953Srrs}
685163953Srrs
686163953Srrs
687163953SrrsBlockBegin* GraphBuilder::ScopeData::block_at(int bci) {
688163953Srrs  if (parsing_jsr()) {
689163953Srrs    // It is necessary to clone all blocks associated with a
690163953Srrs    // subroutine, including those for exception handlers in the scope
691163953Srrs    // of the method containing the jsr (because those exception
692163953Srrs    // handlers may contain ret instructions in some cases).
693163953Srrs    BlockBegin* block = bci2block()->at(bci);
694163953Srrs    if (block != NULL && block == parent()->bci2block()->at(bci)) {
695275427Stuexen      BlockBegin* new_block = new BlockBegin(block->bci());
696163953Srrs#ifndef PRODUCT
697163953Srrs      if (PrintInitialBlockList) {
698163953Srrs        tty->print_cr("CFG: cloned block %d (bci %d) as block %d for jsr",
699163953Srrs                      block->block_id(), block->bci(), new_block->block_id());
700163953Srrs      }
701243882Sglebius#endif
702163953Srrs      // copy data from cloned blocked
703163953Srrs      new_block->set_depth_first_number(block->depth_first_number());
704163953Srrs      if (block->is_set(BlockBegin::parser_loop_header_flag)) new_block->set(BlockBegin::parser_loop_header_flag);
705163953Srrs      // Preserve certain flags for assertion checking
706163953Srrs      if (block->is_set(BlockBegin::subroutine_entry_flag)) new_block->set(BlockBegin::subroutine_entry_flag);
707163953Srrs      if (block->is_set(BlockBegin::exception_entry_flag))  new_block->set(BlockBegin::exception_entry_flag);
708163953Srrs
709163953Srrs      // copy was_visited_flag to allow early detection of bailouts
710165647Srrs      // if a block that is used in a jsr has already been visited before,
711163953Srrs      // it is shared between the normal control flow and a subroutine
712165647Srrs      // BlockBegin::try_merge returns false when the flag is set, this leads
713163953Srrs      // to a compilation bailout
714172090Srrs      if (block->is_set(BlockBegin::was_visited_flag))  new_block->set(BlockBegin::was_visited_flag);
715163953Srrs
716163953Srrs      bci2block()->at_put(bci, new_block);
717163953Srrs      block = new_block;
718163953Srrs    }
719163953Srrs    return block;
720163953Srrs  } else {
721163953Srrs    return bci2block()->at(bci);
722283650Stuexen  }
723283650Stuexen}
724163953Srrs
725163953Srrs
726163953SrrsXHandlers* GraphBuilder::ScopeData::xhandlers() const {
727163953Srrs  if (_jsr_xhandlers == NULL) {
728163953Srrs    assert(!parsing_jsr(), "");
729163953Srrs    return scope()->xhandlers();
730163953Srrs  }
731303956Stuexen  assert(parsing_jsr(), "");
732163953Srrs  return _jsr_xhandlers;
733163953Srrs}
734166675Srrs
735166675Srrs
736163953Srrsvoid GraphBuilder::ScopeData::set_scope(IRScope* scope) {
737224641Stuexen  _scope = scope;
738224641Stuexen  bool parent_has_handler = false;
739246588Stuexen  if (parent() != NULL) {
740246588Stuexen    parent_has_handler = parent()->has_handler();
741246588Stuexen  }
742246588Stuexen  _has_handler = parent_has_handler || scope->xhandlers()->has_handlers();
743246588Stuexen}
744246588Stuexen
745246588Stuexen
746224641Stuexenvoid GraphBuilder::ScopeData::set_inline_cleanup_info(BlockBegin* block,
747224641Stuexen                                                      Instruction* return_prev,
748224641Stuexen                                                      ValueStack* return_state) {
749224641Stuexen  _cleanup_block       = block;
750224641Stuexen  _cleanup_return_prev = return_prev;
751224641Stuexen  _cleanup_state       = return_state;
752163953Srrs}
753224641Stuexen
754163953Srrs
755224641Stuexenvoid GraphBuilder::ScopeData::add_to_work_list(BlockBegin* block) {
756246588Stuexen  if (_work_list == NULL) {
757163953Srrs    _work_list = new BlockList();
758163953Srrs  }
759163953Srrs
760163953Srrs  if (!block->is_set(BlockBegin::is_on_work_list_flag)) {
761163953Srrs    // Do not start parsing the continuation block while in a
762163953Srrs    // sub-scope
763163953Srrs    if (parsing_jsr()) {
764163953Srrs      if (block == jsr_continuation()) {
765163953Srrs        return;
766163953Srrs      }
767163953Srrs    } else {
768163953Srrs      if (block == continuation()) {
769224641Stuexen        return;
770224641Stuexen      }
771224641Stuexen    }
772224641Stuexen    block->set(BlockBegin::is_on_work_list_flag);
773224641Stuexen    _work_list->push(block);
774224641Stuexen
775224641Stuexen    sort_top_into_worklist(_work_list, block);
776224641Stuexen  }
777163953Srrs}
778163953Srrs
779224641Stuexen
780303956Stuexenvoid GraphBuilder::sort_top_into_worklist(BlockList* worklist, BlockBegin* top) {
781303956Stuexen  assert(worklist->top() == top, "");
782163953Srrs  // sort block descending into work list
783163953Srrs  const int dfn = top->depth_first_number();
784163953Srrs  assert(dfn != -1, "unknown depth first number");
785163953Srrs  int i = worklist->length()-2;
786163953Srrs  while (i >= 0) {
787163953Srrs    BlockBegin* b = worklist->at(i);
788163953Srrs    if (b->depth_first_number() < dfn) {
789263237Stuexen      worklist->at_put(i+1, b);
790165220Srrs    } else {
791172090Srrs      break;
792163953Srrs    }
793163953Srrs    i --;
794163953Srrs  }
795163953Srrs  if (i >= -1) worklist->at_put(i + 1, top);
796163953Srrs}
797163953Srrs
798283650Stuexenint GraphBuilder::ScopeData::caller_stack_size() const {
799283650Stuexen  ValueStack* state = scope()->caller_state();
800163953Srrs  if (state == NULL) {
801171990Srrs    return 0;
802172090Srrs  }
803163953Srrs  return state->stack_size();
804163953Srrs}
805188067Srrs
806163953Srrs
807163953SrrsBlockBegin* GraphBuilder::ScopeData::remove_from_work_list() {
808163953Srrs  if (is_work_list_empty()) {
809163953Srrs    return NULL;
810163953Srrs  }
811163953Srrs  return _work_list->pop();
812163953Srrs}
813163953Srrs
814171943Srrs
815228907Stuexenbool GraphBuilder::ScopeData::is_work_list_empty() const {
816163953Srrs  return (_work_list == NULL || _work_list->length() == 0);
817163953Srrs}
818163953Srrs
819163953Srrs
820178202Srrsvoid GraphBuilder::ScopeData::setup_jsr_xhandlers() {
821178202Srrs  assert(parsing_jsr(), "");
822178202Srrs  // clone all the exception handlers from the scope
823178202Srrs  XHandlers* handlers = new XHandlers(scope()->xhandlers());
824178202Srrs  const int n = handlers->length();
825178202Srrs  for (int i = 0; i < n; i++) {
826178202Srrs    // The XHandlers need to be adjusted to dispatch to the cloned
827178202Srrs    // handler block instead of the default one but the synthetic
828209289Stuexen    // unlocker needs to be handled specially.  The synthetic unlocker
829209289Stuexen    // should be left alone since there can be only one and all code
830209289Stuexen    // should dispatch to the same one.
831209289Stuexen    XHandler* h = handlers->handler_at(i);
832209289Stuexen    assert(h->handler_bci() != SynchronizationEntryBCI, "must be real");
833228907Stuexen    h->set_entry_block(block_at(h->handler_bci()));
834209289Stuexen  }
835209289Stuexen  _jsr_xhandlers = handlers;
836209289Stuexen}
837209289Stuexen
838209289Stuexen
839209289Stuexenint GraphBuilder::ScopeData::num_returns() {
840209289Stuexen  if (parsing_jsr()) {
841209289Stuexen    return parent()->num_returns();
842178202Srrs  }
843178202Srrs  return _num_returns;
844178202Srrs}
845178202Srrs
846178202Srrs
847209289Stuexenvoid GraphBuilder::ScopeData::incr_num_returns() {
848209289Stuexen  if (parsing_jsr()) {
849209289Stuexen    parent()->incr_num_returns();
850209289Stuexen  } else {
851209289Stuexen    ++_num_returns;
852275427Stuexen  }
853178202Srrs}
854178202Srrs
855178202Srrs
856178202Srrs// Implementation of GraphBuilder
857178202Srrs
858178202Srrs#define INLINE_BAILOUT(msg)        { inline_bailout(msg); return false; }
859178202Srrs
860178202Srrs
861275427Stuexenvoid GraphBuilder::load_constant() {
862178202Srrs  ciConstant con = stream()->get_constant();
863178202Srrs  if (con.basic_type() == T_ILLEGAL) {
864178202Srrs    BAILOUT("could not resolve a constant");
865178202Srrs  } else {
866178202Srrs    ValueType* t = illegalType;
867178202Srrs    ValueStack* patch_state = NULL;
868178202Srrs    switch (con.basic_type()) {
869178202Srrs      case T_BOOLEAN: t = new IntConstant     (con.as_boolean()); break;
870163953Srrs      case T_BYTE   : t = new IntConstant     (con.as_byte   ()); break;
871163953Srrs      case T_CHAR   : t = new IntConstant     (con.as_char   ()); break;
872163953Srrs      case T_SHORT  : t = new IntConstant     (con.as_short  ()); break;
873163953Srrs      case T_INT    : t = new IntConstant     (con.as_int    ()); break;
874163953Srrs      case T_LONG   : t = new LongConstant    (con.as_long   ()); break;
875233005Stuexen      case T_FLOAT  : t = new FloatConstant   (con.as_float  ()); break;
876171943Srrs      case T_DOUBLE : t = new DoubleConstant  (con.as_double ()); break;
877228907Stuexen      case T_ARRAY  : t = new ArrayConstant   (con.as_object ()->as_array   ()); break;
878163953Srrs      case T_OBJECT :
879163953Srrs       {
880163953Srrs        ciObject* obj = con.as_object();
881243558Stuexen        if (obj->is_klass()) {
882243558Stuexen          ciKlass* klass = obj->as_klass();
883163953Srrs          if (!klass->is_loaded() || PatchALot) {
884204096Stuexen            patch_state = state()->copy();
885163953Srrs            t = new ObjectConstant(obj);
886204096Stuexen          } else {
887163953Srrs            t = new InstanceConstant(klass->java_mirror());
888163953Srrs          }
889171943Srrs        } else {
890163953Srrs          t = new InstanceConstant(obj->as_instance());
891291078Stuexen        }
892291078Stuexen        break;
893291078Stuexen       }
894291078Stuexen      default       : ShouldNotReachHere();
895291078Stuexen    }
896291078Stuexen    Value x;
897163953Srrs    if (patch_state != NULL) {
898163953Srrs      x = new Constant(t, patch_state);
899291078Stuexen    } else {
900163953Srrs      x = new Constant(t);
901188067Srrs    }
902188067Srrs    push(t, append(x));
903188067Srrs  }
904188067Srrs}
905188067Srrs
906163953Srrs
907163953Srrsvoid GraphBuilder::load_local(ValueType* type, int index) {
908163953Srrs  Value x = state()->load_local(index);
909163953Srrs  push(type, x);
910163953Srrs}
911291078Stuexen
912163953Srrs
913163953Srrsvoid GraphBuilder::store_local(ValueType* type, int index) {
914163953Srrs  Value x = pop(type);
915168299Srrs  store_local(state(), x, type, index);
916163953Srrs}
917163953Srrs
918163953Srrs
919163953Srrsvoid GraphBuilder::store_local(ValueStack* state, Value x, ValueType* type, int index) {
920291078Stuexen  if (parsing_jsr()) {
921291078Stuexen    // We need to do additional tracking of the location of the return
922291078Stuexen    // address for jsrs since we don't handle arbitrary jsr/ret
923291078Stuexen    // constructs. Here we are figuring out in which circumstances we
924291078Stuexen    // need to bail out.
925291078Stuexen    if (x->type()->is_address()) {
926291078Stuexen      scope_data()->set_jsr_return_address_local(index);
927291078Stuexen
928291078Stuexen      // Also check parent jsrs (if any) at this time to see whether
929291078Stuexen      // they are using this local. We don't handle skipping over a
930291078Stuexen      // ret.
931291078Stuexen      for (ScopeData* cur_scope_data = scope_data()->parent();
932291078Stuexen           cur_scope_data != NULL && cur_scope_data->parsing_jsr() && cur_scope_data->scope() == scope();
933291078Stuexen           cur_scope_data = cur_scope_data->parent()) {
934291078Stuexen        if (cur_scope_data->jsr_return_address_local() == index) {
935291078Stuexen          BAILOUT("subroutine overwrites return address from previous subroutine");
936291078Stuexen        }
937291078Stuexen      }
938291078Stuexen    } else if (index == scope_data()->jsr_return_address_local()) {
939291078Stuexen      scope_data()->set_jsr_return_address_local(-1);
940291078Stuexen    }
941291137Stuexen  }
942291137Stuexen
943163953Srrs  state->store_local(index, round_fp(x));
944163953Srrs}
945303956Stuexen
946163953Srrs
947163953Srrsvoid GraphBuilder::load_indexed(BasicType type) {
948163953Srrs  Value index = ipop();
949291137Stuexen  Value array = apop();
950291078Stuexen  Value length = NULL;
951291078Stuexen  if (CSEArrayLength ||
952291078Stuexen      (array->as_AccessField() && array->as_AccessField()->field()->is_constant()) ||
953291078Stuexen      (array->as_NewArray() && array->as_NewArray()->length() && array->as_NewArray()->length()->type()->is_constant())) {
954291078Stuexen    length = append(new ArrayLength(array, lock_stack()));
955291078Stuexen  }
956163953Srrs  push(as_ValueType(type), append(new LoadIndexed(array, index, length, type, lock_stack())));
957163953Srrs}
958291078Stuexen
959291078Stuexen
960163953Srrsvoid GraphBuilder::store_indexed(BasicType type) {
961291078Stuexen  Value value = pop(as_ValueType(type));
962303956Stuexen  Value index = ipop();
963303956Stuexen  Value array = apop();
964163953Srrs  Value length = NULL;
965163953Srrs  if (CSEArrayLength ||
966163953Srrs      (array->as_AccessField() && array->as_AccessField()->field()->is_constant()) ||
967163953Srrs      (array->as_NewArray() && array->as_NewArray()->length() && array->as_NewArray()->length()->type()->is_constant())) {
968163953Srrs    length = append(new ArrayLength(array, lock_stack()));
969163953Srrs  }
970163953Srrs  StoreIndexed* result = new StoreIndexed(array, index, length, type, value, lock_stack());
971263237Stuexen  append(result);
972165220Srrs  _memory->store_value(value);
973163953Srrs}
974172090Srrs
975291078Stuexen
976291078Stuexenvoid GraphBuilder::stack_op(Bytecodes::Code code) {
977163953Srrs  switch (code) {
978163953Srrs    case Bytecodes::_pop:
979291078Stuexen      { state()->raw_pop();
980291078Stuexen      }
981291078Stuexen      break;
982291078Stuexen    case Bytecodes::_pop2:
983291078Stuexen      { state()->raw_pop();
984291078Stuexen        state()->raw_pop();
985163953Srrs      }
986291078Stuexen      break;
987291078Stuexen    case Bytecodes::_dup:
988163953Srrs      { Value w = state()->raw_pop();
989163953Srrs        state()->raw_push(w);
990163953Srrs        state()->raw_push(w);
991163953Srrs      }
992163953Srrs      break;
993163953Srrs    case Bytecodes::_dup_x1:
994163953Srrs      { Value w1 = state()->raw_pop();
995163953Srrs        Value w2 = state()->raw_pop();
996163953Srrs        state()->raw_push(w1);
997163953Srrs        state()->raw_push(w2);
998178251Srrs        state()->raw_push(w1);
999163953Srrs      }
1000163953Srrs      break;
1001163953Srrs    case Bytecodes::_dup_x2:
1002163953Srrs      { Value w1 = state()->raw_pop();
1003178251Srrs        Value w2 = state()->raw_pop();
1004163953Srrs        Value w3 = state()->raw_pop();
1005163953Srrs        state()->raw_push(w1);
1006163953Srrs        state()->raw_push(w3);
1007163953Srrs        state()->raw_push(w2);
1008163953Srrs        state()->raw_push(w1);
1009163953Srrs      }
1010172091Srrs      break;
1011172091Srrs    case Bytecodes::_dup2:
1012172091Srrs      { Value w1 = state()->raw_pop();
1013166675Srrs        Value w2 = state()->raw_pop();
1014168124Srrs        state()->raw_push(w2);
1015163953Srrs        state()->raw_push(w1);
1016166675Srrs        state()->raw_push(w2);
1017167598Srrs        state()->raw_push(w1);
1018167598Srrs      }
1019163953Srrs      break;
1020167598Srrs    case Bytecodes::_dup2_x1:
1021167598Srrs      { Value w1 = state()->raw_pop();
1022166675Srrs        Value w2 = state()->raw_pop();
1023257555Stuexen        Value w3 = state()->raw_pop();
1024257555Stuexen        state()->raw_push(w2);
1025257555Stuexen        state()->raw_push(w1);
1026257555Stuexen        state()->raw_push(w3);
1027257555Stuexen        state()->raw_push(w2);
1028257555Stuexen        state()->raw_push(w1);
1029257555Stuexen      }
1030257555Stuexen      break;
1031257555Stuexen    case Bytecodes::_dup2_x2:
1032257555Stuexen      { Value w1 = state()->raw_pop();
1033167598Srrs        Value w2 = state()->raw_pop();
1034163953Srrs        Value w3 = state()->raw_pop();
1035163953Srrs        Value w4 = state()->raw_pop();
1036163953Srrs        state()->raw_push(w2);
1037163953Srrs        state()->raw_push(w1);
1038163953Srrs        state()->raw_push(w4);
1039163953Srrs        state()->raw_push(w3);
1040163953Srrs        state()->raw_push(w2);
1041246595Stuexen        state()->raw_push(w1);
1042257555Stuexen      }
1043246595Stuexen      break;
1044257555Stuexen    case Bytecodes::_swap:
1045257555Stuexen      { Value w1 = state()->raw_pop();
1046257555Stuexen        Value w2 = state()->raw_pop();
1047246595Stuexen        state()->raw_push(w1);
1048246595Stuexen        state()->raw_push(w2);
1049246595Stuexen      }
1050257555Stuexen      break;
1051163953Srrs    default:
1052246595Stuexen      ShouldNotReachHere();
1053246595Stuexen      break;
1054257555Stuexen  }
1055246595Stuexen}
1056257555Stuexen
1057257555Stuexen
1058246595Stuexenvoid GraphBuilder::arithmetic_op(ValueType* type, Bytecodes::Code code, ValueStack* stack) {
1059246595Stuexen  Value y = pop(type);
1060257555Stuexen  Value x = pop(type);
1061246595Stuexen  // NOTE: strictfp can be queried from current method since we don't
1062257555Stuexen  // inline methods with differing strictfp bits
1063246595Stuexen  Value res = new ArithmeticOp(code, x, y, method()->is_strict(), stack);
1064257555Stuexen  // Note: currently single-precision floating-point rounding on Intel is handled at the LIRGenerator level
1065257555Stuexen  res = append(res);
1066246595Stuexen  if (method()->is_strict()) {
1067246595Stuexen    res = round_fp(res);
1068246595Stuexen  }
1069246595Stuexen  push(type, res);
1070246595Stuexen}
1071257555Stuexen
1072246595Stuexen
1073257555Stuexenvoid GraphBuilder::negate_op(ValueType* type) {
1074246595Stuexen  push(type, append(new NegateOp(pop(type))));
1075257555Stuexen}
1076257555Stuexen
1077163953Srrs
1078257555Stuexenvoid GraphBuilder::shift_op(ValueType* type, Bytecodes::Code code) {
1079163953Srrs  Value s = ipop();
1080163953Srrs  Value x = pop(type);
1081167598Srrs  // try to simplify
1082167598Srrs  // Note: This code should go into the canonicalizer as soon as it can
1083167598Srrs  //       can handle canonicalized forms that contain more than one node.
1084167598Srrs  if (CanonicalizeNodes && code == Bytecodes::_iushr) {
1085163953Srrs    // pattern: x >>> s
1086167598Srrs    IntConstant* s1 = s->type()->as_IntConstant();
1087163953Srrs    if (s1 != NULL) {
1088167598Srrs      // pattern: x >>> s1, with s1 constant
1089163953Srrs      ShiftOp* l = x->as_ShiftOp();
1090163953Srrs      if (l != NULL && l->op() == Bytecodes::_ishl) {
1091163953Srrs        // pattern: (a << b) >>> s1
1092167598Srrs        IntConstant* s0 = l->y()->type()->as_IntConstant();
1093163953Srrs        if (s0 != NULL) {
1094163953Srrs          // pattern: (a << s0) >>> s1
1095163953Srrs          const int s0c = s0->value() & 0x1F; // only the low 5 bits are significant for shifts
1096163953Srrs          const int s1c = s1->value() & 0x1F; // only the low 5 bits are significant for shifts
1097163953Srrs          if (s0c == s1c) {
1098163953Srrs            if (s0c == 0) {
1099163953Srrs              // pattern: (a << 0) >>> 0 => simplify to: a
1100163953Srrs              ipush(l->x());
1101163953Srrs            } else {
1102163953Srrs              // pattern: (a << s0c) >>> s0c => simplify to: a & m, with m constant
1103167598Srrs              assert(0 < s0c && s0c < BitsPerInt, "adjust code below to handle corner cases");
1104163953Srrs              const int m = (1 << (BitsPerInt - s0c)) - 1;
1105163953Srrs              Value s = append(new Constant(new IntConstant(m)));
1106163953Srrs              ipush(append(new LogicOp(Bytecodes::_iand, l->x(), s)));
1107178251Srrs            }
1108221249Stuexen            return;
1109178251Srrs          }
1110178251Srrs        }
1111178251Srrs      }
1112163953Srrs    }
1113271221Stuexen  }
1114178251Srrs  // could not simplify
1115178251Srrs  push(type, append(new ShiftOp(code, x, s)));
1116178251Srrs}
1117178251Srrs
1118178251Srrs
1119178251Srrsvoid GraphBuilder::logic_op(ValueType* type, Bytecodes::Code code) {
1120178251Srrs  Value y = pop(type);
1121178251Srrs  Value x = pop(type);
1122267674Stuexen  push(type, append(new LogicOp(code, x, y)));
1123267674Stuexen}
1124267674Stuexen
1125267674Stuexen
1126178251Srrsvoid GraphBuilder::compare_op(ValueType* type, Bytecodes::Code code) {
1127178251Srrs  ValueStack* state_before = state()->copy();
1128178251Srrs  Value y = pop(type);
1129178251Srrs  Value x = pop(type);
1130178251Srrs  ipush(append(new CompareOp(code, x, y, state_before)));
1131178251Srrs}
1132178251Srrs
1133178251Srrs
1134178251Srrsvoid GraphBuilder::convert(Bytecodes::Code op, BasicType from, BasicType to) {
1135178251Srrs  push(as_ValueType(to), append(new Convert(op, pop(as_ValueType(from)), as_ValueType(to))));
1136178251Srrs}
1137178251Srrs
1138178251Srrs
1139178251Srrsvoid GraphBuilder::increment() {
1140178251Srrs  int index = stream()->get_index();
1141178251Srrs  int delta = stream()->is_wide() ? (signed short)Bytes::get_Java_u2(stream()->cur_bcp() + 4) : (signed char)(stream()->cur_bcp()[2]);
1142178251Srrs  load_local(intType, index);
1143178251Srrs  ipush(append(new Constant(new IntConstant(delta))));
1144178251Srrs  arithmetic_op(intType, Bytecodes::_iadd);
1145178251Srrs  store_local(intType, index);
1146178251Srrs}
1147178251Srrs
1148178251Srrs
1149163953Srrsvoid GraphBuilder::_goto(int from_bci, int to_bci) {
1150163953Srrs  profile_bci(from_bci);
1151178251Srrs  append(new Goto(block_at(to_bci), to_bci <= from_bci));
1152221249Stuexen}
1153178251Srrs
1154178251Srrs
1155178251Srrsvoid GraphBuilder::if_node(Value x, If::Condition cond, Value y, ValueStack* state_before) {
1156178251Srrs  BlockBegin* tsux = block_at(stream()->get_dest());
1157163953Srrs  BlockBegin* fsux = block_at(stream()->next_bci());
1158271221Stuexen  bool is_bb = tsux->bci() < stream()->cur_bci() || fsux->bci() < stream()->cur_bci();
1159178251Srrs  If* if_node = append(new If(x, cond, false, y, tsux, fsux, is_bb ? state_before : NULL, is_bb))->as_If();
1160178251Srrs  if (profile_branches() && (if_node != NULL)) {
1161178251Srrs    if_node->set_profiled_method(method());
1162178251Srrs    if_node->set_profiled_bci(bci());
1163178251Srrs    if_node->set_should_profile(true);
1164178251Srrs  }
1165163953Srrs}
1166178251Srrs
1167267674Stuexen
1168267674Stuexenvoid GraphBuilder::if_zero(ValueType* type, If::Condition cond) {
1169267674Stuexen  Value y = append(new Constant(intZero));
1170267674Stuexen  ValueStack* state_before = state()->copy();
1171178251Srrs  Value x = ipop();
1172178251Srrs  if_node(x, cond, y, state_before);
1173163953Srrs}
1174178251Srrs
1175178251Srrs
1176178251Srrsvoid GraphBuilder::if_null(ValueType* type, If::Condition cond) {
1177178251Srrs  Value y = append(new Constant(objectNull));
1178178251Srrs  ValueStack* state_before = state()->copy();
1179178251Srrs  Value x = apop();
1180178251Srrs  if_node(x, cond, y, state_before);
1181178251Srrs}
1182178251Srrs
1183178251Srrs
1184178251Srrsvoid GraphBuilder::if_same(ValueType* type, If::Condition cond) {
1185178251Srrs  ValueStack* state_before = state()->copy();
1186178251Srrs  Value y = pop(type);
1187178251Srrs  Value x = pop(type);
1188178251Srrs  if_node(x, cond, y, state_before);
1189178251Srrs}
1190178251Srrs
1191178251Srrs
1192163953Srrsvoid GraphBuilder::jsr(int dest) {
1193178251Srrs  // We only handle well-formed jsrs (those which are "block-structured").
1194178251Srrs  // If the bytecodes are strange (jumping out of a jsr block) then we
1195178251Srrs  // might end up trying to re-parse a block containing a jsr which
1196178251Srrs  // has already been activated. Watch for this case and bail out.
1197178251Srrs  for (ScopeData* cur_scope_data = scope_data();
1198178251Srrs       cur_scope_data != NULL && cur_scope_data->parsing_jsr() && cur_scope_data->scope() == scope();
1199178251Srrs       cur_scope_data = cur_scope_data->parent()) {
1200178251Srrs    if (cur_scope_data->jsr_entry_bci() == dest) {
1201178251Srrs      BAILOUT("too-complicated jsr/ret structure");
1202178251Srrs    }
1203178251Srrs  }
1204178251Srrs
1205163953Srrs  push(addressType, append(new Constant(new AddressConstant(next_bci()))));
1206163953Srrs  if (!try_inline_jsr(dest)) {
1207178251Srrs    return; // bailed out while parsing and inlining subroutine
1208178251Srrs  }
1209178251Srrs}
1210178251Srrs
1211178251Srrs
1212163953Srrsvoid GraphBuilder::ret(int local_index) {
1213163953Srrs  if (!parsing_jsr()) BAILOUT("ret encountered while not parsing subroutine");
1214163953Srrs
1215163953Srrs  if (local_index != scope_data()->jsr_return_address_local()) {
1216163953Srrs    BAILOUT("can not handle complicated jsr/ret constructs");
1217163953Srrs  }
1218167598Srrs
1219167598Srrs  // Rets simply become (NON-SAFEPOINT) gotos to the jsr continuation
1220167598Srrs  append(new Goto(scope_data()->jsr_continuation(), false));
1221163953Srrs}
1222163953Srrs
1223163953Srrs
1224167598Srrsvoid GraphBuilder::table_switch() {
1225167598Srrs  Bytecode_tableswitch* switch_ = Bytecode_tableswitch_at(method()->code() + bci());
1226246595Stuexen  const int l = switch_->length();
1227246595Stuexen  if (CanonicalizeNodes && l == 1) {
1228246595Stuexen    // total of 2 successors => use If instead of switch
1229246595Stuexen    // Note: This code should go into the canonicalizer as soon as it can
1230246595Stuexen    //       can handle canonicalized forms that contain more than one node.
1231246595Stuexen    Value key = append(new Constant(new IntConstant(switch_->low_key())));
1232246595Stuexen    BlockBegin* tsux = block_at(bci() + switch_->dest_offset_at(0));
1233246595Stuexen    BlockBegin* fsux = block_at(bci() + switch_->default_offset());
1234246595Stuexen    bool is_bb = tsux->bci() < bci() || fsux->bci() < bci();
1235246595Stuexen    ValueStack* state_before = is_bb ? state() : NULL;
1236246595Stuexen    append(new If(ipop(), If::eql, true, key, tsux, fsux, state_before, is_bb));
1237246595Stuexen  } else {
1238246595Stuexen    // collect successors
1239246595Stuexen    BlockList* sux = new BlockList(l + 1, NULL);
1240246595Stuexen    int i;
1241167598Srrs    bool has_bb = false;
1242167598Srrs    for (i = 0; i < l; i++) {
1243167598Srrs      sux->at_put(i, block_at(bci() + switch_->dest_offset_at(i)));
1244167598Srrs      if (switch_->dest_offset_at(i) < 0) has_bb = true;
1245167598Srrs    }
1246163953Srrs    // add default successor
1247163953Srrs    sux->at_put(i, block_at(bci() + switch_->default_offset()));
1248163953Srrs    ValueStack* state_before = has_bb ? state() : NULL;
1249163953Srrs    append(new TableSwitch(ipop(), sux, switch_->low_key(), state_before, has_bb));
1250163953Srrs  }
1251163953Srrs}
1252168124Srrs
1253168124Srrs
1254168124Srrsvoid GraphBuilder::lookup_switch() {
1255168124Srrs  Bytecode_lookupswitch* switch_ = Bytecode_lookupswitch_at(method()->code() + bci());
1256168124Srrs  const int l = switch_->number_of_pairs();
1257168124Srrs  if (CanonicalizeNodes && l == 1) {
1258168124Srrs    // total of 2 successors => use If instead of switch
1259168124Srrs    // Note: This code should go into the canonicalizer as soon as it can
1260172218Srrs    //       can handle canonicalized forms that contain more than one node.
1261168124Srrs    // simplify to If
1262168124Srrs    LookupswitchPair* pair = switch_->pair_at(0);
1263168124Srrs    Value key = append(new Constant(new IntConstant(pair->match())));
1264172218Srrs    BlockBegin* tsux = block_at(bci() + pair->offset());
1265168124Srrs    BlockBegin* fsux = block_at(bci() + switch_->default_offset());
1266168124Srrs    bool is_bb = tsux->bci() < bci() || fsux->bci() < bci();
1267168124Srrs    ValueStack* state_before = is_bb ? state() : NULL;
1268172091Srrs    append(new If(ipop(), If::eql, true, key, tsux, fsux, state_before, is_bb));
1269172091Srrs  } else {
1270172091Srrs    // collect successors & keys
1271163953Srrs    BlockList* sux = new BlockList(l + 1, NULL);
1272168124Srrs    intArray* keys = new intArray(l, 0);
1273163953Srrs    int i;
1274163953Srrs    bool has_bb = false;
1275167598Srrs    for (i = 0; i < l; i++) {
1276163953Srrs      LookupswitchPair* pair = switch_->pair_at(i);
1277163953Srrs      if (pair->offset() < 0) has_bb = true;
1278163953Srrs      sux->at_put(i, block_at(bci() + pair->offset()));
1279163953Srrs      keys->at_put(i, pair->match());
1280163953Srrs    }
1281163953Srrs    // add default successor
1282163953Srrs    sux->at_put(i, block_at(bci() + switch_->default_offset()));
1283163953Srrs    ValueStack* state_before = has_bb ? state() : NULL;
1284167598Srrs    append(new LookupSwitch(ipop(), sux, keys, state_before, has_bb));
1285167598Srrs  }
1286167598Srrs}
1287167598Srrs
1288163953Srrsvoid GraphBuilder::call_register_finalizer() {
1289167598Srrs  // If the receiver requires finalization then emit code to perform
1290167598Srrs  // the registration on return.
1291163953Srrs
1292167598Srrs  // Gather some type information about the receiver
1293167598Srrs  Value receiver = state()->load_local(0);
1294163953Srrs  assert(receiver != NULL, "must have a receiver");
1295221249Stuexen  ciType* declared_type = receiver->declared_type();
1296221249Stuexen  ciType* exact_type = receiver->exact_type();
1297221249Stuexen  if (exact_type == NULL &&
1298275567Stuexen      receiver->as_Local() &&
1299178251Srrs      receiver->as_Local()->java_index() == 0) {
1300163953Srrs    ciInstanceKlass* ik = compilation()->method()->holder();
1301163953Srrs    if (ik->is_final()) {
1302163953Srrs      exact_type = ik;
1303275567Stuexen    } else if (UseCHA && !(ik->has_subklass() || ik->is_interface())) {
1304275567Stuexen      // test class is leaf class
1305275567Stuexen      compilation()->dependency_recorder()->assert_leaf_type(ik);
1306221249Stuexen      exact_type = ik;
1307221249Stuexen    } else {
1308221249Stuexen      declared_type = ik;
1309221249Stuexen    }
1310163953Srrs  }
1311221249Stuexen
1312221249Stuexen  // see if we know statically that registration isn't required
1313221249Stuexen  bool needs_check = true;
1314221249Stuexen  if (exact_type != NULL) {
1315221249Stuexen    needs_check = exact_type->as_instance_klass()->has_finalizer();
1316163953Srrs  } else if (declared_type != NULL) {
1317163953Srrs    ciInstanceKlass* ik = declared_type->as_instance_klass();
1318163953Srrs    if (!Dependencies::has_finalizable_subclass(ik)) {
1319163953Srrs      compilation()->dependency_recorder()->assert_has_no_finalizable_subclasses(ik);
1320163953Srrs      needs_check = false;
1321163953Srrs    }
1322221249Stuexen  }
1323221249Stuexen
1324221249Stuexen  if (needs_check) {
1325275567Stuexen    // Perform the registration of finalizable objects.
1326178251Srrs    load_local(objectType, 0);
1327163953Srrs    append_split(new Intrinsic(voidType, vmIntrinsics::_Object_init,
1328163953Srrs                               state()->pop_arguments(1),
1329163953Srrs                               true, lock_stack(), true));
1330275567Stuexen  }
1331275567Stuexen}
1332275567Stuexen
1333221249Stuexen
1334221249Stuexenvoid GraphBuilder::method_return(Value x) {
1335221249Stuexen  if (RegisterFinalizersAtInit &&
1336221249Stuexen      method()->intrinsic_id() == vmIntrinsics::_Object_init) {
1337163953Srrs    call_register_finalizer();
1338221249Stuexen  }
1339221249Stuexen
1340221249Stuexen  // Check to see whether we are inlining. If so, Return
1341221249Stuexen  // instructions become Gotos to the continuation point.
1342221249Stuexen  if (continuation() != NULL) {
1343163953Srrs    assert(!method()->is_synchronized() || InlineSynchronizedMethods, "can not inline synchronized methods yet");
1344163953Srrs
1345163953Srrs    // If the inlined method is synchronized, the monitor must be
1346163953Srrs    // released before we jump to the continuation block.
1347163953Srrs    if (method()->is_synchronized()) {
1348168124Srrs      int i = state()->caller_state()->locks_size();
1349168124Srrs      assert(state()->locks_size() == i + 1, "receiver must be locked here");
1350168124Srrs      monitorexit(state()->lock_at(i), SynchronizationEntryBCI);
1351168124Srrs    }
1352166675Srrs
1353172218Srrs    state()->truncate_stack(caller_stack_size());
1354168124Srrs    if (x != NULL) {
1355168124Srrs      state()->push(x->type(), x);
1356172218Srrs    }
1357168124Srrs    Goto* goto_callee = new Goto(continuation(), false);
1358168124Srrs
1359168124Srrs    // See whether this is the first return; if so, store off some
1360163953Srrs    // of the state for later examination
1361166675Srrs    if (num_returns() == 0) {
1362166675Srrs      set_inline_cleanup_info(_block, _last, state());
1363163953Srrs    }
1364163953Srrs
1365163953Srrs    // State at end of inlined method is the state of the caller
1366163953Srrs    // without the method parameters on stack, including the
1367163953Srrs    // return value, if any, of the inlined method on operand stack.
1368297312Stuexen    set_state(scope_data()->continuation_state()->copy());
1369167598Srrs    if (x) {
1370170056Srrs      state()->push(x->type(), x);
1371167598Srrs    }
1372163953Srrs
1373169420Srrs    // The current bci() is in the wrong scope, so use the bci() of
1374163953Srrs    // the continuation point.
1375163953Srrs    append_with_bci(goto_callee, scope_data()->continuation()->bci());
1376163953Srrs    incr_num_returns();
1377163953Srrs
1378171943Srrs    return;
1379163953Srrs  }
1380163953Srrs
1381181054Srrs  state()->truncate_stack(0);
1382181054Srrs  if (method()->is_synchronized()) {
1383171943Srrs    // perform the unlocking before exiting the method
1384163953Srrs    Value receiver;
1385163953Srrs    if (!method()->is_static()) {
1386163953Srrs      receiver = _initial_state->local_at(0);
1387163953Srrs    } else {
1388163953Srrs      receiver = append(new Constant(new ClassConstant(method()->holder())));
1389163953Srrs    }
1390163953Srrs    append_split(new MonitorExit(receiver, state()->unlock()));
1391163953Srrs  }
1392171943Srrs
1393163953Srrs  append(new Return(x));
1394163953Srrs}
1395163953Srrs
1396163953Srrs
1397163953Srrsvoid GraphBuilder::access_field(Bytecodes::Code code) {
1398163953Srrs  bool will_link;
1399163953Srrs  ciField* field = stream()->get_field(will_link);
1400171943Srrs  ciInstanceKlass* holder = field->holder();
1401163953Srrs  BasicType field_type = field->type()->basic_type();
1402163953Srrs  ValueType* type = as_ValueType(field_type);
1403163953Srrs  // call will_link again to determine if the field is valid.
1404297312Stuexen  const bool is_loaded = holder->is_loaded() &&
1405163953Srrs                         field->will_link(method()->holder(), code);
1406163953Srrs  const bool is_initialized = is_loaded && holder->is_initialized();
1407297312Stuexen
1408170056Srrs  ValueStack* state_copy = NULL;
1409169352Srrs  if (!is_initialized || PatchALot) {
1410169352Srrs    // save state before instruction for debug info when
1411169352Srrs    // deoptimization happens during patching
1412170931Srrs    state_copy = state()->copy();
1413170931Srrs  }
1414171943Srrs
1415171943Srrs  Value obj = NULL;
1416170056Srrs  if (code == Bytecodes::_getstatic || code == Bytecodes::_putstatic) {
1417171943Srrs    // commoning of class constants should only occur if the class is
1418169352Srrs    // fully initialized and resolved in this constant pool.  The will_link test
1419163953Srrs    // above essentially checks if this class is resolved in this constant pool
1420163953Srrs    // so, the is_initialized flag should be suffiect.
1421163953Srrs    if (state_copy != NULL) {
1422163953Srrs      // build a patching constant
1423163953Srrs      obj = new Constant(new ClassConstant(holder), state_copy);
1424163953Srrs    } else {
1425163953Srrs      obj = new Constant(new ClassConstant(holder));
1426163953Srrs    }
1427163953Srrs  }
1428163953Srrs
1429163953Srrs
1430163953Srrs  const int offset = is_loaded ? field->offset() : -1;
1431166023Srrs  switch (code) {
1432163953Srrs    case Bytecodes::_getstatic: {
1433163953Srrs      // check for compile-time constants, i.e., initialized static final fields
1434163953Srrs      Instruction* constant = NULL;
1435163953Srrs      if (field->is_constant() && !PatchALot) {
1436171943Srrs        ciConstant field_val = field->constant_value();
1437163953Srrs        BasicType field_type = field_val.basic_type();
1438163953Srrs        switch (field_type) {
1439163953Srrs        case T_ARRAY:
1440163953Srrs        case T_OBJECT:
1441163953Srrs          if (field_val.as_object()->should_be_constant()) {
1442163953Srrs            constant =  new Constant(as_ValueType(field_val));
1443163953Srrs          }
1444163953Srrs          break;
1445171572Srrs
1446163953Srrs        default:
1447163953Srrs          constant = new Constant(as_ValueType(field_val));
1448163953Srrs        }
1449163953Srrs      }
1450167695Srrs      if (constant != NULL) {
1451167695Srrs        push(type, append(constant));
1452167695Srrs        state_copy = NULL; // Not a potential deoptimization point (see set_state_before logic below)
1453181054Srrs      } else {
1454163953Srrs        push(type, append(new LoadField(append(obj), offset, field, true,
1455206137Stuexen                                        lock_stack(), state_copy, is_loaded, is_initialized)));
1456291904Stuexen      }
1457298902Stuexen      break;
1458171531Srrs    }
1459171531Srrs    case Bytecodes::_putstatic:
1460163953Srrs      { Value val = pop(type);
1461163953Srrs        append(new StoreField(append(obj), offset, field, val, true, lock_stack(), state_copy, is_loaded, is_initialized));
1462163953Srrs      }
1463163953Srrs      break;
1464225559Stuexen    case Bytecodes::_getfield :
1465225559Stuexen      {
1466225559Stuexen        LoadField* load = new LoadField(apop(), offset, field, false, lock_stack(), state_copy, is_loaded, true);
1467225559Stuexen        Value replacement = is_loaded ? _memory->load(load) : load;
1468225559Stuexen        if (replacement != load) {
1469171943Srrs          assert(replacement->bci() != -99 || replacement->as_Phi() || replacement->as_Local(),
1470163953Srrs                 "should already by linked");
1471221249Stuexen          push(type, replacement);
1472221249Stuexen        } else {
1473221249Stuexen          push(type, append(load));
1474163953Srrs        }
1475221249Stuexen        break;
1476221249Stuexen      }
1477221249Stuexen
1478221249Stuexen    case Bytecodes::_putfield :
1479163953Srrs      { Value val = pop(type);
1480221249Stuexen        StoreField* store = new StoreField(apop(), offset, field, val, false, lock_stack(), state_copy, is_loaded, true);
1481221249Stuexen        if (is_loaded) store = _memory->store(store);
1482221249Stuexen        if (store != NULL) {
1483221249Stuexen          append(store);
1484221249Stuexen        }
1485163953Srrs      }
1486170056Srrs      break;
1487223132Stuexen    default                   :
1488167598Srrs      ShouldNotReachHere();
1489170056Srrs      break;
1490283650Stuexen  }
1491283650Stuexen}
1492170056Srrs
1493170056Srrs
1494167598SrrsDependencies* GraphBuilder::dependency_recorder() const {
1495167598Srrs  assert(DeoptC1, "need debug information");
1496163953Srrs  return compilation()->dependency_recorder();
1497163953Srrs}
1498163953Srrs
1499163953Srrs
1500163953Srrsvoid GraphBuilder::invoke(Bytecodes::Code code) {
1501163953Srrs  bool will_link;
1502163953Srrs  ciMethod* target = stream()->get_method(will_link);
1503163953Srrs  // we have to make sure the argument size (incl. the receiver)
1504163953Srrs  // is correct for compilation (the call would fail later during
1505169378Srrs  // linkage anyway) - was bug (gri 7/28/99)
1506172090Srrs  if (target->is_loaded() && target->is_static() != (code == Bytecodes::_invokestatic)) BAILOUT("will cause link error");
1507163953Srrs  ciInstanceKlass* klass = target->holder();
1508163953Srrs
1509163953Srrs  // check if CHA possible: if so, change the code to invoke_special
1510163953Srrs  ciInstanceKlass* calling_klass = method()->holder();
1511163953Srrs  ciKlass* holder = stream()->get_declared_method_holder();
1512163953Srrs  ciInstanceKlass* callee_holder = ciEnv::get_instance_klass_for_declared_method_holder(holder);
1513163953Srrs  ciInstanceKlass* actual_recv = callee_holder;
1514163953Srrs
1515169655Srrs  // some methods are obviously bindable without any type checks so
1516163953Srrs  // convert them directly to an invokespecial.
1517169655Srrs  if (target->is_loaded() && !target->is_abstract() &&
1518163953Srrs      target->can_be_statically_bound() && code == Bytecodes::_invokevirtual) {
1519228907Stuexen    code = Bytecodes::_invokespecial;
1520163953Srrs  }
1521163953Srrs
1522169420Srrs  // NEEDS_CLEANUP
1523169655Srrs  // I've added the target-is_loaded() test below but I don't really understand
1524169655Srrs  // how klass->is_loaded() can be true and yet target->is_loaded() is false.
1525166675Srrs  // this happened while running the JCK invokevirtual tests under doit.  TKR
1526166675Srrs  ciMethod* cha_monomorphic_target = NULL;
1527169655Srrs  ciMethod* exact_target = NULL;
1528166675Srrs  if (UseCHA && DeoptC1 && klass->is_loaded() && target->is_loaded() &&
1529169655Srrs      !target->is_method_handle_invoke()) {
1530166675Srrs    Value receiver = NULL;
1531223132Stuexen    ciInstanceKlass* receiver_klass = NULL;
1532166675Srrs    bool type_is_exact = false;
1533166675Srrs    // try to find a precise receiver type
1534171943Srrs    if (will_link && !target->is_static()) {
1535166675Srrs      int index = state()->stack_size() - (target->arg_size_no_receiver() + 1);
1536166675Srrs      receiver = state()->stack_at(index);
1537166675Srrs      ciType* type = receiver->exact_type();
1538166675Srrs      if (type != NULL && type->is_loaded() &&
1539166675Srrs          type->is_instance_klass() && !type->as_instance_klass()->is_interface()) {
1540169420Srrs        receiver_klass = (ciInstanceKlass*) type;
1541169420Srrs        type_is_exact = true;
1542163953Srrs      }
1543169420Srrs      if (type == NULL) {
1544234464Stuexen        type = receiver->declared_type();
1545166675Srrs        if (type != NULL && type->is_loaded() &&
1546171943Srrs            type->is_instance_klass() && !type->as_instance_klass()->is_interface()) {
1547166675Srrs          receiver_klass = (ciInstanceKlass*) type;
1548166675Srrs          if (receiver_klass->is_leaf_type() && !receiver_klass->is_final()) {
1549166675Srrs            // Insert a dependency on this type since
1550166675Srrs            // find_monomorphic_target may assume it's already done.
1551169420Srrs            dependency_recorder()->assert_leaf_type(receiver_klass);
1552169420Srrs            type_is_exact = true;
1553163953Srrs          }
1554163953Srrs        }
1555166675Srrs      }
1556166675Srrs    }
1557163953Srrs    if (receiver_klass != NULL && type_is_exact &&
1558171943Srrs        receiver_klass->is_loaded() && code != Bytecodes::_invokespecial) {
1559166675Srrs      // If we have the exact receiver type we can bind directly to
1560163953Srrs      // the method to call.
1561163953Srrs      exact_target = target->resolve_invoke(calling_klass, receiver_klass);
1562166675Srrs      if (exact_target != NULL) {
1563171943Srrs        target = exact_target;
1564166675Srrs        code = Bytecodes::_invokespecial;
1565166675Srrs      }
1566163953Srrs    }
1567233005Stuexen    if (receiver_klass != NULL &&
1568171943Srrs        receiver_klass->is_subtype_of(actual_recv) &&
1569163953Srrs        actual_recv->is_initialized()) {
1570171943Srrs      actual_recv = receiver_klass;
1571163953Srrs    }
1572163953Srrs
1573166675Srrs    if ((code == Bytecodes::_invokevirtual && callee_holder->is_initialized()) ||
1574163953Srrs        (code == Bytecodes::_invokeinterface && callee_holder->is_initialized() && !actual_recv->is_interface())) {
1575163953Srrs      // Use CHA on the receiver to select a more precise method.
1576163953Srrs      cha_monomorphic_target = target->find_monomorphic_target(calling_klass, callee_holder, actual_recv);
1577163953Srrs    } else if (code == Bytecodes::_invokeinterface && callee_holder->is_loaded() && receiver != NULL) {
1578163953Srrs      // if there is only one implementor of this interface then we
1579163953Srrs      // may be able bind this invoke directly to the implementing
1580163953Srrs      // klass but we need both a dependence on the single interface
1581163953Srrs      // and on the method we bind to.  Additionally since all we know
1582166675Srrs      // about the receiver type is the it's supposed to implement the
1583163953Srrs      // interface we have to insert a check that it's the class we
1584166675Srrs      // expect.  Interface types are not checked by the verifier so
1585163953Srrs      // they are roughly equivalent to Object.
1586163953Srrs      ciInstanceKlass* singleton = NULL;
1587166675Srrs      if (target->holder()->nof_implementors() == 1) {
1588163953Srrs        singleton = target->holder()->implementor(0);
1589163953Srrs      }
1590171943Srrs      if (singleton) {
1591171943Srrs        cha_monomorphic_target = target->find_monomorphic_target(calling_klass, target->holder(), singleton);
1592171943Srrs        if (cha_monomorphic_target != NULL) {
1593171943Srrs          // If CHA is able to bind this invoke then update the class
1594171943Srrs          // to match that class, otherwise klass will refer to the
1595171943Srrs          // interface.
1596171943Srrs          klass = cha_monomorphic_target->holder();
1597171943Srrs          actual_recv = target->holder();
1598163953Srrs
1599163953Srrs          // insert a check it's really the expected class.
1600166675Srrs          CheckCast* c = new CheckCast(klass, receiver, NULL);
1601163953Srrs          c->set_incompatible_class_change_check();
1602163953Srrs          c->set_direct_compare(klass->is_final());
1603166675Srrs          append_split(c);
1604163953Srrs        }
1605163953Srrs      }
1606166675Srrs    }
1607163953Srrs  }
1608163953Srrs
1609163953Srrs  if (cha_monomorphic_target != NULL) {
1610166675Srrs    if (cha_monomorphic_target->is_abstract()) {
1611163953Srrs      // Do not optimize for abstract methods
1612166675Srrs      cha_monomorphic_target = NULL;
1613163953Srrs    }
1614163953Srrs  }
1615163953Srrs
1616171943Srrs  if (cha_monomorphic_target != NULL) {
1617163953Srrs    if (!(target->is_final_method())) {
1618163953Srrs      // If we inlined because CHA revealed only a single target method,
1619166675Srrs      // then we are dependent on that target method not getting overridden
1620171943Srrs      // by dynamic class loading.  Be sure to test the "static" receiver
1621163953Srrs      // dest_method here, as opposed to the actual receiver, which may
1622163953Srrs      // falsely lead us to believe that the receiver is final or private.
1623171943Srrs      dependency_recorder()->assert_unique_concrete_method(actual_recv, cha_monomorphic_target);
1624163953Srrs    }
1625163953Srrs    code = Bytecodes::_invokespecial;
1626163953Srrs  }
1627166675Srrs  // check if we could do inlining
1628166675Srrs  if (!PatchALot && Inline && klass->is_loaded() &&
1629163953Srrs      (klass->is_initialized() || klass->is_interface() && target->holder()->is_initialized())
1630163953Srrs      && target->will_link(klass, callee_holder, code)) {
1631170091Srrs    // callee is known => check if we have static binding
1632170091Srrs    assert(target->is_loaded(), "callee must be known");
1633170091Srrs    if (code == Bytecodes::_invokestatic
1634170091Srrs     || code == Bytecodes::_invokespecial
1635170091Srrs     || code == Bytecodes::_invokevirtual && target->is_final_method()
1636167598Srrs    ) {
1637170091Srrs      // static binding => check if callee is ok
1638170091Srrs      ciMethod* inline_target = (cha_monomorphic_target != NULL)
1639170091Srrs                                  ? cha_monomorphic_target
1640170091Srrs                                  : target;
1641171943Srrs      bool res = try_inline(inline_target, (cha_monomorphic_target != NULL) || (exact_target != NULL));
1642170091Srrs      CHECK_BAILOUT();
1643170091Srrs
1644170091Srrs#ifndef PRODUCT
1645170091Srrs      // printing
1646181054Srrs      if (PrintInlining && !res) {
1647181054Srrs        // if it was successfully inlined, then it was already printed.
1648181054Srrs        print_inline_result(inline_target, res);
1649181054Srrs      }
1650181054Srrs#endif
1651181054Srrs      clear_inline_bailout();
1652181054Srrs      if (res) {
1653181054Srrs        // Register dependence if JVMTI has either breakpoint
1654181054Srrs        // setting or hotswapping of methods capabilities since they may
1655181054Srrs        // cause deoptimization.
1656181054Srrs        if (compilation()->env()->jvmti_can_hotswap_or_post_breakpoint()) {
1657181054Srrs          dependency_recorder()->assert_evol_method(inline_target);
1658223132Stuexen        }
1659181054Srrs        return;
1660163953Srrs      }
1661163953Srrs    }
1662166675Srrs  }
1663166675Srrs  // If we attempted an inline which did not succeed because of a
1664166675Srrs  // bailout during construction of the callee graph, the entire
1665166675Srrs  // compilation has to be aborted. This is fairly rare and currently
1666166675Srrs  // seems to only occur for jasm-generated classes which contain
1667223132Stuexen  // jsr/ret pairs which are not associated with finally clauses and
1668163953Srrs  // do not have exception handlers in the containing method, and are
1669163953Srrs  // therefore not caught early enough to abort the inlining without
1670163953Srrs  // corrupting the graph. (We currently bail out with a non-empty
1671166675Srrs  // stack at a ret in these situations.)
1672166675Srrs  CHECK_BAILOUT();
1673166675Srrs
1674168943Srrs  // inlining not successful => standard invoke
1675168943Srrs  bool is_loaded = target->is_loaded();
1676168943Srrs  bool has_receiver =
1677168943Srrs    code == Bytecodes::_invokespecial   ||
1678168943Srrs    code == Bytecodes::_invokevirtual   ||
1679168943Srrs    code == Bytecodes::_invokeinterface;
1680168943Srrs  bool is_invokedynamic = code == Bytecodes::_invokedynamic;
1681168943Srrs  ValueType* result_type = as_ValueType(target->return_type());
1682168943Srrs
1683166675Srrs  // We require the debug info to be the "state before" because
1684223132Stuexen  // invokedynamics may deoptimize.
1685163953Srrs  ValueStack* state_before = is_invokedynamic ? state()->copy() : NULL;
1686297662Srrs
1687297662Srrs  Values* args = state()->pop_arguments(target->arg_size_no_receiver());
1688297662Srrs  Value recv = has_receiver ? apop() : NULL;
1689297662Srrs  int vtable_index = methodOopDesc::invalid_vtable_index;
1690297662Srrs
1691297662Srrs#ifdef SPARC
1692297662Srrs  // Currently only supported on Sparc.
1693297662Srrs  // The UseInlineCaches only controls dispatch to invokevirtuals for
1694297662Srrs  // loaded classes which we weren't able to statically bind.
1695297662Srrs  if (!UseInlineCaches && is_loaded && code == Bytecodes::_invokevirtual
1696297662Srrs      && !target->can_be_statically_bound()) {
1697297662Srrs    // Find a vtable index if one is available
1698297662Srrs    vtable_index = target->resolve_vtable_index(calling_klass, callee_holder);
1699297662Srrs  }
1700297662Srrs#endif
1701297662Srrs
1702297662Srrs  if (recv != NULL &&
1703297662Srrs      (code == Bytecodes::_invokespecial ||
1704297662Srrs       !is_loaded || target->is_final() ||
1705297662Srrs       profile_calls())) {
1706297662Srrs    // invokespecial always needs a NULL check.  invokevirtual where
1707297662Srrs    // the target is final or where it's not known that whether the
1708297662Srrs    // target is final requires a NULL check.  Otherwise normal
1709297662Srrs    // invokevirtual will perform the null check during the lookup
1710297662Srrs    // logic or the unverified entry point.  Profiling of calls
1711297662Srrs    // requires that the null check is performed in all cases.
1712297662Srrs    null_check(recv);
1713297662Srrs  }
1714297662Srrs
1715297662Srrs  if (profile_calls()) {
1716297662Srrs    assert(cha_monomorphic_target == NULL || exact_target == NULL, "both can not be set");
1717163953Srrs    ciKlass* target_klass = NULL;
1718163953Srrs    if (cha_monomorphic_target != NULL) {
1719166675Srrs      target_klass = cha_monomorphic_target->holder();
1720166675Srrs    } else if (exact_target != NULL) {
1721166675Srrs      target_klass = exact_target->holder();
1722211944Stuexen    }
1723211944Stuexen    profile_call(recv, target_klass);
1724211944Stuexen  }
1725211944Stuexen
1726166675Srrs  Invoke* result = new Invoke(code, result_type, recv, args, vtable_index, target, state_before);
1727224918Stuexen  // push result
1728224918Stuexen  append_split(result);
1729224918Stuexen
1730223132Stuexen  if (result_type != voidType) {
1731223132Stuexen    if (method()->is_strict()) {
1732223132Stuexen      push(result_type, round_fp(result));
1733223132Stuexen    } else {
1734223132Stuexen      push(result_type, result);
1735223132Stuexen    }
1736223132Stuexen  }
1737163953Srrs}
1738223132Stuexen
1739223132Stuexen
1740223132Stuexenvoid GraphBuilder::new_instance(int klass_index) {
1741223132Stuexen  bool will_link;
1742163953Srrs  ciKlass* klass = stream()->get_klass(will_link);
1743171440Srrs  assert(klass->is_instance_klass(), "must be an instance klass");
1744171440Srrs  NewInstance* new_instance = new NewInstance(klass->as_instance_klass());
1745171440Srrs  _memory->new_instance(new_instance);
1746171440Srrs  apush(append_split(new_instance));
1747171440Srrs}
1748171440Srrs
1749171440Srrs
1750171440Srrsvoid GraphBuilder::new_type_array() {
1751171440Srrs  apush(append_split(new NewTypeArray(ipop(), (BasicType)stream()->get_index())));
1752171440Srrs}
1753224918Stuexen
1754224918Stuexen
1755224918Stuexenvoid GraphBuilder::new_object_array() {
1756223132Stuexen  bool will_link;
1757223132Stuexen  ciKlass* klass = stream()->get_klass(will_link);
1758223132Stuexen  ValueStack* state_before = !klass->is_loaded() || PatchALot ? state()->copy() : NULL;
1759223132Stuexen  NewArray* n = new NewObjectArray(klass, ipop(), state_before);
1760223132Stuexen  apush(append_split(n));
1761223132Stuexen}
1762223132Stuexen
1763171440Srrs
1764223132Stuexenbool GraphBuilder::direct_compare(ciKlass* k) {
1765223132Stuexen  if (k->is_loaded() && k->is_instance_klass() && !UseSlowPath) {
1766223132Stuexen    ciInstanceKlass* ik = k->as_instance_klass();
1767223132Stuexen    if (ik->is_final()) {
1768171440Srrs      return true;
1769219057Srrs    } else {
1770219057Srrs      if (DeoptC1 && UseCHA && !(ik->has_subklass() || ik->is_interface())) {
1771219057Srrs        // test class is leaf class
1772219057Srrs        dependency_recorder()->assert_leaf_type(ik);
1773219057Srrs        return true;
1774219057Srrs      }
1775219057Srrs    }
1776219057Srrs  }
1777219057Srrs  return false;
1778219057Srrs}
1779219057Srrs
1780219057Srrs
1781223132Stuexenvoid GraphBuilder::check_cast(int klass_index) {
1782223132Stuexen  bool will_link;
1783219057Srrs  ciKlass* klass = stream()->get_klass(will_link);
1784219057Srrs  ValueStack* state_before = !klass->is_loaded() || PatchALot ? state()->copy() : NULL;
1785219057Srrs  CheckCast* c = new CheckCast(klass, apop(), state_before);
1786223132Stuexen  apush(append_split(c));
1787219057Srrs  c->set_direct_compare(direct_compare(klass));
1788217760Stuexen  if (profile_checkcasts()) {
1789217760Stuexen    c->set_profiled_method(method());
1790217760Stuexen    c->set_profiled_bci(bci());
1791217760Stuexen    c->set_should_profile(true);
1792217760Stuexen  }
1793217760Stuexen}
1794217760Stuexen
1795217760Stuexen
1796217760Stuexenvoid GraphBuilder::instance_of(int klass_index) {
1797217760Stuexen  bool will_link;
1798224918Stuexen  ciKlass* klass = stream()->get_klass(will_link);
1799224918Stuexen  ValueStack* state_before = !klass->is_loaded() || PatchALot ? state()->copy() : NULL;
1800224918Stuexen  InstanceOf* i = new InstanceOf(klass, apop(), state_before);
1801223132Stuexen  ipush(append_split(i));
1802223132Stuexen  i->set_direct_compare(direct_compare(klass));
1803223132Stuexen}
1804223132Stuexen
1805223132Stuexen
1806223132Stuexenvoid GraphBuilder::monitorenter(Value x, int bci) {
1807223132Stuexen  // save state before locking in case of deoptimization after a NullPointerException
1808217760Stuexen  ValueStack* lock_stack_before = lock_stack();
1809223132Stuexen  append_with_bci(new MonitorEnter(x, state()->lock(scope(), x), lock_stack_before), bci);
1810223132Stuexen  kill_all();
1811223132Stuexen}
1812223132Stuexen
1813217760Stuexen
1814217760Stuexenvoid GraphBuilder::monitorexit(Value x, int bci) {
1815217760Stuexen  // Note: the comment below is only relevant for the case where we do
1816217760Stuexen  // not deoptimize due to asynchronous exceptions (!(DeoptC1 &&
1817217760Stuexen  // DeoptOnAsyncException), which is not used anymore)
1818217760Stuexen
1819217760Stuexen  // Note: Potentially, the monitor state in an exception handler
1820217760Stuexen  //       can be wrong due to wrong 'initialization' of the handler
1821277804Sdelphij  //       via a wrong asynchronous exception path. This can happen,
1822277804Sdelphij  //       if the exception handler range for asynchronous exceptions
1823277804Sdelphij  //       is too long (see also java bug 4327029, and comment in
1824217760Stuexen  //       GraphBuilder::handle_exception()). This may cause 'under-
1825217760Stuexen  //       flow' of the monitor stack => bailout instead.
1826217760Stuexen  if (state()->locks_size() < 1) BAILOUT("monitor stack underflow");
1827223132Stuexen  append_with_bci(new MonitorExit(x, state()->unlock()), bci);
1828217760Stuexen  kill_all();
1829217760Stuexen}
1830217760Stuexen
1831217760Stuexen
1832217760Stuexenvoid GraphBuilder::new_multi_array(int dimensions) {
1833217760Stuexen  bool will_link;
1834217760Stuexen  ciKlass* klass = stream()->get_klass(will_link);
1835217760Stuexen  ValueStack* state_before = !klass->is_loaded() || PatchALot ? state()->copy() : NULL;
1836217760Stuexen
1837217760Stuexen  Values* dims = new Values(dimensions, NULL);
1838223132Stuexen  // fill in all dimensions
1839217760Stuexen  int i = dimensions;
1840163953Srrs  while (i-- > 0) dims->at_put(i, ipop());
1841163953Srrs  // create array
1842163953Srrs  NewArray* n = new NewMultiArray(klass, dims, state_before);
1843163953Srrs  apush(append_split(n));
1844166675Srrs}
1845163953Srrs
1846167598Srrs
1847163953Srrsvoid GraphBuilder::throw_op(int bci) {
1848163953Srrs  // We require that the debug info for a Throw be the "state before"
1849163953Srrs  // the Throw (i.e., exception oop is still on TOS)
1850163953Srrs  ValueStack* state_before = state()->copy();
1851163953Srrs  Throw* t = new Throw(apop(), state_before);
1852167598Srrs  append_with_bci(t, bci);
1853163953Srrs}
1854163953Srrs
1855163953Srrs
1856163953SrrsValue GraphBuilder::round_fp(Value fp_value) {
1857163953Srrs  // no rounding needed if SSE2 is used
1858172091Srrs  if (RoundFPResults && UseSSE < 2) {
1859171943Srrs    // Must currently insert rounding node for doubleword values that
1860223132Stuexen    // are results of expressions (i.e., not loads from memory or
1861223132Stuexen    // constants)
1862172091Srrs    if (fp_value->type()->tag() == doubleTag &&
1863223132Stuexen        fp_value->as_Constant() == NULL &&
1864163953Srrs        fp_value->as_Local() == NULL &&       // method parameters need no rounding
1865169655Srrs        fp_value->as_RoundFP() == NULL) {
1866163953Srrs      return append(new RoundFP(fp_value));
1867169655Srrs    }
1868163953Srrs  }
1869169655Srrs  return fp_value;
1870293913Stuexen}
1871293913Stuexen
1872293913Stuexen
1873293913StuexenInstruction* GraphBuilder::append_with_bci(Instruction* instr, int bci) {
1874293913Stuexen  Canonicalizer canon(instr, bci);
1875293913Stuexen  Instruction* i1 = canon.canonical();
1876293913Stuexen  if (i1->bci() != -99) {
1877293913Stuexen    // Canonicalizer returned an instruction which was already
1878163953Srrs    // appended so simply return it.
1879169655Srrs    return i1;
1880169655Srrs  } else if (UseLocalValueNumbering) {
1881163953Srrs    // Lookup the instruction in the ValueMap and add it to the map if
1882169655Srrs    // it's not found.
1883169655Srrs    Instruction* i2 = vmap()->find_insert(i1);
1884169655Srrs    if (i2 != i1) {
1885223132Stuexen      // found an entry in the value map, so just return it.
1886169655Srrs      assert(i2->bci() != -1, "should already be linked");
1887169655Srrs      return i2;
1888169655Srrs    }
1889169655Srrs    ValueNumberingEffects vne(vmap());
1890297312Stuexen    i1->visit(&vne);
1891297312Stuexen  }
1892169655Srrs
1893169655Srrs  if (i1->as_Phi() == NULL && i1->as_Local() == NULL) {
1894293913Stuexen    // i1 was not eliminated => append it
1895293913Stuexen    assert(i1->next() == NULL, "shouldn't already be linked");
1896293913Stuexen    _last = _last->set_next(i1, canon.bci());
1897293913Stuexen    if (++_instruction_count >= InstructionCountCutoff
1898293913Stuexen        && !bailed_out()) {
1899293913Stuexen      // set the bailout state but complete normal processing.  We
1900293913Stuexen      // might do a little more work before noticing the bailout so we
1901293913Stuexen      // want processing to continue normally until it's noticed.
1902163953Srrs      bailout("Method and/or inlining is too large");
1903185694Srrs    }
1904169655Srrs
1905169655Srrs#ifndef PRODUCT
1906169655Srrs    if (PrintIRDuringConstruction) {
1907297312Stuexen      InstructionPrinter ip;
1908297312Stuexen      ip.print_line(i1);
1909297312Stuexen      if (Verbose) {
1910297312Stuexen        state()->print();
1911297312Stuexen      }
1912169655Srrs    }
1913169655Srrs#endif
1914171943Srrs    assert(_last == i1, "adjust code below");
1915163953Srrs    StateSplit* s = i1->as_StateSplit();
1916163953Srrs    if (s != NULL && i1->as_BlockEnd() == NULL) {
1917163953Srrs      if (EliminateFieldAccess) {
1918163953Srrs        Intrinsic* intrinsic = s->as_Intrinsic();
1919223132Stuexen        if (s->as_Invoke() != NULL || (intrinsic && !intrinsic->preserves_state())) {
1920223132Stuexen          _memory->kill();
1921223132Stuexen        }
1922223132Stuexen      }
1923223132Stuexen      s->set_state(state()->copy());
1924163953Srrs    }
1925163953Srrs    // set up exception handlers for this instruction if necessary
1926163953Srrs    if (i1->can_trap()) {
1927163953Srrs      assert(exception_state() != NULL || !has_handler(), "must have setup exception state");
1928163953Srrs      i1->set_exception_handlers(handle_exception(bci));
1929166675Srrs    }
1930166675Srrs  }
1931166675Srrs  return i1;
1932166675Srrs}
1933166675Srrs
1934166675Srrs
1935163953SrrsInstruction* GraphBuilder::append(Instruction* instr) {
1936224918Stuexen  assert(instr->as_StateSplit() == NULL || instr->as_BlockEnd() != NULL, "wrong append used");
1937224918Stuexen  return append_with_bci(instr, bci());
1938224918Stuexen}
1939223132Stuexen
1940223132Stuexen
1941223132StuexenInstruction* GraphBuilder::append_split(StateSplit* instr) {
1942223132Stuexen  return append_with_bci(instr, bci());
1943223132Stuexen}
1944223132Stuexen
1945223132Stuexen
1946163953Srrsvoid GraphBuilder::null_check(Value value) {
1947223132Stuexen  if (value->as_NewArray() != NULL || value->as_NewInstance() != NULL) {
1948223132Stuexen    return;
1949223132Stuexen  } else {
1950223132Stuexen    Constant* con = value->as_Constant();
1951163953Srrs    if (con) {
1952167598Srrs      ObjectType* c = con->type()->as_ObjectType();
1953167598Srrs      if (c && c->is_loaded()) {
1954170056Srrs        ObjectConstant* oc = c->as_ObjectConstant();
1955167598Srrs        if (!oc || !oc->value()->is_null_object()) {
1956170056Srrs          return;
1957170056Srrs        }
1958223132Stuexen      }
1959167598Srrs    }
1960167598Srrs  }
1961167598Srrs  append(new NullCheck(value, lock_stack()));
1962167598Srrs}
1963167598Srrs
1964167598Srrs
1965167598Srrs
1966167598SrrsXHandlers* GraphBuilder::handle_exception(int cur_bci) {
1967167598Srrs  // fast path if it is guaranteed that no exception handlers are present
1968167598Srrs  if (!has_handler()) {
1969171943Srrs    // TODO: check if return NULL is possible (avoids empty lists)
1970223132Stuexen    return new XHandlers();
1971223132Stuexen  }
1972223132Stuexen
1973167598Srrs  XHandlers*  exception_handlers = new XHandlers();
1974167598Srrs  ScopeData*  cur_scope_data = scope_data();
1975167598Srrs  ValueStack* s = exception_state();
1976167598Srrs  int scope_count = 0;
1977167598Srrs
1978171943Srrs  assert(s != NULL, "exception state must be set");
1979167598Srrs  do {
1980167598Srrs    assert(cur_scope_data->scope() == s->scope(), "scopes do not match");
1981167598Srrs    assert(cur_bci == SynchronizationEntryBCI || cur_bci == cur_scope_data->stream()->cur_bci(), "invalid bci");
1982163953Srrs
1983163953Srrs    // join with all potential exception handlers
1984163953Srrs    XHandlers* list = cur_scope_data->xhandlers();
1985163953Srrs    const int n = list->length();
1986166675Srrs    for (int i = 0; i < n; i++) {
1987166675Srrs      XHandler* h = list->handler_at(i);
1988166675Srrs      if (h->covers(cur_bci)) {
1989166675Srrs        // h is a potential exception handler => join it
1990163953Srrs        compilation()->set_has_exception_handlers(true);
1991163953Srrs
1992163953Srrs        BlockBegin* entry = h->entry_block();
1993223132Stuexen        if (entry == block()) {
1994166675Srrs          // It's acceptable for an exception handler to cover itself
1995171943Srrs          // but we don't handle that in the parser currently.  It's
1996166675Srrs          // very rare so we bailout instead of trying to handle it.
1997163953Srrs          BAILOUT_("exception handler covers itself", exception_handlers);
1998223132Stuexen        }
1999163953Srrs        assert(entry->bci() == h->handler_bci(), "must match");
2000170056Srrs        assert(entry->bci() == -1 || entry == cur_scope_data->block_at(entry->bci()), "blocks must correspond");
2001163953Srrs
2002170056Srrs        // previously this was a BAILOUT, but this is not necessary
2003163953Srrs        // now because asynchronous exceptions are not handled this way.
2004170056Srrs        assert(entry->state() == NULL || s->locks_size() == entry->state()->locks_size(), "locks do not match");
2005170056Srrs
2006166675Srrs        // xhandler start with an empty expression stack
2007170056Srrs        s->truncate_stack(cur_scope_data->caller_stack_size());
2008170056Srrs
2009166675Srrs        // Note: Usually this join must work. However, very
2010166675Srrs        // complicated jsr-ret structures where we don't ret from
2011224918Stuexen        // the subroutine can cause the objects on the monitor
2012224918Stuexen        // stacks to not match because blocks can be parsed twice.
2013224918Stuexen        // The only test case we've seen so far which exhibits this
2014223132Stuexen        // problem is caught by the infinite recursion test in
2015223132Stuexen        // GraphBuilder::jsr() if the join doesn't work.
2016223132Stuexen        if (!entry->try_merge(s)) {
2017223132Stuexen          BAILOUT_("error while joining with exception handler, prob. due to complicated jsr/rets", exception_handlers);
2018223132Stuexen        }
2019223132Stuexen
2020223132Stuexen        // add current state for correct handling of phi functions at begin of xhandler
2021223132Stuexen        int phi_operand = entry->add_exception_state(s);
2022163953Srrs
2023223132Stuexen        // add entry to the list of xhandlers of this block
2024223132Stuexen        _block->add_exception_handler(entry);
2025223132Stuexen
2026223132Stuexen        // add back-edge from xhandler entry to this block
2027163953Srrs        if (!entry->is_predecessor(_block)) {
2028163953Srrs          entry->add_predecessor(_block);
2029166675Srrs        }
2030163953Srrs
2031163953Srrs        // clone XHandler because phi_operand and scope_count can not be shared
2032166675Srrs        XHandler* new_xhandler = new XHandler(h);
2033166675Srrs        new_xhandler->set_phi_operand(phi_operand);
2034166675Srrs        new_xhandler->set_scope_count(scope_count);
2035166675Srrs        exception_handlers->append(new_xhandler);
2036166675Srrs
2037166675Srrs        // fill in exception handler subgraph lazily
2038166675Srrs        assert(!entry->is_set(BlockBegin::was_visited_flag), "entry must not be visited yet");
2039166675Srrs        cur_scope_data->add_to_work_list(entry);
2040223132Stuexen
2041166675Srrs        // stop when reaching catchall
2042171943Srrs        if (h->catch_type() == 0) {
2043163953Srrs          return exception_handlers;
2044163953Srrs        }
2045223132Stuexen      }
2046163953Srrs    }
2047170056Srrs
2048163953Srrs    // Set up iteration for next time.
2049217895Stuexen    // If parsing a jsr, do not grab exception handlers from the
2050163953Srrs    // parent scopes for this method (already got them, and they
2051217895Stuexen    // needed to be cloned)
2052217895Stuexen    if (cur_scope_data->parsing_jsr()) {
2053166675Srrs      IRScope* tmp_scope = cur_scope_data->scope();
2054217895Stuexen      while (cur_scope_data->parent() != NULL &&
2055217895Stuexen             cur_scope_data->parent()->scope() == tmp_scope) {
2056217895Stuexen        cur_scope_data = cur_scope_data->parent();
2057217894Stuexen      }
2058224918Stuexen    }
2059224918Stuexen    if (cur_scope_data != NULL) {
2060224918Stuexen      if (cur_scope_data->parent() != NULL) {
2061223132Stuexen        // must use pop_scope instead of caller_state to preserve all monitors
2062223132Stuexen        s = s->pop_scope();
2063223132Stuexen      }
2064223132Stuexen      cur_bci = cur_scope_data->scope()->caller_bci();
2065223132Stuexen      cur_scope_data = cur_scope_data->parent();
2066223132Stuexen      scope_count++;
2067223132Stuexen    }
2068217894Stuexen  } while (cur_scope_data != NULL);
2069223132Stuexen
2070223132Stuexen  return exception_handlers;
2071223132Stuexen}
2072223132Stuexen
2073163953Srrs
2074163953Srrs// Helper class for simplifying Phis.
2075163953Srrsclass PhiSimplifier : public BlockClosure {
2076167598Srrs private:
2077163953Srrs  bool _has_substitutions;
2078163953Srrs  Value simplify(Value v);
2079167598Srrs
2080170056Srrs public:
2081163953Srrs  PhiSimplifier(BlockBegin* start) : _has_substitutions(false) {
2082167598Srrs    start->iterate_preorder(this);
2083167598Srrs    if (_has_substitutions) {
2084167598Srrs      SubstitutionResolver sr(start);
2085163953Srrs    }
2086224918Stuexen  }
2087224918Stuexen  void block_do(BlockBegin* b);
2088224918Stuexen  bool has_substitutions() const { return _has_substitutions; }
2089223132Stuexen};
2090223132Stuexen
2091223132Stuexen
2092223132StuexenValue PhiSimplifier::simplify(Value v) {
2093223132Stuexen  Phi* phi = v->as_Phi();
2094223132Stuexen
2095223132Stuexen  if (phi == NULL) {
2096223132Stuexen    // no phi function
2097223132Stuexen    return v;
2098223132Stuexen  } else if (v->has_subst()) {
2099223132Stuexen    // already substituted; subst can be phi itself -> simplify
2100167598Srrs    return simplify(v->subst());
2101223132Stuexen  } else if (phi->is_set(Phi::cannot_simplify)) {
2102223132Stuexen    // already tried to simplify phi before
2103167598Srrs    return phi;
2104163953Srrs  } else if (phi->is_set(Phi::visited)) {
2105223132Stuexen    // break cycles in phi functions
2106223132Stuexen    return phi;
2107223132Stuexen  } else if (phi->type()->is_illegal()) {
2108223132Stuexen    // illegal phi functions are ignored anyway
2109163953Srrs    return phi;
2110163953Srrs
2111167598Srrs  } else {
2112163953Srrs    // mark phi function as processed to break cycles in phi functions
2113163953Srrs    phi->set(Phi::visited);
2114163953Srrs
2115163953Srrs    // simplify x = [y, x] and x = [y, y] to y
2116163953Srrs    Value subst = NULL;
2117166675Srrs    int opd_count = phi->operand_count();
2118223132Stuexen    for (int i = 0; i < opd_count; i++) {
2119163953Srrs      Value opd = phi->operand_at(i);
2120163953Srrs      assert(opd != NULL, "Operand must exist!");
2121163953Srrs
2122163953Srrs      if (opd->type()->is_illegal()) {
2123163953Srrs        // if one operand is illegal, the entire phi function is illegal
2124163953Srrs        phi->make_illegal();
2125163953Srrs        phi->clear(Phi::visited);
2126163953Srrs        return phi;
2127163953Srrs      }
2128163953Srrs
2129163953Srrs      Value new_opd = simplify(opd);
2130163953Srrs      assert(new_opd != NULL, "Simplified operand must exist!");
2131163953Srrs
2132163953Srrs      if (new_opd != phi && new_opd != subst) {
2133163953Srrs        if (subst == NULL) {
2134163953Srrs          subst = new_opd;
2135163953Srrs        } else {
2136163953Srrs          // no simplification possible
2137163953Srrs          phi->set(Phi::cannot_simplify);
2138163953Srrs          phi->clear(Phi::visited);
2139163953Srrs          return phi;
2140163953Srrs        }
2141163953Srrs      }
2142163953Srrs    }
2143163953Srrs
2144163953Srrs    // sucessfully simplified phi function
2145163953Srrs    assert(subst != NULL, "illegal phi function");
2146163953Srrs    _has_substitutions = true;
2147185694Srrs    phi->clear(Phi::visited);
2148185694Srrs    phi->set_subst(subst);
2149185694Srrs
2150163953Srrs#ifndef PRODUCT
2151202520Srrs    if (PrintPhiFunctions) {
2152163953Srrs      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());
2153166675Srrs    }
2154223132Stuexen#endif
2155163953Srrs
2156163953Srrs    return subst;
2157166675Srrs  }
2158166675Srrs}
2159166675Srrs
2160166675Srrs
2161166675Srrsvoid PhiSimplifier::block_do(BlockBegin* b) {
2162166675Srrs  for_each_phi_fun(b, phi,
2163166675Srrs    simplify(phi);
2164166675Srrs  );
2165166675Srrs
2166223132Stuexen#ifdef ASSERT
2167163953Srrs  for_each_phi_fun(b, phi,
2168163953Srrs                   assert(phi->operand_count() != 1 || phi->subst() != phi, "missed trivial simplification");
2169166675Srrs  );
2170166675Srrs
2171166675Srrs  ValueStack* state = b->state()->caller_state();
2172166675Srrs  int index;
2173166675Srrs  Value value;
2174166675Srrs  for_each_state(state) {
2175166675Srrs    for_each_local_value(state, index, value) {
2176166675Srrs      Phi* phi = value->as_Phi();
2177223132Stuexen      assert(phi == NULL || phi->block() != b, "must not have phi function to simplify in caller state");
2178163953Srrs    }
2179163953Srrs  }
2180166675Srrs#endif
2181166675Srrs}
2182166675Srrs
2183166675Srrs// This method is called after all blocks are filled with HIR instructions
2184166675Srrs// It eliminates all Phi functions of the form x = [y, y] and x = [y, x]
2185168124Srrsvoid GraphBuilder::eliminate_redundant_phis(BlockBegin* start) {
2186166675Srrs  PhiSimplifier simplifier(start);
2187166675Srrs}
2188223132Stuexen
2189163953Srrs
2190163953Srrsvoid GraphBuilder::connect_to_end(BlockBegin* beg) {
2191163953Srrs  // setup iteration
2192166675Srrs  kill_all();
2193166675Srrs  _block = beg;
2194163953Srrs  _state = beg->state()->copy();
2195163953Srrs  _last  = beg;
2196166675Srrs  iterate_bytecodes_for_block(beg->bci());
2197166675Srrs}
2198166675Srrs
2199166675Srrs
2200166675SrrsBlockEnd* GraphBuilder::iterate_bytecodes_for_block(int bci) {
2201166675Srrs#ifndef PRODUCT
2202166675Srrs  if (PrintIRDuringConstruction) {
2203166675Srrs    tty->cr();
2204275567Stuexen    InstructionPrinter ip;
2205221249Stuexen    ip.print_instr(_block); tty->cr();
2206275567Stuexen    ip.print_stack(_block->state()); tty->cr();
2207275567Stuexen    ip.print_inline_level(_block);
2208275567Stuexen    ip.print_head();
2209275567Stuexen    tty->print_cr("locals size: %d stack size: %d", state()->locals_size(), state()->stack_size());
2210275567Stuexen  }
2211221249Stuexen#endif
2212275567Stuexen  _skip_block = false;
2213275567Stuexen  assert(state() != NULL, "ValueStack missing!");
2214275567Stuexen  ciBytecodeStream s(method());
2215221249Stuexen  s.reset_to_bci(bci);
2216275567Stuexen  int prev_bci = bci;
2217275567Stuexen  scope_data()->set_stream(&s);
2218221249Stuexen  // iterate
2219275567Stuexen  Bytecodes::Code code = Bytecodes::_illegal;
2220275567Stuexen  bool push_exception = false;
2221275567Stuexen
2222221249Stuexen  if (block()->is_set(BlockBegin::exception_entry_flag) && block()->next() == NULL) {
2223275567Stuexen    // first thing in the exception entry block should be the exception object.
2224275567Stuexen    push_exception = true;
2225166675Srrs  }
2226163953Srrs
2227166675Srrs  while (!bailed_out() && last()->as_BlockEnd() == NULL &&
2228166675Srrs         (code = stream()->next()) != ciBytecodeStream::EOBC() &&
2229223132Stuexen         (block_at(s.cur_bci()) == NULL || block_at(s.cur_bci()) == block())) {
2230166675Srrs
2231171943Srrs    if (has_handler() && can_trap(method(), code)) {
2232166675Srrs      // copy the state because it is modified before handle_exception is called
2233163953Srrs      set_exception_state(state()->copy());
2234223132Stuexen    } else {
2235163953Srrs      // handle_exception is not called for this bytecode
2236163953Srrs      set_exception_state(NULL);
2237163953Srrs    }
2238163953Srrs
2239163953Srrs    // Check for active jsr during OSR compilation
2240163953Srrs    if (compilation()->is_osr_compile()
2241163953Srrs        && scope()->is_top_scope()
2242166675Srrs        && parsing_jsr()
2243163953Srrs        && s.cur_bci() == compilation()->osr_bci()) {
2244163953Srrs      bailout("OSR not supported while a jsr is active");
2245163953Srrs    }
2246163953Srrs
2247166675Srrs    if (push_exception) {
2248166675Srrs      apush(append(new ExceptionObject()));
2249163953Srrs      push_exception = false;
2250166675Srrs    }
2251166675Srrs
2252166675Srrs    // handle bytecode
2253166675Srrs    switch (code) {
2254166675Srrs      case Bytecodes::_nop            : /* nothing to do */ break;
2255166675Srrs      case Bytecodes::_aconst_null    : apush(append(new Constant(objectNull            ))); break;
2256275567Stuexen      case Bytecodes::_iconst_m1      : ipush(append(new Constant(new IntConstant   (-1)))); break;
2257221249Stuexen      case Bytecodes::_iconst_0       : ipush(append(new Constant(intZero               ))); break;
2258275567Stuexen      case Bytecodes::_iconst_1       : ipush(append(new Constant(intOne                ))); break;
2259275567Stuexen      case Bytecodes::_iconst_2       : ipush(append(new Constant(new IntConstant   ( 2)))); break;
2260275567Stuexen      case Bytecodes::_iconst_3       : ipush(append(new Constant(new IntConstant   ( 3)))); break;
2261275567Stuexen      case Bytecodes::_iconst_4       : ipush(append(new Constant(new IntConstant   ( 4)))); break;
2262275567Stuexen      case Bytecodes::_iconst_5       : ipush(append(new Constant(new IntConstant   ( 5)))); break;
2263221249Stuexen      case Bytecodes::_lconst_0       : lpush(append(new Constant(new LongConstant  ( 0)))); break;
2264275567Stuexen      case Bytecodes::_lconst_1       : lpush(append(new Constant(new LongConstant  ( 1)))); break;
2265275567Stuexen      case Bytecodes::_fconst_0       : fpush(append(new Constant(new FloatConstant ( 0)))); break;
2266275567Stuexen      case Bytecodes::_fconst_1       : fpush(append(new Constant(new FloatConstant ( 1)))); break;
2267221249Stuexen      case Bytecodes::_fconst_2       : fpush(append(new Constant(new FloatConstant ( 2)))); break;
2268275567Stuexen      case Bytecodes::_dconst_0       : dpush(append(new Constant(new DoubleConstant( 0)))); break;
2269275567Stuexen      case Bytecodes::_dconst_1       : dpush(append(new Constant(new DoubleConstant( 1)))); break;
2270221249Stuexen      case Bytecodes::_bipush         : ipush(append(new Constant(new IntConstant(((signed char*)s.cur_bcp())[1])))); break;
2271275567Stuexen      case Bytecodes::_sipush         : ipush(append(new Constant(new IntConstant((short)Bytes::get_Java_u2(s.cur_bcp()+1))))); break;
2272275567Stuexen      case Bytecodes::_ldc            : // fall through
2273275567Stuexen      case Bytecodes::_ldc_w          : // fall through
2274221249Stuexen      case Bytecodes::_ldc2_w         : load_constant(); break;
2275275567Stuexen      case Bytecodes::_iload          : load_local(intType     , s.get_index()); break;
2276275567Stuexen      case Bytecodes::_lload          : load_local(longType    , s.get_index()); break;
2277275567Stuexen      case Bytecodes::_fload          : load_local(floatType   , s.get_index()); break;
2278221249Stuexen      case Bytecodes::_dload          : load_local(doubleType  , s.get_index()); break;
2279221249Stuexen      case Bytecodes::_aload          : load_local(instanceType, s.get_index()); break;
2280166675Srrs      case Bytecodes::_iload_0        : load_local(intType   , 0); break;
2281166675Srrs      case Bytecodes::_iload_1        : load_local(intType   , 1); break;
2282166675Srrs      case Bytecodes::_iload_2        : load_local(intType   , 2); break;
2283166675Srrs      case Bytecodes::_iload_3        : load_local(intType   , 3); break;
2284166675Srrs      case Bytecodes::_lload_0        : load_local(longType  , 0); break;
2285166675Srrs      case Bytecodes::_lload_1        : load_local(longType  , 1); break;
2286221249Stuexen      case Bytecodes::_lload_2        : load_local(longType  , 2); break;
2287178251Srrs      case Bytecodes::_lload_3        : load_local(longType  , 3); break;
2288275567Stuexen      case Bytecodes::_fload_0        : load_local(floatType , 0); break;
2289166675Srrs      case Bytecodes::_fload_1        : load_local(floatType , 1); break;
2290275567Stuexen      case Bytecodes::_fload_2        : load_local(floatType , 2); break;
2291166675Srrs      case Bytecodes::_fload_3        : load_local(floatType , 3); break;
2292166675Srrs      case Bytecodes::_dload_0        : load_local(doubleType, 0); break;
2293166675Srrs      case Bytecodes::_dload_1        : load_local(doubleType, 1); break;
2294166675Srrs      case Bytecodes::_dload_2        : load_local(doubleType, 2); break;
2295275567Stuexen      case Bytecodes::_dload_3        : load_local(doubleType, 3); break;
2296275567Stuexen      case Bytecodes::_aload_0        : load_local(objectType, 0); break;
2297178251Srrs      case Bytecodes::_aload_1        : load_local(objectType, 1); break;
2298166675Srrs      case Bytecodes::_aload_2        : load_local(objectType, 2); break;
2299166675Srrs      case Bytecodes::_aload_3        : load_local(objectType, 3); break;
2300166675Srrs      case Bytecodes::_iaload         : load_indexed(T_INT   ); break;
2301166675Srrs      case Bytecodes::_laload         : load_indexed(T_LONG  ); break;
2302166675Srrs      case Bytecodes::_faload         : load_indexed(T_FLOAT ); break;
2303163953Srrs      case Bytecodes::_daload         : load_indexed(T_DOUBLE); break;
2304166675Srrs      case Bytecodes::_aaload         : load_indexed(T_OBJECT); break;
2305166675Srrs      case Bytecodes::_baload         : load_indexed(T_BYTE  ); break;
2306171943Srrs      case Bytecodes::_caload         : load_indexed(T_CHAR  ); break;
2307166675Srrs      case Bytecodes::_saload         : load_indexed(T_SHORT ); break;
2308163953Srrs      case Bytecodes::_istore         : store_local(intType   , s.get_index()); break;
2309223132Stuexen      case Bytecodes::_lstore         : store_local(longType  , s.get_index()); break;
2310163953Srrs      case Bytecodes::_fstore         : store_local(floatType , s.get_index()); break;
2311163953Srrs      case Bytecodes::_dstore         : store_local(doubleType, s.get_index()); break;
2312163953Srrs      case Bytecodes::_astore         : store_local(objectType, s.get_index()); break;
2313166675Srrs      case Bytecodes::_istore_0       : store_local(intType   , 0); break;
2314163953Srrs      case Bytecodes::_istore_1       : store_local(intType   , 1); break;
2315163953Srrs      case Bytecodes::_istore_2       : store_local(intType   , 2); break;
2316163953Srrs      case Bytecodes::_istore_3       : store_local(intType   , 3); break;
2317166675Srrs      case Bytecodes::_lstore_0       : store_local(longType  , 0); break;
2318166675Srrs      case Bytecodes::_lstore_1       : store_local(longType  , 1); break;
2319163953Srrs      case Bytecodes::_lstore_2       : store_local(longType  , 2); break;
2320163953Srrs      case Bytecodes::_lstore_3       : store_local(longType  , 3); break;
2321166675Srrs      case Bytecodes::_fstore_0       : store_local(floatType , 0); break;
2322168124Srrs      case Bytecodes::_fstore_1       : store_local(floatType , 1); break;
2323169655Srrs      case Bytecodes::_fstore_2       : store_local(floatType , 2); break;
2324163953Srrs      case Bytecodes::_fstore_3       : store_local(floatType , 3); break;
2325169655Srrs      case Bytecodes::_dstore_0       : store_local(doubleType, 0); break;
2326166675Srrs      case Bytecodes::_dstore_1       : store_local(doubleType, 1); break;
2327223132Stuexen      case Bytecodes::_dstore_2       : store_local(doubleType, 2); break;
2328163953Srrs      case Bytecodes::_dstore_3       : store_local(doubleType, 3); break;
2329163953Srrs      case Bytecodes::_astore_0       : store_local(objectType, 0); break;
2330163953Srrs      case Bytecodes::_astore_1       : store_local(objectType, 1); break;
2331163953Srrs      case Bytecodes::_astore_2       : store_local(objectType, 2); break;
2332163953Srrs      case Bytecodes::_astore_3       : store_local(objectType, 3); break;
2333275567Stuexen      case Bytecodes::_iastore        : store_indexed(T_INT   ); break;
2334163953Srrs      case Bytecodes::_lastore        : store_indexed(T_LONG  ); break;
2335275567Stuexen      case Bytecodes::_fastore        : store_indexed(T_FLOAT ); break;
2336275567Stuexen      case Bytecodes::_dastore        : store_indexed(T_DOUBLE); break;
2337275567Stuexen      case Bytecodes::_aastore        : store_indexed(T_OBJECT); break;
2338275567Stuexen      case Bytecodes::_bastore        : store_indexed(T_BYTE  ); break;
2339275567Stuexen      case Bytecodes::_castore        : store_indexed(T_CHAR  ); break;
2340166675Srrs      case Bytecodes::_sastore        : store_indexed(T_SHORT ); break;
2341166675Srrs      case Bytecodes::_pop            : // fall through
2342163953Srrs      case Bytecodes::_pop2           : // fall through
2343275567Stuexen      case Bytecodes::_dup            : // fall through
2344275567Stuexen      case Bytecodes::_dup_x1         : // fall through
2345275567Stuexen      case Bytecodes::_dup_x2         : // fall through
2346275567Stuexen      case Bytecodes::_dup2           : // fall through
2347275567Stuexen      case Bytecodes::_dup2_x1        : // fall through
2348275567Stuexen      case Bytecodes::_dup2_x2        : // fall through
2349275567Stuexen      case Bytecodes::_swap           : stack_op(code); break;
2350275567Stuexen      case Bytecodes::_iadd           : arithmetic_op(intType   , code); break;
2351275567Stuexen      case Bytecodes::_ladd           : arithmetic_op(longType  , code); break;
2352275567Stuexen      case Bytecodes::_fadd           : arithmetic_op(floatType , code); break;
2353275567Stuexen      case Bytecodes::_dadd           : arithmetic_op(doubleType, code); break;
2354166675Srrs      case Bytecodes::_isub           : arithmetic_op(intType   , code); break;
2355275567Stuexen      case Bytecodes::_lsub           : arithmetic_op(longType  , code); break;
2356275567Stuexen      case Bytecodes::_fsub           : arithmetic_op(floatType , code); break;
2357275567Stuexen      case Bytecodes::_dsub           : arithmetic_op(doubleType, code); break;
2358275567Stuexen      case Bytecodes::_imul           : arithmetic_op(intType   , code); break;
2359275567Stuexen      case Bytecodes::_lmul           : arithmetic_op(longType  , code); break;
2360275567Stuexen      case Bytecodes::_fmul           : arithmetic_op(floatType , code); break;
2361275567Stuexen      case Bytecodes::_dmul           : arithmetic_op(doubleType, code); break;
2362275567Stuexen      case Bytecodes::_idiv           : arithmetic_op(intType   , code, lock_stack()); break;
2363166675Srrs      case Bytecodes::_ldiv           : arithmetic_op(longType  , code, lock_stack()); break;
2364166675Srrs      case Bytecodes::_fdiv           : arithmetic_op(floatType , code); break;
2365166675Srrs      case Bytecodes::_ddiv           : arithmetic_op(doubleType, code); break;
2366166675Srrs      case Bytecodes::_irem           : arithmetic_op(intType   , code, lock_stack()); break;
2367166675Srrs      case Bytecodes::_lrem           : arithmetic_op(longType  , code, lock_stack()); break;
2368166675Srrs      case Bytecodes::_frem           : arithmetic_op(floatType , code); break;
2369166675Srrs      case Bytecodes::_drem           : arithmetic_op(doubleType, code); break;
2370275567Stuexen      case Bytecodes::_ineg           : negate_op(intType   ); break;
2371166675Srrs      case Bytecodes::_lneg           : negate_op(longType  ); break;
2372275567Stuexen      case Bytecodes::_fneg           : negate_op(floatType ); break;
2373163953Srrs      case Bytecodes::_dneg           : negate_op(doubleType); break;
2374166675Srrs      case Bytecodes::_ishl           : shift_op(intType , code); break;
2375163953Srrs      case Bytecodes::_lshl           : shift_op(longType, code); break;
2376163953Srrs      case Bytecodes::_ishr           : shift_op(intType , code); break;
2377275567Stuexen      case Bytecodes::_lshr           : shift_op(longType, code); break;
2378221249Stuexen      case Bytecodes::_iushr          : shift_op(intType , code); break;
2379275567Stuexen      case Bytecodes::_lushr          : shift_op(longType, code); break;
2380171943Srrs      case Bytecodes::_iand           : logic_op(intType , code); break;
2381171943Srrs      case Bytecodes::_land           : logic_op(longType, code); break;
2382275567Stuexen      case Bytecodes::_ior            : logic_op(intType , code); break;
2383275567Stuexen      case Bytecodes::_lor            : logic_op(longType, code); break;
2384171943Srrs      case Bytecodes::_ixor           : logic_op(intType , code); break;
2385171943Srrs      case Bytecodes::_lxor           : logic_op(longType, code); break;
2386171943Srrs      case Bytecodes::_iinc           : increment(); break;
2387171943Srrs      case Bytecodes::_i2l            : convert(code, T_INT   , T_LONG  ); break;
2388171943Srrs      case Bytecodes::_i2f            : convert(code, T_INT   , T_FLOAT ); break;
2389221249Stuexen      case Bytecodes::_i2d            : convert(code, T_INT   , T_DOUBLE); break;
2390221249Stuexen      case Bytecodes::_l2i            : convert(code, T_LONG  , T_INT   ); break;
2391221249Stuexen      case Bytecodes::_l2f            : convert(code, T_LONG  , T_FLOAT ); break;
2392275567Stuexen      case Bytecodes::_l2d            : convert(code, T_LONG  , T_DOUBLE); break;
2393171943Srrs      case Bytecodes::_f2i            : convert(code, T_FLOAT , T_INT   ); break;
2394171943Srrs      case Bytecodes::_f2l            : convert(code, T_FLOAT , T_LONG  ); break;
2395275567Stuexen      case Bytecodes::_f2d            : convert(code, T_FLOAT , T_DOUBLE); break;
2396171943Srrs      case Bytecodes::_d2i            : convert(code, T_DOUBLE, T_INT   ); break;
2397171943Srrs      case Bytecodes::_d2l            : convert(code, T_DOUBLE, T_LONG  ); break;
2398171943Srrs      case Bytecodes::_d2f            : convert(code, T_DOUBLE, T_FLOAT ); break;
2399171943Srrs      case Bytecodes::_i2b            : convert(code, T_INT   , T_BYTE  ); break;
2400171943Srrs      case Bytecodes::_i2c            : convert(code, T_INT   , T_CHAR  ); break;
2401171943Srrs      case Bytecodes::_i2s            : convert(code, T_INT   , T_SHORT ); break;
2402221249Stuexen      case Bytecodes::_lcmp           : compare_op(longType  , code); break;
2403221249Stuexen      case Bytecodes::_fcmpl          : compare_op(floatType , code); break;
2404221249Stuexen      case Bytecodes::_fcmpg          : compare_op(floatType , code); break;
2405171943Srrs      case Bytecodes::_dcmpl          : compare_op(doubleType, code); break;
2406171943Srrs      case Bytecodes::_dcmpg          : compare_op(doubleType, code); break;
2407171943Srrs      case Bytecodes::_ifeq           : if_zero(intType   , If::eql); break;
2408171943Srrs      case Bytecodes::_ifne           : if_zero(intType   , If::neq); break;
2409171943Srrs      case Bytecodes::_iflt           : if_zero(intType   , If::lss); break;
2410171943Srrs      case Bytecodes::_ifge           : if_zero(intType   , If::geq); break;
2411275567Stuexen      case Bytecodes::_ifgt           : if_zero(intType   , If::gtr); break;
2412224641Stuexen      case Bytecodes::_ifle           : if_zero(intType   , If::leq); break;
2413163953Srrs      case Bytecodes::_if_icmpeq      : if_same(intType   , If::eql); break;
2414275567Stuexen      case Bytecodes::_if_icmpne      : if_same(intType   , If::neq); break;
2415224641Stuexen      case Bytecodes::_if_icmplt      : if_same(intType   , If::lss); break;
2416163953Srrs      case Bytecodes::_if_icmpge      : if_same(intType   , If::geq); break;
2417283666Stuexen      case Bytecodes::_if_icmpgt      : if_same(intType   , If::gtr); break;
2418283666Stuexen      case Bytecodes::_if_icmple      : if_same(intType   , If::leq); break;
2419283666Stuexen      case Bytecodes::_if_acmpeq      : if_same(objectType, If::eql); break;
2420283666Stuexen      case Bytecodes::_if_acmpne      : if_same(objectType, If::neq); break;
2421283666Stuexen      case Bytecodes::_goto           : _goto(s.cur_bci(), s.get_dest()); break;
2422283666Stuexen      case Bytecodes::_jsr            : jsr(s.get_dest()); break;
2423283666Stuexen      case Bytecodes::_ret            : ret(s.get_index()); break;
2424283666Stuexen      case Bytecodes::_tableswitch    : table_switch(); break;
2425283666Stuexen      case Bytecodes::_lookupswitch   : lookup_switch(); break;
2426283666Stuexen      case Bytecodes::_ireturn        : method_return(ipop()); break;
2427283666Stuexen      case Bytecodes::_lreturn        : method_return(lpop()); break;
2428283666Stuexen      case Bytecodes::_freturn        : method_return(fpop()); break;
2429283666Stuexen      case Bytecodes::_dreturn        : method_return(dpop()); break;
2430283666Stuexen      case Bytecodes::_areturn        : method_return(apop()); break;
2431283666Stuexen      case Bytecodes::_return         : method_return(NULL  ); break;
2432163953Srrs      case Bytecodes::_getstatic      : // fall through
2433225635Stuexen      case Bytecodes::_putstatic      : // fall through
2434163953Srrs      case Bytecodes::_getfield       : // fall through
2435225635Stuexen      case Bytecodes::_putfield       : access_field(code); break;
2436163953Srrs      case Bytecodes::_invokevirtual  : // fall through
2437225635Stuexen      case Bytecodes::_invokespecial  : // fall through
2438163953Srrs      case Bytecodes::_invokestatic   : // fall through
2439225635Stuexen      case Bytecodes::_invokedynamic  : // fall through
2440284332Stuexen      case Bytecodes::_invokeinterface: invoke(code); break;
2441284332Stuexen      case Bytecodes::_new            : new_instance(s.get_index_big()); break;
2442163953Srrs      case Bytecodes::_newarray       : new_type_array(); break;
2443163953Srrs      case Bytecodes::_anewarray      : new_object_array(); break;
2444225549Stuexen      case Bytecodes::_arraylength    : ipush(append(new ArrayLength(apop(), lock_stack()))); break;
2445226252Stuexen      case Bytecodes::_athrow         : throw_op(s.cur_bci()); break;
2446224870Stuexen      case Bytecodes::_checkcast      : check_cast(s.get_index_big()); break;
2447163953Srrs      case Bytecodes::_instanceof     : instance_of(s.get_index_big()); break;
2448167598Srrs      // Note: we do not have special handling for the monitorenter bytecode if DeoptC1 && DeoptOnAsyncException
2449225549Stuexen      case Bytecodes::_monitorenter   : monitorenter(apop(), s.cur_bci()); break;
2450225549Stuexen      case Bytecodes::_monitorexit    : monitorexit (apop(), s.cur_bci()); break;
2451225549Stuexen      case Bytecodes::_wide           : ShouldNotReachHere(); break;
2452163953Srrs      case Bytecodes::_multianewarray : new_multi_array(s.cur_bcp()[3]); break;
2453163953Srrs      case Bytecodes::_ifnull         : if_null(objectType, If::eql); break;
2454163953Srrs      case Bytecodes::_ifnonnull      : if_null(objectType, If::neq); break;
2455163953Srrs      case Bytecodes::_goto_w         : _goto(s.cur_bci(), s.get_far_dest()); break;
2456163953Srrs      case Bytecodes::_jsr_w          : jsr(s.get_far_dest()); break;
2457163953Srrs      case Bytecodes::_breakpoint     : BAILOUT_("concurrent setting of breakpoint", NULL);
2458163953Srrs      default                         : ShouldNotReachHere(); break;
2459163953Srrs    }
2460163953Srrs    // save current bci to setup Goto at the end
2461283666Stuexen    prev_bci = s.cur_bci();
2462225549Stuexen  }
2463226252Stuexen  CHECK_BAILOUT_(NULL);
2464225549Stuexen  // stop processing of this block (see try_inline_full)
2465225549Stuexen  if (_skip_block) {
2466167598Srrs    _skip_block = false;
2467225549Stuexen    assert(_last && _last->as_BlockEnd(), "");
2468225549Stuexen    return _last->as_BlockEnd();
2469225549Stuexen  }
2470225549Stuexen  // if there are any, check if last instruction is a BlockEnd instruction
2471163953Srrs  BlockEnd* end = last()->as_BlockEnd();
2472163953Srrs  if (end == NULL) {
2473225635Stuexen    // all blocks must end with a BlockEnd instruction => add a Goto
2474224641Stuexen    end = new Goto(block_at(s.cur_bci()), false);
2475224641Stuexen    _last = _last->set_next(end, prev_bci);
2476163953Srrs  }
2477163953Srrs  assert(end == last()->as_BlockEnd(), "inconsistency");
2478225635Stuexen
2479225635Stuexen  // if the method terminates, we don't need the stack anymore
2480225635Stuexen  if (end->as_Return() != NULL) {
2481170056Srrs    state()->clear_stack();
2482170056Srrs  } else if (end->as_Throw() != NULL) {
2483225635Stuexen    // May have exception handler in caller scopes
2484163953Srrs    state()->truncate_stack(scope()->lock_stack_size());
2485163953Srrs  }
2486163953Srrs
2487163953Srrs  // connect to begin & set state
2488224918Stuexen  // NOTE that inlining may have changed the block we are parsing
2489224918Stuexen  block()->set_end(end);
2490224918Stuexen  end->set_state(state());
2491223132Stuexen  // propagate state
2492223132Stuexen  for (int i = end->number_of_sux() - 1; i >= 0; i--) {
2493223132Stuexen    BlockBegin* sux = end->sux_at(i);
2494223132Stuexen    assert(sux->is_predecessor(block()), "predecessor missing");
2495223132Stuexen    // be careful, bailout if bytecodes are strange
2496223132Stuexen    if (!sux->try_merge(state())) BAILOUT_("block join failed", NULL);
2497225549Stuexen    scope_data()->add_to_work_list(end->sux_at(i));
2498226252Stuexen  }
2499225549Stuexen
2500225549Stuexen  scope_data()->set_stream(NULL);
2501167598Srrs
2502225549Stuexen  // done
2503225549Stuexen  return end;
2504225549Stuexen}
2505223132Stuexen
2506223132Stuexen
2507163953Srrsvoid GraphBuilder::iterate_all_blocks(bool start_in_current_block_for_inlining) {
2508223132Stuexen  do {
2509223132Stuexen    if (start_in_current_block_for_inlining && !bailed_out()) {
2510170056Srrs      iterate_bytecodes_for_block(0);
2511223132Stuexen      start_in_current_block_for_inlining = false;
2512223132Stuexen    } else {
2513223132Stuexen      BlockBegin* b;
2514223132Stuexen      while ((b = scope_data()->remove_from_work_list()) != NULL) {
2515223132Stuexen        if (!b->is_set(BlockBegin::was_visited_flag)) {
2516225635Stuexen          if (b->is_set(BlockBegin::osr_entry_flag)) {
2517225635Stuexen            // we're about to parse the osr entry block, so make sure
2518225635Stuexen            // we setup the OSR edge leading into this block so that
2519225635Stuexen            // Phis get setup correctly.
2520225635Stuexen            setup_osr_entry_block();
2521223132Stuexen            // this is no longer the osr entry block, so clear it.
2522170056Srrs            b->clear(BlockBegin::osr_entry_flag);
2523223132Stuexen          }
2524223132Stuexen          b->set(BlockBegin::was_visited_flag);
2525170056Srrs          connect_to_end(b);
2526163953Srrs        }
2527223132Stuexen      }
2528223132Stuexen    }
2529223132Stuexen  } while (!bailed_out() && !scope_data()->is_work_list_empty());
2530223132Stuexen}
2531163953Srrs
2532163953Srrs
2533163953Srrsbool GraphBuilder::_is_initialized = false;
2534163953Srrsbool GraphBuilder::_can_trap      [Bytecodes::number_of_java_codes];
2535163953Srrsbool GraphBuilder::_is_async[Bytecodes::number_of_java_codes];
2536275567Stuexen
2537163953Srrsvoid GraphBuilder::initialize() {
2538275567Stuexen  // make sure initialization happens only once (need a
2539275567Stuexen  // lock here, if we allow the compiler to be re-entrant)
2540275567Stuexen  if (is_initialized()) return;
2541275567Stuexen  _is_initialized = true;
2542275567Stuexen
2543166675Srrs  // the following bytecodes are assumed to potentially
2544166675Srrs  // throw exceptions in compiled code - note that e.g.
2545166675Srrs  // monitorexit & the return bytecodes do not throw
2546275567Stuexen  // exceptions since monitor pairing proved that they
2547275567Stuexen  // succeed (if monitor pairing succeeded)
2548275567Stuexen  Bytecodes::Code can_trap_list[] =
2549275567Stuexen    { Bytecodes::_ldc
2550275567Stuexen    , Bytecodes::_ldc_w
2551275567Stuexen    , Bytecodes::_ldc2_w
2552275567Stuexen    , Bytecodes::_iaload
2553275567Stuexen    , Bytecodes::_laload
2554275567Stuexen    , Bytecodes::_faload
2555275567Stuexen    , Bytecodes::_daload
2556275567Stuexen    , Bytecodes::_aaload
2557166675Srrs    , Bytecodes::_baload
2558275567Stuexen    , Bytecodes::_caload
2559275567Stuexen    , Bytecodes::_saload
2560275567Stuexen    , Bytecodes::_iastore
2561275567Stuexen    , Bytecodes::_lastore
2562275567Stuexen    , Bytecodes::_fastore
2563275567Stuexen    , Bytecodes::_dastore
2564275567Stuexen    , Bytecodes::_aastore
2565275567Stuexen    , Bytecodes::_bastore
2566166675Srrs    , Bytecodes::_castore
2567166675Srrs    , Bytecodes::_sastore
2568166675Srrs    , Bytecodes::_idiv
2569166675Srrs    , Bytecodes::_ldiv
2570166675Srrs    , Bytecodes::_irem
2571166675Srrs    , Bytecodes::_lrem
2572166675Srrs    , Bytecodes::_getstatic
2573275567Stuexen    , Bytecodes::_putstatic
2574166675Srrs    , Bytecodes::_getfield
2575275567Stuexen    , Bytecodes::_putfield
2576166675Srrs    , Bytecodes::_invokevirtual
2577166675Srrs    , Bytecodes::_invokespecial
2578163953Srrs    , Bytecodes::_invokestatic
2579166675Srrs    , Bytecodes::_invokedynamic
2580163953Srrs    , Bytecodes::_invokeinterface
2581275567Stuexen    , Bytecodes::_new
2582217635Srrs    , Bytecodes::_newarray
2583217638Stuexen    , Bytecodes::_anewarray
2584217635Srrs    , Bytecodes::_arraylength
2585217635Srrs    , Bytecodes::_athrow
2586217638Stuexen    , Bytecodes::_checkcast
2587217635Srrs    , Bytecodes::_instanceof
2588217635Srrs    , Bytecodes::_monitorenter
2589217638Stuexen    , Bytecodes::_multianewarray
2590217635Srrs    };
2591217635Srrs
2592166675Srrs  // the following bytecodes are assumed to potentially
2593219014Stuexen  // throw asynchronous exceptions in compiled code due
2594166675Srrs  // to safepoints (note: these entries could be merged
2595166675Srrs  // with the can_trap_list - however, we need to know
2596222029Stuexen  // which ones are asynchronous for now - see also the
2597283666Stuexen  // comment in GraphBuilder::handle_exception)
2598283666Stuexen  Bytecodes::Code is_async_list[] =
2599283666Stuexen    { Bytecodes::_ifeq
2600283666Stuexen    , Bytecodes::_ifne
2601283666Stuexen    , Bytecodes::_iflt
2602283666Stuexen    , Bytecodes::_ifge
2603283666Stuexen    , Bytecodes::_ifgt
2604283666Stuexen    , Bytecodes::_ifle
2605283666Stuexen    , Bytecodes::_if_icmpeq
2606283666Stuexen    , Bytecodes::_if_icmpne
2607283666Stuexen    , Bytecodes::_if_icmplt
2608283666Stuexen    , Bytecodes::_if_icmpge
2609283666Stuexen    , Bytecodes::_if_icmpgt
2610283666Stuexen    , Bytecodes::_if_icmple
2611166675Srrs    , Bytecodes::_if_acmpeq
2612223132Stuexen    , Bytecodes::_if_acmpne
2613163953Srrs    , Bytecodes::_goto
2614275567Stuexen    , Bytecodes::_jsr
2615163953Srrs    , Bytecodes::_ret
2616163953Srrs    , Bytecodes::_tableswitch
2617171943Srrs    , Bytecodes::_lookupswitch
2618163953Srrs    , Bytecodes::_ireturn
2619163953Srrs    , Bytecodes::_lreturn
2620223132Stuexen    , Bytecodes::_freturn
2621163953Srrs    , Bytecodes::_dreturn
2622163953Srrs    , Bytecodes::_areturn
2623163953Srrs    , Bytecodes::_return
2624163953Srrs    , Bytecodes::_ifnull
2625163953Srrs    , Bytecodes::_ifnonnull
2626166675Srrs    , Bytecodes::_goto_w
2627163953Srrs    , Bytecodes::_jsr_w
2628166675Srrs    };
2629223132Stuexen
2630163953Srrs  // inititialize trap tables
2631163953Srrs  for (int i = 0; i < Bytecodes::number_of_java_codes; i++) {
2632163953Srrs    _can_trap[i] = false;
2633163953Srrs    _is_async[i] = false;
2634163953Srrs  }
2635163953Srrs  // set standard trap info
2636166675Srrs  for (uint j = 0; j < ARRAY_SIZE(can_trap_list); j++) {
2637166675Srrs    _can_trap[can_trap_list[j]] = true;
2638163953Srrs  }
2639163953Srrs
2640223132Stuexen  // We now deoptimize if an asynchronous exception is thrown. This
2641163953Srrs  // considerably cleans up corner case issues related to javac's
2642163953Srrs  // incorrect exception handler ranges for async exceptions and
2643163953Srrs  // allows us to precisely analyze the types of exceptions from
2644287282Stuexen  // certain bytecodes.
2645173179Srrs  if (!(DeoptC1 && DeoptOnAsyncException)) {
2646163953Srrs    // set asynchronous trap info
2647163953Srrs    for (uint k = 0; k < ARRAY_SIZE(is_async_list); k++) {
2648163953Srrs      assert(!_can_trap[is_async_list[k]], "can_trap_list and is_async_list should be disjoint");
2649163953Srrs      _can_trap[is_async_list[k]] = true;
2650163953Srrs      _is_async[is_async_list[k]] = true;
2651163953Srrs    }
2652163953Srrs  }
2653163953Srrs}
2654163953Srrs
2655163953Srrs
2656163953SrrsBlockBegin* GraphBuilder::header_block(BlockBegin* entry, BlockBegin::Flag f, ValueStack* state) {
2657163953Srrs  assert(entry->is_set(f), "entry/flag mismatch");
2658163953Srrs  // create header block
2659163953Srrs  BlockBegin* h = new BlockBegin(entry->bci());
2660163953Srrs  h->set_depth_first_number(0);
2661163953Srrs
2662163953Srrs  Value l = h;
2663163953Srrs  if (profile_branches()) {
2664163953Srrs    // Increment the invocation count on entry to the method.  We
2665163953Srrs    // can't use profile_invocation here because append isn't setup to
2666163953Srrs    // work properly at this point.  The instruction have to be
2667163953Srrs    // appended to the instruction stream by hand.
2668217635Srrs    Value m = new Constant(new ObjectConstant(compilation()->method()));
2669217635Srrs    h->set_next(m, 0);
2670217635Srrs    Value p = new ProfileCounter(m, methodOopDesc::interpreter_invocation_counter_offset_in_bytes(), 1);
2671217635Srrs    m->set_next(p, 0);
2672217638Stuexen    l = p;
2673217635Srrs  }
2674217635Srrs
2675217638Stuexen  BlockEnd* g = new Goto(entry, false);
2676217635Srrs  l->set_next(g, entry->bci());
2677217635Srrs  h->set_end(g);
2678163953Srrs  h->set(f);
2679219014Stuexen  // setup header block end state
2680163953Srrs  ValueStack* s = state->copy(); // can use copy since stack is empty (=> no phis)
2681163953Srrs  assert(s->stack_is_empty(), "must have empty stack at entry point");
2682283666Stuexen  g->set_state(s);
2683283666Stuexen  return h;
2684283666Stuexen}
2685283666Stuexen
2686283666Stuexen
2687283666Stuexen
2688283666StuexenBlockBegin* GraphBuilder::setup_start_block(int osr_bci, BlockBegin* std_entry, BlockBegin* osr_entry, ValueStack* state) {
2689283666Stuexen  BlockBegin* start = new BlockBegin(0);
2690283666Stuexen
2691283666Stuexen  // This code eliminates the empty start block at the beginning of
2692283666Stuexen  // each method.  Previously, each method started with the
2693283666Stuexen  // start-block created below, and this block was followed by the
2694283666Stuexen  // header block that was always empty.  This header block is only
2695283666Stuexen  // necesary if std_entry is also a backward branch target because
2696163953Srrs  // then phi functions may be necessary in the header block.  It's
2697163953Srrs  // also necessary when profiling so that there's a single block that
2698223132Stuexen  // can increment the interpreter_invocation_count.
2699223132Stuexen  BlockBegin* new_header_block;
2700163953Srrs  if (std_entry->number_of_preds() == 0 && !profile_branches()) {
2701163953Srrs    new_header_block = std_entry;
2702163953Srrs  } else {
2703163953Srrs    new_header_block = header_block(std_entry, BlockBegin::std_entry_flag, state);
2704163953Srrs  }
2705166675Srrs
2706166675Srrs  // setup start block (root for the IR graph)
2707166675Srrs  Base* base =
2708166675Srrs    new Base(
2709166675Srrs      new_header_block,
2710166675Srrs      osr_entry
2711166675Srrs    );
2712166675Srrs  start->set_next(base, 0);
2713166675Srrs  start->set_end(base);
2714224918Stuexen  // create & setup state for start block
2715224918Stuexen  start->set_state(state->copy());
2716224918Stuexen  base->set_state(state->copy());
2717223132Stuexen
2718223132Stuexen  if (base->std_entry()->state() == NULL) {
2719223132Stuexen    // setup states for header blocks
2720223132Stuexen    base->std_entry()->merge(state);
2721223132Stuexen  }
2722223132Stuexen
2723223132Stuexen  assert(base->std_entry()->state() != NULL, "");
2724223132Stuexen  return start;
2725223132Stuexen}
2726163953Srrs
2727223132Stuexen
2728223132Stuexenvoid GraphBuilder::setup_osr_entry_block() {
2729223132Stuexen  assert(compilation()->is_osr_compile(), "only for osrs");
2730223132Stuexen
2731163953Srrs  int osr_bci = compilation()->osr_bci();
2732215410Stuexen  ciBytecodeStream s(method());
2733215410Stuexen  s.reset_to_bci(osr_bci);
2734215410Stuexen  s.next();
2735215410Stuexen  scope_data()->set_stream(&s);
2736215410Stuexen
2737215410Stuexen  // create a new block to be the osr setup code
2738215410Stuexen  _osr_entry = new BlockBegin(osr_bci);
2739215410Stuexen  _osr_entry->set(BlockBegin::osr_entry_flag);
2740215410Stuexen  _osr_entry->set_depth_first_number(0);
2741215410Stuexen  BlockBegin* target = bci2block()->at(osr_bci);
2742215410Stuexen  assert(target != NULL && target->is_set(BlockBegin::osr_entry_flag), "must be there");
2743215410Stuexen  // the osr entry has no values for locals
2744215410Stuexen  ValueStack* state = target->state()->copy();
2745215410Stuexen  _osr_entry->set_state(state);
2746215410Stuexen
2747215410Stuexen  kill_all();
2748223132Stuexen  _block = _osr_entry;
2749215410Stuexen  _state = _osr_entry->state()->copy();
2750223132Stuexen  _last  = _osr_entry;
2751215410Stuexen  Value e = append(new OsrEntry());
2752215410Stuexen  e->set_needs_null_check(false);
2753223132Stuexen
2754215410Stuexen  // OSR buffer is
2755163953Srrs  //
2756163953Srrs  // locals[nlocals-1..0]
2757163953Srrs  // monitors[number_of_locks-1..0]
2758163953Srrs  //
2759166675Srrs  // locals is a direct copy of the interpreter frame so in the osr buffer
2760166675Srrs  // so first slot in the local array is the last local from the interpreter
2761163953Srrs  // and last slot is local[0] (receiver) from the interpreter
2762163953Srrs  //
2763171477Srrs  // Similarly with locks. The first lock slot in the osr buffer is the nth lock
2764163953Srrs  // from the interpreter frame, the nth lock slot in the osr buffer is 0th lock
2765163953Srrs  // in the interpreter frame (the method lock if a sync method)
2766163953Srrs
2767163953Srrs  // Initialize monitors in the compiled activation.
2768163953Srrs
2769163953Srrs  int index;
2770224918Stuexen  Value local;
2771224918Stuexen
2772224918Stuexen  // find all the locals that the interpreter thinks contain live oops
2773223132Stuexen  const BitMap live_oops = method()->live_local_oops_at_bci(osr_bci);
2774223132Stuexen
2775223132Stuexen  // compute the offset into the locals so that we can treat the buffer
2776223132Stuexen  // as if the locals were still in the interpreter frame
2777223132Stuexen  int locals_offset = BytesPerWord * (method()->max_locals() - 1);
2778223132Stuexen  for_each_local_value(state, index, local) {
2779223132Stuexen    int offset = locals_offset - (index + local->type()->size() - 1) * BytesPerWord;
2780223132Stuexen    Value get;
2781223132Stuexen    if (local->type()->is_object_kind() && !live_oops.at(index)) {
2782223132Stuexen      // The interpreter thinks this local is dead but the compiler
2783223132Stuexen      // doesn't so pretend that the interpreter passed in null.
2784163953Srrs      get = append(new Constant(objectNull));
2785223132Stuexen    } else {
2786223132Stuexen      get = append(new UnsafeGetRaw(as_BasicType(local->type()), e,
2787223132Stuexen                                    append(new Constant(new IntConstant(offset))),
2788223132Stuexen                                    0,
2789163953Srrs                                    true));
2790163953Srrs    }
2791163953Srrs    _state->store_local(index, get);
2792163953Srrs  }
2793163953Srrs
2794166675Srrs  // the storage for the OSR buffer is freed manually in the LIRGenerator.
2795166675Srrs
2796166675Srrs  assert(state->caller_state() == NULL, "should be top scope");
2797166675Srrs  state->clear_locals();
2798170056Srrs  Goto* g = new Goto(target, false);
2799166675Srrs  g->set_state(_state->copy());
2800166675Srrs  append(g);
2801224918Stuexen  _osr_entry->set_end(g);
2802224918Stuexen  target->merge(_osr_entry->end()->state());
2803224918Stuexen
2804223132Stuexen  scope_data()->set_stream(NULL);
2805223132Stuexen}
2806223132Stuexen
2807223132Stuexen
2808223132StuexenValueStack* GraphBuilder::state_at_entry() {
2809223132Stuexen  ValueStack* state = new ValueStack(scope(), method()->max_locals(), method()->max_stack());
2810223132Stuexen
2811163953Srrs  // Set up locals for receiver
2812223132Stuexen  int idx = 0;
2813223132Stuexen  if (!method()->is_static()) {
2814223132Stuexen    // we should always see the receiver
2815223132Stuexen    state->store_local(idx, new Local(objectType, idx));
2816163953Srrs    idx = 1;
2817163953Srrs  }
2818163953Srrs
2819163953Srrs  // Set up locals for incoming arguments
2820163953Srrs  ciSignature* sig = method()->signature();
2821166675Srrs  for (int i = 0; i < sig->count(); i++) {
2822163953Srrs    ciType* type = sig->type_at(i);
2823163953Srrs    BasicType basic_type = type->basic_type();
2824163953Srrs    // don't allow T_ARRAY to propagate into locals types
2825163953Srrs    if (basic_type == T_ARRAY) basic_type = T_OBJECT;
2826163953Srrs    ValueType* vt = as_ValueType(basic_type);
2827163953Srrs    state->store_local(idx, new Local(vt, idx));
2828223132Stuexen    idx += type->size();
2829223132Stuexen  }
2830163953Srrs
2831163953Srrs  // lock synchronized method
2832163953Srrs  if (method()->is_synchronized()) {
2833163953Srrs    state->lock(scope(), NULL);
2834163953Srrs  }
2835163953Srrs
2836166675Srrs  return state;
2837166675Srrs}
2838166675Srrs
2839166675Srrs
2840275567StuexenGraphBuilder::GraphBuilder(Compilation* compilation, IRScope* scope)
2841170056Srrs  : _scope_data(NULL)
2842275567Stuexen  , _exception_state(NULL)
2843275567Stuexen  , _instruction_count(0)
2844275567Stuexen  , _osr_entry(NULL)
2845275567Stuexen  , _memory(new MemoryBuffer())
2846275567Stuexen  , _compilation(compilation)
2847275567Stuexen  , _inline_bailout_msg(NULL)
2848275567Stuexen{
2849275567Stuexen  int osr_bci = compilation->osr_bci();
2850275567Stuexen
2851275567Stuexen  // determine entry points and bci2block mapping
2852275567Stuexen  BlockListBuilder blm(compilation, scope, osr_bci);
2853275567Stuexen  CHECK_BAILOUT();
2854275567Stuexen
2855275567Stuexen  BlockList* bci2block = blm.bci2block();
2856275567Stuexen  BlockBegin* start_block = bci2block->at(0);
2857275567Stuexen
2858275567Stuexen  assert(is_initialized(), "GraphBuilder must have been initialized");
2859275567Stuexen  push_root_scope(scope, bci2block, start_block);
2860275567Stuexen
2861275567Stuexen  // setup state for std entry
2862275567Stuexen  _initial_state = state_at_entry();
2863275567Stuexen  start_block->merge(_initial_state);
2864275567Stuexen
2865275567Stuexen  // complete graph
2866166675Srrs  _vmap        = new ValueMap();
2867223132Stuexen  scope->compute_lock_stack_size();
2868166675Srrs  switch (scope->method()->intrinsic_id()) {
2869223132Stuexen  case vmIntrinsics::_dabs          : // fall through
2870163953Srrs  case vmIntrinsics::_dsqrt         : // fall through
2871163953Srrs  case vmIntrinsics::_dsin          : // fall through
2872223132Stuexen  case vmIntrinsics::_dcos          : // fall through
2873163953Srrs  case vmIntrinsics::_dtan          : // fall through
2874163953Srrs  case vmIntrinsics::_dlog          : // fall through
2875163953Srrs  case vmIntrinsics::_dlog10        : // fall through
2876163953Srrs    {
2877163953Srrs      // Compiles where the root method is an intrinsic need a special
2878163953Srrs      // compilation environment because the bytecodes for the method
2879163953Srrs      // shouldn't be parsed during the compilation, only the special
2880163953Srrs      // Intrinsic node should be emitted.  If this isn't done the the
2881166675Srrs      // code for the inlined version will be different than the root
2882166675Srrs      // compiled version which could lead to monotonicity problems on
2883163953Srrs      // intel.
2884163953Srrs
2885163953Srrs      // Set up a stream so that appending instructions works properly.
2886163953Srrs      ciBytecodeStream s(scope->method());
2887166675Srrs      s.reset_to_bci(0);
2888168299Srrs      scope_data()->set_stream(&s);
2889163953Srrs      s.next();
2890163953Srrs
2891163953Srrs      // setup the initial block state
2892163953Srrs      _block = start_block;
2893163953Srrs      _state = start_block->state()->copy();
2894166675Srrs      _last  = start_block;
2895223132Stuexen      load_local(doubleType, 0);
2896163953Srrs
2897163953Srrs      // Emit the intrinsic node.
2898163953Srrs      bool result = try_inline_intrinsics(scope->method());
2899163953Srrs      if (!result) BAILOUT("failed to inline intrinsic");
2900163953Srrs      method_return(dpop());
2901181054Srrs
2902181054Srrs      // connect the begin and end blocks and we're all done.
2903163953Srrs      BlockEnd* end = last()->as_BlockEnd();
2904181054Srrs      block()->set_end(end);
2905163953Srrs      end->set_state(state());
2906166675Srrs      break;
2907163953Srrs    }
2908163953Srrs  default:
2909163953Srrs    scope_data()->add_to_work_list(start_block);
2910163953Srrs    iterate_all_blocks();
2911163953Srrs    break;
2912163953Srrs  }
2913166675Srrs  CHECK_BAILOUT();
2914166675Srrs
2915166675Srrs  _start = setup_start_block(osr_bci, start_block, _osr_entry, _initial_state);
2916166675Srrs
2917163953Srrs  eliminate_redundant_phis(_start);
2918185694Srrs
2919163953Srrs  NOT_PRODUCT(if (PrintValueNumbering && Verbose) print_stats());
2920163953Srrs  // for osr compile, bailout if some requirements are not fulfilled
2921224918Stuexen  if (osr_bci != -1) {
2922224918Stuexen    BlockBegin* osr_block = blm.bci2block()->at(osr_bci);
2923224918Stuexen    assert(osr_block->is_set(BlockBegin::was_visited_flag),"osr entry must have been visited for osr compile");
2924223132Stuexen
2925223132Stuexen    // check if osr entry point has empty stack - we cannot handle non-empty stacks at osr entry points
2926223132Stuexen    if (!osr_block->state()->stack_is_empty()) {
2927223132Stuexen      BAILOUT("stack not empty at OSR entry point");
2928223132Stuexen    }
2929223132Stuexen  }
2930223132Stuexen#ifndef PRODUCT
2931223132Stuexen  if (PrintCompilation && Verbose) tty->print_cr("Created %d Instructions", _instruction_count);
2932163953Srrs#endif
2933223132Stuexen}
2934223132Stuexen
2935223132Stuexen
2936163953SrrsValueStack* GraphBuilder::lock_stack() {
2937163953Srrs  // return a new ValueStack representing just the current lock stack
2938163953Srrs  // (for debug info at safepoints in exception throwing or handling)
2939163953Srrs  ValueStack* new_stack = state()->copy_locks();
2940163953Srrs  return new_stack;
2941163953Srrs}
2942166675Srrs
2943163953Srrs
2944166675Srrsint GraphBuilder::recursive_inline_level(ciMethod* cur_callee) const {
2945166675Srrs  int recur_level = 0;
2946166675Srrs  for (IRScope* s = scope(); s != NULL; s = s->caller()) {
2947166675Srrs    if (s->method() == cur_callee) {
2948163953Srrs      ++recur_level;
2949163953Srrs    }
2950163953Srrs  }
2951163953Srrs  return recur_level;
2952166675Srrs}
2953163953Srrs
2954171943Srrs
2955166675Srrsbool GraphBuilder::try_inline(ciMethod* callee, bool holder_known) {
2956166675Srrs  // Clear out any existing inline bailout condition
2957169420Srrs  clear_inline_bailout();
2958234832Stuexen
2959223132Stuexen  if (callee->should_exclude()) {
2960163953Srrs    // callee is excluded
2961163953Srrs    INLINE_BAILOUT("excluded by CompilerOracle")
2962163953Srrs  } else if (!callee->can_be_compiled()) {
2963224918Stuexen    // callee is not compilable (prob. has breakpoints)
2964224918Stuexen    INLINE_BAILOUT("not compilable")
2965224918Stuexen  } else if (callee->intrinsic_id() != vmIntrinsics::_none && try_inline_intrinsics(callee)) {
2966223132Stuexen    // intrinsics can be native or not
2967223132Stuexen    return true;
2968223132Stuexen  } else if (callee->is_native()) {
2969223132Stuexen    // non-intrinsic natives cannot be inlined
2970223132Stuexen    INLINE_BAILOUT("non-intrinsic native")
2971223132Stuexen  } else if (callee->is_abstract()) {
2972223132Stuexen    INLINE_BAILOUT("abstract")
2973223132Stuexen  } else {
2974223132Stuexen    return try_inline_full(callee, holder_known);
2975223132Stuexen  }
2976223132Stuexen}
2977234832Stuexen
2978223132Stuexen
2979223132Stuexenbool GraphBuilder::try_inline_intrinsics(ciMethod* callee) {
2980223132Stuexen  if (!InlineNatives           ) INLINE_BAILOUT("intrinsic method inlining disabled");
2981223132Stuexen  if (callee->is_synchronized()) {
2982223132Stuexen    // We don't currently support any synchronized intrinsics
2983163953Srrs    return false;
2984163953Srrs  }
2985163953Srrs
2986163953Srrs  // callee seems like a good candidate
2987163953Srrs  // determine id
2988163953Srrs  bool preserves_state = false;
2989163953Srrs  bool cantrap = true;
2990163953Srrs  vmIntrinsics::ID id = callee->intrinsic_id();
2991163953Srrs  switch (id) {
2992166675Srrs    case vmIntrinsics::_arraycopy     :
2993163953Srrs      if (!InlineArrayCopy) return false;
2994166675Srrs      break;
2995166675Srrs
2996166675Srrs    case vmIntrinsics::_currentTimeMillis:
2997166675Srrs    case vmIntrinsics::_nanoTime:
2998166675Srrs      preserves_state = true;
2999166675Srrs      cantrap = false;
3000166675Srrs      break;
3001166675Srrs
3002166675Srrs    case vmIntrinsics::_floatToRawIntBits   :
3003166675Srrs    case vmIntrinsics::_intBitsToFloat      :
3004171943Srrs    case vmIntrinsics::_doubleToRawLongBits :
3005166675Srrs    case vmIntrinsics::_longBitsToDouble    :
3006166675Srrs      if (!InlineMathNatives) return false;
3007169420Srrs      preserves_state = true;
3008234832Stuexen      cantrap = false;
3009223132Stuexen      break;
3010166675Srrs
3011166675Srrs    case vmIntrinsics::_getClass      :
3012166675Srrs      if (!InlineClassNatives) return false;
3013171943Srrs      preserves_state = true;
3014163953Srrs      break;
3015163953Srrs
3016163953Srrs    case vmIntrinsics::_currentThread :
3017163953Srrs      if (!InlineThreadNatives) return false;
3018223132Stuexen      preserves_state = true;
3019223132Stuexen      cantrap = false;
3020223132Stuexen      break;
3021223132Stuexen
3022163953Srrs    case vmIntrinsics::_dabs          : // fall through
3023223132Stuexen    case vmIntrinsics::_dsqrt         : // fall through
3024223132Stuexen    case vmIntrinsics::_dsin          : // fall through
3025163953Srrs    case vmIntrinsics::_dcos          : // fall through
3026223132Stuexen    case vmIntrinsics::_dtan          : // fall through
3027223132Stuexen    case vmIntrinsics::_dlog          : // fall through
3028223132Stuexen    case vmIntrinsics::_dlog10        : // fall through
3029223132Stuexen      if (!InlineMathNatives) return false;
3030223132Stuexen      cantrap = false;
3031223132Stuexen      preserves_state = true;
3032223132Stuexen      break;
3033223132Stuexen
3034223132Stuexen    // sun/misc/AtomicLong.attemptUpdate
3035223132Stuexen    case vmIntrinsics::_attemptUpdate :
3036223132Stuexen      if (!VM_Version::supports_cx8()) return false;
3037223132Stuexen      if (!InlineAtomicLong) return false;
3038223132Stuexen      preserves_state = true;
3039223132Stuexen      break;
3040223132Stuexen
3041223132Stuexen    // Use special nodes for Unsafe instructions so we can more easily
3042223132Stuexen    // perform an address-mode optimization on the raw variants
3043223132Stuexen    case vmIntrinsics::_getObject : return append_unsafe_get_obj(callee, T_OBJECT,  false);
3044223132Stuexen    case vmIntrinsics::_getBoolean: return append_unsafe_get_obj(callee, T_BOOLEAN, false);
3045223132Stuexen    case vmIntrinsics::_getByte   : return append_unsafe_get_obj(callee, T_BYTE,    false);
3046223132Stuexen    case vmIntrinsics::_getShort  : return append_unsafe_get_obj(callee, T_SHORT,   false);
3047223132Stuexen    case vmIntrinsics::_getChar   : return append_unsafe_get_obj(callee, T_CHAR,    false);
3048223132Stuexen    case vmIntrinsics::_getInt    : return append_unsafe_get_obj(callee, T_INT,     false);
3049223132Stuexen    case vmIntrinsics::_getLong   : return append_unsafe_get_obj(callee, T_LONG,    false);
3050223132Stuexen    case vmIntrinsics::_getFloat  : return append_unsafe_get_obj(callee, T_FLOAT,   false);
3051223132Stuexen    case vmIntrinsics::_getDouble : return append_unsafe_get_obj(callee, T_DOUBLE,  false);
3052223132Stuexen
3053223132Stuexen    case vmIntrinsics::_putObject : return append_unsafe_put_obj(callee, T_OBJECT,  false);
3054223132Stuexen    case vmIntrinsics::_putBoolean: return append_unsafe_put_obj(callee, T_BOOLEAN, false);
3055223132Stuexen    case vmIntrinsics::_putByte   : return append_unsafe_put_obj(callee, T_BYTE,    false);
3056223132Stuexen    case vmIntrinsics::_putShort  : return append_unsafe_put_obj(callee, T_SHORT,   false);
3057223132Stuexen    case vmIntrinsics::_putChar   : return append_unsafe_put_obj(callee, T_CHAR,    false);
3058223132Stuexen    case vmIntrinsics::_putInt    : return append_unsafe_put_obj(callee, T_INT,     false);
3059223132Stuexen    case vmIntrinsics::_putLong   : return append_unsafe_put_obj(callee, T_LONG,    false);
3060223132Stuexen    case vmIntrinsics::_putFloat  : return append_unsafe_put_obj(callee, T_FLOAT,   false);
3061223132Stuexen    case vmIntrinsics::_putDouble : return append_unsafe_put_obj(callee, T_DOUBLE,  false);
3062235009Stuexen
3063235009Stuexen    case vmIntrinsics::_getObjectVolatile : return append_unsafe_get_obj(callee, T_OBJECT,  true);
3064235009Stuexen    case vmIntrinsics::_getBooleanVolatile: return append_unsafe_get_obj(callee, T_BOOLEAN, true);
3065235009Stuexen    case vmIntrinsics::_getByteVolatile   : return append_unsafe_get_obj(callee, T_BYTE,    true);
3066235009Stuexen    case vmIntrinsics::_getShortVolatile  : return append_unsafe_get_obj(callee, T_SHORT,   true);
3067235009Stuexen    case vmIntrinsics::_getCharVolatile   : return append_unsafe_get_obj(callee, T_CHAR,    true);
3068235075Stuexen    case vmIntrinsics::_getIntVolatile    : return append_unsafe_get_obj(callee, T_INT,     true);
3069235075Stuexen    case vmIntrinsics::_getLongVolatile   : return append_unsafe_get_obj(callee, T_LONG,    true);
3070235075Stuexen    case vmIntrinsics::_getFloatVolatile  : return append_unsafe_get_obj(callee, T_FLOAT,   true);
3071223132Stuexen    case vmIntrinsics::_getDoubleVolatile : return append_unsafe_get_obj(callee, T_DOUBLE,  true);
3072223132Stuexen
3073223132Stuexen    case vmIntrinsics::_putObjectVolatile : return append_unsafe_put_obj(callee, T_OBJECT,  true);
3074223132Stuexen    case vmIntrinsics::_putBooleanVolatile: return append_unsafe_put_obj(callee, T_BOOLEAN, true);
3075223132Stuexen    case vmIntrinsics::_putByteVolatile   : return append_unsafe_put_obj(callee, T_BYTE,    true);
3076223132Stuexen    case vmIntrinsics::_putShortVolatile  : return append_unsafe_put_obj(callee, T_SHORT,   true);
3077223132Stuexen    case vmIntrinsics::_putCharVolatile   : return append_unsafe_put_obj(callee, T_CHAR,    true);
3078223132Stuexen    case vmIntrinsics::_putIntVolatile    : return append_unsafe_put_obj(callee, T_INT,     true);
3079223132Stuexen    case vmIntrinsics::_putLongVolatile   : return append_unsafe_put_obj(callee, T_LONG,    true);
3080223132Stuexen    case vmIntrinsics::_putFloatVolatile  : return append_unsafe_put_obj(callee, T_FLOAT,   true);
3081223132Stuexen    case vmIntrinsics::_putDoubleVolatile : return append_unsafe_put_obj(callee, T_DOUBLE,  true);
3082224918Stuexen
3083224918Stuexen    case vmIntrinsics::_getByte_raw   : return append_unsafe_get_raw(callee, T_BYTE);
3084224918Stuexen    case vmIntrinsics::_getShort_raw  : return append_unsafe_get_raw(callee, T_SHORT);
3085223132Stuexen    case vmIntrinsics::_getChar_raw   : return append_unsafe_get_raw(callee, T_CHAR);
3086223132Stuexen    case vmIntrinsics::_getInt_raw    : return append_unsafe_get_raw(callee, T_INT);
3087223132Stuexen    case vmIntrinsics::_getLong_raw   : return append_unsafe_get_raw(callee, T_LONG);
3088223132Stuexen    case vmIntrinsics::_getFloat_raw  : return append_unsafe_get_raw(callee, T_FLOAT);
3089223132Stuexen    case vmIntrinsics::_getDouble_raw : return append_unsafe_get_raw(callee, T_DOUBLE);
3090223132Stuexen
3091223132Stuexen    case vmIntrinsics::_putByte_raw   : return append_unsafe_put_raw(callee, T_BYTE);
3092223132Stuexen    case vmIntrinsics::_putShort_raw  : return append_unsafe_put_raw(callee, T_SHORT);
3093223132Stuexen    case vmIntrinsics::_putChar_raw   : return append_unsafe_put_raw(callee, T_CHAR);
3094223132Stuexen    case vmIntrinsics::_putInt_raw    : return append_unsafe_put_raw(callee, T_INT);
3095223132Stuexen    case vmIntrinsics::_putLong_raw   : return append_unsafe_put_raw(callee, T_LONG);
3096223132Stuexen    case vmIntrinsics::_putFloat_raw  : return append_unsafe_put_raw(callee, T_FLOAT);
3097223132Stuexen    case vmIntrinsics::_putDouble_raw : return append_unsafe_put_raw(callee, T_DOUBLE);
3098223132Stuexen
3099223132Stuexen    case vmIntrinsics::_prefetchRead        : return append_unsafe_prefetch(callee, false, false);
3100223132Stuexen    case vmIntrinsics::_prefetchWrite       : return append_unsafe_prefetch(callee, false, true);
3101223132Stuexen    case vmIntrinsics::_prefetchReadStatic  : return append_unsafe_prefetch(callee, true,  false);
3102223132Stuexen    case vmIntrinsics::_prefetchWriteStatic : return append_unsafe_prefetch(callee, true,  true);
3103223132Stuexen
3104223132Stuexen    case vmIntrinsics::_checkIndex    :
3105223132Stuexen      if (!InlineNIOCheckIndex) return false;
3106223132Stuexen      preserves_state = true;
3107230104Stuexen      break;
3108223132Stuexen    case vmIntrinsics::_putOrderedObject : return append_unsafe_put_obj(callee, T_OBJECT,  true);
3109223132Stuexen    case vmIntrinsics::_putOrderedInt    : return append_unsafe_put_obj(callee, T_INT,     true);
3110223132Stuexen    case vmIntrinsics::_putOrderedLong   : return append_unsafe_put_obj(callee, T_LONG,    true);
3111223132Stuexen
3112223132Stuexen    case vmIntrinsics::_compareAndSwapLong:
3113223132Stuexen      if (!VM_Version::supports_cx8()) return false;
3114223132Stuexen      // fall through
3115223132Stuexen    case vmIntrinsics::_compareAndSwapInt:
3116223132Stuexen    case vmIntrinsics::_compareAndSwapObject:
3117223132Stuexen      append_unsafe_CAS(callee);
3118223132Stuexen      return true;
3119223132Stuexen
3120223132Stuexen    default                       : return false; // do not inline
3121223132Stuexen  }
3122223132Stuexen  // create intrinsic node
3123223132Stuexen  const bool has_receiver = !callee->is_static();
3124223132Stuexen  ValueType* result_type = as_ValueType(callee->return_type());
3125223132Stuexen
3126230104Stuexen  Values* args = state()->pop_arguments(callee->arg_size());
3127223132Stuexen  ValueStack* locks = lock_stack();
3128223132Stuexen  if (profile_calls()) {
3129223132Stuexen    // Don't profile in the special case where the root method
3130223132Stuexen    // is the intrinsic
3131223132Stuexen    if (callee != method()) {
3132223132Stuexen      Value recv = NULL;
3133223132Stuexen      if (has_receiver) {
3134223132Stuexen        recv = args->at(0);
3135223132Stuexen        null_check(recv);
3136223132Stuexen      }
3137223132Stuexen      profile_call(recv, NULL);
3138223132Stuexen    }
3139223132Stuexen  }
3140223132Stuexen
3141223132Stuexen  Intrinsic* result = new Intrinsic(result_type, id, args, has_receiver, lock_stack(),
3142223132Stuexen                                    preserves_state, cantrap);
3143223132Stuexen  // append instruction & push result
3144223132Stuexen  Value value = append_split(result);
3145223132Stuexen  if (result_type != voidType) push(result_type, value);
3146223132Stuexen
3147223162Stuexen#ifndef PRODUCT
3148223132Stuexen  // printing
3149223132Stuexen  if (PrintInlining) {
3150223132Stuexen    print_inline_result(callee, true);
3151223132Stuexen  }
3152224918Stuexen#endif
3153224918Stuexen
3154224918Stuexen  // done
3155223132Stuexen  return true;
3156223132Stuexen}
3157223132Stuexen
3158223162Stuexen
3159223132Stuexenbool GraphBuilder::try_inline_jsr(int jsr_dest_bci) {
3160223132Stuexen  // Introduce a new callee continuation point - all Ret instructions
3161223132Stuexen  // will be replaced with Gotos to this point.
3162223132Stuexen  BlockBegin* cont = block_at(next_bci());
3163223132Stuexen  assert(cont != NULL, "continuation must exist (BlockListBuilder starts a new block after a jsr");
3164223132Stuexen
3165223132Stuexen  // Note: can not assign state to continuation yet, as we have to
3166223132Stuexen  // pick up the state from the Ret instructions.
3167223132Stuexen
3168223132Stuexen  // Push callee scope
3169223132Stuexen  push_scope_for_jsr(cont, jsr_dest_bci);
3170223132Stuexen
3171223132Stuexen  // Temporarily set up bytecode stream so we can append instructions
3172223162Stuexen  // (only using the bci of this stream)
3173223162Stuexen  scope_data()->set_stream(scope_data()->parent()->stream());
3174223162Stuexen
3175223162Stuexen  BlockBegin* jsr_start_block = block_at(jsr_dest_bci);
3176223162Stuexen  assert(jsr_start_block != NULL, "jsr start block must exist");
3177223162Stuexen  assert(!jsr_start_block->is_set(BlockBegin::was_visited_flag), "should not have visited jsr yet");
3178223162Stuexen  Goto* goto_sub = new Goto(jsr_start_block, false);
3179223162Stuexen  goto_sub->set_state(state());
3180223162Stuexen  // Must copy state to avoid wrong sharing when parsing bytecodes
3181223162Stuexen  assert(jsr_start_block->state() == NULL, "should have fresh jsr starting block");
3182223162Stuexen  jsr_start_block->set_state(state()->copy());
3183223162Stuexen  append(goto_sub);
3184224918Stuexen  _block->set_end(goto_sub);
3185224918Stuexen  _last = _block = jsr_start_block;
3186224918Stuexen
3187223162Stuexen  // Clear out bytecode stream
3188223162Stuexen  scope_data()->set_stream(NULL);
3189223162Stuexen
3190223162Stuexen  scope_data()->add_to_work_list(jsr_start_block);
3191223162Stuexen
3192223162Stuexen  // Ready to resume parsing in subroutine
3193223162Stuexen  iterate_all_blocks();
3194223162Stuexen
3195223162Stuexen  // If we bailed out during parsing, return immediately (this is bad news)
3196223162Stuexen  CHECK_BAILOUT_(false);
3197223162Stuexen
3198223162Stuexen  // Detect whether the continuation can actually be reached. If not,
3199223162Stuexen  // it has not had state set by the join() operations in
3200223162Stuexen  // iterate_bytecodes_for_block()/ret() and we should not touch the
3201224641Stuexen  // iteration state. The calling activation of
3202224641Stuexen  // iterate_bytecodes_for_block will then complete normally.
3203224641Stuexen  if (cont->state() != NULL) {
3204224641Stuexen    if (!cont->is_set(BlockBegin::was_visited_flag)) {
3205275567Stuexen      // add continuation to work list instead of parsing it immediately
3206224641Stuexen      scope_data()->parent()->add_to_work_list(cont);
3207275567Stuexen    }
3208275567Stuexen  }
3209275567Stuexen
3210275567Stuexen  assert(jsr_continuation() == cont, "continuation must not have changed");
3211275567Stuexen  assert(!jsr_continuation()->is_set(BlockBegin::was_visited_flag) ||
3212224641Stuexen         jsr_continuation()->is_set(BlockBegin::parser_loop_header_flag),
3213224641Stuexen         "continuation can only be visited in case of backward branches");
3214224641Stuexen  assert(_last && _last->as_BlockEnd(), "block must have end");
3215275567Stuexen
3216275567Stuexen  // continuation is in work list, so end iteration of current block
3217275567Stuexen  _skip_block = true;
3218275567Stuexen  pop_scope_for_jsr();
3219275567Stuexen
3220275567Stuexen  return true;
3221275567Stuexen}
3222275567Stuexen
3223275567Stuexen
3224275567Stuexen// Inline the entry of a synchronized method as a monitor enter and
3225275567Stuexen// register the exception handler which releases the monitor if an
3226224641Stuexen// exception is thrown within the callee. Note that the monitor enter
3227275567Stuexen// cannot throw an exception itself, because the receiver is
3228275567Stuexen// guaranteed to be non-null by the explicit null check at the
3229275567Stuexen// beginning of inlining.
3230275567Stuexenvoid GraphBuilder::inline_sync_entry(Value lock, BlockBegin* sync_handler) {
3231275567Stuexen  assert(lock != NULL && sync_handler != NULL, "lock or handler missing");
3232275567Stuexen
3233275567Stuexen  set_exception_state(state()->copy());
3234275567Stuexen  monitorenter(lock, SynchronizationEntryBCI);
3235224641Stuexen  assert(_last->as_MonitorEnter() != NULL, "monitor enter expected");
3236224641Stuexen  _last->set_needs_null_check(false);
3237224641Stuexen
3238224641Stuexen  sync_handler->set(BlockBegin::exception_entry_flag);
3239224641Stuexen  sync_handler->set(BlockBegin::is_on_work_list_flag);
3240224641Stuexen
3241224641Stuexen  ciExceptionHandler* desc = new ciExceptionHandler(method()->holder(), 0, method()->code_size(), -1, 0);
3242275567Stuexen  XHandler* h = new XHandler(desc);
3243224641Stuexen  h->set_entry_block(sync_handler);
3244275567Stuexen  scope_data()->xhandlers()->append(h);
3245224641Stuexen  scope_data()->set_has_handler();
3246224641Stuexen}
3247224641Stuexen
3248224641Stuexen
3249275567Stuexen// If an exception is thrown and not handled within an inlined
3250224641Stuexen// synchronized method, the monitor must be released before the
3251275567Stuexen// exception is rethrown in the outer scope. Generate the appropriate
3252224641Stuexen// instructions here.
3253224641Stuexenvoid GraphBuilder::fill_sync_handler(Value lock, BlockBegin* sync_handler, bool default_handler) {
3254275567Stuexen  BlockBegin* orig_block = _block;
3255275567Stuexen  ValueStack* orig_state = _state;
3256224641Stuexen  Instruction* orig_last = _last;
3257224641Stuexen  _last = _block = sync_handler;
3258224641Stuexen  _state = sync_handler->state()->copy();
3259224641Stuexen
3260224641Stuexen  assert(sync_handler != NULL, "handler missing");
3261224641Stuexen  assert(!sync_handler->is_set(BlockBegin::was_visited_flag), "is visited here");
3262224641Stuexen
3263224641Stuexen  assert(lock != NULL || default_handler, "lock or handler missing");
3264275567Stuexen
3265224641Stuexen  XHandler* h = scope_data()->xhandlers()->remove_last();
3266224641Stuexen  assert(h->entry_block() == sync_handler, "corrupt list of handlers");
3267275567Stuexen
3268224641Stuexen  block()->set(BlockBegin::was_visited_flag);
3269224641Stuexen  Value exception = append_with_bci(new ExceptionObject(), SynchronizationEntryBCI);
3270224641Stuexen  assert(exception->is_pinned(), "must be");
3271224641Stuexen
3272224641Stuexen  int bci = SynchronizationEntryBCI;
3273224641Stuexen  if (lock) {
3274224641Stuexen    assert(state()->locks_size() > 0 && state()->lock_at(state()->locks_size() - 1) == lock, "lock is missing");
3275224641Stuexen    if (lock->bci() == -99) {
3276224641Stuexen      lock = append_with_bci(lock, -1);
3277224641Stuexen    }
3278224641Stuexen
3279224641Stuexen    // exit the monitor in the context of the synchronized method
3280224641Stuexen    monitorexit(lock, SynchronizationEntryBCI);
3281224641Stuexen
3282224641Stuexen    // exit the context of the synchronized method
3283275567Stuexen    if (!default_handler) {
3284275567Stuexen      pop_scope();
3285224641Stuexen      _state = _state->copy();
3286224641Stuexen      bci = _state->scope()->caller_bci();
3287300733Stuexen      _state = _state->pop_scope()->copy();
3288224641Stuexen    }
3289224641Stuexen  }
3290224641Stuexen
3291300733Stuexen  // perform the throw as if at the the call site
3292224641Stuexen  apush(exception);
3293224641Stuexen
3294224641Stuexen  set_exception_state(state()->copy());
3295224641Stuexen  throw_op(bci);
3296224918Stuexen
3297224918Stuexen  BlockEnd* end = last()->as_BlockEnd();
3298224918Stuexen  block()->set_end(end);
3299224641Stuexen  end->set_state(state());
3300224641Stuexen
3301224641Stuexen  _block = orig_block;
3302224641Stuexen  _state = orig_state;
3303300733Stuexen  _last = orig_last;
3304224641Stuexen}
3305224641Stuexen
3306224641Stuexen
3307224641Stuexenbool GraphBuilder::try_inline_full(ciMethod* callee, bool holder_known) {
3308224641Stuexen  assert(!callee->is_native(), "callee must not be native");
3309224641Stuexen
3310224641Stuexen  // first perform tests of things it's not possible to inline
3311224641Stuexen  if (callee->has_exception_handlers() &&
3312224641Stuexen      !InlineMethodsWithExceptionHandlers) INLINE_BAILOUT("callee has exception handlers");
3313224641Stuexen  if (callee->is_synchronized() &&
3314224641Stuexen      !InlineSynchronizedMethods         ) INLINE_BAILOUT("callee is synchronized");
3315227755Stuexen  if (!callee->holder()->is_initialized()) INLINE_BAILOUT("callee's klass not initialized yet");
3316227755Stuexen  if (!callee->has_balanced_monitors())    INLINE_BAILOUT("callee's monitors do not match");
3317227755Stuexen
3318227755Stuexen  // Proper inlining of methods with jsrs requires a little more work.
3319275567Stuexen  if (callee->has_jsrs()                 ) INLINE_BAILOUT("jsrs not handled properly by inliner yet");
3320227755Stuexen
3321275567Stuexen  // now perform tests that are based on flag settings
3322275567Stuexen  if (inline_level() > MaxInlineLevel                         ) INLINE_BAILOUT("too-deep inlining");
3323275567Stuexen  if (recursive_inline_level(callee) > MaxRecursiveInlineLevel) INLINE_BAILOUT("too-deep recursive inlining");
3324275567Stuexen  if (callee->code_size() > max_inline_size()                 ) INLINE_BAILOUT("callee is too large");
3325275567Stuexen
3326227755Stuexen  // don't inline throwable methods unless the inlining tree is rooted in a throwable class
3327227755Stuexen  if (callee->name() == ciSymbol::object_initializer_name() &&
3328227755Stuexen      callee->holder()->is_subclass_of(ciEnv::current()->Throwable_klass())) {
3329275567Stuexen    // Throwable constructor call
3330275567Stuexen    IRScope* top = scope();
3331275567Stuexen    while (top->caller() != NULL) {
3332275567Stuexen      top = top->caller();
3333275567Stuexen    }
3334275567Stuexen    if (!top->method()->holder()->is_subclass_of(ciEnv::current()->Throwable_klass())) {
3335275567Stuexen      INLINE_BAILOUT("don't inline Throwable constructors");
3336275567Stuexen    }
3337275567Stuexen  }
3338275567Stuexen
3339275567Stuexen  // When SSE2 is used on intel, then no special handling is needed
3340275567Stuexen  // for strictfp because the enum-constant is fixed at compile time,
3341275567Stuexen  // the check for UseSSE2 is needed here
3342275567Stuexen  if (strict_fp_requires_explicit_rounding && UseSSE < 2 && method()->is_strict() != callee->is_strict()) {
3343275567Stuexen    INLINE_BAILOUT("caller and callee have different strict fp requirements");
3344275567Stuexen  }
3345275567Stuexen
3346227755Stuexen  if (compilation()->env()->num_inlined_bytecodes() > DesiredMethodLimit) {
3347275567Stuexen    INLINE_BAILOUT("total inlining greater than DesiredMethodLimit");
3348227755Stuexen  }
3349227755Stuexen
3350227755Stuexen#ifndef PRODUCT
3351227755Stuexen      // printing
3352227755Stuexen  if (PrintInlining) {
3353227755Stuexen    print_inline_result(callee, true);
3354227755Stuexen  }
3355227755Stuexen#endif
3356227755Stuexen
3357227755Stuexen  // NOTE: Bailouts from this point on, which occur at the
3358275567Stuexen  // GraphBuilder level, do not cause bailout just of the inlining but
3359227755Stuexen  // in fact of the entire compilation.
3360227755Stuexen
3361227755Stuexen  BlockBegin* orig_block = block();
3362227755Stuexen
3363275567Stuexen  const int args_base = state()->stack_size() - callee->arg_size();
3364227755Stuexen  assert(args_base >= 0, "stack underflow during inlining");
3365275567Stuexen
3366227755Stuexen  // Insert null check if necessary
3367227755Stuexen  Value recv = NULL;
3368275567Stuexen  if (code() != Bytecodes::_invokestatic) {
3369275567Stuexen    // note: null check must happen even if first instruction of callee does
3370227755Stuexen    //       an implicit null check since the callee is in a different scope
3371227755Stuexen    //       and we must make sure exception handling does the right thing
3372227755Stuexen    assert(!callee->is_static(), "callee must not be static");
3373227755Stuexen    assert(callee->arg_size() > 0, "must have at least a receiver");
3374227755Stuexen    recv = state()->stack_at(args_base);
3375227755Stuexen    null_check(recv);
3376227755Stuexen  }
3377227755Stuexen
3378275567Stuexen  if (profile_inlined_calls()) {
3379227755Stuexen    profile_call(recv, holder_known ? callee->holder() : NULL);
3380227755Stuexen  }
3381275567Stuexen
3382227755Stuexen  profile_invocation(callee);
3383227755Stuexen
3384227755Stuexen  // Introduce a new callee continuation point - if the callee has
3385227755Stuexen  // more than one return instruction or the return does not allow
3386227755Stuexen  // fall-through of control flow, all return instructions of the
3387227755Stuexen  // callee will need to be replaced by Goto's pointing to this
3388227755Stuexen  // continuation point.
3389227755Stuexen  BlockBegin* cont = block_at(next_bci());
3390227755Stuexen  bool continuation_existed = true;
3391227755Stuexen  if (cont == NULL) {
3392227755Stuexen    cont = new BlockBegin(next_bci());
3393227755Stuexen    // low number so that continuation gets parsed as early as possible
3394227755Stuexen    cont->set_depth_first_number(0);
3395227755Stuexen#ifndef PRODUCT
3396227755Stuexen    if (PrintInitialBlockList) {
3397275567Stuexen      tty->print_cr("CFG: created block %d (bci %d) as continuation for inline at bci %d",
3398227755Stuexen                    cont->block_id(), cont->bci(), bci());
3399227755Stuexen    }
3400227755Stuexen#endif
3401227755Stuexen    continuation_existed = false;
3402227755Stuexen  }
3403227755Stuexen  // Record number of predecessors of continuation block before
3404227755Stuexen  // inlining, to detect if inlined method has edges to its
3405227755Stuexen  // continuation after inlining.
3406227755Stuexen  int continuation_preds = cont->number_of_preds();
3407227755Stuexen
3408227755Stuexen  // Push callee scope
3409227755Stuexen  push_scope(callee, cont);
3410227755Stuexen
3411227755Stuexen  // the BlockListBuilder for the callee could have bailed out
3412227755Stuexen  CHECK_BAILOUT_(false);
3413227755Stuexen
3414227755Stuexen  // Temporarily set up bytecode stream so we can append instructions
3415227755Stuexen  // (only using the bci of this stream)
3416227755Stuexen  scope_data()->set_stream(scope_data()->parent()->stream());
3417257274Stuexen
3418227755Stuexen  // Pass parameters into callee state: add assignments
3419227755Stuexen  // note: this will also ensure that all arguments are computed before being passed
3420227755Stuexen  ValueStack* callee_state = state();
3421269436Stuexen  ValueStack* caller_state = scope()->caller_state();
3422269436Stuexen  { int i = args_base;
3423269436Stuexen    while (i < caller_state->stack_size()) {
3424269436Stuexen      const int par_no = i - args_base;
3425269436Stuexen      Value  arg = caller_state->stack_at_inc(i);
3426269436Stuexen      // NOTE: take base() of arg->type() to avoid problems storing
3427269436Stuexen      // constants
3428269436Stuexen      store_local(callee_state, arg, arg->type()->base(), par_no);
3429269436Stuexen    }
3430269436Stuexen  }
3431269436Stuexen
3432269436Stuexen  // Remove args from stack.
3433269436Stuexen  // Note that we preserve locals state in case we can use it later
3434269436Stuexen  // (see use of pop_scope() below)
3435269436Stuexen  caller_state->truncate_stack(args_base);
3436269436Stuexen  callee_state->truncate_stack(args_base);
3437269436Stuexen
3438269436Stuexen  // Setup state that is used at returns form the inlined method.
3439269436Stuexen  // This is essentially the state of the continuation block,
3440269436Stuexen  // but without the return value on stack, if any, this will
3441269436Stuexen  // be pushed at the return instruction (see method_return).
3442269436Stuexen  scope_data()->set_continuation_state(caller_state->copy());
3443269436Stuexen
3444269436Stuexen  // Compute lock stack size for callee scope now that args have been passed
3445269436Stuexen  scope()->compute_lock_stack_size();
3446269436Stuexen
3447269436Stuexen  Value lock;
3448269448Stuexen  BlockBegin* sync_handler;
3449269448Stuexen
3450269448Stuexen  // Inline the locking of the receiver if the callee is synchronized
3451269448Stuexen  if (callee->is_synchronized()) {
3452269448Stuexen    lock = callee->is_static() ? append(new Constant(new InstanceConstant(callee->holder()->java_mirror())))
3453269448Stuexen                               : state()->local_at(0);
3454269448Stuexen    sync_handler = new BlockBegin(-1);
3455269448Stuexen    inline_sync_entry(lock, sync_handler);
3456269448Stuexen
3457269448Stuexen    // recompute the lock stack size
3458269448Stuexen    scope()->compute_lock_stack_size();
3459269448Stuexen  }
3460269448Stuexen
3461269448Stuexen
3462269448Stuexen  BlockBegin* callee_start_block = block_at(0);
3463269448Stuexen  if (callee_start_block != NULL) {
3464269448Stuexen    assert(callee_start_block->is_set(BlockBegin::parser_loop_header_flag), "must be loop header");
3465269448Stuexen    Goto* goto_callee = new Goto(callee_start_block, false);
3466269448Stuexen    goto_callee->set_state(state());
3467269448Stuexen    // The state for this goto is in the scope of the callee, so use
3468269448Stuexen    // the entry bci for the callee instead of the call site bci.
3469269448Stuexen    append_with_bci(goto_callee, 0);
3470269448Stuexen    _block->set_end(goto_callee);
3471269448Stuexen    callee_start_block->merge(callee_state);
3472269448Stuexen
3473269448Stuexen    _last = _block = callee_start_block;
3474269448Stuexen
3475269858Stuexen    scope_data()->add_to_work_list(callee_start_block);
3476269858Stuexen  }
3477269858Stuexen
3478269858Stuexen  // Clear out bytecode stream
3479269858Stuexen  scope_data()->set_stream(NULL);
3480269858Stuexen
3481269858Stuexen  // Ready to resume parsing in callee (either in the same block we
3482269858Stuexen  // were in before or in the callee's start block)
3483269858Stuexen  iterate_all_blocks(callee_start_block == NULL);
3484269858Stuexen
3485269858Stuexen  // If we bailed out during parsing, return immediately (this is bad news)
3486269858Stuexen  if (bailed_out()) return false;
3487269858Stuexen
3488269858Stuexen  // iterate_all_blocks theoretically traverses in random order; in
3489269858Stuexen  // practice, we have only traversed the continuation if we are
3490269858Stuexen  // inlining into a subroutine
3491269858Stuexen  assert(continuation_existed ||
3492269858Stuexen         !continuation()->is_set(BlockBegin::was_visited_flag),
3493269858Stuexen         "continuation should not have been parsed yet if we created it");
3494269858Stuexen
3495269858Stuexen  // If we bailed out during parsing, return immediately (this is bad news)
3496269858Stuexen  CHECK_BAILOUT_(false);
3497269858Stuexen
3498269858Stuexen  // At this point we are almost ready to return and resume parsing of
3499269858Stuexen  // the caller back in the GraphBuilder. The only thing we want to do
3500269858Stuexen  // first is an optimization: during parsing of the callee we
3501269858Stuexen  // generated at least one Goto to the continuation block. If we
3502269858Stuexen  // generated exactly one, and if the inlined method spanned exactly
3503269858Stuexen  // one block (and we didn't have to Goto its entry), then we snip
3504269858Stuexen  // off the Goto to the continuation, allowing control to fall
3505269858Stuexen  // through back into the caller block and effectively performing
3506269858Stuexen  // block merging. This allows load elimination and CSE to take place
3507269858Stuexen  // across multiple callee scopes if they are relatively simple, and
3508269858Stuexen  // is currently essential to making inlining profitable.
3509269858Stuexen  if (   num_returns() == 1
3510269858Stuexen      && block() == orig_block
3511269858Stuexen      && block() == inline_cleanup_block()) {
3512269858Stuexen    _last = inline_cleanup_return_prev();
3513269858Stuexen    _state = inline_cleanup_state()->pop_scope();
3514269858Stuexen  } else if (continuation_preds == cont->number_of_preds()) {
3515269858Stuexen    // Inlining caused that the instructions after the invoke in the
3516269858Stuexen    // caller are not reachable any more. So skip filling this block
3517269858Stuexen    // with instructions!
3518269858Stuexen    assert (cont == continuation(), "");
3519269858Stuexen    assert(_last && _last->as_BlockEnd(), "");
3520269858Stuexen    _skip_block = true;
3521269858Stuexen  } else {
3522269858Stuexen    // Resume parsing in continuation block unless it was already parsed.
3523269858Stuexen    // Note that if we don't change _last here, iteration in
3524269858Stuexen    // iterate_bytecodes_for_block will stop when we return.
3525269858Stuexen    if (!continuation()->is_set(BlockBegin::was_visited_flag)) {
3526269858Stuexen      // add continuation to work list instead of parsing it immediately
3527269858Stuexen      assert(_last && _last->as_BlockEnd(), "");
3528269858Stuexen      scope_data()->parent()->add_to_work_list(continuation());
3529269527Stuexen      _skip_block = true;
3530269527Stuexen    }
3531269527Stuexen  }
3532269527Stuexen
3533269527Stuexen  // Fill the exception handler for synchronized methods with instructions
3534269527Stuexen  if (callee->is_synchronized() && sync_handler->state() != NULL) {
3535269527Stuexen    fill_sync_handler(lock, sync_handler);
3536269527Stuexen  } else {
3537269527Stuexen    pop_scope();
3538269527Stuexen  }
3539269527Stuexen
3540269527Stuexen  compilation()->notice_inlined_method(callee);
3541269527Stuexen
3542269527Stuexen  return true;
3543269527Stuexen}
3544269527Stuexen
3545269527Stuexen
3546269527Stuexenvoid GraphBuilder::inline_bailout(const char* msg) {
3547269527Stuexen  assert(msg != NULL, "inline bailout msg must exist");
3548269527Stuexen  _inline_bailout_msg = msg;
3549269527Stuexen}
3550269527Stuexen
3551269527Stuexen
3552269527Stuexenvoid GraphBuilder::clear_inline_bailout() {
3553269527Stuexen  _inline_bailout_msg = NULL;
3554269527Stuexen}
3555269527Stuexen
3556269475Stuexen
3557269475Stuexenvoid GraphBuilder::push_root_scope(IRScope* scope, BlockList* bci2block, BlockBegin* start) {
3558269475Stuexen  ScopeData* data = new ScopeData(NULL);
3559269475Stuexen  data->set_scope(scope);
3560269475Stuexen  data->set_bci2block(bci2block);
3561269475Stuexen  _scope_data = data;
3562269475Stuexen  _block = start;
3563269475Stuexen}
3564269475Stuexen
3565269475Stuexen
3566269475Stuexenvoid GraphBuilder::push_scope(ciMethod* callee, BlockBegin* continuation) {
3567269475Stuexen  IRScope* callee_scope = new IRScope(compilation(), scope(), bci(), callee, -1, false);
3568269475Stuexen  scope()->add_callee(callee_scope);
3569269475Stuexen
3570269475Stuexen  BlockListBuilder blb(compilation(), callee_scope, -1);
3571269475Stuexen  CHECK_BAILOUT();
3572269475Stuexen
3573269475Stuexen  if (!blb.bci2block()->at(0)->is_set(BlockBegin::parser_loop_header_flag)) {
3574269475Stuexen    // this scope can be inlined directly into the caller so remove
3575269475Stuexen    // the block at bci 0.
3576269475Stuexen    blb.bci2block()->at_put(0, NULL);
3577269475Stuexen  }
3578269475Stuexen
3579269475Stuexen  callee_scope->set_caller_state(state());
3580269475Stuexen  set_state(state()->push_scope(callee_scope));
3581269475Stuexen
3582269475Stuexen  ScopeData* data = new ScopeData(scope_data());
3583269481Stuexen  data->set_scope(callee_scope);
3584269481Stuexen  data->set_bci2block(blb.bci2block());
3585269481Stuexen  data->set_continuation(continuation);
3586269481Stuexen  _scope_data = data;
3587269481Stuexen}
3588269481Stuexen
3589269481Stuexen
3590269481Stuexenvoid GraphBuilder::push_scope_for_jsr(BlockBegin* jsr_continuation, int jsr_dest_bci) {
3591269481Stuexen  ScopeData* data = new ScopeData(scope_data());
3592269481Stuexen  data->set_parsing_jsr();
3593269481Stuexen  data->set_jsr_entry_bci(jsr_dest_bci);
3594269481Stuexen  data->set_jsr_return_address_local(-1);
3595269481Stuexen  // Must clone bci2block list as we will be mutating it in order to
3596269481Stuexen  // properly clone all blocks in jsr region as well as exception
3597269481Stuexen  // handlers containing rets
3598269481Stuexen  BlockList* new_bci2block = new BlockList(bci2block()->length());
3599269481Stuexen  new_bci2block->push_all(bci2block());
3600269481Stuexen  data->set_bci2block(new_bci2block);
3601269481Stuexen  data->set_scope(scope());
3602269481Stuexen  data->setup_jsr_xhandlers();
3603269481Stuexen  data->set_continuation(continuation());
3604269481Stuexen  if (continuation() != NULL) {
3605269481Stuexen    assert(continuation_state() != NULL, "");
3606269481Stuexen    data->set_continuation_state(continuation_state()->copy());
3607269481Stuexen  }
3608269481Stuexen  data->set_jsr_continuation(jsr_continuation);
3609269481Stuexen  _scope_data = data;
3610235021Stuexen}
3611235021Stuexen
3612235021Stuexen
3613235021Stuexenvoid GraphBuilder::pop_scope() {
3614235021Stuexen  int number_of_locks = scope()->number_of_locks();
3615235021Stuexen  _scope_data = scope_data()->parent();
3616235021Stuexen  // accumulate minimum number of monitor slots to be reserved
3617235021Stuexen  scope()->set_min_number_of_locks(number_of_locks);
3618235021Stuexen}
3619235021Stuexen
3620235021Stuexen
3621235021Stuexenvoid GraphBuilder::pop_scope_for_jsr() {
3622235021Stuexen  _scope_data = scope_data()->parent();
3623235021Stuexen}
3624235021Stuexen
3625235021Stuexenbool GraphBuilder::append_unsafe_get_obj(ciMethod* callee, BasicType t, bool is_volatile) {
3626235021Stuexen  if (InlineUnsafeOps) {
3627235021Stuexen    Values* args = state()->pop_arguments(callee->arg_size());
3628235021Stuexen    null_check(args->at(0));
3629235021Stuexen    Instruction* offset = args->at(2);
3630235021Stuexen#ifndef _LP64
3631235021Stuexen    offset = append(new Convert(Bytecodes::_l2i, offset, as_ValueType(T_INT)));
3632235021Stuexen#endif
3633235021Stuexen    Instruction* op = append(new UnsafeGetObject(t, args->at(1), offset, is_volatile));
3634235021Stuexen    push(op->type(), op);
3635235021Stuexen    compilation()->set_has_unsafe_access(true);
3636235021Stuexen  }
3637269945Stuexen  return InlineUnsafeOps;
3638269945Stuexen}
3639269945Stuexen
3640269945Stuexen
3641269945Stuexenbool GraphBuilder::append_unsafe_put_obj(ciMethod* callee, BasicType t, bool is_volatile) {
3642269945Stuexen  if (InlineUnsafeOps) {
3643269945Stuexen    Values* args = state()->pop_arguments(callee->arg_size());
3644269945Stuexen    null_check(args->at(0));
3645269945Stuexen    Instruction* offset = args->at(2);
3646269945Stuexen#ifndef _LP64
3647269945Stuexen    offset = append(new Convert(Bytecodes::_l2i, offset, as_ValueType(T_INT)));
3648269945Stuexen#endif
3649269945Stuexen    Instruction* op = append(new UnsafePutObject(t, args->at(1), offset, args->at(3), is_volatile));
3650275967Stuexen    compilation()->set_has_unsafe_access(true);
3651269945Stuexen    kill_all();
3652275967Stuexen  }
3653275967Stuexen  return InlineUnsafeOps;
3654269945Stuexen}
3655269945Stuexen
3656269945Stuexen
3657269945Stuexenbool GraphBuilder::append_unsafe_get_raw(ciMethod* callee, BasicType t) {
3658269945Stuexen  if (InlineUnsafeOps) {
3659269945Stuexen    Values* args = state()->pop_arguments(callee->arg_size());
3660269945Stuexen    null_check(args->at(0));
3661275954Stuexen    Instruction* op = append(new UnsafeGetRaw(t, args->at(1), false));
3662275954Stuexen    push(op->type(), op);
3663275967Stuexen    compilation()->set_has_unsafe_access(true);
3664275967Stuexen  }
3665275954Stuexen  return InlineUnsafeOps;
3666275954Stuexen}
3667275954Stuexen
3668269945Stuexen
3669269945Stuexenbool GraphBuilder::append_unsafe_put_raw(ciMethod* callee, BasicType t) {
3670269945Stuexen  if (InlineUnsafeOps) {
3671269945Stuexen    Values* args = state()->pop_arguments(callee->arg_size());
3672269945Stuexen    null_check(args->at(0));
3673269945Stuexen    Instruction* op = append(new UnsafePutRaw(t, args->at(1), args->at(2)));
3674269945Stuexen    compilation()->set_has_unsafe_access(true);
3675269945Stuexen  }
3676269945Stuexen  return InlineUnsafeOps;
3677269945Stuexen}
3678269945Stuexen
3679269945Stuexen
3680269945Stuexenbool GraphBuilder::append_unsafe_prefetch(ciMethod* callee, bool is_static, bool is_store) {
3681269945Stuexen  if (InlineUnsafeOps) {
3682269945Stuexen    Values* args = state()->pop_arguments(callee->arg_size());
3683269945Stuexen    int obj_arg_index = 1; // Assume non-static case
3684269945Stuexen    if (is_static) {
3685269945Stuexen      obj_arg_index = 0;
3686269945Stuexen    } else {
3687275967Stuexen      null_check(args->at(0));
3688275967Stuexen    }
3689269945Stuexen    Instruction* offset = args->at(obj_arg_index + 1);
3690269945Stuexen#ifndef _LP64
3691269945Stuexen    offset = append(new Convert(Bytecodes::_l2i, offset, as_ValueType(T_INT)));
3692269945Stuexen#endif
3693269945Stuexen    Instruction* op = is_store ? append(new UnsafePrefetchWrite(args->at(obj_arg_index), offset))
3694269945Stuexen                               : append(new UnsafePrefetchRead (args->at(obj_arg_index), offset));
3695269945Stuexen    compilation()->set_has_unsafe_access(true);
3696269945Stuexen  }
3697269945Stuexen  return InlineUnsafeOps;
3698269945Stuexen}
3699269945Stuexen
3700269945Stuexen
3701269945Stuexenvoid GraphBuilder::append_unsafe_CAS(ciMethod* callee) {
3702269945Stuexen  ValueType* result_type = as_ValueType(callee->return_type());
3703269945Stuexen  assert(result_type->is_int(), "int result");
3704279859Stuexen  Values* args = state()->pop_arguments(callee->arg_size());
3705279859Stuexen
3706279859Stuexen  // Pop off some args to speically handle, then push back
3707279859Stuexen  Value newval = args->pop();
3708279859Stuexen  Value cmpval = args->pop();
3709279859Stuexen  Value offset = args->pop();
3710279859Stuexen  Value src = args->pop();
3711279859Stuexen  Value unsafe_obj = args->pop();
3712279859Stuexen
3713279859Stuexen  // Separately handle the unsafe arg. It is not needed for code
3714279859Stuexen  // generation, but must be null checked
3715279859Stuexen  null_check(unsafe_obj);
3716279859Stuexen
3717279859Stuexen#ifndef _LP64
3718279859Stuexen  offset = append(new Convert(Bytecodes::_l2i, offset, as_ValueType(T_INT)));
3719279859Stuexen#endif
3720279859Stuexen
3721279859Stuexen  args->push(src);
3722279859Stuexen  args->push(offset);
3723279859Stuexen  args->push(cmpval);
3724279859Stuexen  args->push(newval);
3725279859Stuexen
3726279859Stuexen  // An unsafe CAS can alias with other field accesses, but we don't
3727279859Stuexen  // know which ones so mark the state as no preserved.  This will
3728279859Stuexen  // cause CSE to invalidate memory across it.
3729279859Stuexen  bool preserves_state = false;
3730279859Stuexen  Intrinsic* result = new Intrinsic(result_type, callee->intrinsic_id(), args, false, lock_stack(), preserves_state);
3731163953Srrs  append_split(result);
3732171943Srrs  push(result_type, result);
3733163953Srrs  compilation()->set_has_unsafe_access(true);
3734163953Srrs}
3735163953Srrs
3736223132Stuexen
3737223132Stuexen#ifndef PRODUCT
3738223132Stuexenvoid GraphBuilder::print_inline_result(ciMethod* callee, bool res) {
3739163953Srrs  const char sync_char      = callee->is_synchronized()        ? 's' : ' ';
3740163953Srrs  const char exception_char = callee->has_exception_handlers() ? '!' : ' ';
3741163953Srrs  const char monitors_char  = callee->has_monitor_bytecodes()  ? 'm' : ' ';
3742163953Srrs  tty->print("     %c%c%c ", sync_char, exception_char, monitors_char);
3743166675Srrs  for (int i = 0; i < scope()->level(); i++) tty->print("  ");
3744166675Srrs  if (res) {
3745163953Srrs    tty->print("  ");
3746166675Srrs  } else {
3747166675Srrs    tty->print("- ");
3748163953Srrs  }
3749171943Srrs  tty->print("@ %d  ", bci());
3750167598Srrs  callee->print_short_name();
3751163953Srrs  tty->print(" (%d bytes)", callee->code_size());
3752166675Srrs  if (_inline_bailout_msg) {
3753169420Srrs    tty->print("  %s", _inline_bailout_msg);
3754171943Srrs  }
3755163953Srrs  tty->cr();
3756163953Srrs
3757163953Srrs  if (res && CIPrintMethodCodes) {
3758233005Stuexen    callee->print_codes();
3759169420Srrs  }
3760171943Srrs}
3761228907Stuexen
3762167598Srrs
3763168299Srrsvoid GraphBuilder::print_stats() {
3764163953Srrs  vmap()->print();
3765163953Srrs}
3766166675Srrs#endif // PRODUCT
3767163953Srrs
3768163953Srrs
3769163953Srrsvoid GraphBuilder::profile_call(Value recv, ciKlass* known_holder) {
3770163953Srrs  append(new ProfileCall(method(), bci(), recv, known_holder));
3771163953Srrs}
3772163953Srrs
3773163953Srrs
3774163953Srrsvoid GraphBuilder::profile_invocation(ciMethod* callee) {
3775166675Srrs  if (profile_calls()) {
3776163953Srrs    // increment the interpreter_invocation_count for the inlinee
3777163953Srrs    Value m = append(new Constant(new ObjectConstant(callee)));
3778163953Srrs    append(new ProfileCounter(m, methodOopDesc::interpreter_invocation_counter_offset_in_bytes(), 1));
3779166675Srrs  }
3780163953Srrs}
3781163953Srrs
3782163953Srrs
3783163953Srrsvoid GraphBuilder::profile_bci(int bci) {
3784171943Srrs  if (profile_branches()) {
3785171943Srrs    ciMethodData* md = method()->method_data();
3786171943Srrs    if (md == NULL) {
3787171943Srrs      BAILOUT("out of memory building methodDataOop");
3788171943Srrs    }
3789224641Stuexen    ciProfileData* data = md->bci_to_data(bci);
3790224641Stuexen    assert(data != NULL && data->is_JumpData(), "need JumpData for goto");
3791224641Stuexen    Value mdo = append(new Constant(new ObjectConstant(md)));
3792224641Stuexen    append(new ProfileCounter(mdo, md->byte_offset_of_slot(data, JumpData::taken_offset()), 1));
3793224641Stuexen  }
3794224641Stuexen}
3795171943Srrs