c1_Optimizer.cpp revision 1739:f02a8bbe6ed4
177957Sbenno/*
277957Sbenno * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
377957Sbenno * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
477957Sbenno *
577957Sbenno * This code is free software; you can redistribute it and/or modify it
677957Sbenno * under the terms of the GNU General Public License version 2 only, as
777957Sbenno * published by the Free Software Foundation.
877957Sbenno *
977957Sbenno * This code is distributed in the hope that it will be useful, but WITHOUT
1077957Sbenno * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1177957Sbenno * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1277957Sbenno * version 2 for more details (a copy is included in the LICENSE file that
1377957Sbenno * accompanied this code).
1477957Sbenno *
1577957Sbenno * You should have received a copy of the GNU General Public License version
1677957Sbenno * 2 along with this work; if not, write to the Free Software Foundation,
1777957Sbenno * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1877957Sbenno *
1977957Sbenno * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2077957Sbenno * or visit www.oracle.com if you need additional information or have any
2177957Sbenno * questions.
2277957Sbenno *
2377957Sbenno */
2477957Sbenno
2577957Sbenno#include "incls/_precompiled.incl"
2677957Sbenno#include "incls/_c1_Optimizer.cpp.incl"
2777957Sbenno
2877957Sbennodefine_array(ValueSetArray, ValueSet*);
2977957Sbennodefine_stack(ValueSetList, ValueSetArray);
3077957Sbenno
3177957Sbenno
3277957SbennoOptimizer::Optimizer(IR* ir) {
3377957Sbenno  assert(ir->is_valid(), "IR must be valid");
3477957Sbenno  _ir = ir;
3577957Sbenno}
3677957Sbenno
3777957Sbennoclass CE_Eliminator: public BlockClosure {
38176770Sraj private:
39176770Sraj  IR* _hir;
4077957Sbenno  int _cee_count;                                // the number of CEs successfully eliminated
4177957Sbenno  int _has_substitution;
4277957Sbenno
4377957Sbenno public:
4477957Sbenno  CE_Eliminator(IR* hir) : _cee_count(0), _hir(hir) {
45152310Sgrehan    _has_substitution = false;
4677957Sbenno    _hir->iterate_preorder(this);
47152310Sgrehan    if (_has_substitution) {
48152310Sgrehan      // substituted some phis so resolve the substitution
4977957Sbenno      SubstitutionResolver sr(_hir);
5090643Sbenno    }
5190643Sbenno  }
5290643Sbenno  int cee_count() const                          { return _cee_count; }
5390643Sbenno
54152310Sgrehan  void adjust_exception_edges(BlockBegin* block, BlockBegin* sux) {
55152310Sgrehan    int e = sux->number_of_exception_handlers();
56152310Sgrehan    for (int i = 0; i < e; i++) {
57152310Sgrehan      BlockBegin* xhandler = sux->exception_handler_at(i);
58152310Sgrehan      block->add_exception_handler(xhandler);
59152310Sgrehan
60152310Sgrehan      assert(xhandler->is_predecessor(sux), "missing predecessor");
61152310Sgrehan      if (sux->number_of_preds() == 0) {
62152310Sgrehan        // sux is disconnected from graph so disconnect from exception handlers
63152310Sgrehan        xhandler->remove_predecessor(sux);
64152310Sgrehan      }
6577957Sbenno      if (!xhandler->is_predecessor(block)) {
66152310Sgrehan        xhandler->add_predecessor(block);
67152310Sgrehan      }
68152310Sgrehan    }
6977957Sbenno  }
7077957Sbenno
7177957Sbenno  virtual void block_do(BlockBegin* block) {
7277957Sbenno    // 1) find conditional expression
7377957Sbenno    // check if block ends with an If
7477957Sbenno    If* if_ = block->end()->as_If();
7577957Sbenno    if (if_ == NULL) return;
7677957Sbenno
7777957Sbenno    // check if If works on int or object types
7877957Sbenno    // (we cannot handle If's working on long, float or doubles yet,
7977957Sbenno    // since IfOp doesn't support them - these If's show up if cmp
8077957Sbenno    // operations followed by If's are eliminated)
8177957Sbenno    ValueType* if_type = if_->x()->type();
8277957Sbenno    if (!if_type->is_int() && !if_type->is_object()) return;
8377957Sbenno
8490643Sbenno    BlockBegin* t_block = if_->tsux();
8590643Sbenno    BlockBegin* f_block = if_->fsux();
8690643Sbenno    Instruction* t_cur = t_block->next();
8790643Sbenno    Instruction* f_cur = f_block->next();
8890643Sbenno
8990643Sbenno    // one Constant may be present between BlockBegin and BlockEnd
9077957Sbenno    Value t_const = NULL;
9196250Sbenno    Value f_const = NULL;
9296250Sbenno    if (t_cur->as_Constant() != NULL && !t_cur->can_trap()) {
93152310Sgrehan      t_const = t_cur;
94152310Sgrehan      t_cur = t_cur->next();
95152310Sgrehan    }
96152310Sgrehan    if (f_cur->as_Constant() != NULL && !f_cur->can_trap()) {
97217044Snwhitehorn      f_const = f_cur;
98152310Sgrehan      f_cur = f_cur->next();
99255415Snwhitehorn    }
100255415Snwhitehorn
101255415Snwhitehorn    // check if both branches end with a goto
102152310Sgrehan    Goto* t_goto = t_cur->as_Goto();
103152310Sgrehan    if (t_goto == NULL) return;
104152310Sgrehan    Goto* f_goto = f_cur->as_Goto();
105152310Sgrehan    if (f_goto == NULL) return;
106152310Sgrehan
107152310Sgrehan    // check if both gotos merge into the same block
108152310Sgrehan    BlockBegin* sux = t_goto->default_sux();
109152310Sgrehan    if (sux != f_goto->default_sux()) return;
110152310Sgrehan
111152310Sgrehan    // check if at least one word was pushed on sux_state
112152310Sgrehan    ValueStack* sux_state = sux->state();
113152310Sgrehan    if (sux_state->stack_size() <= if_->state()->stack_size()) return;
114152310Sgrehan
115152310Sgrehan    // check if phi function is present at end of successor stack and that
116152310Sgrehan    // only this phi was pushed on the stack
117152310Sgrehan    Value sux_phi = sux_state->stack_at(if_->state()->stack_size());
118152310Sgrehan    if (sux_phi == NULL || sux_phi->as_Phi() == NULL || sux_phi->as_Phi()->block() != sux) return;
119152310Sgrehan    if (sux_phi->type()->size() != sux_state->stack_size() - if_->state()->stack_size()) return;
120152310Sgrehan
121152310Sgrehan    // get the values that were pushed in the true- and false-branch
122152310Sgrehan    Value t_value = t_goto->state()->stack_at(if_->state()->stack_size());
123152310Sgrehan    Value f_value = f_goto->state()->stack_at(if_->state()->stack_size());
124152310Sgrehan
125152310Sgrehan    // backend does not support floats
12677957Sbenno    assert(t_value->type()->base() == f_value->type()->base(), "incompatible types");
12777957Sbenno    if (t_value->type()->is_float_kind()) return;
128152310Sgrehan
12977957Sbenno    // check that successor has no other phi functions but sux_phi
13077957Sbenno    // this can happen when t_block or f_block contained additonal stores to local variables
13177957Sbenno    // that are no longer represented by explicit instructions
13277957Sbenno    for_each_phi_fun(sux, phi,
13377957Sbenno                     if (phi != sux_phi) return;
13477957Sbenno                     );
135209975Snwhitehorn    // true and false blocks can't have phis
13677957Sbenno    for_each_phi_fun(t_block, phi, return; );
13777957Sbenno    for_each_phi_fun(f_block, phi, return; );
138183290Snwhitehorn
139209975Snwhitehorn    // 2) substitute conditional expression
14077957Sbenno    //    with an IfOp followed by a Goto
14177957Sbenno    // cut if_ away and get node before
14277957Sbenno    Instruction* cur_end = if_->prev(block);
14377957Sbenno
14477957Sbenno    // append constants of true- and false-block if necessary
14577957Sbenno    // clone constants because original block must not be destroyed
14677957Sbenno    assert((t_value != f_const && f_value != t_const) || t_const == f_const, "mismatch");
14777957Sbenno    if (t_value == t_const) {
14877957Sbenno      t_value = new Constant(t_const->type());
14977957Sbenno      NOT_PRODUCT(t_value->set_printable_bci(if_->printable_bci()));
15077957Sbenno      cur_end = cur_end->set_next(t_value);
15177957Sbenno    }
15277957Sbenno    if (f_value == f_const) {
15377957Sbenno      f_value = new Constant(f_const->type());
15477957Sbenno      NOT_PRODUCT(f_value->set_printable_bci(if_->printable_bci()));
15577957Sbenno      cur_end = cur_end->set_next(f_value);
15677957Sbenno    }
15777957Sbenno
15877957Sbenno    // it is very unlikely that the condition can be statically decided
15977957Sbenno    // (this was checked previously by the Canonicalizer), so always
16077957Sbenno    // append IfOp
161236141Sraj    Value result = new IfOp(if_->x(), if_->cond(), if_->y(), t_value, f_value);
162176770Sraj    NOT_PRODUCT(result->set_printable_bci(if_->printable_bci()));
163176770Sraj    cur_end = cur_end->set_next(result);
164176770Sraj
165176770Sraj    // append Goto to successor
166176770Sraj    ValueStack* state_before = if_->is_safepoint() ? if_->state_before() : NULL;
167176770Sraj    Goto* goto_ = new Goto(sux, state_before, if_->is_safepoint() || t_goto->is_safepoint() || f_goto->is_safepoint());
168176770Sraj
169176770Sraj    // prepare state for Goto
170176770Sraj    ValueStack* goto_state = if_->state();
171176770Sraj    while (sux_state->scope() != goto_state->scope()) {
172176770Sraj      goto_state = goto_state->caller_state();
173176770Sraj      assert(goto_state != NULL, "states do not match up");
174176770Sraj    }
175176770Sraj    goto_state = goto_state->copy(ValueStack::StateAfter, goto_state->bci());
176176770Sraj    goto_state->push(result->type(), result);
177176770Sraj    assert(goto_state->is_same(sux_state), "states must match now");
178176770Sraj    goto_->set_state(goto_state);
179176770Sraj
180176770Sraj    cur_end = cur_end->set_next(goto_, goto_state->bci());
181176770Sraj
182176770Sraj    // Adjust control flow graph
183176770Sraj    BlockBegin::disconnect_edge(block, t_block);
184176770Sraj    BlockBegin::disconnect_edge(block, f_block);
185176770Sraj    if (t_block->number_of_preds() == 0) {
186176770Sraj      BlockBegin::disconnect_edge(t_block, sux);
187176770Sraj    }
188176770Sraj    adjust_exception_edges(block, t_block);
189176770Sraj    if (f_block->number_of_preds() == 0) {
190209975Snwhitehorn      BlockBegin::disconnect_edge(f_block, sux);
191176770Sraj    }
192176770Sraj    adjust_exception_edges(block, f_block);
193176770Sraj
194176770Sraj    // update block end
195176770Sraj    block->set_end(goto_);
196176770Sraj
197176770Sraj    // substitute the phi if possible
198176770Sraj    if (sux_phi->as_Phi()->operand_count() == 1) {
199176770Sraj      assert(sux_phi->as_Phi()->operand_at(0) == result, "screwed up phi");
200176770Sraj      sux_phi->set_subst(result);
201176770Sraj      _has_substitution = true;
202176770Sraj    }
203176770Sraj
204176770Sraj    // 3) successfully eliminated a conditional expression
205176770Sraj    _cee_count++;
206176770Sraj    if (PrintCEE) {
207176770Sraj      tty->print_cr("%d. CEE in B%d (B%d B%d)", cee_count(), block->block_id(), t_block->block_id(), f_block->block_id());
208176770Sraj    }
209176770Sraj
210287015Sjhibbits    _hir->verify();
211287015Sjhibbits  }
212287015Sjhibbits};
213176770Sraj
214176770Sraj
215295520Sjhibbitsvoid Optimizer::eliminate_conditional_expressions() {
216176770Sraj  // find conditional expressions & replace them with IfOps
217176770Sraj  CE_Eliminator ce(ir());
218176770Sraj}
219176770Sraj
220176770Sraj
221236141Srajclass BlockMerger: public BlockClosure {
222236141Sraj private:
223176770Sraj  IR* _hir;
224295520Sjhibbits  int _merge_count;              // the number of block pairs successfully merged
225295520Sjhibbits
226295520Sjhibbits public:
227295520Sjhibbits  BlockMerger(IR* hir)
228295520Sjhibbits  : _hir(hir)
229176770Sraj  , _merge_count(0)
230176770Sraj  {
231295520Sjhibbits    _hir->iterate_preorder(this);
232176770Sraj  }
233176770Sraj
234176770Sraj  bool try_merge(BlockBegin* block) {
235176770Sraj    BlockEnd* end = block->end();
236176770Sraj    if (end->as_Goto() != NULL) {
237176770Sraj      assert(end->number_of_sux() == 1, "end must have exactly one successor");
238176770Sraj      // Note: It would be sufficient to check for the number of successors (= 1)
239176770Sraj      //       in order to decide if this block can be merged potentially. That
240176770Sraj      //       would then also include switch statements w/ only a default case.
241295520Sjhibbits      //       However, in that case we would need to make sure the switch tag
242295520Sjhibbits      //       expression is executed if it can produce observable side effects.
243295520Sjhibbits      //       We should probably have the canonicalizer simplifying such switch
244236141Sraj      //       statements and then we are sure we don't miss these merge opportunities
245236141Sraj      //       here (was bug - gri 7/7/99).
246236141Sraj      BlockBegin* sux = end->default_sux();
247236141Sraj      if (sux->number_of_preds() == 1 && !sux->is_entry_block() && !end->is_safepoint()) {
248236141Sraj        // merge the two blocks
249236141Sraj
250236141Sraj#ifdef ASSERT
251236141Sraj        // verify that state at the end of block and at the beginning of sux are equal
252236141Sraj        // no phi functions must be present at beginning of sux
253236141Sraj        ValueStack* sux_state = sux->state();
254236141Sraj        ValueStack* end_state = end->state();
255236141Sraj
256236141Sraj        assert(end_state->scope() == sux_state->scope(), "scopes must match");
257236141Sraj        assert(end_state->stack_size() == sux_state->stack_size(), "stack not equal");
258236141Sraj        assert(end_state->locals_size() == sux_state->locals_size(), "locals not equal");
259236141Sraj
260236141Sraj        int index;
261236141Sraj        Value sux_value;
262236141Sraj        for_each_stack_value(sux_state, index, sux_value) {
263236141Sraj          assert(sux_value == end_state->stack_at(index), "stack not equal");
264176770Sraj        }
265295520Sjhibbits        for_each_local_value(sux_state, index, sux_value) {
266295520Sjhibbits          assert(sux_value == end_state->local_at(index), "locals not equal");
267295520Sjhibbits        }
268295520Sjhibbits        assert(sux_state->caller_state() == end_state->caller_state(), "caller not equal");
269295520Sjhibbits#endif
270176770Sraj
271176770Sraj        // find instruction before end & append first instruction of sux block
272295520Sjhibbits        Instruction* prev = end->prev(block);
273295520Sjhibbits        Instruction* next = sux->next();
274295520Sjhibbits        assert(prev->as_BlockEnd() == NULL, "must not be a BlockEnd");
275295520Sjhibbits        prev->set_next(next);
276295520Sjhibbits        sux->disconnect_from_graph();
277295520Sjhibbits        block->set_end(sux->end());
278295520Sjhibbits        // add exception handlers of deleted block, if any
279295520Sjhibbits        for (int k = 0; k < sux->number_of_exception_handlers(); k++) {
280176770Sraj          BlockBegin* xhandler = sux->exception_handler_at(k);
281285148Sjhibbits          block->add_exception_handler(xhandler);
282176770Sraj
283          // also substitute predecessor of exception handler
284          assert(xhandler->is_predecessor(sux), "missing predecessor");
285          xhandler->remove_predecessor(sux);
286          if (!xhandler->is_predecessor(block)) {
287            xhandler->add_predecessor(block);
288          }
289        }
290
291        // debugging output
292        _merge_count++;
293        if (PrintBlockElimination) {
294          tty->print_cr("%d. merged B%d & B%d (stack size = %d)",
295                        _merge_count, block->block_id(), sux->block_id(), sux->state()->stack_size());
296        }
297
298        _hir->verify();
299
300        If* if_ = block->end()->as_If();
301        if (if_) {
302          IfOp* ifop    = if_->x()->as_IfOp();
303          Constant* con = if_->y()->as_Constant();
304          bool swapped = false;
305          if (!con || !ifop) {
306            ifop = if_->y()->as_IfOp();
307            con  = if_->x()->as_Constant();
308            swapped = true;
309          }
310          if (con && ifop) {
311            Constant* tval = ifop->tval()->as_Constant();
312            Constant* fval = ifop->fval()->as_Constant();
313            if (tval && fval) {
314              // Find the instruction before if_, starting with ifop.
315              // When if_ and ifop are not in the same block, prev
316              // becomes NULL In such (rare) cases it is not
317              // profitable to perform the optimization.
318              Value prev = ifop;
319              while (prev != NULL && prev->next() != if_) {
320                prev = prev->next();
321              }
322
323              if (prev != NULL) {
324                Instruction::Condition cond = if_->cond();
325                BlockBegin* tsux = if_->tsux();
326                BlockBegin* fsux = if_->fsux();
327                if (swapped) {
328                  cond = Instruction::mirror(cond);
329                }
330
331                BlockBegin* tblock = tval->compare(cond, con, tsux, fsux);
332                BlockBegin* fblock = fval->compare(cond, con, tsux, fsux);
333                if (tblock != fblock && !if_->is_safepoint()) {
334                  If* newif = new If(ifop->x(), ifop->cond(), false, ifop->y(),
335                                     tblock, fblock, if_->state_before(), if_->is_safepoint());
336                  newif->set_state(if_->state()->copy());
337
338                  assert(prev->next() == if_, "must be guaranteed by above search");
339                  NOT_PRODUCT(newif->set_printable_bci(if_->printable_bci()));
340                  prev->set_next(newif);
341                  block->set_end(newif);
342
343                  _merge_count++;
344                  if (PrintBlockElimination) {
345                    tty->print_cr("%d. replaced If and IfOp at end of B%d with single If", _merge_count, block->block_id());
346                  }
347
348                  _hir->verify();
349                }
350              }
351            }
352          }
353        }
354
355        return true;
356      }
357    }
358    return false;
359  }
360
361  virtual void block_do(BlockBegin* block) {
362    _hir->verify();
363    // repeat since the same block may merge again
364    while (try_merge(block)) {
365      _hir->verify();
366    }
367  }
368};
369
370
371void Optimizer::eliminate_blocks() {
372  // merge blocks if possible
373  BlockMerger bm(ir());
374}
375
376
377class NullCheckEliminator;
378class NullCheckVisitor: public InstructionVisitor {
379private:
380  NullCheckEliminator* _nce;
381  NullCheckEliminator* nce() { return _nce; }
382
383public:
384  NullCheckVisitor() {}
385
386  void set_eliminator(NullCheckEliminator* nce) { _nce = nce; }
387
388  void do_Phi            (Phi*             x);
389  void do_Local          (Local*           x);
390  void do_Constant       (Constant*        x);
391  void do_LoadField      (LoadField*       x);
392  void do_StoreField     (StoreField*      x);
393  void do_ArrayLength    (ArrayLength*     x);
394  void do_LoadIndexed    (LoadIndexed*     x);
395  void do_StoreIndexed   (StoreIndexed*    x);
396  void do_NegateOp       (NegateOp*        x);
397  void do_ArithmeticOp   (ArithmeticOp*    x);
398  void do_ShiftOp        (ShiftOp*         x);
399  void do_LogicOp        (LogicOp*         x);
400  void do_CompareOp      (CompareOp*       x);
401  void do_IfOp           (IfOp*            x);
402  void do_Convert        (Convert*         x);
403  void do_NullCheck      (NullCheck*       x);
404  void do_Invoke         (Invoke*          x);
405  void do_NewInstance    (NewInstance*     x);
406  void do_NewTypeArray   (NewTypeArray*    x);
407  void do_NewObjectArray (NewObjectArray*  x);
408  void do_NewMultiArray  (NewMultiArray*   x);
409  void do_CheckCast      (CheckCast*       x);
410  void do_InstanceOf     (InstanceOf*      x);
411  void do_MonitorEnter   (MonitorEnter*    x);
412  void do_MonitorExit    (MonitorExit*     x);
413  void do_Intrinsic      (Intrinsic*       x);
414  void do_BlockBegin     (BlockBegin*      x);
415  void do_Goto           (Goto*            x);
416  void do_If             (If*              x);
417  void do_IfInstanceOf   (IfInstanceOf*    x);
418  void do_TableSwitch    (TableSwitch*     x);
419  void do_LookupSwitch   (LookupSwitch*    x);
420  void do_Return         (Return*          x);
421  void do_Throw          (Throw*           x);
422  void do_Base           (Base*            x);
423  void do_OsrEntry       (OsrEntry*        x);
424  void do_ExceptionObject(ExceptionObject* x);
425  void do_RoundFP        (RoundFP*         x);
426  void do_UnsafeGetRaw   (UnsafeGetRaw*    x);
427  void do_UnsafePutRaw   (UnsafePutRaw*    x);
428  void do_UnsafeGetObject(UnsafeGetObject* x);
429  void do_UnsafePutObject(UnsafePutObject* x);
430  void do_UnsafePrefetchRead (UnsafePrefetchRead*  x);
431  void do_UnsafePrefetchWrite(UnsafePrefetchWrite* x);
432  void do_ProfileCall    (ProfileCall*     x);
433  void do_ProfileInvoke  (ProfileInvoke*   x);
434};
435
436
437// Because of a static contained within (for the purpose of iteration
438// over instructions), it is only valid to have one of these active at
439// a time
440class NullCheckEliminator: public ValueVisitor {
441 private:
442  Optimizer*        _opt;
443
444  ValueSet*         _visitable_instructions;        // Visit each instruction only once per basic block
445  BlockList*        _work_list;                   // Basic blocks to visit
446
447  bool visitable(Value x) {
448    assert(_visitable_instructions != NULL, "check");
449    return _visitable_instructions->contains(x);
450  }
451  void mark_visited(Value x) {
452    assert(_visitable_instructions != NULL, "check");
453    _visitable_instructions->remove(x);
454  }
455  void mark_visitable(Value x) {
456    assert(_visitable_instructions != NULL, "check");
457    _visitable_instructions->put(x);
458  }
459  void clear_visitable_state() {
460    assert(_visitable_instructions != NULL, "check");
461    _visitable_instructions->clear();
462  }
463
464  ValueSet*         _set;                         // current state, propagated to subsequent BlockBegins
465  ValueSetList      _block_states;                // BlockBegin null-check states for all processed blocks
466  NullCheckVisitor  _visitor;
467  NullCheck*        _last_explicit_null_check;
468
469  bool set_contains(Value x)                      { assert(_set != NULL, "check"); return _set->contains(x); }
470  void set_put     (Value x)                      { assert(_set != NULL, "check"); _set->put(x); }
471  void set_remove  (Value x)                      { assert(_set != NULL, "check"); _set->remove(x); }
472
473  BlockList* work_list()                          { return _work_list; }
474
475  void iterate_all();
476  void iterate_one(BlockBegin* block);
477
478  ValueSet* state()                               { return _set; }
479  void      set_state_from (ValueSet* state)      { _set->set_from(state); }
480  ValueSet* state_for      (BlockBegin* block)    { return _block_states[block->block_id()]; }
481  void      set_state_for  (BlockBegin* block, ValueSet* stack) { _block_states[block->block_id()] = stack; }
482  // Returns true if caused a change in the block's state.
483  bool      merge_state_for(BlockBegin* block,
484                            ValueSet*   incoming_state);
485
486 public:
487  // constructor
488  NullCheckEliminator(Optimizer* opt)
489    : _opt(opt)
490    , _set(new ValueSet())
491    , _last_explicit_null_check(NULL)
492    , _block_states(BlockBegin::number_of_blocks(), NULL)
493    , _work_list(new BlockList()) {
494    _visitable_instructions = new ValueSet();
495    _visitor.set_eliminator(this);
496  }
497
498  Optimizer*  opt()                               { return _opt; }
499  IR*         ir ()                               { return opt()->ir(); }
500
501  // Process a graph
502  void iterate(BlockBegin* root);
503
504  void visit(Value* f);
505
506  // In some situations (like NullCheck(x); getfield(x)) the debug
507  // information from the explicit NullCheck can be used to populate
508  // the getfield, even if the two instructions are in different
509  // scopes; this allows implicit null checks to be used but the
510  // correct exception information to be generated. We must clear the
511  // last-traversed NullCheck when we reach a potentially-exception-
512  // throwing instruction, as well as in some other cases.
513  void        set_last_explicit_null_check(NullCheck* check) { _last_explicit_null_check = check; }
514  NullCheck*  last_explicit_null_check()                     { return _last_explicit_null_check; }
515  Value       last_explicit_null_check_obj()                 { return (_last_explicit_null_check
516                                                                         ? _last_explicit_null_check->obj()
517                                                                         : NULL); }
518  NullCheck*  consume_last_explicit_null_check() {
519    _last_explicit_null_check->unpin(Instruction::PinExplicitNullCheck);
520    _last_explicit_null_check->set_can_trap(false);
521    return _last_explicit_null_check;
522  }
523  void        clear_last_explicit_null_check()               { _last_explicit_null_check = NULL; }
524
525  // Handlers for relevant instructions
526  // (separated out from NullCheckVisitor for clarity)
527
528  // The basic contract is that these must leave the instruction in
529  // the desired state; must not assume anything about the state of
530  // the instruction. We make multiple passes over some basic blocks
531  // and the last pass is the only one whose result is valid.
532  void handle_AccessField     (AccessField* x);
533  void handle_ArrayLength     (ArrayLength* x);
534  void handle_LoadIndexed     (LoadIndexed* x);
535  void handle_StoreIndexed    (StoreIndexed* x);
536  void handle_NullCheck       (NullCheck* x);
537  void handle_Invoke          (Invoke* x);
538  void handle_NewInstance     (NewInstance* x);
539  void handle_NewArray        (NewArray* x);
540  void handle_AccessMonitor   (AccessMonitor* x);
541  void handle_Intrinsic       (Intrinsic* x);
542  void handle_ExceptionObject (ExceptionObject* x);
543  void handle_Phi             (Phi* x);
544};
545
546
547// NEEDS_CLEANUP
548// There may be other instructions which need to clear the last
549// explicit null check. Anything across which we can not hoist the
550// debug information for a NullCheck instruction must clear it. It
551// might be safer to pattern match "NullCheck ; {AccessField,
552// ArrayLength, LoadIndexed}" but it is more easily structured this way.
553// Should test to see performance hit of clearing it for all handlers
554// with empty bodies below. If it is negligible then we should leave
555// that in for safety, otherwise should think more about it.
556void NullCheckVisitor::do_Phi            (Phi*             x) { nce()->handle_Phi(x);      }
557void NullCheckVisitor::do_Local          (Local*           x) {}
558void NullCheckVisitor::do_Constant       (Constant*        x) { /* FIXME: handle object constants */ }
559void NullCheckVisitor::do_LoadField      (LoadField*       x) { nce()->handle_AccessField(x); }
560void NullCheckVisitor::do_StoreField     (StoreField*      x) { nce()->handle_AccessField(x); }
561void NullCheckVisitor::do_ArrayLength    (ArrayLength*     x) { nce()->handle_ArrayLength(x); }
562void NullCheckVisitor::do_LoadIndexed    (LoadIndexed*     x) { nce()->handle_LoadIndexed(x); }
563void NullCheckVisitor::do_StoreIndexed   (StoreIndexed*    x) { nce()->handle_StoreIndexed(x); }
564void NullCheckVisitor::do_NegateOp       (NegateOp*        x) {}
565void NullCheckVisitor::do_ArithmeticOp   (ArithmeticOp*    x) { if (x->can_trap()) nce()->clear_last_explicit_null_check(); }
566void NullCheckVisitor::do_ShiftOp        (ShiftOp*         x) {}
567void NullCheckVisitor::do_LogicOp        (LogicOp*         x) {}
568void NullCheckVisitor::do_CompareOp      (CompareOp*       x) {}
569void NullCheckVisitor::do_IfOp           (IfOp*            x) {}
570void NullCheckVisitor::do_Convert        (Convert*         x) {}
571void NullCheckVisitor::do_NullCheck      (NullCheck*       x) { nce()->handle_NullCheck(x); }
572void NullCheckVisitor::do_Invoke         (Invoke*          x) { nce()->handle_Invoke(x); }
573void NullCheckVisitor::do_NewInstance    (NewInstance*     x) { nce()->handle_NewInstance(x); }
574void NullCheckVisitor::do_NewTypeArray   (NewTypeArray*    x) { nce()->handle_NewArray(x); }
575void NullCheckVisitor::do_NewObjectArray (NewObjectArray*  x) { nce()->handle_NewArray(x); }
576void NullCheckVisitor::do_NewMultiArray  (NewMultiArray*   x) { nce()->handle_NewArray(x); }
577void NullCheckVisitor::do_CheckCast      (CheckCast*       x) {}
578void NullCheckVisitor::do_InstanceOf     (InstanceOf*      x) {}
579void NullCheckVisitor::do_MonitorEnter   (MonitorEnter*    x) { nce()->handle_AccessMonitor(x); }
580void NullCheckVisitor::do_MonitorExit    (MonitorExit*     x) { nce()->handle_AccessMonitor(x); }
581void NullCheckVisitor::do_Intrinsic      (Intrinsic*       x) { nce()->clear_last_explicit_null_check(); }
582void NullCheckVisitor::do_BlockBegin     (BlockBegin*      x) {}
583void NullCheckVisitor::do_Goto           (Goto*            x) {}
584void NullCheckVisitor::do_If             (If*              x) {}
585void NullCheckVisitor::do_IfInstanceOf   (IfInstanceOf*    x) {}
586void NullCheckVisitor::do_TableSwitch    (TableSwitch*     x) {}
587void NullCheckVisitor::do_LookupSwitch   (LookupSwitch*    x) {}
588void NullCheckVisitor::do_Return         (Return*          x) {}
589void NullCheckVisitor::do_Throw          (Throw*           x) { nce()->clear_last_explicit_null_check(); }
590void NullCheckVisitor::do_Base           (Base*            x) {}
591void NullCheckVisitor::do_OsrEntry       (OsrEntry*        x) {}
592void NullCheckVisitor::do_ExceptionObject(ExceptionObject* x) { nce()->handle_ExceptionObject(x); }
593void NullCheckVisitor::do_RoundFP        (RoundFP*         x) {}
594void NullCheckVisitor::do_UnsafeGetRaw   (UnsafeGetRaw*    x) {}
595void NullCheckVisitor::do_UnsafePutRaw   (UnsafePutRaw*    x) {}
596void NullCheckVisitor::do_UnsafeGetObject(UnsafeGetObject* x) {}
597void NullCheckVisitor::do_UnsafePutObject(UnsafePutObject* x) {}
598void NullCheckVisitor::do_UnsafePrefetchRead (UnsafePrefetchRead*  x) {}
599void NullCheckVisitor::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {}
600void NullCheckVisitor::do_ProfileCall    (ProfileCall*     x) { nce()->clear_last_explicit_null_check(); }
601void NullCheckVisitor::do_ProfileInvoke  (ProfileInvoke*   x) {}
602
603
604void NullCheckEliminator::visit(Value* p) {
605  assert(*p != NULL, "should not find NULL instructions");
606  if (visitable(*p)) {
607    mark_visited(*p);
608    (*p)->visit(&_visitor);
609  }
610}
611
612bool NullCheckEliminator::merge_state_for(BlockBegin* block, ValueSet* incoming_state) {
613  ValueSet* state = state_for(block);
614  if (state == NULL) {
615    state = incoming_state->copy();
616    set_state_for(block, state);
617    return true;
618  } else {
619    bool changed = state->set_intersect(incoming_state);
620    if (PrintNullCheckElimination && changed) {
621      tty->print_cr("Block %d's null check state changed", block->block_id());
622    }
623    return changed;
624  }
625}
626
627
628void NullCheckEliminator::iterate_all() {
629  while (work_list()->length() > 0) {
630    iterate_one(work_list()->pop());
631  }
632}
633
634
635void NullCheckEliminator::iterate_one(BlockBegin* block) {
636  clear_visitable_state();
637  // clear out an old explicit null checks
638  set_last_explicit_null_check(NULL);
639
640  if (PrintNullCheckElimination) {
641    tty->print_cr(" ...iterating block %d in null check elimination for %s::%s%s",
642                  block->block_id(),
643                  ir()->method()->holder()->name()->as_utf8(),
644                  ir()->method()->name()->as_utf8(),
645                  ir()->method()->signature()->as_symbol()->as_utf8());
646  }
647
648  // Create new state if none present (only happens at root)
649  if (state_for(block) == NULL) {
650    ValueSet* tmp_state = new ValueSet();
651    set_state_for(block, tmp_state);
652    // Initial state is that local 0 (receiver) is non-null for
653    // non-static methods
654    ValueStack* stack  = block->state();
655    IRScope*    scope  = stack->scope();
656    ciMethod*   method = scope->method();
657    if (!method->is_static()) {
658      Local* local0 = stack->local_at(0)->as_Local();
659      assert(local0 != NULL, "must be");
660      assert(local0->type() == objectType, "invalid type of receiver");
661
662      if (local0 != NULL) {
663        // Local 0 is used in this scope
664        tmp_state->put(local0);
665        if (PrintNullCheckElimination) {
666          tty->print_cr("Local 0 (value %d) proven non-null upon entry", local0->id());
667        }
668      }
669    }
670  }
671
672  // Must copy block's state to avoid mutating it during iteration
673  // through the block -- otherwise "not-null" states can accidentally
674  // propagate "up" through the block during processing of backward
675  // branches and algorithm is incorrect (and does not converge)
676  set_state_from(state_for(block));
677
678  // allow visiting of Phis belonging to this block
679  for_each_phi_fun(block, phi,
680                   mark_visitable(phi);
681                   );
682
683  BlockEnd* e = block->end();
684  assert(e != NULL, "incomplete graph");
685  int i;
686
687  // Propagate the state before this block into the exception
688  // handlers.  They aren't true successors since we aren't guaranteed
689  // to execute the whole block before executing them.  Also putting
690  // them on first seems to help reduce the amount of iteration to
691  // reach a fixed point.
692  for (i = 0; i < block->number_of_exception_handlers(); i++) {
693    BlockBegin* next = block->exception_handler_at(i);
694    if (merge_state_for(next, state())) {
695      if (!work_list()->contains(next)) {
696        work_list()->push(next);
697      }
698    }
699  }
700
701  // Iterate through block, updating state.
702  for (Instruction* instr = block; instr != NULL; instr = instr->next()) {
703    // Mark instructions in this block as visitable as they are seen
704    // in the instruction list.  This keeps the iteration from
705    // visiting instructions which are references in other blocks or
706    // visiting instructions more than once.
707    mark_visitable(instr);
708    if (instr->is_pinned() || instr->can_trap() || (instr->as_NullCheck() != NULL)) {
709      mark_visited(instr);
710      instr->input_values_do(this);
711      instr->visit(&_visitor);
712    }
713  }
714
715  // Propagate state to successors if necessary
716  for (i = 0; i < e->number_of_sux(); i++) {
717    BlockBegin* next = e->sux_at(i);
718    if (merge_state_for(next, state())) {
719      if (!work_list()->contains(next)) {
720        work_list()->push(next);
721      }
722    }
723  }
724}
725
726
727void NullCheckEliminator::iterate(BlockBegin* block) {
728  work_list()->push(block);
729  iterate_all();
730}
731
732void NullCheckEliminator::handle_AccessField(AccessField* x) {
733  if (x->is_static()) {
734    if (x->as_LoadField() != NULL) {
735      // If the field is a non-null static final object field (as is
736      // often the case for sun.misc.Unsafe), put this LoadField into
737      // the non-null map
738      ciField* field = x->field();
739      if (field->is_constant()) {
740        ciConstant field_val = field->constant_value();
741        BasicType field_type = field_val.basic_type();
742        if (field_type == T_OBJECT || field_type == T_ARRAY) {
743          ciObject* obj_val = field_val.as_object();
744          if (!obj_val->is_null_object()) {
745            if (PrintNullCheckElimination) {
746              tty->print_cr("AccessField %d proven non-null by static final non-null oop check",
747                            x->id());
748            }
749            set_put(x);
750          }
751        }
752      }
753    }
754    // Be conservative
755    clear_last_explicit_null_check();
756    return;
757  }
758
759  Value obj = x->obj();
760  if (set_contains(obj)) {
761    // Value is non-null => update AccessField
762    if (last_explicit_null_check_obj() == obj && !x->needs_patching()) {
763      x->set_explicit_null_check(consume_last_explicit_null_check());
764      x->set_needs_null_check(true);
765      if (PrintNullCheckElimination) {
766        tty->print_cr("Folded NullCheck %d into AccessField %d's null check for value %d",
767                      x->explicit_null_check()->id(), x->id(), obj->id());
768      }
769    } else {
770      x->set_explicit_null_check(NULL);
771      x->set_needs_null_check(false);
772      if (PrintNullCheckElimination) {
773        tty->print_cr("Eliminated AccessField %d's null check for value %d", x->id(), obj->id());
774      }
775    }
776  } else {
777    set_put(obj);
778    if (PrintNullCheckElimination) {
779      tty->print_cr("AccessField %d of value %d proves value to be non-null", x->id(), obj->id());
780    }
781    // Ensure previous passes do not cause wrong state
782    x->set_needs_null_check(true);
783    x->set_explicit_null_check(NULL);
784  }
785  clear_last_explicit_null_check();
786}
787
788
789void NullCheckEliminator::handle_ArrayLength(ArrayLength* x) {
790  Value array = x->array();
791  if (set_contains(array)) {
792    // Value is non-null => update AccessArray
793    if (last_explicit_null_check_obj() == array) {
794      x->set_explicit_null_check(consume_last_explicit_null_check());
795      x->set_needs_null_check(true);
796      if (PrintNullCheckElimination) {
797        tty->print_cr("Folded NullCheck %d into ArrayLength %d's null check for value %d",
798                      x->explicit_null_check()->id(), x->id(), array->id());
799      }
800    } else {
801      x->set_explicit_null_check(NULL);
802      x->set_needs_null_check(false);
803      if (PrintNullCheckElimination) {
804        tty->print_cr("Eliminated ArrayLength %d's null check for value %d", x->id(), array->id());
805      }
806    }
807  } else {
808    set_put(array);
809    if (PrintNullCheckElimination) {
810      tty->print_cr("ArrayLength %d of value %d proves value to be non-null", x->id(), array->id());
811    }
812    // Ensure previous passes do not cause wrong state
813    x->set_needs_null_check(true);
814    x->set_explicit_null_check(NULL);
815  }
816  clear_last_explicit_null_check();
817}
818
819
820void NullCheckEliminator::handle_LoadIndexed(LoadIndexed* x) {
821  Value array = x->array();
822  if (set_contains(array)) {
823    // Value is non-null => update AccessArray
824    if (last_explicit_null_check_obj() == array) {
825      x->set_explicit_null_check(consume_last_explicit_null_check());
826      x->set_needs_null_check(true);
827      if (PrintNullCheckElimination) {
828        tty->print_cr("Folded NullCheck %d into LoadIndexed %d's null check for value %d",
829                      x->explicit_null_check()->id(), x->id(), array->id());
830      }
831    } else {
832      x->set_explicit_null_check(NULL);
833      x->set_needs_null_check(false);
834      if (PrintNullCheckElimination) {
835        tty->print_cr("Eliminated LoadIndexed %d's null check for value %d", x->id(), array->id());
836      }
837    }
838  } else {
839    set_put(array);
840    if (PrintNullCheckElimination) {
841      tty->print_cr("LoadIndexed %d of value %d proves value to be non-null", x->id(), array->id());
842    }
843    // Ensure previous passes do not cause wrong state
844    x->set_needs_null_check(true);
845    x->set_explicit_null_check(NULL);
846  }
847  clear_last_explicit_null_check();
848}
849
850
851void NullCheckEliminator::handle_StoreIndexed(StoreIndexed* x) {
852  Value array = x->array();
853  if (set_contains(array)) {
854    // Value is non-null => update AccessArray
855    if (PrintNullCheckElimination) {
856      tty->print_cr("Eliminated StoreIndexed %d's null check for value %d", x->id(), array->id());
857    }
858    x->set_needs_null_check(false);
859  } else {
860    set_put(array);
861    if (PrintNullCheckElimination) {
862      tty->print_cr("StoreIndexed %d of value %d proves value to be non-null", x->id(), array->id());
863    }
864    // Ensure previous passes do not cause wrong state
865    x->set_needs_null_check(true);
866  }
867  clear_last_explicit_null_check();
868}
869
870
871void NullCheckEliminator::handle_NullCheck(NullCheck* x) {
872  Value obj = x->obj();
873  if (set_contains(obj)) {
874    // Already proven to be non-null => this NullCheck is useless
875    if (PrintNullCheckElimination) {
876      tty->print_cr("Eliminated NullCheck %d for value %d", x->id(), obj->id());
877    }
878    // Don't unpin since that may shrink obj's live range and make it unavailable for debug info.
879    // The code generator won't emit LIR for a NullCheck that cannot trap.
880    x->set_can_trap(false);
881  } else {
882    // May be null => add to map and set last explicit NullCheck
883    x->set_can_trap(true);
884    // make sure it's pinned if it can trap
885    x->pin(Instruction::PinExplicitNullCheck);
886    set_put(obj);
887    set_last_explicit_null_check(x);
888    if (PrintNullCheckElimination) {
889      tty->print_cr("NullCheck %d of value %d proves value to be non-null", x->id(), obj->id());
890    }
891  }
892}
893
894
895void NullCheckEliminator::handle_Invoke(Invoke* x) {
896  if (!x->has_receiver()) {
897    // Be conservative
898    clear_last_explicit_null_check();
899    return;
900  }
901
902  Value recv = x->receiver();
903  if (!set_contains(recv)) {
904    set_put(recv);
905    if (PrintNullCheckElimination) {
906      tty->print_cr("Invoke %d of value %d proves value to be non-null", x->id(), recv->id());
907    }
908  }
909  clear_last_explicit_null_check();
910}
911
912
913void NullCheckEliminator::handle_NewInstance(NewInstance* x) {
914  set_put(x);
915  if (PrintNullCheckElimination) {
916    tty->print_cr("NewInstance %d is non-null", x->id());
917  }
918}
919
920
921void NullCheckEliminator::handle_NewArray(NewArray* x) {
922  set_put(x);
923  if (PrintNullCheckElimination) {
924    tty->print_cr("NewArray %d is non-null", x->id());
925  }
926}
927
928
929void NullCheckEliminator::handle_ExceptionObject(ExceptionObject* x) {
930  set_put(x);
931  if (PrintNullCheckElimination) {
932    tty->print_cr("ExceptionObject %d is non-null", x->id());
933  }
934}
935
936
937void NullCheckEliminator::handle_AccessMonitor(AccessMonitor* x) {
938  Value obj = x->obj();
939  if (set_contains(obj)) {
940    // Value is non-null => update AccessMonitor
941    if (PrintNullCheckElimination) {
942      tty->print_cr("Eliminated AccessMonitor %d's null check for value %d", x->id(), obj->id());
943    }
944    x->set_needs_null_check(false);
945  } else {
946    set_put(obj);
947    if (PrintNullCheckElimination) {
948      tty->print_cr("AccessMonitor %d of value %d proves value to be non-null", x->id(), obj->id());
949    }
950    // Ensure previous passes do not cause wrong state
951    x->set_needs_null_check(true);
952  }
953  clear_last_explicit_null_check();
954}
955
956
957void NullCheckEliminator::handle_Intrinsic(Intrinsic* x) {
958  if (!x->has_receiver()) {
959    // Be conservative
960    clear_last_explicit_null_check();
961    return;
962  }
963
964  Value recv = x->receiver();
965  if (set_contains(recv)) {
966    // Value is non-null => update Intrinsic
967    if (PrintNullCheckElimination) {
968      tty->print_cr("Eliminated Intrinsic %d's null check for value %d", x->id(), recv->id());
969    }
970    x->set_needs_null_check(false);
971  } else {
972    set_put(recv);
973    if (PrintNullCheckElimination) {
974      tty->print_cr("Intrinsic %d of value %d proves value to be non-null", x->id(), recv->id());
975    }
976    // Ensure previous passes do not cause wrong state
977    x->set_needs_null_check(true);
978  }
979  clear_last_explicit_null_check();
980}
981
982
983void NullCheckEliminator::handle_Phi(Phi* x) {
984  int i;
985  bool all_non_null = true;
986  if (x->is_illegal()) {
987    all_non_null = false;
988  } else {
989    for (i = 0; i < x->operand_count(); i++) {
990      Value input = x->operand_at(i);
991      if (!set_contains(input)) {
992        all_non_null = false;
993      }
994    }
995  }
996
997  if (all_non_null) {
998    // Value is non-null => update Phi
999    if (PrintNullCheckElimination) {
1000      tty->print_cr("Eliminated Phi %d's null check for phifun because all inputs are non-null", x->id());
1001    }
1002    x->set_needs_null_check(false);
1003  } else if (set_contains(x)) {
1004    set_remove(x);
1005  }
1006}
1007
1008
1009void Optimizer::eliminate_null_checks() {
1010  ResourceMark rm;
1011
1012  NullCheckEliminator nce(this);
1013
1014  if (PrintNullCheckElimination) {
1015    tty->print_cr("Starting null check elimination for method %s::%s%s",
1016                  ir()->method()->holder()->name()->as_utf8(),
1017                  ir()->method()->name()->as_utf8(),
1018                  ir()->method()->signature()->as_symbol()->as_utf8());
1019  }
1020
1021  // Apply to graph
1022  nce.iterate(ir()->start());
1023
1024  // walk over the graph looking for exception
1025  // handlers and iterate over them as well
1026  int nblocks = BlockBegin::number_of_blocks();
1027  BlockList blocks(nblocks);
1028  boolArray visited_block(nblocks, false);
1029
1030  blocks.push(ir()->start());
1031  visited_block[ir()->start()->block_id()] = true;
1032  for (int i = 0; i < blocks.length(); i++) {
1033    BlockBegin* b = blocks[i];
1034    // exception handlers need to be treated as additional roots
1035    for (int e = b->number_of_exception_handlers(); e-- > 0; ) {
1036      BlockBegin* excp = b->exception_handler_at(e);
1037      int id = excp->block_id();
1038      if (!visited_block[id]) {
1039        blocks.push(excp);
1040        visited_block[id] = true;
1041        nce.iterate(excp);
1042      }
1043    }
1044    // traverse successors
1045    BlockEnd *end = b->end();
1046    for (int s = end->number_of_sux(); s-- > 0; ) {
1047      BlockBegin* next = end->sux_at(s);
1048      int id = next->block_id();
1049      if (!visited_block[id]) {
1050        blocks.push(next);
1051        visited_block[id] = true;
1052      }
1053    }
1054  }
1055
1056
1057  if (PrintNullCheckElimination) {
1058    tty->print_cr("Done with null check elimination for method %s::%s%s",
1059                  ir()->method()->holder()->name()->as_utf8(),
1060                  ir()->method()->name()->as_utf8(),
1061                  ir()->method()->signature()->as_symbol()->as_utf8());
1062  }
1063}
1064