c1_Instruction.cpp revision 1472:c18cbe5936b8
1/*
2 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#include "incls/_precompiled.incl"
26#include "incls/_c1_Instruction.cpp.incl"
27
28
29// Implementation of Instruction
30
31
32int Instruction::_next_id = 0;
33
34#ifdef ASSERT
35void Instruction::create_hi_word() {
36  assert(type()->is_double_word() && _hi_word == NULL, "only double word has high word");
37  _hi_word = new HiWord(this);
38}
39#endif
40
41Instruction::Condition Instruction::mirror(Condition cond) {
42  switch (cond) {
43    case eql: return eql;
44    case neq: return neq;
45    case lss: return gtr;
46    case leq: return geq;
47    case gtr: return lss;
48    case geq: return leq;
49  }
50  ShouldNotReachHere();
51  return eql;
52}
53
54
55Instruction::Condition Instruction::negate(Condition cond) {
56  switch (cond) {
57    case eql: return neq;
58    case neq: return eql;
59    case lss: return geq;
60    case leq: return gtr;
61    case gtr: return leq;
62    case geq: return lss;
63  }
64  ShouldNotReachHere();
65  return eql;
66}
67
68
69Instruction* Instruction::prev(BlockBegin* block) {
70  Instruction* p = NULL;
71  Instruction* q = block;
72  while (q != this) {
73    assert(q != NULL, "this is not in the block's instruction list");
74    p = q; q = q->next();
75  }
76  return p;
77}
78
79
80#ifndef PRODUCT
81void Instruction::print() {
82  InstructionPrinter ip;
83  print(ip);
84}
85
86
87void Instruction::print_line() {
88  InstructionPrinter ip;
89  ip.print_line(this);
90}
91
92
93void Instruction::print(InstructionPrinter& ip) {
94  ip.print_head();
95  ip.print_line(this);
96  tty->cr();
97}
98#endif // PRODUCT
99
100
101// perform constant and interval tests on index value
102bool AccessIndexed::compute_needs_range_check() {
103  Constant* clength = length()->as_Constant();
104  Constant* cindex = index()->as_Constant();
105  if (clength && cindex) {
106    IntConstant* l = clength->type()->as_IntConstant();
107    IntConstant* i = cindex->type()->as_IntConstant();
108    if (l && i && i->value() < l->value() && i->value() >= 0) {
109      return false;
110    }
111  }
112  return true;
113}
114
115
116ciType* LoadIndexed::exact_type() const {
117  ciType* array_type = array()->exact_type();
118  if (array_type == NULL) {
119    return NULL;
120  }
121  assert(array_type->is_array_klass(), "what else?");
122  ciArrayKlass* ak = (ciArrayKlass*)array_type;
123
124  if (ak->element_type()->is_instance_klass()) {
125    ciInstanceKlass* ik = (ciInstanceKlass*)ak->element_type();
126    if (ik->is_loaded() && ik->is_final()) {
127      return ik;
128    }
129  }
130  return NULL;
131}
132
133
134ciType* LoadIndexed::declared_type() const {
135  ciType* array_type = array()->declared_type();
136  if (array_type == NULL) {
137    return NULL;
138  }
139  assert(array_type->is_array_klass(), "what else?");
140  ciArrayKlass* ak = (ciArrayKlass*)array_type;
141  return ak->element_type();
142}
143
144
145ciType* LoadField::declared_type() const {
146  return field()->type();
147}
148
149
150ciType* LoadField::exact_type() const {
151  ciType* type = declared_type();
152  // for primitive arrays, the declared type is the exact type
153  if (type->is_type_array_klass()) {
154    return type;
155  }
156  if (type->is_instance_klass()) {
157    ciInstanceKlass* ik = (ciInstanceKlass*)type;
158    if (ik->is_loaded() && ik->is_final()) {
159      return type;
160    }
161  }
162  return NULL;
163}
164
165
166ciType* NewTypeArray::exact_type() const {
167  return ciTypeArrayKlass::make(elt_type());
168}
169
170
171ciType* NewObjectArray::exact_type() const {
172  return ciObjArrayKlass::make(klass());
173}
174
175
176ciType* NewInstance::exact_type() const {
177  return klass();
178}
179
180
181ciType* CheckCast::declared_type() const {
182  return klass();
183}
184
185ciType* CheckCast::exact_type() const {
186  if (klass()->is_instance_klass()) {
187    ciInstanceKlass* ik = (ciInstanceKlass*)klass();
188    if (ik->is_loaded() && ik->is_final()) {
189      return ik;
190    }
191  }
192  return NULL;
193}
194
195
196void ArithmeticOp::other_values_do(void f(Value*)) {
197  if (lock_stack() != NULL) lock_stack()->values_do(f);
198}
199
200void NullCheck::other_values_do(void f(Value*)) {
201  lock_stack()->values_do(f);
202}
203
204void AccessArray::other_values_do(void f(Value*)) {
205  if (lock_stack() != NULL) lock_stack()->values_do(f);
206}
207
208
209// Implementation of AccessField
210
211void AccessField::other_values_do(void f(Value*)) {
212  if (state_before() != NULL) state_before()->values_do(f);
213  if (lock_stack() != NULL) lock_stack()->values_do(f);
214}
215
216
217// Implementation of StoreIndexed
218
219IRScope* StoreIndexed::scope() const {
220  return lock_stack()->scope();
221}
222
223
224// Implementation of ArithmeticOp
225
226bool ArithmeticOp::is_commutative() const {
227  switch (op()) {
228    case Bytecodes::_iadd: // fall through
229    case Bytecodes::_ladd: // fall through
230    case Bytecodes::_fadd: // fall through
231    case Bytecodes::_dadd: // fall through
232    case Bytecodes::_imul: // fall through
233    case Bytecodes::_lmul: // fall through
234    case Bytecodes::_fmul: // fall through
235    case Bytecodes::_dmul: return true;
236  }
237  return false;
238}
239
240
241bool ArithmeticOp::can_trap() const {
242  switch (op()) {
243    case Bytecodes::_idiv: // fall through
244    case Bytecodes::_ldiv: // fall through
245    case Bytecodes::_irem: // fall through
246    case Bytecodes::_lrem: return true;
247  }
248  return false;
249}
250
251
252// Implementation of LogicOp
253
254bool LogicOp::is_commutative() const {
255#ifdef ASSERT
256  switch (op()) {
257    case Bytecodes::_iand: // fall through
258    case Bytecodes::_land: // fall through
259    case Bytecodes::_ior : // fall through
260    case Bytecodes::_lor : // fall through
261    case Bytecodes::_ixor: // fall through
262    case Bytecodes::_lxor: break;
263    default              : ShouldNotReachHere();
264  }
265#endif
266  // all LogicOps are commutative
267  return true;
268}
269
270
271// Implementation of CompareOp
272
273void CompareOp::other_values_do(void f(Value*)) {
274  if (state_before() != NULL) state_before()->values_do(f);
275}
276
277
278// Implementation of IfOp
279
280bool IfOp::is_commutative() const {
281  return cond() == eql || cond() == neq;
282}
283
284
285// Implementation of StateSplit
286
287void StateSplit::substitute(BlockList& list, BlockBegin* old_block, BlockBegin* new_block) {
288  NOT_PRODUCT(bool assigned = false;)
289  for (int i = 0; i < list.length(); i++) {
290    BlockBegin** b = list.adr_at(i);
291    if (*b == old_block) {
292      *b = new_block;
293      NOT_PRODUCT(assigned = true;)
294    }
295  }
296  assert(assigned == true, "should have assigned at least once");
297}
298
299
300IRScope* StateSplit::scope() const {
301  return _state->scope();
302}
303
304
305void StateSplit::state_values_do(void f(Value*)) {
306  if (state() != NULL) state()->values_do(f);
307}
308
309
310void BlockBegin::state_values_do(void f(Value*)) {
311  StateSplit::state_values_do(f);
312
313  if (is_set(BlockBegin::exception_entry_flag)) {
314    for (int i = 0; i < number_of_exception_states(); i++) {
315      exception_state_at(i)->values_do(f);
316    }
317  }
318}
319
320
321void MonitorEnter::state_values_do(void f(Value*)) {
322  StateSplit::state_values_do(f);
323  _lock_stack_before->values_do(f);
324}
325
326
327void Intrinsic::state_values_do(void f(Value*)) {
328  StateSplit::state_values_do(f);
329  if (lock_stack() != NULL) lock_stack()->values_do(f);
330}
331
332
333// Implementation of Invoke
334
335
336Invoke::Invoke(Bytecodes::Code code, ValueType* result_type, Value recv, Values* args,
337               int vtable_index, ciMethod* target, ValueStack* state_before)
338  : StateSplit(result_type)
339  , _code(code)
340  , _recv(recv)
341  , _args(args)
342  , _vtable_index(vtable_index)
343  , _target(target)
344  , _state_before(state_before)
345{
346  set_flag(TargetIsLoadedFlag,   target->is_loaded());
347  set_flag(TargetIsFinalFlag,    target_is_loaded() && target->is_final_method());
348  set_flag(TargetIsStrictfpFlag, target_is_loaded() && target->is_strict());
349
350  assert(args != NULL, "args must exist");
351#ifdef ASSERT
352  values_do(assert_value);
353#endif // ASSERT
354
355  // provide an initial guess of signature size.
356  _signature = new BasicTypeList(number_of_arguments() + (has_receiver() ? 1 : 0));
357  if (has_receiver()) {
358    _signature->append(as_BasicType(receiver()->type()));
359  } else if (is_invokedynamic()) {
360    // Add the synthetic MethodHandle argument to the signature.
361    _signature->append(T_OBJECT);
362  }
363  for (int i = 0; i < number_of_arguments(); i++) {
364    ValueType* t = argument_at(i)->type();
365    BasicType bt = as_BasicType(t);
366    _signature->append(bt);
367  }
368}
369
370
371void Invoke::state_values_do(void f(Value*)) {
372  StateSplit::state_values_do(f);
373  if (state_before() != NULL) state_before()->values_do(f);
374  if (state()        != NULL) state()->values_do(f);
375}
376
377
378// Implementation of Contant
379intx Constant::hash() const {
380  if (_state == NULL) {
381    switch (type()->tag()) {
382    case intTag:
383      return HASH2(name(), type()->as_IntConstant()->value());
384    case longTag:
385      {
386        jlong temp = type()->as_LongConstant()->value();
387        return HASH3(name(), high(temp), low(temp));
388      }
389    case floatTag:
390      return HASH2(name(), jint_cast(type()->as_FloatConstant()->value()));
391    case doubleTag:
392      {
393        jlong temp = jlong_cast(type()->as_DoubleConstant()->value());
394        return HASH3(name(), high(temp), low(temp));
395      }
396    case objectTag:
397      assert(type()->as_ObjectType()->is_loaded(), "can't handle unloaded values");
398      return HASH2(name(), type()->as_ObjectType()->constant_value());
399    }
400  }
401  return 0;
402}
403
404bool Constant::is_equal(Value v) const {
405  if (v->as_Constant() == NULL) return false;
406
407  switch (type()->tag()) {
408    case intTag:
409      {
410        IntConstant* t1 =    type()->as_IntConstant();
411        IntConstant* t2 = v->type()->as_IntConstant();
412        return (t1 != NULL && t2 != NULL &&
413                t1->value() == t2->value());
414      }
415    case longTag:
416      {
417        LongConstant* t1 =    type()->as_LongConstant();
418        LongConstant* t2 = v->type()->as_LongConstant();
419        return (t1 != NULL && t2 != NULL &&
420                t1->value() == t2->value());
421      }
422    case floatTag:
423      {
424        FloatConstant* t1 =    type()->as_FloatConstant();
425        FloatConstant* t2 = v->type()->as_FloatConstant();
426        return (t1 != NULL && t2 != NULL &&
427                jint_cast(t1->value()) == jint_cast(t2->value()));
428      }
429    case doubleTag:
430      {
431        DoubleConstant* t1 =    type()->as_DoubleConstant();
432        DoubleConstant* t2 = v->type()->as_DoubleConstant();
433        return (t1 != NULL && t2 != NULL &&
434                jlong_cast(t1->value()) == jlong_cast(t2->value()));
435      }
436    case objectTag:
437      {
438        ObjectType* t1 =    type()->as_ObjectType();
439        ObjectType* t2 = v->type()->as_ObjectType();
440        return (t1 != NULL && t2 != NULL &&
441                t1->is_loaded() && t2->is_loaded() &&
442                t1->constant_value() == t2->constant_value());
443      }
444  }
445  return false;
446}
447
448
449BlockBegin* Constant::compare(Instruction::Condition cond, Value right,
450                              BlockBegin* true_sux, BlockBegin* false_sux) {
451  Constant* rc = right->as_Constant();
452  // other is not a constant
453  if (rc == NULL) return NULL;
454
455  ValueType* lt = type();
456  ValueType* rt = rc->type();
457  // different types
458  if (lt->base() != rt->base()) return NULL;
459  switch (lt->tag()) {
460  case intTag: {
461    int x = lt->as_IntConstant()->value();
462    int y = rt->as_IntConstant()->value();
463    switch (cond) {
464    case If::eql: return x == y ? true_sux : false_sux;
465    case If::neq: return x != y ? true_sux : false_sux;
466    case If::lss: return x <  y ? true_sux : false_sux;
467    case If::leq: return x <= y ? true_sux : false_sux;
468    case If::gtr: return x >  y ? true_sux : false_sux;
469    case If::geq: return x >= y ? true_sux : false_sux;
470    }
471    break;
472  }
473  case longTag: {
474    jlong x = lt->as_LongConstant()->value();
475    jlong y = rt->as_LongConstant()->value();
476    switch (cond) {
477    case If::eql: return x == y ? true_sux : false_sux;
478    case If::neq: return x != y ? true_sux : false_sux;
479    case If::lss: return x <  y ? true_sux : false_sux;
480    case If::leq: return x <= y ? true_sux : false_sux;
481    case If::gtr: return x >  y ? true_sux : false_sux;
482    case If::geq: return x >= y ? true_sux : false_sux;
483    }
484    break;
485  }
486  case objectTag: {
487    ciObject* xvalue = lt->as_ObjectType()->constant_value();
488    ciObject* yvalue = rt->as_ObjectType()->constant_value();
489    assert(xvalue != NULL && yvalue != NULL, "not constants");
490    if (xvalue->is_loaded() && yvalue->is_loaded()) {
491      switch (cond) {
492      case If::eql: return xvalue == yvalue ? true_sux : false_sux;
493      case If::neq: return xvalue != yvalue ? true_sux : false_sux;
494      }
495    }
496    break;
497  }
498  }
499  return NULL;
500}
501
502
503void Constant::other_values_do(void f(Value*)) {
504  if (state() != NULL) state()->values_do(f);
505}
506
507
508// Implementation of NewArray
509
510void NewArray::other_values_do(void f(Value*)) {
511  if (state_before() != NULL) state_before()->values_do(f);
512}
513
514
515// Implementation of TypeCheck
516
517void TypeCheck::other_values_do(void f(Value*)) {
518  if (state_before() != NULL) state_before()->values_do(f);
519}
520
521
522// Implementation of BlockBegin
523
524int BlockBegin::_next_block_id = 0;
525
526
527void BlockBegin::set_end(BlockEnd* end) {
528  assert(end != NULL, "should not reset block end to NULL");
529  BlockEnd* old_end = _end;
530  if (end == old_end) {
531    return;
532  }
533  // Must make the predecessors/successors match up with the
534  // BlockEnd's notion.
535  int i, n;
536  if (old_end != NULL) {
537    // disconnect from the old end
538    old_end->set_begin(NULL);
539
540    // disconnect this block from it's current successors
541    for (i = 0; i < _successors.length(); i++) {
542      _successors.at(i)->remove_predecessor(this);
543    }
544  }
545  _end = end;
546
547  _successors.clear();
548  // Now reset successors list based on BlockEnd
549  n = end->number_of_sux();
550  for (i = 0; i < n; i++) {
551    BlockBegin* sux = end->sux_at(i);
552    _successors.append(sux);
553    sux->_predecessors.append(this);
554  }
555  _end->set_begin(this);
556}
557
558
559void BlockBegin::disconnect_edge(BlockBegin* from, BlockBegin* to) {
560  // disconnect any edges between from and to
561#ifndef PRODUCT
562  if (PrintIR && Verbose) {
563    tty->print_cr("Disconnected edge B%d -> B%d", from->block_id(), to->block_id());
564  }
565#endif
566  for (int s = 0; s < from->number_of_sux();) {
567    BlockBegin* sux = from->sux_at(s);
568    if (sux == to) {
569      int index = sux->_predecessors.index_of(from);
570      if (index >= 0) {
571        sux->_predecessors.remove_at(index);
572      }
573      from->_successors.remove_at(s);
574    } else {
575      s++;
576    }
577  }
578}
579
580
581void BlockBegin::disconnect_from_graph() {
582  // disconnect this block from all other blocks
583  for (int p = 0; p < number_of_preds(); p++) {
584    pred_at(p)->remove_successor(this);
585  }
586  for (int s = 0; s < number_of_sux(); s++) {
587    sux_at(s)->remove_predecessor(this);
588  }
589}
590
591void BlockBegin::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) {
592  // modify predecessors before substituting successors
593  for (int i = 0; i < number_of_sux(); i++) {
594    if (sux_at(i) == old_sux) {
595      // remove old predecessor before adding new predecessor
596      // otherwise there is a dead predecessor in the list
597      new_sux->remove_predecessor(old_sux);
598      new_sux->add_predecessor(this);
599    }
600  }
601  old_sux->remove_predecessor(this);
602  end()->substitute_sux(old_sux, new_sux);
603}
604
605
606
607// In general it is not possible to calculate a value for the field "depth_first_number"
608// of the inserted block, without recomputing the values of the other blocks
609// in the CFG. Therefore the value of "depth_first_number" in BlockBegin becomes meaningless.
610BlockBegin* BlockBegin::insert_block_between(BlockBegin* sux) {
611  // Try to make the bci close to a block with a single pred or sux,
612  // since this make the block layout algorithm work better.
613  int bci = -1;
614  if (sux->number_of_preds() == 1) {
615    bci = sux->bci();
616  } else {
617    bci = end()->bci();
618  }
619
620  BlockBegin* new_sux = new BlockBegin(bci);
621
622  // mark this block (special treatment when block order is computed)
623  new_sux->set(critical_edge_split_flag);
624
625  // This goto is not a safepoint.
626  Goto* e = new Goto(sux, false);
627  new_sux->set_next(e, bci);
628  new_sux->set_end(e);
629  // setup states
630  ValueStack* s = end()->state();
631  new_sux->set_state(s->copy());
632  e->set_state(s->copy());
633  assert(new_sux->state()->locals_size() == s->locals_size(), "local size mismatch!");
634  assert(new_sux->state()->stack_size() == s->stack_size(), "stack size mismatch!");
635  assert(new_sux->state()->locks_size() == s->locks_size(), "locks size mismatch!");
636
637  // link predecessor to new block
638  end()->substitute_sux(sux, new_sux);
639
640  // The ordering needs to be the same, so remove the link that the
641  // set_end call above added and substitute the new_sux for this
642  // block.
643  sux->remove_predecessor(new_sux);
644
645  // the successor could be the target of a switch so it might have
646  // multiple copies of this predecessor, so substitute the new_sux
647  // for the first and delete the rest.
648  bool assigned = false;
649  BlockList& list = sux->_predecessors;
650  for (int i = 0; i < list.length(); i++) {
651    BlockBegin** b = list.adr_at(i);
652    if (*b == this) {
653      if (assigned) {
654        list.remove_at(i);
655        // reprocess this index
656        i--;
657      } else {
658        assigned = true;
659        *b = new_sux;
660      }
661      // link the new block back to it's predecessors.
662      new_sux->add_predecessor(this);
663    }
664  }
665  assert(assigned == true, "should have assigned at least once");
666  return new_sux;
667}
668
669
670void BlockBegin::remove_successor(BlockBegin* pred) {
671  int idx;
672  while ((idx = _successors.index_of(pred)) >= 0) {
673    _successors.remove_at(idx);
674  }
675}
676
677
678void BlockBegin::add_predecessor(BlockBegin* pred) {
679  _predecessors.append(pred);
680}
681
682
683void BlockBegin::remove_predecessor(BlockBegin* pred) {
684  int idx;
685  while ((idx = _predecessors.index_of(pred)) >= 0) {
686    _predecessors.remove_at(idx);
687  }
688}
689
690
691void BlockBegin::add_exception_handler(BlockBegin* b) {
692  assert(b != NULL && (b->is_set(exception_entry_flag)), "exception handler must exist");
693  // add only if not in the list already
694  if (!_exception_handlers.contains(b)) _exception_handlers.append(b);
695}
696
697int BlockBegin::add_exception_state(ValueStack* state) {
698  assert(is_set(exception_entry_flag), "only for xhandlers");
699  if (_exception_states == NULL) {
700    _exception_states = new ValueStackStack(4);
701  }
702  _exception_states->append(state);
703  return _exception_states->length() - 1;
704}
705
706
707void BlockBegin::iterate_preorder(boolArray& mark, BlockClosure* closure) {
708  if (!mark.at(block_id())) {
709    mark.at_put(block_id(), true);
710    closure->block_do(this);
711    BlockEnd* e = end(); // must do this after block_do because block_do may change it!
712    { for (int i = number_of_exception_handlers() - 1; i >= 0; i--) exception_handler_at(i)->iterate_preorder(mark, closure); }
713    { for (int i = e->number_of_sux            () - 1; i >= 0; i--) e->sux_at           (i)->iterate_preorder(mark, closure); }
714  }
715}
716
717
718void BlockBegin::iterate_postorder(boolArray& mark, BlockClosure* closure) {
719  if (!mark.at(block_id())) {
720    mark.at_put(block_id(), true);
721    BlockEnd* e = end();
722    { for (int i = number_of_exception_handlers() - 1; i >= 0; i--) exception_handler_at(i)->iterate_postorder(mark, closure); }
723    { for (int i = e->number_of_sux            () - 1; i >= 0; i--) e->sux_at           (i)->iterate_postorder(mark, closure); }
724    closure->block_do(this);
725  }
726}
727
728
729void BlockBegin::iterate_preorder(BlockClosure* closure) {
730  boolArray mark(number_of_blocks(), false);
731  iterate_preorder(mark, closure);
732}
733
734
735void BlockBegin::iterate_postorder(BlockClosure* closure) {
736  boolArray mark(number_of_blocks(), false);
737  iterate_postorder(mark, closure);
738}
739
740
741void BlockBegin::block_values_do(void f(Value*)) {
742  for (Instruction* n = this; n != NULL; n = n->next()) n->values_do(f);
743}
744
745
746#ifndef PRODUCT
747  #define TRACE_PHI(code) if (PrintPhiFunctions) { code; }
748#else
749  #define TRACE_PHI(coce)
750#endif
751
752
753bool BlockBegin::try_merge(ValueStack* new_state) {
754  TRACE_PHI(tty->print_cr("********** try_merge for block B%d", block_id()));
755
756  // local variables used for state iteration
757  int index;
758  Value new_value, existing_value;
759
760  ValueStack* existing_state = state();
761  if (existing_state == NULL) {
762    TRACE_PHI(tty->print_cr("first call of try_merge for this block"));
763
764    if (is_set(BlockBegin::was_visited_flag)) {
765      // this actually happens for complicated jsr/ret structures
766      return false; // BAILOUT in caller
767    }
768
769    // copy state because it is altered
770    new_state = new_state->copy();
771
772    // Use method liveness to invalidate dead locals
773    MethodLivenessResult liveness = new_state->scope()->method()->liveness_at_bci(bci());
774    if (liveness.is_valid()) {
775      assert((int)liveness.size() == new_state->locals_size(), "error in use of liveness");
776
777      for_each_local_value(new_state, index, new_value) {
778        if (!liveness.at(index) || new_value->type()->is_illegal()) {
779          new_state->invalidate_local(index);
780          TRACE_PHI(tty->print_cr("invalidating dead local %d", index));
781        }
782      }
783    }
784
785    if (is_set(BlockBegin::parser_loop_header_flag)) {
786      TRACE_PHI(tty->print_cr("loop header block, initializing phi functions"));
787
788      for_each_stack_value(new_state, index, new_value) {
789        new_state->setup_phi_for_stack(this, index);
790        TRACE_PHI(tty->print_cr("creating phi-function %c%d for stack %d", new_state->stack_at(index)->type()->tchar(), new_state->stack_at(index)->id(), index));
791      }
792
793      BitMap requires_phi_function = new_state->scope()->requires_phi_function();
794
795      for_each_local_value(new_state, index, new_value) {
796        bool requires_phi = requires_phi_function.at(index) || (new_value->type()->is_double_word() && requires_phi_function.at(index + 1));
797        if (requires_phi || !SelectivePhiFunctions) {
798          new_state->setup_phi_for_local(this, index);
799          TRACE_PHI(tty->print_cr("creating phi-function %c%d for local %d", new_state->local_at(index)->type()->tchar(), new_state->local_at(index)->id(), index));
800        }
801      }
802    }
803
804    // initialize state of block
805    set_state(new_state);
806
807  } else if (existing_state->is_same_across_scopes(new_state)) {
808    TRACE_PHI(tty->print_cr("exisiting state found"));
809
810    // Inlining may cause the local state not to match up, so walk up
811    // the new state until we get to the same scope as the
812    // existing and then start processing from there.
813    while (existing_state->scope() != new_state->scope()) {
814      new_state = new_state->caller_state();
815      assert(new_state != NULL, "could not match up scopes");
816
817      assert(false, "check if this is necessary");
818    }
819
820    assert(existing_state->scope() == new_state->scope(), "not matching");
821    assert(existing_state->locals_size() == new_state->locals_size(), "not matching");
822    assert(existing_state->stack_size() == new_state->stack_size(), "not matching");
823
824    if (is_set(BlockBegin::was_visited_flag)) {
825      TRACE_PHI(tty->print_cr("loop header block, phis must be present"));
826
827      if (!is_set(BlockBegin::parser_loop_header_flag)) {
828        // this actually happens for complicated jsr/ret structures
829        return false; // BAILOUT in caller
830      }
831
832      for_each_local_value(existing_state, index, existing_value) {
833        Value new_value = new_state->local_at(index);
834        if (new_value == NULL || new_value->type()->tag() != existing_value->type()->tag()) {
835          // The old code invalidated the phi function here
836          // Because dead locals are replaced with NULL, this is a very rare case now, so simply bail out
837          return false; // BAILOUT in caller
838        }
839      }
840
841#ifdef ASSERT
842      // check that all necessary phi functions are present
843      for_each_stack_value(existing_state, index, existing_value) {
844        assert(existing_value->as_Phi() != NULL && existing_value->as_Phi()->block() == this, "phi function required");
845      }
846      for_each_local_value(existing_state, index, existing_value) {
847        assert(existing_value == new_state->local_at(index) || (existing_value->as_Phi() != NULL && existing_value->as_Phi()->as_Phi()->block() == this), "phi function required");
848      }
849#endif
850
851    } else {
852      TRACE_PHI(tty->print_cr("creating phi functions on demand"));
853
854      // create necessary phi functions for stack
855      for_each_stack_value(existing_state, index, existing_value) {
856        Value new_value = new_state->stack_at(index);
857        Phi* existing_phi = existing_value->as_Phi();
858
859        if (new_value != existing_value && (existing_phi == NULL || existing_phi->block() != this)) {
860          existing_state->setup_phi_for_stack(this, index);
861          TRACE_PHI(tty->print_cr("creating phi-function %c%d for stack %d", existing_state->stack_at(index)->type()->tchar(), existing_state->stack_at(index)->id(), index));
862        }
863      }
864
865      // create necessary phi functions for locals
866      for_each_local_value(existing_state, index, existing_value) {
867        Value new_value = new_state->local_at(index);
868        Phi* existing_phi = existing_value->as_Phi();
869
870        if (new_value == NULL || new_value->type()->tag() != existing_value->type()->tag()) {
871          existing_state->invalidate_local(index);
872          TRACE_PHI(tty->print_cr("invalidating local %d because of type mismatch", index));
873        } else if (new_value != existing_value && (existing_phi == NULL || existing_phi->block() != this)) {
874          existing_state->setup_phi_for_local(this, index);
875          TRACE_PHI(tty->print_cr("creating phi-function %c%d for local %d", existing_state->local_at(index)->type()->tchar(), existing_state->local_at(index)->id(), index));
876        }
877      }
878    }
879
880    assert(existing_state->caller_state() == new_state->caller_state(), "caller states must be equal");
881
882  } else {
883    assert(false, "stack or locks not matching (invalid bytecodes)");
884    return false;
885  }
886
887  TRACE_PHI(tty->print_cr("********** try_merge for block B%d successful", block_id()));
888
889  return true;
890}
891
892
893#ifndef PRODUCT
894void BlockBegin::print_block() {
895  InstructionPrinter ip;
896  print_block(ip, false);
897}
898
899
900void BlockBegin::print_block(InstructionPrinter& ip, bool live_only) {
901  ip.print_instr(this); tty->cr();
902  ip.print_stack(this->state()); tty->cr();
903  ip.print_inline_level(this);
904  ip.print_head();
905  for (Instruction* n = next(); n != NULL; n = n->next()) {
906    if (!live_only || n->is_pinned() || n->use_count() > 0) {
907      ip.print_line(n);
908    }
909  }
910  tty->cr();
911}
912#endif // PRODUCT
913
914
915// Implementation of BlockList
916
917void BlockList::iterate_forward (BlockClosure* closure) {
918  const int l = length();
919  for (int i = 0; i < l; i++) closure->block_do(at(i));
920}
921
922
923void BlockList::iterate_backward(BlockClosure* closure) {
924  for (int i = length() - 1; i >= 0; i--) closure->block_do(at(i));
925}
926
927
928void BlockList::blocks_do(void f(BlockBegin*)) {
929  for (int i = length() - 1; i >= 0; i--) f(at(i));
930}
931
932
933void BlockList::values_do(void f(Value*)) {
934  for (int i = length() - 1; i >= 0; i--) at(i)->block_values_do(f);
935}
936
937
938#ifndef PRODUCT
939void BlockList::print(bool cfg_only, bool live_only) {
940  InstructionPrinter ip;
941  for (int i = 0; i < length(); i++) {
942    BlockBegin* block = at(i);
943    if (cfg_only) {
944      ip.print_instr(block); tty->cr();
945    } else {
946      block->print_block(ip, live_only);
947    }
948  }
949}
950#endif // PRODUCT
951
952
953// Implementation of BlockEnd
954
955void BlockEnd::set_begin(BlockBegin* begin) {
956  BlockList* sux = NULL;
957  if (begin != NULL) {
958    sux = begin->successors();
959  } else if (_begin != NULL) {
960    // copy our sux list
961    BlockList* sux = new BlockList(_begin->number_of_sux());
962    for (int i = 0; i < _begin->number_of_sux(); i++) {
963      sux->append(_begin->sux_at(i));
964    }
965  }
966  _sux = sux;
967  _begin = begin;
968}
969
970
971void BlockEnd::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) {
972  substitute(*_sux, old_sux, new_sux);
973}
974
975
976void BlockEnd::other_values_do(void f(Value*)) {
977  if (state_before() != NULL) state_before()->values_do(f);
978}
979
980
981// Implementation of Phi
982
983// Normal phi functions take their operands from the last instruction of the
984// predecessor. Special handling is needed for xhanlder entries because there
985// the state of arbitrary instructions are needed.
986
987Value Phi::operand_at(int i) const {
988  ValueStack* state;
989  if (_block->is_set(BlockBegin::exception_entry_flag)) {
990    state = _block->exception_state_at(i);
991  } else {
992    state = _block->pred_at(i)->end()->state();
993  }
994  assert(state != NULL, "");
995
996  if (is_local()) {
997    return state->local_at(local_index());
998  } else {
999    return state->stack_at(stack_index());
1000  }
1001}
1002
1003
1004int Phi::operand_count() const {
1005  if (_block->is_set(BlockBegin::exception_entry_flag)) {
1006    return _block->number_of_exception_states();
1007  } else {
1008    return _block->number_of_preds();
1009  }
1010}
1011
1012
1013// Implementation of Throw
1014
1015void Throw::state_values_do(void f(Value*)) {
1016  BlockEnd::state_values_do(f);
1017}
1018