src/hotspot/cpu/x86/c1_LinearScan_x86.cpp
author jlahoda
Tue, 24 Sep 2019 15:40:26 +0200
branchJDK-8226585-branch
changeset 58290 d885633d9de4
parent 51333 f6641fcf7b7e
permissions -rw-r--r--
Converting the test to a combo-framework test.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
46630
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 39219
diff changeset
     2
 * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3800
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3800
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3800
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#include "c1/c1_Instruction.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
#include "c1/c1_LinearScan.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "utilities/bitMap.inline.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
//----------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
// Allocation of FPU stack slots (Intel x86 only)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
//----------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
void LinearScan::allocate_fpu_stack() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  // First compute which FPU registers are live at the start of each basic block
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  // (To minimize the amount of work we have to do if we have to merge FPU stacks)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  if (ComputeExactFPURegisterUsage) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
    Interval* intervals_in_register, *intervals_in_memory;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
    create_unhandled_lists(&intervals_in_register, &intervals_in_memory, is_in_fpu_register, NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
    // ignore memory intervals by overwriting intervals_in_memory
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
    // the dummy interval is needed to enforce the walker to walk until the given id:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
    // without it, the walker stops when the unhandled-list is empty -> live information
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    // beyond this point would be incorrect.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
    Interval* dummy_interval = new Interval(any_reg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    dummy_interval->add_range(max_jint - 2, max_jint - 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    dummy_interval->set_next(Interval::end());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    intervals_in_memory = dummy_interval;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    IntervalWalker iw(this, intervals_in_register, intervals_in_memory);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    const int num_blocks = block_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    for (int i = 0; i < num_blocks; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
      BlockBegin* b = block_at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
      // register usage is only needed for merging stacks -> compute only
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
      // when more than one predecessor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
      // the block must not have any spill moves at the beginning (checked by assertions)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
      // spill moves would use intervals that are marked as handled and so the usage bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
      // would been set incorrectly
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
      // NOTE: the check for number_of_preds > 1 is necessary. A block with only one
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
      //       predecessor may have spill moves at the begin of the block.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
      //       If an interval ends at the current instruction id, it is not possible
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
      //       to decide if the register is live or not at the block begin -> the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
      //       register information would be incorrect.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
      if (b->number_of_preds() > 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
        int id = b->first_lir_instruction_id();
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38018
diff changeset
    70
        ResourceBitMap regs(FrameMap::nof_fpu_regs);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
        iw.walk_to(id);   // walk after the first instruction (always a label) of the block
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
        assert(iw.current_position() == id, "did not walk completely to id");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
        // Only consider FPU values in registers
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
        Interval* interval = iw.active_first(fixedKind);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
        while (interval != Interval::end()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
          int reg = interval->assigned_reg();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
          assert(reg >= pd_first_fpu_reg && reg <= pd_last_fpu_reg, "no fpu register");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
          assert(interval->assigned_regHi() == -1, "must not have hi register (doubles stored in one register)");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
          assert(interval->from() <= id && id < interval->to(), "interval out of range");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
          if (TraceFPURegisterUsage) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
            tty->print("fpu reg %d is live because of ", reg - pd_first_fpu_reg); interval->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
          regs.set_bit(reg - pd_first_fpu_reg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
          interval = interval->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
        b->set_fpu_register_usage(regs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
        if (TraceFPURegisterUsage) {
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 22234
diff changeset
    97
          tty->print("FPU regs for block %d, LIR instr %d): ", b->block_id(), id); regs.print_on(tty); tty->cr();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  FpuStackAllocator alloc(ir()->compilation(), this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  _fpu_stack_allocator = &alloc;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  alloc.allocate();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  _fpu_stack_allocator = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
FpuStackAllocator::FpuStackAllocator(Compilation* compilation, LinearScan* allocator)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  : _compilation(compilation)
51333
f6641fcf7b7e 8208670: Compiler changes to allow enabling -Wreorder
tschatzl
parents: 47216
diff changeset
   113
  , _allocator(allocator)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  , _lir(NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  , _pos(-1)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  , _sim(compilation)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  , _temp_sim(compilation)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
{}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
void FpuStackAllocator::allocate() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  int num_blocks = allocator()->block_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  for (int i = 0; i < num_blocks; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    // Set up to process block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    BlockBegin* block = allocator()->block_at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
    intArray* fpu_stack_state = block->fpu_stack_state();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
      tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
      tty->print_cr("------- Begin of new Block %d -------", block->block_id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    assert(fpu_stack_state != NULL ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
           block->end()->as_Base() != NULL ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
           block->is_set(BlockBegin::exception_entry_flag),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
           "FPU stack state must be present due to linear-scan order for FPU stack allocation");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
    // note: exception handler entries always start with an empty fpu stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
    //       because stack merging would be too complicated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
    if (fpu_stack_state != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
      sim()->read_state(fpu_stack_state);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
      sim()->clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
      tty->print("Reading FPU state for block %d:", block->block_id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
      sim()->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
      tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    allocate_block(block);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    CHECK_BAILOUT();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
void FpuStackAllocator::allocate_block(BlockBegin* block) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  bool processed_merge = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  LIR_OpList* insts = block->lir()->instructions_list();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  set_lir(block->lir());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  set_pos(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  // Note: insts->length() may change during loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  while (pos() < insts->length()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
    LIR_Op* op = insts->at(pos());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
    _debug_information_computed = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
    if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
      op->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    check_invalid_lir_op(op);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    LIR_OpBranch* branch = op->as_OpBranch();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    LIR_Op1* op1 = op->as_Op1();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
    LIR_Op2* op2 = op->as_Op2();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    LIR_OpCall* opCall = op->as_OpCall();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
    if (branch != NULL && branch->block() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
      if (!processed_merge) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
        // propagate stack at first branch to a successor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
        processed_merge = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
        bool required_merge = merge_fpu_stack_with_successors(block);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
        assert(!required_merge || branch->cond() == lir_cond_always, "splitting of critical edges should prevent FPU stack mismatches at cond branches");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
    } else if (op1 != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
      handle_op1(op1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    } else if (op2 != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
      handle_op2(op2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
    } else if (opCall != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
      handle_opCall(opCall);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    compute_debug_information(op);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
    set_pos(1 + pos());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  // Propagate stack when block does not end with branch
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
  if (!processed_merge) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
    merge_fpu_stack_with_successors(block);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
void FpuStackAllocator::compute_debug_information(LIR_Op* op) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  if (!_debug_information_computed && op->id() != -1 && allocator()->has_info(op->id())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
    visitor.visit(op);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
    // exception handling
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
    if (allocator()->compilation()->has_exception_handlers()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
      XHandlers* xhandlers = visitor.all_xhandler();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
      int n = xhandlers->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
      for (int k = 0; k < n; k++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
        allocate_exception_handler(xhandlers->handler_at(k));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
      assert(visitor.all_xhandler()->length() == 0, "missed exception handler");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
    // compute debug information
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    int n = visitor.info_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
    assert(n > 0, "should not visit operation otherwise");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
    for (int j = 0; j < n; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
      CodeEmitInfo* info = visitor.info_at(j);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
      // Compute debug information
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
      allocator()->compute_debug_info(info, op->id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  _debug_information_computed = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
void FpuStackAllocator::allocate_exception_handler(XHandler* xhandler) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  if (!sim()->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
    LIR_List* old_lir = lir();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
    int old_pos = pos();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
    intArray* old_state = sim()->write_state();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
    if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
      tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
      tty->print_cr("------- begin of exception handler -------");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
    if (xhandler->entry_code() == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
      // need entry code to clear FPU stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
      LIR_List* entry_code = new LIR_List(_compilation);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
      entry_code->jump(xhandler->entry_block());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
      xhandler->set_entry_code(entry_code);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
    LIR_OpList* insts = xhandler->entry_code()->instructions_list();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
    set_lir(xhandler->entry_code());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
    set_pos(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
    // Note: insts->length() may change during loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
    while (pos() < insts->length()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
      LIR_Op* op = insts->at(pos());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
      if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
        op->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
      check_invalid_lir_op(op);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
      switch (op->code()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
        case lir_move:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
          assert(op->as_Op1() != NULL, "must be LIR_Op1");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
          assert(pos() != insts->length() - 1, "must not be last operation");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
          handle_op1((LIR_Op1*)op);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
          break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
        case lir_branch:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
          assert(op->as_OpBranch()->cond() == lir_cond_always, "must be unconditional branch");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
          assert(pos() == insts->length() - 1, "must be last operation");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
          // remove all remaining dead registers from FPU stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
          clear_fpu_stack(LIR_OprFact::illegalOpr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
          break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
        default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
          // other operations not allowed in exception entry code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
          ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
      set_pos(pos() + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
    if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
      tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
      tty->print_cr("------- end of exception handler -------");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
    set_lir(old_lir);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
    set_pos(old_pos);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
    sim()->read_state(old_state);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
int FpuStackAllocator::fpu_num(LIR_Opr opr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  assert(opr->is_fpu_register() && !opr->is_xmm_register(), "shouldn't call this otherwise");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  return opr->is_single_fpu() ? opr->fpu_regnr() : opr->fpu_regnrLo();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
int FpuStackAllocator::tos_offset(LIR_Opr opr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  return sim()->offset_from_tos(fpu_num(opr));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
LIR_Opr FpuStackAllocator::to_fpu_stack(LIR_Opr opr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
  assert(opr->is_fpu_register() && !opr->is_xmm_register(), "shouldn't call this otherwise");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
  int stack_offset = tos_offset(opr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
  if (opr->is_single_fpu()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
    return LIR_OprFact::single_fpu(stack_offset)->make_fpu_stack_offset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
    assert(opr->is_double_fpu(), "shouldn't call this otherwise");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
    return LIR_OprFact::double_fpu(stack_offset)->make_fpu_stack_offset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
LIR_Opr FpuStackAllocator::to_fpu_stack_top(LIR_Opr opr, bool dont_check_offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
  assert(opr->is_fpu_register() && !opr->is_xmm_register(), "shouldn't call this otherwise");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
  assert(dont_check_offset || tos_offset(opr) == 0, "operand is not on stack top");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
  int stack_offset = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
  if (opr->is_single_fpu()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
    return LIR_OprFact::single_fpu(stack_offset)->make_fpu_stack_offset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
    assert(opr->is_double_fpu(), "shouldn't call this otherwise");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
    return LIR_OprFact::double_fpu(stack_offset)->make_fpu_stack_offset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
void FpuStackAllocator::insert_op(LIR_Op* op) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
  lir()->insert_before(pos(), op);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
  set_pos(1 + pos());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
void FpuStackAllocator::insert_exchange(int offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
  if (offset > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
    LIR_Op1* fxch_op = new LIR_Op1(lir_fxch, LIR_OprFact::intConst(offset), LIR_OprFact::illegalOpr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    insert_op(fxch_op);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
    sim()->swap(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
    if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
      tty->print("Exchanged register: %d         New state: ", sim()->get_slot(0)); sim()->print(); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
void FpuStackAllocator::insert_exchange(LIR_Opr opr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
  insert_exchange(tos_offset(opr));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
void FpuStackAllocator::insert_free(int offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
  // move stack slot to the top of stack and then pop it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
  insert_exchange(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
  LIR_Op* fpop = new LIR_Op0(lir_fpop_raw);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
  insert_op(fpop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
  sim()->pop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
    if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
      tty->print("Inserted pop                   New state: "); sim()->print(); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
void FpuStackAllocator::insert_free_if_dead(LIR_Opr opr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
  if (sim()->contains(fpu_num(opr))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
    int res_slot = tos_offset(opr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
    insert_free(res_slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
void FpuStackAllocator::insert_free_if_dead(LIR_Opr opr, LIR_Opr ignore) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
  if (fpu_num(opr) != fpu_num(ignore) && sim()->contains(fpu_num(opr))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
    int res_slot = tos_offset(opr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
    insert_free(res_slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
void FpuStackAllocator::insert_copy(LIR_Opr from, LIR_Opr to) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
  int offset = tos_offset(from);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  LIR_Op1* fld = new LIR_Op1(lir_fld, LIR_OprFact::intConst(offset), LIR_OprFact::illegalOpr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
  insert_op(fld);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
  sim()->push(fpu_num(to));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
  if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
    tty->print("Inserted copy (%d -> %d)         New state: ", fpu_num(from), fpu_num(to)); sim()->print(); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
void FpuStackAllocator::do_rename(LIR_Opr from, LIR_Opr to) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
  sim()->rename(fpu_num(from), fpu_num(to));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
void FpuStackAllocator::do_push(LIR_Opr opr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
  sim()->push(fpu_num(opr));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
void FpuStackAllocator::pop_if_last_use(LIR_Op* op, LIR_Opr opr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
  assert(op->fpu_pop_count() == 0, "fpu_pop_count alredy set");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
  assert(tos_offset(opr) == 0, "can only pop stack top");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
  if (opr->is_last_use()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
    op->set_fpu_pop_count(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
    sim()->pop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
void FpuStackAllocator::pop_always(LIR_Op* op, LIR_Opr opr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
  assert(op->fpu_pop_count() == 0, "fpu_pop_count alredy set");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
  assert(tos_offset(opr) == 0, "can only pop stack top");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
  op->set_fpu_pop_count(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
  sim()->pop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
void FpuStackAllocator::clear_fpu_stack(LIR_Opr preserve) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
  int result_stack_size = (preserve->is_fpu_register() && !preserve->is_xmm_register() ? 1 : 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  while (sim()->stack_size() > result_stack_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
    assert(!sim()->slot_is_empty(0), "not allowed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
    if (result_stack_size == 0 || sim()->get_slot(0) != fpu_num(preserve)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
      insert_free(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
      // move "preserve" to bottom of stack so that all other stack slots can be popped
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
      insert_exchange(sim()->stack_size() - 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
void FpuStackAllocator::handle_op1(LIR_Op1* op1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
  LIR_Opr in  = op1->in_opr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
  LIR_Opr res = op1->result_opr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
  LIR_Opr new_in  = in;  // new operands relative to the actual fpu stack top
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
  LIR_Opr new_res = res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
  // Note: this switch is processed for all LIR_Op1, regardless if they have FPU-arguments,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
  //       so checks for is_float_kind() are necessary inside the cases
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  switch (op1->code()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
    case lir_return: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
      // FPU-Stack must only contain the (optional) fpu return value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
      // All remaining dead values are popped from the stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
      // If the input operand is a fpu-register, it is exchanged to the bottom of the stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
      clear_fpu_stack(in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
      if (in->is_fpu_register() && !in->is_xmm_register()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
        new_in = to_fpu_stack_top(in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
    case lir_move: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
      if (in->is_fpu_register() && !in->is_xmm_register()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
        if (res->is_xmm_register()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
          // move from fpu register to xmm register (necessary for operations that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
          // are not available in the SSE instruction set)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
          insert_exchange(in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
          new_in = to_fpu_stack_top(in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
          pop_always(op1, in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
        } else if (res->is_fpu_register() && !res->is_xmm_register()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
          // move from fpu-register to fpu-register:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
          // * input and result register equal:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
          //   nothing to do
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
          // * input register is last use:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
          //   rename the input register to result register -> input register
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
          //   not present on fpu-stack afterwards
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
          // * input register not last use:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
          //   duplicate input register to result register to preserve input
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
          //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
          // Note: The LIR-Assembler does not produce any code for fpu register moves,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
          //       so input and result stack index must be equal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
          if (fpu_num(in) == fpu_num(res)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
            // nothing to do
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
          } else if (in->is_last_use()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
            insert_free_if_dead(res);//, in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
            do_rename(in, res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
          } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
            insert_free_if_dead(res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
            insert_copy(in, res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
          new_in = to_fpu_stack(res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
          new_res = new_in;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
          // move from fpu-register to memory
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
          // input operand must be on top of stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
          insert_exchange(in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
          // create debug information here because afterwards the register may have been popped
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
          compute_debug_information(op1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
          new_in = to_fpu_stack_top(in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
          pop_if_last_use(op1, in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
      } else if (res->is_fpu_register() && !res->is_xmm_register()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
        // move from memory/constant to fpu register
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
        // result is pushed on the stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
        insert_free_if_dead(res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
        // create debug information before register is pushed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
        compute_debug_information(op1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
        do_push(res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
        new_res = to_fpu_stack_top(res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
    case lir_neg: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
      if (in->is_fpu_register() && !in->is_xmm_register()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
        assert(res->is_fpu_register() && !res->is_xmm_register(), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
        assert(in->is_last_use(), "old value gets destroyed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
        insert_free_if_dead(res, in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
        insert_exchange(in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
        new_in = to_fpu_stack_top(in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
        do_rename(in, res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
        new_res = to_fpu_stack_top(res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
    case lir_convert: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
      Bytecodes::Code bc = op1->as_OpConvert()->bytecode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
      switch (bc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
        case Bytecodes::_d2f:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
        case Bytecodes::_f2d:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
          assert(res->is_fpu_register(), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
          assert(in->is_fpu_register(), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
          if (!in->is_xmm_register() && !res->is_xmm_register()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
            // this is quite the same as a move from fpu-register to fpu-register
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
            // Note: input and result operands must have different types
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
            if (fpu_num(in) == fpu_num(res)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
              // nothing to do
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
              new_in = to_fpu_stack(in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
            } else if (in->is_last_use()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
              insert_free_if_dead(res);//, in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
              new_in = to_fpu_stack(in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
              do_rename(in, res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
              insert_free_if_dead(res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
              insert_copy(in, res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
              new_in = to_fpu_stack_top(in, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
            new_res = to_fpu_stack(res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
          break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
        case Bytecodes::_i2f:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
        case Bytecodes::_l2f:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
        case Bytecodes::_i2d:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
        case Bytecodes::_l2d:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
          assert(res->is_fpu_register(), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
          if (!res->is_xmm_register()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
            insert_free_if_dead(res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
            do_push(res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
            new_res = to_fpu_stack_top(res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
          break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
        case Bytecodes::_f2i:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
        case Bytecodes::_d2i:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
          assert(in->is_fpu_register(), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
          if (!in->is_xmm_register()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
            insert_exchange(in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
            new_in = to_fpu_stack_top(in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
            // TODO: update registes of stub
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
          break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
        case Bytecodes::_f2l:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
        case Bytecodes::_d2l:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
          assert(in->is_fpu_register(), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
          if (!in->is_xmm_register()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
            insert_exchange(in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
            new_in = to_fpu_stack_top(in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
            pop_always(op1, in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
          break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
        case Bytecodes::_i2l:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
        case Bytecodes::_l2i:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
        case Bytecodes::_i2b:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
        case Bytecodes::_i2c:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
        case Bytecodes::_i2s:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
          // no fpu operands
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
          break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
        default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
          ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
    case lir_roundfp: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
      assert(in->is_fpu_register() && !in->is_xmm_register(), "input must be in register");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
      assert(res->is_stack(), "result must be on stack");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
      insert_exchange(in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
      new_in = to_fpu_stack_top(in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
      pop_if_last_use(op1, in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
    default: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
      assert(!in->is_float_kind() && !res->is_float_kind(), "missed a fpu-operation");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
  op1->set_in_opr(new_in);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
  op1->set_result_opr(new_res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
void FpuStackAllocator::handle_op2(LIR_Op2* op2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
  LIR_Opr left  = op2->in_opr1();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
  if (!left->is_float_kind()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
  if (left->is_xmm_register()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
  LIR_Opr right = op2->in_opr2();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
  LIR_Opr res   = op2->result_opr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
  LIR_Opr new_left  = left;  // new operands relative to the actual fpu stack top
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
  LIR_Opr new_right = right;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
  LIR_Opr new_res   = res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
  assert(!left->is_xmm_register() && !right->is_xmm_register() && !res->is_xmm_register(), "not for xmm registers");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
  switch (op2->code()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
    case lir_cmp:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
    case lir_cmp_fd2i:
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   677
    case lir_ucmp_fd2i:
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   678
    case lir_assert: {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
      assert(left->is_fpu_register(), "invalid LIR");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
      assert(right->is_fpu_register(), "invalid LIR");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
      // the left-hand side must be on top of stack.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
      // the right-hand side is never popped, even if is_last_use is set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
      insert_exchange(left);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
      new_left = to_fpu_stack_top(left);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
      new_right = to_fpu_stack(right);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
      pop_if_last_use(op2, left);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
    case lir_mul_strictfp:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
    case lir_div_strictfp: {
12739
09f26b73ae66 7133857: exp() and pow() should use the x87 ISA on x86
roland
parents: 7397
diff changeset
   693
      assert(op2->tmp1_opr()->is_fpu_register(), "strict operations need temporary fpu stack slot");
09f26b73ae66 7133857: exp() and pow() should use the x87 ISA on x86
roland
parents: 7397
diff changeset
   694
      insert_free_if_dead(op2->tmp1_opr());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
      assert(sim()->stack_size() <= 7, "at least one stack slot must be free");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
      // fall-through: continue with the normal handling of lir_mul and lir_div
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
    case lir_add:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
    case lir_sub:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
    case lir_mul:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
    case lir_div: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
      assert(left->is_fpu_register(), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
      assert(res->is_fpu_register(), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
      assert(left->is_equal(res), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
      // either the left-hand or the right-hand side must be on top of stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
      // (if right is not a register, left must be on top)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
      if (!right->is_fpu_register()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
        insert_exchange(left);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
        new_left = to_fpu_stack_top(left);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
        // no exchange necessary if right is alredy on top of stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
        if (tos_offset(right) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
          new_left = to_fpu_stack(left);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
          new_right = to_fpu_stack_top(right);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
          insert_exchange(left);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
          new_left = to_fpu_stack_top(left);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
          new_right = to_fpu_stack(right);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
        if (right->is_last_use()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
          op2->set_fpu_pop_count(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
          if (tos_offset(right) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
            sim()->pop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
          } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
            // if left is on top of stack, the result is placed in the stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
            // slot of right, so a renaming from right to res is necessary
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
            assert(tos_offset(left) == 0, "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
            sim()->pop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
            do_rename(right, res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
      new_res = to_fpu_stack(res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
    case lir_rem: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
      assert(left->is_fpu_register(), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
      assert(right->is_fpu_register(), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
      assert(res->is_fpu_register(), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
      assert(left->is_equal(res), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   746
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
      // Must bring both operands to top of stack with following operand ordering:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
      // * fpu stack before rem: ... right left
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
      // * fpu stack after rem:  ... left
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
      if (tos_offset(right) != 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
        insert_exchange(right);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
        insert_exchange(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   754
      insert_exchange(left);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
      assert(tos_offset(right) == 1, "check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
      assert(tos_offset(left) == 0, "check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
      new_left = to_fpu_stack_top(left);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
      new_right = to_fpu_stack(right);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
      op2->set_fpu_pop_count(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
      sim()->pop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
      do_rename(right, res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
      new_res = to_fpu_stack_top(res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
    case lir_abs:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
    case lir_sqrt: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
      // Right argument appears to be unused
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
      assert(right->is_illegal(), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
      assert(left->is_fpu_register(), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
      assert(res->is_fpu_register(), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
      assert(left->is_last_use(), "old value gets destroyed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   776
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
      insert_free_if_dead(res, left);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
      insert_exchange(left);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
      do_rename(left, res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
      new_left = to_fpu_stack_top(res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
      new_res = new_left;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   783
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
      op2->set_fpu_stack_size(sim()->stack_size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
489c9b5090e2 Initial load
duke
parents:
diff changeset
   788
    default: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   789
      assert(false, "missed a fpu-operation");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
  op2->set_in_opr1(new_left);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   794
  op2->set_in_opr2(new_right);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
  op2->set_result_opr(new_res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   797
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
void FpuStackAllocator::handle_opCall(LIR_OpCall* opCall) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
  LIR_Opr res = opCall->result_opr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   800
489c9b5090e2 Initial load
duke
parents:
diff changeset
   801
  // clear fpu-stack before call
489c9b5090e2 Initial load
duke
parents:
diff changeset
   802
  // it may contain dead values that could not have been remved by previous operations
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
  clear_fpu_stack(LIR_OprFact::illegalOpr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   804
  assert(sim()->is_empty(), "fpu stack must be empty now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
  // compute debug information before (possible) fpu result is pushed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
  compute_debug_information(opCall);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
  if (res->is_fpu_register() && !res->is_xmm_register()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   810
    do_push(res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   811
    opCall->set_result_opr(to_fpu_stack_top(res));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   812
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   813
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   814
489c9b5090e2 Initial load
duke
parents:
diff changeset
   815
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
void FpuStackAllocator::check_invalid_lir_op(LIR_Op* op) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   817
  switch (op->code()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   818
    case lir_24bit_FPU:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   819
    case lir_reset_FPU:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
    case lir_ffree:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   821
      assert(false, "operations not allowed in lir. If one of these operations is needed, check if they have fpu operands");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   822
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   823
489c9b5090e2 Initial load
duke
parents:
diff changeset
   824
    case lir_fpop_raw:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   825
    case lir_fxch:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   826
    case lir_fld:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
      assert(false, "operations only inserted by FpuStackAllocator");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
      break;
46630
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 39219
diff changeset
   829
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 39219
diff changeset
   830
    default:
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 39219
diff changeset
   831
      break;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   833
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
void FpuStackAllocator::merge_insert_add(LIR_List* instrs, FpuStackSim* cur_sim, int reg) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   838
  LIR_Op1* move = new LIR_Op1(lir_move, LIR_OprFact::doubleConst(0), LIR_OprFact::double_fpu(reg)->make_fpu_stack_offset());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   839
489c9b5090e2 Initial load
duke
parents:
diff changeset
   840
  instrs->instructions_list()->push(move);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
  cur_sim->push(reg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
  move->set_result_opr(to_fpu_stack(move->result_opr()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   844
489c9b5090e2 Initial load
duke
parents:
diff changeset
   845
  #ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   846
    if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   847
      tty->print("Added new register: %d         New state: ", reg); cur_sim->print(); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
  #endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
void FpuStackAllocator::merge_insert_xchg(LIR_List* instrs, FpuStackSim* cur_sim, int slot) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
  assert(slot > 0, "no exchange necessary");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
489c9b5090e2 Initial load
duke
parents:
diff changeset
   855
  LIR_Op1* fxch = new LIR_Op1(lir_fxch, LIR_OprFact::intConst(slot));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
  instrs->instructions_list()->push(fxch);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
  cur_sim->swap(slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
  #ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
    if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
      tty->print("Exchanged register: %d         New state: ", cur_sim->get_slot(slot)); cur_sim->print(); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
  #endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
void FpuStackAllocator::merge_insert_pop(LIR_List* instrs, FpuStackSim* cur_sim) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   867
  int reg = cur_sim->get_slot(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
  LIR_Op* fpop = new LIR_Op0(lir_fpop_raw);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
  instrs->instructions_list()->push(fpop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
  cur_sim->pop(reg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
  #ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
    if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
      tty->print("Removed register: %d           New state: ", reg); cur_sim->print(); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
  #endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
489c9b5090e2 Initial load
duke
parents:
diff changeset
   880
bool FpuStackAllocator::merge_rename(FpuStackSim* cur_sim, FpuStackSim* sux_sim, int start_slot, int change_slot) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
  int reg = cur_sim->get_slot(change_slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
  for (int slot = start_slot; slot >= 0; slot--) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
    int new_reg = sux_sim->get_slot(slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
    if (!cur_sim->contains(new_reg)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   887
      cur_sim->set_slot(change_slot, new_reg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
      #ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
        if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
          tty->print("Renamed register %d to %d       New state: ", reg, new_reg); cur_sim->print(); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
      #endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
489c9b5090e2 Initial load
duke
parents:
diff changeset
   895
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   896
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   897
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   898
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   899
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   900
489c9b5090e2 Initial load
duke
parents:
diff changeset
   901
489c9b5090e2 Initial load
duke
parents:
diff changeset
   902
void FpuStackAllocator::merge_fpu_stack(LIR_List* instrs, FpuStackSim* cur_sim, FpuStackSim* sux_sim) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   903
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   904
  if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   905
    tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   906
    tty->print("before merging: pred: "); cur_sim->print(); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   907
    tty->print("                 sux: "); sux_sim->print(); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   908
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   909
489c9b5090e2 Initial load
duke
parents:
diff changeset
   910
  int slot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   911
  for (slot = 0; slot < cur_sim->stack_size(); slot++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   912
    assert(!cur_sim->slot_is_empty(slot), "not handled by algorithm");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   913
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   914
  for (slot = 0; slot < sux_sim->stack_size(); slot++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   915
    assert(!sux_sim->slot_is_empty(slot), "not handled by algorithm");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
  // size difference between cur and sux that must be resolved by adding or removing values form the stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
  int size_diff = cur_sim->stack_size() - sux_sim->stack_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
  if (!ComputeExactFPURegisterUsage) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
    // add slots that are currently free, but used in successor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
    // When the exact FPU register usage is computed, the stack does
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
    // not contain dead values at merging -> no values must be added
489c9b5090e2 Initial load
duke
parents:
diff changeset
   926
489c9b5090e2 Initial load
duke
parents:
diff changeset
   927
    int sux_slot = sux_sim->stack_size() - 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   928
    while (size_diff < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   929
      assert(sux_slot >= 0, "slot out of bounds -> error in algorithm");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   930
489c9b5090e2 Initial load
duke
parents:
diff changeset
   931
      int reg = sux_sim->get_slot(sux_slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   932
      if (!cur_sim->contains(reg)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   933
        merge_insert_add(instrs, cur_sim, reg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   934
        size_diff++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   935
489c9b5090e2 Initial load
duke
parents:
diff changeset
   936
        if (sux_slot + size_diff != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   937
          merge_insert_xchg(instrs, cur_sim, sux_slot + size_diff);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   938
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   939
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   940
     sux_slot--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   942
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   943
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
  assert(cur_sim->stack_size() >= sux_sim->stack_size(), "stack size must be equal or greater now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
  assert(size_diff == cur_sim->stack_size() - sux_sim->stack_size(), "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   946
489c9b5090e2 Initial load
duke
parents:
diff changeset
   947
  // stack merge algorithm:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   948
  // 1) as long as the current stack top is not in the right location (that meens
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
  //    it should not be on the stack top), exchange it into the right location
489c9b5090e2 Initial load
duke
parents:
diff changeset
   950
  // 2) if the stack top is right, but the remaining stack is not ordered correctly,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   951
  //    the stack top is exchanged away to get another value on top ->
489c9b5090e2 Initial load
duke
parents:
diff changeset
   952
  //    now step 1) can be continued
489c9b5090e2 Initial load
duke
parents:
diff changeset
   953
  // the stack can also contain unused items -> these items are removed from stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   954
489c9b5090e2 Initial load
duke
parents:
diff changeset
   955
  int finished_slot = sux_sim->stack_size() - 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   956
  while (finished_slot >= 0 || size_diff > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   957
    while (size_diff > 0 || (cur_sim->stack_size() > 0 && cur_sim->get_slot(0) != sux_sim->get_slot(0))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
      int reg = cur_sim->get_slot(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   959
      if (sux_sim->contains(reg)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   960
        int sux_slot = sux_sim->offset_from_tos(reg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   961
        merge_insert_xchg(instrs, cur_sim, sux_slot + size_diff);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   962
489c9b5090e2 Initial load
duke
parents:
diff changeset
   963
      } else if (!merge_rename(cur_sim, sux_sim, finished_slot, 0)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   964
        assert(size_diff > 0, "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
489c9b5090e2 Initial load
duke
parents:
diff changeset
   966
        merge_insert_pop(instrs, cur_sim);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   967
        size_diff--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   968
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   969
      assert(cur_sim->stack_size() == 0 || cur_sim->get_slot(0) != reg, "register must have been changed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   970
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   971
489c9b5090e2 Initial load
duke
parents:
diff changeset
   972
    while (finished_slot >= 0 && cur_sim->get_slot(finished_slot) == sux_sim->get_slot(finished_slot)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   973
      finished_slot--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   974
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   975
489c9b5090e2 Initial load
duke
parents:
diff changeset
   976
    if (finished_slot >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   977
      int reg = cur_sim->get_slot(finished_slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   978
489c9b5090e2 Initial load
duke
parents:
diff changeset
   979
      if (sux_sim->contains(reg) || !merge_rename(cur_sim, sux_sim, finished_slot, finished_slot)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   980
        assert(sux_sim->contains(reg) || size_diff > 0, "must be");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   981
        merge_insert_xchg(instrs, cur_sim, finished_slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   982
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   983
      assert(cur_sim->get_slot(finished_slot) != reg, "register must have been changed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   984
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   985
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   986
489c9b5090e2 Initial load
duke
parents:
diff changeset
   987
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   988
  if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   989
    tty->print("after merging:  pred: "); cur_sim->print(); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   990
    tty->print("                 sux: "); sux_sim->print(); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   991
    tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   992
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   993
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   994
  assert(cur_sim->stack_size() == sux_sim->stack_size(), "stack size must be equal now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   995
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   996
489c9b5090e2 Initial load
duke
parents:
diff changeset
   997
489c9b5090e2 Initial load
duke
parents:
diff changeset
   998
void FpuStackAllocator::merge_cleanup_fpu_stack(LIR_List* instrs, FpuStackSim* cur_sim, BitMap& live_fpu_regs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   999
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1000
  if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1001
    tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1002
    tty->print("before cleanup: state: "); cur_sim->print(); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1003
    tty->print("                live:  "); live_fpu_regs.print_on(tty); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1004
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1005
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1006
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1007
  int slot = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1008
  while (slot < cur_sim->stack_size()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1009
    int reg = cur_sim->get_slot(slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1010
    if (!live_fpu_regs.at(reg)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1011
      if (slot != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1012
        merge_insert_xchg(instrs, cur_sim, slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1013
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1014
      merge_insert_pop(instrs, cur_sim);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1015
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1016
      slot++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1017
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1018
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1019
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1020
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1021
  if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1022
    tty->print("after cleanup:  state: "); cur_sim->print(); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1023
    tty->print("                live:  "); live_fpu_regs.print_on(tty); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1024
    tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1025
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1026
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1027
  // check if fpu stack only contains live registers
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1028
  for (unsigned int i = 0; i < live_fpu_regs.size(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1029
    if (live_fpu_regs.at(i) != cur_sim->contains(i)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1030
      tty->print_cr("mismatch between required and actual stack content");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1031
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1032
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1033
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1034
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1035
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1036
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1037
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1038
bool FpuStackAllocator::merge_fpu_stack_with_successors(BlockBegin* block) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1039
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1040
  if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1041
    tty->print_cr("Propagating FPU stack state for B%d at LIR_Op position %d to successors:",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1042
                  block->block_id(), pos());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1043
    sim()->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1044
    tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1045
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1046
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1047
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1048
  bool changed = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1049
  int number_of_sux = block->number_of_sux();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1050
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1051
  if (number_of_sux == 1 && block->sux_at(0)->number_of_preds() > 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1052
    // The successor has at least two incoming edges, so a stack merge will be necessary
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1053
    // If this block is the first predecessor, cleanup the current stack and propagate it
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1054
    // If this block is not the first predecessor, a stack merge will be necessary
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1055
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1056
    BlockBegin* sux = block->sux_at(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1057
    intArray* state = sux->fpu_stack_state();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1058
    LIR_List* instrs = new LIR_List(_compilation);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1059
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1060
    if (state != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1061
      // Merge with a successors that already has a FPU stack state
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1062
      // the block must only have one successor because critical edges must been split
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1063
      FpuStackSim* cur_sim = sim();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1064
      FpuStackSim* sux_sim = temp_sim();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1065
      sux_sim->read_state(state);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1066
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1067
      merge_fpu_stack(instrs, cur_sim, sux_sim);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1068
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1069
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1070
      // propagate current FPU stack state to successor without state
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1071
      // clean up stack first so that there are no dead values on the stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1072
      if (ComputeExactFPURegisterUsage) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1073
        FpuStackSim* cur_sim = sim();
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38018
diff changeset
  1074
        ResourceBitMap live_fpu_regs = block->sux_at(0)->fpu_register_usage();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1075
        assert(live_fpu_regs.size() == FrameMap::nof_fpu_regs, "missing register usage");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1076
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1077
        merge_cleanup_fpu_stack(instrs, cur_sim, live_fpu_regs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1078
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1079
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1080
      intArray* state = sim()->write_state();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1081
      if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1082
        tty->print_cr("Setting FPU stack state of B%d (merge path)", sux->block_id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1083
        sim()->print(); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1084
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1085
      sux->set_fpu_stack_state(state);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1086
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1087
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1088
    if (instrs->instructions_list()->length() > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1089
      lir()->insert_before(pos(), instrs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1090
      set_pos(instrs->instructions_list()->length() + pos());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1091
      changed = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1092
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1093
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1094
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1095
    // Propagate unmodified Stack to successors where a stack merge is not necessary
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1096
    intArray* state = sim()->write_state();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1097
    for (int i = 0; i < number_of_sux; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1098
      BlockBegin* sux = block->sux_at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1099
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1100
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1101
      for (int j = 0; j < sux->number_of_preds(); j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1102
        assert(block == sux->pred_at(j), "all critical edges must be broken");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1103
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1104
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1105
      // check if new state is same
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1106
      if (sux->fpu_stack_state() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1107
        intArray* sux_state = sux->fpu_stack_state();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1108
        assert(state->length() == sux_state->length(), "overwriting existing stack state");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1109
        for (int j = 0; j < state->length(); j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1110
          assert(state->at(j) == sux_state->at(j), "overwriting existing stack state");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1111
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1112
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1113
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1114
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1115
      if (TraceFPUStack) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1116
        tty->print_cr("Setting FPU stack state of B%d", sux->block_id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1117
        sim()->print(); tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1118
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1119
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1120
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1121
      sux->set_fpu_stack_state(state);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1122
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1123
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1124
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1125
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1126
  // assertions that FPU stack state conforms to all successors' states
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1127
  intArray* cur_state = sim()->write_state();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1128
  for (int i = 0; i < number_of_sux; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1129
    BlockBegin* sux = block->sux_at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1130
    intArray* sux_state = sux->fpu_stack_state();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1131
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1132
    assert(sux_state != NULL, "no fpu state");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1133
    assert(cur_state->length() == sux_state->length(), "incorrect length");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1134
    for (int i = 0; i < cur_state->length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1135
      assert(cur_state->at(i) == sux_state->at(i), "element not equal");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1136
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1137
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1138
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1139
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1140
  return changed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1141
}