aoqi@0: /* aoqi@0: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "c1/c1_Compilation.hpp" aoqi@0: #include "c1/c1_FrameMap.hpp" aoqi@0: #include "c1/c1_GraphBuilder.hpp" aoqi@0: #include "c1/c1_IR.hpp" aoqi@0: #include "c1/c1_InstructionPrinter.hpp" aoqi@0: #include "c1/c1_Optimizer.hpp" aoqi@0: #include "utilities/bitMap.inline.hpp" aoqi@0: aoqi@0: aoqi@0: // Implementation of XHandlers aoqi@0: // aoqi@0: // Note: This code could eventually go away if we are aoqi@0: // just using the ciExceptionHandlerStream. aoqi@0: aoqi@0: XHandlers::XHandlers(ciMethod* method) : _list(method->exception_table_length()) { aoqi@0: ciExceptionHandlerStream s(method); aoqi@0: while (!s.is_done()) { aoqi@0: _list.append(new XHandler(s.handler())); aoqi@0: s.next(); aoqi@0: } aoqi@0: assert(s.count() == method->exception_table_length(), "exception table lengths inconsistent"); aoqi@0: } aoqi@0: aoqi@0: // deep copy of all XHandler contained in list aoqi@0: XHandlers::XHandlers(XHandlers* other) : aoqi@0: _list(other->length()) aoqi@0: { aoqi@0: for (int i = 0; i < other->length(); i++) { aoqi@0: _list.append(new XHandler(other->handler_at(i))); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Returns whether a particular exception type can be caught. Also aoqi@0: // returns true if klass is unloaded or any exception handler aoqi@0: // classes are unloaded. type_is_exact indicates whether the throw aoqi@0: // is known to be exactly that class or it might throw a subtype. aoqi@0: bool XHandlers::could_catch(ciInstanceKlass* klass, bool type_is_exact) const { aoqi@0: // the type is unknown so be conservative aoqi@0: if (!klass->is_loaded()) { aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: for (int i = 0; i < length(); i++) { aoqi@0: XHandler* handler = handler_at(i); aoqi@0: if (handler->is_catch_all()) { aoqi@0: // catch of ANY aoqi@0: return true; aoqi@0: } aoqi@0: ciInstanceKlass* handler_klass = handler->catch_klass(); aoqi@0: // if it's unknown it might be catchable aoqi@0: if (!handler_klass->is_loaded()) { aoqi@0: return true; aoqi@0: } aoqi@0: // if the throw type is definitely a subtype of the catch type aoqi@0: // then it can be caught. aoqi@0: if (klass->is_subtype_of(handler_klass)) { aoqi@0: return true; aoqi@0: } aoqi@0: if (!type_is_exact) { aoqi@0: // If the type isn't exactly known then it can also be caught by aoqi@0: // catch statements where the inexact type is a subtype of the aoqi@0: // catch type. aoqi@0: // given: foo extends bar extends Exception aoqi@0: // throw bar can be caught by catch foo, catch bar, and catch aoqi@0: // Exception, however it can't be caught by any handlers without aoqi@0: // bar in its type hierarchy. aoqi@0: if (handler_klass->is_subtype_of(klass)) { aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool XHandlers::equals(XHandlers* others) const { aoqi@0: if (others == NULL) return false; aoqi@0: if (length() != others->length()) return false; aoqi@0: aoqi@0: for (int i = 0; i < length(); i++) { aoqi@0: if (!handler_at(i)->equals(others->handler_at(i))) return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: bool XHandler::equals(XHandler* other) const { aoqi@0: assert(entry_pco() != -1 && other->entry_pco() != -1, "must have entry_pco"); aoqi@0: aoqi@0: if (entry_pco() != other->entry_pco()) return false; aoqi@0: if (scope_count() != other->scope_count()) return false; aoqi@0: if (_desc != other->_desc) return false; aoqi@0: aoqi@0: assert(entry_block() == other->entry_block(), "entry_block must be equal when entry_pco is equal"); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Implementation of IRScope aoqi@0: BlockBegin* IRScope::build_graph(Compilation* compilation, int osr_bci) { aoqi@0: GraphBuilder gm(compilation, this); aoqi@0: NOT_PRODUCT(if (PrintValueNumbering && Verbose) gm.print_stats()); aoqi@0: if (compilation->bailed_out()) return NULL; aoqi@0: return gm.start(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: IRScope::IRScope(Compilation* compilation, IRScope* caller, int caller_bci, ciMethod* method, int osr_bci, bool create_graph) aoqi@0: : _callees(2) aoqi@0: , _compilation(compilation) aoqi@0: , _requires_phi_function(method->max_locals()) aoqi@0: { aoqi@0: _caller = caller; aoqi@0: _level = caller == NULL ? 0 : caller->level() + 1; aoqi@0: _method = method; aoqi@0: _xhandlers = new XHandlers(method); aoqi@0: _number_of_locks = 0; aoqi@0: _monitor_pairing_ok = method->has_balanced_monitors(); aoqi@0: _wrote_final = false; aoqi@0: _start = NULL; aoqi@0: aoqi@0: if (osr_bci == -1) { aoqi@0: _requires_phi_function.clear(); aoqi@0: } else { aoqi@0: // selective creation of phi functions is not possibel in osr-methods aoqi@0: _requires_phi_function.set_range(0, method->max_locals()); aoqi@0: } aoqi@0: aoqi@0: assert(method->holder()->is_loaded() , "method holder must be loaded"); aoqi@0: aoqi@0: // build graph if monitor pairing is ok aoqi@0: if (create_graph && monitor_pairing_ok()) _start = build_graph(compilation, osr_bci); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: int IRScope::max_stack() const { aoqi@0: int my_max = method()->max_stack(); aoqi@0: int callee_max = 0; aoqi@0: for (int i = 0; i < number_of_callees(); i++) { aoqi@0: callee_max = MAX2(callee_max, callee_no(i)->max_stack()); aoqi@0: } aoqi@0: return my_max + callee_max; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool IRScopeDebugInfo::should_reexecute() { aoqi@0: ciMethod* cur_method = scope()->method(); aoqi@0: int cur_bci = bci(); aoqi@0: if (cur_method != NULL && cur_bci != SynchronizationEntryBCI) { aoqi@0: Bytecodes::Code code = cur_method->java_code_at_bci(cur_bci); aoqi@0: return Interpreter::bytecode_should_reexecute(code); aoqi@0: } else aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Implementation of CodeEmitInfo aoqi@0: aoqi@0: // Stack must be NON-null aoqi@0: CodeEmitInfo::CodeEmitInfo(ValueStack* stack, XHandlers* exception_handlers, bool deoptimize_on_exception) aoqi@0: : _scope(stack->scope()) aoqi@0: , _scope_debug_info(NULL) aoqi@0: , _oop_map(NULL) aoqi@0: , _stack(stack) aoqi@0: , _exception_handlers(exception_handlers) aoqi@0: , _is_method_handle_invoke(false) aoqi@0: , _deoptimize_on_exception(deoptimize_on_exception) { aoqi@0: assert(_stack != NULL, "must be non null"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: CodeEmitInfo::CodeEmitInfo(CodeEmitInfo* info, ValueStack* stack) aoqi@0: : _scope(info->_scope) aoqi@0: , _exception_handlers(NULL) aoqi@0: , _scope_debug_info(NULL) aoqi@0: , _oop_map(NULL) aoqi@0: , _stack(stack == NULL ? info->_stack : stack) aoqi@0: , _is_method_handle_invoke(info->_is_method_handle_invoke) aoqi@0: , _deoptimize_on_exception(info->_deoptimize_on_exception) { aoqi@0: aoqi@0: // deep copy of exception handlers aoqi@0: if (info->_exception_handlers != NULL) { aoqi@0: _exception_handlers = new XHandlers(info->_exception_handlers); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void CodeEmitInfo::record_debug_info(DebugInformationRecorder* recorder, int pc_offset) { aoqi@0: // record the safepoint before recording the debug info for enclosing scopes aoqi@0: recorder->add_safepoint(pc_offset, _oop_map->deep_copy()); aoqi@0: _scope_debug_info->record_debug_info(recorder, pc_offset, true/*topmost*/, _is_method_handle_invoke); aoqi@0: recorder->end_safepoint(pc_offset); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void CodeEmitInfo::add_register_oop(LIR_Opr opr) { aoqi@0: assert(_oop_map != NULL, "oop map must already exist"); aoqi@0: assert(opr->is_single_cpu(), "should not call otherwise"); aoqi@0: aoqi@0: VMReg name = frame_map()->regname(opr); aoqi@0: _oop_map->set_oop(name); aoqi@0: } aoqi@0: aoqi@0: // Mirror the stack size calculation in the deopt code aoqi@0: // How much stack space would we need at this point in the program in aoqi@0: // case of deoptimization? aoqi@0: int CodeEmitInfo::interpreter_frame_size() const { aoqi@0: ValueStack* state = _stack; aoqi@0: int size = 0; aoqi@0: int callee_parameters = 0; aoqi@0: int callee_locals = 0; aoqi@0: int extra_args = state->scope()->method()->max_stack() - state->stack_size(); aoqi@0: aoqi@0: while (state != NULL) { aoqi@0: int locks = state->locks_size(); aoqi@0: int temps = state->stack_size(); aoqi@0: bool is_top_frame = (state == _stack); aoqi@0: ciMethod* method = state->scope()->method(); aoqi@0: aoqi@0: int frame_size = BytesPerWord * Interpreter::size_activation(method->max_stack(), aoqi@0: temps + callee_parameters, aoqi@0: extra_args, aoqi@0: locks, aoqi@0: callee_parameters, aoqi@0: callee_locals, aoqi@0: is_top_frame); aoqi@0: size += frame_size; aoqi@0: aoqi@0: callee_parameters = method->size_of_parameters(); aoqi@0: callee_locals = method->max_locals(); aoqi@0: extra_args = 0; aoqi@0: state = state->caller_state(); aoqi@0: } aoqi@0: return size + Deoptimization::last_frame_adjust(0, callee_locals) * BytesPerWord; aoqi@0: } aoqi@0: aoqi@0: // Implementation of IR aoqi@0: aoqi@0: IR::IR(Compilation* compilation, ciMethod* method, int osr_bci) : aoqi@0: _locals_size(in_WordSize(-1)) aoqi@0: , _num_loops(0) { aoqi@0: // setup IR fields aoqi@0: _compilation = compilation; aoqi@0: _top_scope = new IRScope(compilation, NULL, -1, method, osr_bci, true); aoqi@0: _code = NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void IR::optimize_blocks() { aoqi@0: Optimizer opt(this); aoqi@0: if (!compilation()->profile_branches()) { aoqi@0: if (DoCEE) { aoqi@0: opt.eliminate_conditional_expressions(); aoqi@0: #ifndef PRODUCT aoqi@0: if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after CEE"); print(true); } aoqi@0: if (PrintIR || PrintIR1 ) { tty->print_cr("IR after CEE"); print(false); } aoqi@0: #endif aoqi@0: } aoqi@0: if (EliminateBlocks) { aoqi@0: opt.eliminate_blocks(); aoqi@0: #ifndef PRODUCT aoqi@0: if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after block elimination"); print(true); } aoqi@0: if (PrintIR || PrintIR1 ) { tty->print_cr("IR after block elimination"); print(false); } aoqi@0: #endif aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void IR::eliminate_null_checks() { aoqi@0: Optimizer opt(this); aoqi@0: if (EliminateNullChecks) { aoqi@0: opt.eliminate_null_checks(); aoqi@0: #ifndef PRODUCT aoqi@0: if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after null check elimination"); print(true); } aoqi@0: if (PrintIR || PrintIR1 ) { tty->print_cr("IR after null check elimination"); print(false); } aoqi@0: #endif aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: static int sort_pairs(BlockPair** a, BlockPair** b) { aoqi@0: if ((*a)->from() == (*b)->from()) { aoqi@0: return (*a)->to()->block_id() - (*b)->to()->block_id(); aoqi@0: } else { aoqi@0: return (*a)->from()->block_id() - (*b)->from()->block_id(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: class CriticalEdgeFinder: public BlockClosure { aoqi@0: BlockPairList blocks; aoqi@0: IR* _ir; aoqi@0: aoqi@0: public: aoqi@0: CriticalEdgeFinder(IR* ir): _ir(ir) {} aoqi@0: void block_do(BlockBegin* bb) { aoqi@0: BlockEnd* be = bb->end(); aoqi@0: int nos = be->number_of_sux(); aoqi@0: if (nos >= 2) { aoqi@0: for (int i = 0; i < nos; i++) { aoqi@0: BlockBegin* sux = be->sux_at(i); aoqi@0: if (sux->number_of_preds() >= 2) { aoqi@0: blocks.append(new BlockPair(bb, sux)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void split_edges() { aoqi@0: BlockPair* last_pair = NULL; aoqi@0: blocks.sort(sort_pairs); aoqi@0: for (int i = 0; i < blocks.length(); i++) { aoqi@0: BlockPair* pair = blocks.at(i); aoqi@0: if (last_pair != NULL && pair->is_same(last_pair)) continue; aoqi@0: BlockBegin* from = pair->from(); aoqi@0: BlockBegin* to = pair->to(); aoqi@0: BlockBegin* split = from->insert_block_between(to); aoqi@0: #ifndef PRODUCT aoqi@0: if ((PrintIR || PrintIR1) && Verbose) { aoqi@0: tty->print_cr("Split critical edge B%d -> B%d (new block B%d)", aoqi@0: from->block_id(), to->block_id(), split->block_id()); aoqi@0: } aoqi@0: #endif aoqi@0: last_pair = pair; aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: void IR::split_critical_edges() { aoqi@0: CriticalEdgeFinder cef(this); aoqi@0: aoqi@0: iterate_preorder(&cef); aoqi@0: cef.split_edges(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: class UseCountComputer: public ValueVisitor, BlockClosure { aoqi@0: private: aoqi@0: void visit(Value* n) { aoqi@0: // Local instructions and Phis for expression stack values at the aoqi@0: // start of basic blocks are not added to the instruction list aoqi@0: if (!(*n)->is_linked() && (*n)->can_be_linked()) { aoqi@0: assert(false, "a node was not appended to the graph"); aoqi@0: Compilation::current()->bailout("a node was not appended to the graph"); aoqi@0: } aoqi@0: // use n's input if not visited before aoqi@0: if (!(*n)->is_pinned() && !(*n)->has_uses()) { aoqi@0: // note: a) if the instruction is pinned, it will be handled by compute_use_count aoqi@0: // b) if the instruction has uses, it was touched before aoqi@0: // => in both cases we don't need to update n's values aoqi@0: uses_do(n); aoqi@0: } aoqi@0: // use n aoqi@0: (*n)->_use_count++; aoqi@0: } aoqi@0: aoqi@0: Values* worklist; aoqi@0: int depth; aoqi@0: enum { aoqi@0: max_recurse_depth = 20 aoqi@0: }; aoqi@0: aoqi@0: void uses_do(Value* n) { aoqi@0: depth++; aoqi@0: if (depth > max_recurse_depth) { aoqi@0: // don't allow the traversal to recurse too deeply aoqi@0: worklist->push(*n); aoqi@0: } else { aoqi@0: (*n)->input_values_do(this); aoqi@0: // special handling for some instructions aoqi@0: if ((*n)->as_BlockEnd() != NULL) { aoqi@0: // note on BlockEnd: aoqi@0: // must 'use' the stack only if the method doesn't aoqi@0: // terminate, however, in those cases stack is empty aoqi@0: (*n)->state_values_do(this); aoqi@0: } aoqi@0: } aoqi@0: depth--; aoqi@0: } aoqi@0: aoqi@0: void block_do(BlockBegin* b) { aoqi@0: depth = 0; aoqi@0: // process all pinned nodes as the roots of expression trees aoqi@0: for (Instruction* n = b; n != NULL; n = n->next()) { aoqi@0: if (n->is_pinned()) uses_do(&n); aoqi@0: } aoqi@0: assert(depth == 0, "should have counted back down"); aoqi@0: aoqi@0: // now process any unpinned nodes which recursed too deeply aoqi@0: while (worklist->length() > 0) { aoqi@0: Value t = worklist->pop(); aoqi@0: if (!t->is_pinned()) { aoqi@0: // compute the use count aoqi@0: uses_do(&t); aoqi@0: aoqi@0: // pin the instruction so that LIRGenerator doesn't recurse aoqi@0: // too deeply during it's evaluation. aoqi@0: t->pin(); aoqi@0: } aoqi@0: } aoqi@0: assert(depth == 0, "should have counted back down"); aoqi@0: } aoqi@0: aoqi@0: UseCountComputer() { aoqi@0: worklist = new Values(); aoqi@0: depth = 0; aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: static void compute(BlockList* blocks) { aoqi@0: UseCountComputer ucc; aoqi@0: blocks->iterate_backward(&ucc); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // helper macro for short definition of trace-output inside code aoqi@0: #ifndef PRODUCT aoqi@0: #define TRACE_LINEAR_SCAN(level, code) \ aoqi@0: if (TraceLinearScanLevel >= level) { \ aoqi@0: code; \ aoqi@0: } aoqi@0: #else aoqi@0: #define TRACE_LINEAR_SCAN(level, code) aoqi@0: #endif aoqi@0: aoqi@0: class ComputeLinearScanOrder : public StackObj { aoqi@0: private: aoqi@0: int _max_block_id; // the highest block_id of a block aoqi@0: int _num_blocks; // total number of blocks (smaller than _max_block_id) aoqi@0: int _num_loops; // total number of loops aoqi@0: bool _iterative_dominators;// method requires iterative computation of dominatiors aoqi@0: aoqi@0: BlockList* _linear_scan_order; // the resulting list of blocks in correct order aoqi@0: aoqi@0: BitMap _visited_blocks; // used for recursive processing of blocks aoqi@0: BitMap _active_blocks; // used for recursive processing of blocks aoqi@0: BitMap _dominator_blocks; // temproary BitMap used for computation of dominator aoqi@0: intArray _forward_branches; // number of incoming forward branches for each block aoqi@0: BlockList _loop_end_blocks; // list of all loop end blocks collected during count_edges aoqi@0: BitMap2D _loop_map; // two-dimensional bit set: a bit is set if a block is contained in a loop aoqi@0: BlockList _work_list; // temporary list (used in mark_loops and compute_order) aoqi@0: BlockList _loop_headers; aoqi@0: aoqi@0: Compilation* _compilation; aoqi@0: aoqi@0: // accessors for _visited_blocks and _active_blocks aoqi@0: void init_visited() { _active_blocks.clear(); _visited_blocks.clear(); } aoqi@0: bool is_visited(BlockBegin* b) const { return _visited_blocks.at(b->block_id()); } aoqi@0: bool is_active(BlockBegin* b) const { return _active_blocks.at(b->block_id()); } aoqi@0: void set_visited(BlockBegin* b) { assert(!is_visited(b), "already set"); _visited_blocks.set_bit(b->block_id()); } aoqi@0: void set_active(BlockBegin* b) { assert(!is_active(b), "already set"); _active_blocks.set_bit(b->block_id()); } aoqi@0: void clear_active(BlockBegin* b) { assert(is_active(b), "not already"); _active_blocks.clear_bit(b->block_id()); } aoqi@0: aoqi@0: // accessors for _forward_branches aoqi@0: void inc_forward_branches(BlockBegin* b) { _forward_branches.at_put(b->block_id(), _forward_branches.at(b->block_id()) + 1); } aoqi@0: int dec_forward_branches(BlockBegin* b) { _forward_branches.at_put(b->block_id(), _forward_branches.at(b->block_id()) - 1); return _forward_branches.at(b->block_id()); } aoqi@0: aoqi@0: // accessors for _loop_map aoqi@0: bool is_block_in_loop (int loop_idx, BlockBegin* b) const { return _loop_map.at(loop_idx, b->block_id()); } aoqi@0: void set_block_in_loop (int loop_idx, BlockBegin* b) { _loop_map.set_bit(loop_idx, b->block_id()); } aoqi@0: void clear_block_in_loop(int loop_idx, int block_id) { _loop_map.clear_bit(loop_idx, block_id); } aoqi@0: aoqi@0: // count edges between blocks aoqi@0: void count_edges(BlockBegin* cur, BlockBegin* parent); aoqi@0: aoqi@0: // loop detection aoqi@0: void mark_loops(); aoqi@0: void clear_non_natural_loops(BlockBegin* start_block); aoqi@0: void assign_loop_depth(BlockBegin* start_block); aoqi@0: aoqi@0: // computation of final block order aoqi@0: BlockBegin* common_dominator(BlockBegin* a, BlockBegin* b); aoqi@0: void compute_dominator(BlockBegin* cur, BlockBegin* parent); aoqi@0: int compute_weight(BlockBegin* cur); aoqi@0: bool ready_for_processing(BlockBegin* cur); aoqi@0: void sort_into_work_list(BlockBegin* b); aoqi@0: void append_block(BlockBegin* cur); aoqi@0: void compute_order(BlockBegin* start_block); aoqi@0: aoqi@0: // fixup of dominators for non-natural loops aoqi@0: bool compute_dominators_iter(); aoqi@0: void compute_dominators(); aoqi@0: aoqi@0: // debug functions aoqi@0: NOT_PRODUCT(void print_blocks();) aoqi@0: DEBUG_ONLY(void verify();) aoqi@0: aoqi@0: Compilation* compilation() const { return _compilation; } aoqi@0: public: aoqi@0: ComputeLinearScanOrder(Compilation* c, BlockBegin* start_block); aoqi@0: aoqi@0: // accessors for final result aoqi@0: BlockList* linear_scan_order() const { return _linear_scan_order; } aoqi@0: int num_loops() const { return _num_loops; } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: ComputeLinearScanOrder::ComputeLinearScanOrder(Compilation* c, BlockBegin* start_block) : aoqi@0: _max_block_id(BlockBegin::number_of_blocks()), aoqi@0: _num_blocks(0), aoqi@0: _num_loops(0), aoqi@0: _iterative_dominators(false), aoqi@0: _visited_blocks(_max_block_id), aoqi@0: _active_blocks(_max_block_id), aoqi@0: _dominator_blocks(_max_block_id), aoqi@0: _forward_branches(_max_block_id, 0), aoqi@0: _loop_end_blocks(8), aoqi@0: _work_list(8), aoqi@0: _linear_scan_order(NULL), // initialized later with correct size aoqi@0: _loop_map(0, 0), // initialized later with correct size aoqi@0: _compilation(c) aoqi@0: { aoqi@0: TRACE_LINEAR_SCAN(2, tty->print_cr("***** computing linear-scan block order")); aoqi@0: aoqi@0: init_visited(); aoqi@0: count_edges(start_block, NULL); aoqi@0: aoqi@0: if (compilation()->is_profiling()) { aoqi@0: ciMethod *method = compilation()->method(); aoqi@0: if (!method->is_accessor()) { aoqi@0: ciMethodData* md = method->method_data_or_null(); aoqi@0: assert(md != NULL, "Sanity"); aoqi@0: md->set_compilation_stats(_num_loops, _num_blocks); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (_num_loops > 0) { aoqi@0: mark_loops(); aoqi@0: clear_non_natural_loops(start_block); aoqi@0: assign_loop_depth(start_block); aoqi@0: } aoqi@0: aoqi@0: compute_order(start_block); aoqi@0: compute_dominators(); aoqi@0: aoqi@0: NOT_PRODUCT(print_blocks()); aoqi@0: DEBUG_ONLY(verify()); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Traverse the CFG: aoqi@0: // * count total number of blocks aoqi@0: // * count all incoming edges and backward incoming edges aoqi@0: // * number loop header blocks aoqi@0: // * create a list with all loop end blocks aoqi@0: void ComputeLinearScanOrder::count_edges(BlockBegin* cur, BlockBegin* parent) { aoqi@0: TRACE_LINEAR_SCAN(3, tty->print_cr("Enter count_edges for block B%d coming from B%d", cur->block_id(), parent != NULL ? parent->block_id() : -1)); aoqi@0: assert(cur->dominator() == NULL, "dominator already initialized"); aoqi@0: aoqi@0: if (is_active(cur)) { aoqi@0: TRACE_LINEAR_SCAN(3, tty->print_cr("backward branch")); aoqi@0: assert(is_visited(cur), "block must be visisted when block is active"); aoqi@0: assert(parent != NULL, "must have parent"); aoqi@0: aoqi@0: cur->set(BlockBegin::linear_scan_loop_header_flag); aoqi@0: cur->set(BlockBegin::backward_branch_target_flag); aoqi@0: aoqi@0: parent->set(BlockBegin::linear_scan_loop_end_flag); aoqi@0: aoqi@0: // When a loop header is also the start of an exception handler, then the backward branch is aoqi@0: // an exception edge. Because such edges are usually critical edges which cannot be split, the aoqi@0: // loop must be excluded here from processing. aoqi@0: if (cur->is_set(BlockBegin::exception_entry_flag)) { aoqi@0: // Make sure that dominators are correct in this weird situation aoqi@0: _iterative_dominators = true; aoqi@0: return; aoqi@0: } aoqi@0: assert(parent->number_of_sux() == 1 && parent->sux_at(0) == cur, aoqi@0: "loop end blocks must have one successor (critical edges are split)"); aoqi@0: aoqi@0: _loop_end_blocks.append(parent); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // increment number of incoming forward branches aoqi@0: inc_forward_branches(cur); aoqi@0: aoqi@0: if (is_visited(cur)) { aoqi@0: TRACE_LINEAR_SCAN(3, tty->print_cr("block already visited")); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: _num_blocks++; aoqi@0: set_visited(cur); aoqi@0: set_active(cur); aoqi@0: aoqi@0: // recursive call for all successors aoqi@0: int i; aoqi@0: for (i = cur->number_of_sux() - 1; i >= 0; i--) { aoqi@0: count_edges(cur->sux_at(i), cur); aoqi@0: } aoqi@0: for (i = cur->number_of_exception_handlers() - 1; i >= 0; i--) { aoqi@0: count_edges(cur->exception_handler_at(i), cur); aoqi@0: } aoqi@0: aoqi@0: clear_active(cur); aoqi@0: aoqi@0: // Each loop has a unique number. aoqi@0: // When multiple loops are nested, assign_loop_depth assumes that the aoqi@0: // innermost loop has the lowest number. This is guaranteed by setting aoqi@0: // the loop number after the recursive calls for the successors above aoqi@0: // have returned. aoqi@0: if (cur->is_set(BlockBegin::linear_scan_loop_header_flag)) { aoqi@0: assert(cur->loop_index() == -1, "cannot set loop-index twice"); aoqi@0: TRACE_LINEAR_SCAN(3, tty->print_cr("Block B%d is loop header of loop %d", cur->block_id(), _num_loops)); aoqi@0: aoqi@0: cur->set_loop_index(_num_loops); aoqi@0: _loop_headers.append(cur); aoqi@0: _num_loops++; aoqi@0: } aoqi@0: aoqi@0: TRACE_LINEAR_SCAN(3, tty->print_cr("Finished count_edges for block B%d", cur->block_id())); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void ComputeLinearScanOrder::mark_loops() { aoqi@0: TRACE_LINEAR_SCAN(3, tty->print_cr("----- marking loops")); aoqi@0: aoqi@0: _loop_map = BitMap2D(_num_loops, _max_block_id); aoqi@0: _loop_map.clear(); aoqi@0: aoqi@0: for (int i = _loop_end_blocks.length() - 1; i >= 0; i--) { aoqi@0: BlockBegin* loop_end = _loop_end_blocks.at(i); aoqi@0: BlockBegin* loop_start = loop_end->sux_at(0); aoqi@0: int loop_idx = loop_start->loop_index(); aoqi@0: aoqi@0: TRACE_LINEAR_SCAN(3, tty->print_cr("Processing loop from B%d to B%d (loop %d):", loop_start->block_id(), loop_end->block_id(), loop_idx)); aoqi@0: assert(loop_end->is_set(BlockBegin::linear_scan_loop_end_flag), "loop end flag must be set"); aoqi@0: assert(loop_end->number_of_sux() == 1, "incorrect number of successors"); aoqi@0: assert(loop_start->is_set(BlockBegin::linear_scan_loop_header_flag), "loop header flag must be set"); aoqi@0: assert(loop_idx >= 0 && loop_idx < _num_loops, "loop index not set"); aoqi@0: assert(_work_list.is_empty(), "work list must be empty before processing"); aoqi@0: aoqi@0: // add the end-block of the loop to the working list aoqi@0: _work_list.push(loop_end); aoqi@0: set_block_in_loop(loop_idx, loop_end); aoqi@0: do { aoqi@0: BlockBegin* cur = _work_list.pop(); aoqi@0: aoqi@0: TRACE_LINEAR_SCAN(3, tty->print_cr(" processing B%d", cur->block_id())); aoqi@0: assert(is_block_in_loop(loop_idx, cur), "bit in loop map must be set when block is in work list"); aoqi@0: aoqi@0: // recursive processing of all predecessors ends when start block of loop is reached aoqi@0: if (cur != loop_start && !cur->is_set(BlockBegin::osr_entry_flag)) { aoqi@0: for (int j = cur->number_of_preds() - 1; j >= 0; j--) { aoqi@0: BlockBegin* pred = cur->pred_at(j); aoqi@0: aoqi@0: if (!is_block_in_loop(loop_idx, pred) /*&& !pred->is_set(BlockBeginosr_entry_flag)*/) { aoqi@0: // this predecessor has not been processed yet, so add it to work list aoqi@0: TRACE_LINEAR_SCAN(3, tty->print_cr(" pushing B%d", pred->block_id())); aoqi@0: _work_list.push(pred); aoqi@0: set_block_in_loop(loop_idx, pred); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } while (!_work_list.is_empty()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // check for non-natural loops (loops where the loop header does not dominate aoqi@0: // all other loop blocks = loops with mulitple entries). aoqi@0: // such loops are ignored aoqi@0: void ComputeLinearScanOrder::clear_non_natural_loops(BlockBegin* start_block) { aoqi@0: for (int i = _num_loops - 1; i >= 0; i--) { aoqi@0: if (is_block_in_loop(i, start_block)) { aoqi@0: // loop i contains the entry block of the method aoqi@0: // -> this is not a natural loop, so ignore it aoqi@0: TRACE_LINEAR_SCAN(2, tty->print_cr("Loop %d is non-natural, so it is ignored", i)); aoqi@0: aoqi@0: BlockBegin *loop_header = _loop_headers.at(i); aoqi@0: assert(loop_header->is_set(BlockBegin::linear_scan_loop_header_flag), "Must be loop header"); aoqi@0: aoqi@0: for (int j = 0; j < loop_header->number_of_preds(); j++) { aoqi@0: BlockBegin *pred = loop_header->pred_at(j); aoqi@0: pred->clear(BlockBegin::linear_scan_loop_end_flag); aoqi@0: } aoqi@0: aoqi@0: loop_header->clear(BlockBegin::linear_scan_loop_header_flag); aoqi@0: aoqi@0: for (int block_id = _max_block_id - 1; block_id >= 0; block_id--) { aoqi@0: clear_block_in_loop(i, block_id); aoqi@0: } aoqi@0: _iterative_dominators = true; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void ComputeLinearScanOrder::assign_loop_depth(BlockBegin* start_block) { aoqi@0: TRACE_LINEAR_SCAN(3, tty->print_cr("----- computing loop-depth and weight")); aoqi@0: init_visited(); aoqi@0: aoqi@0: assert(_work_list.is_empty(), "work list must be empty before processing"); aoqi@0: _work_list.append(start_block); aoqi@0: aoqi@0: do { aoqi@0: BlockBegin* cur = _work_list.pop(); aoqi@0: aoqi@0: if (!is_visited(cur)) { aoqi@0: set_visited(cur); aoqi@0: TRACE_LINEAR_SCAN(4, tty->print_cr("Computing loop depth for block B%d", cur->block_id())); aoqi@0: aoqi@0: // compute loop-depth and loop-index for the block aoqi@0: assert(cur->loop_depth() == 0, "cannot set loop-depth twice"); aoqi@0: int i; aoqi@0: int loop_depth = 0; aoqi@0: int min_loop_idx = -1; aoqi@0: for (i = _num_loops - 1; i >= 0; i--) { aoqi@0: if (is_block_in_loop(i, cur)) { aoqi@0: loop_depth++; aoqi@0: min_loop_idx = i; aoqi@0: } aoqi@0: } aoqi@0: cur->set_loop_depth(loop_depth); aoqi@0: cur->set_loop_index(min_loop_idx); aoqi@0: aoqi@0: // append all unvisited successors to work list aoqi@0: for (i = cur->number_of_sux() - 1; i >= 0; i--) { aoqi@0: _work_list.append(cur->sux_at(i)); aoqi@0: } aoqi@0: for (i = cur->number_of_exception_handlers() - 1; i >= 0; i--) { aoqi@0: _work_list.append(cur->exception_handler_at(i)); aoqi@0: } aoqi@0: } aoqi@0: } while (!_work_list.is_empty()); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: BlockBegin* ComputeLinearScanOrder::common_dominator(BlockBegin* a, BlockBegin* b) { aoqi@0: assert(a != NULL && b != NULL, "must have input blocks"); aoqi@0: aoqi@0: _dominator_blocks.clear(); aoqi@0: while (a != NULL) { aoqi@0: _dominator_blocks.set_bit(a->block_id()); aoqi@0: assert(a->dominator() != NULL || a == _linear_scan_order->at(0), "dominator must be initialized"); aoqi@0: a = a->dominator(); aoqi@0: } aoqi@0: while (b != NULL && !_dominator_blocks.at(b->block_id())) { aoqi@0: assert(b->dominator() != NULL || b == _linear_scan_order->at(0), "dominator must be initialized"); aoqi@0: b = b->dominator(); aoqi@0: } aoqi@0: aoqi@0: assert(b != NULL, "could not find dominator"); aoqi@0: return b; aoqi@0: } aoqi@0: aoqi@0: void ComputeLinearScanOrder::compute_dominator(BlockBegin* cur, BlockBegin* parent) { aoqi@0: if (cur->dominator() == NULL) { aoqi@0: TRACE_LINEAR_SCAN(4, tty->print_cr("DOM: initializing dominator of B%d to B%d", cur->block_id(), parent->block_id())); aoqi@0: cur->set_dominator(parent); aoqi@0: aoqi@0: } else if (!(cur->is_set(BlockBegin::linear_scan_loop_header_flag) && parent->is_set(BlockBegin::linear_scan_loop_end_flag))) { aoqi@0: TRACE_LINEAR_SCAN(4, tty->print_cr("DOM: computing dominator of B%d: common dominator of B%d and B%d is B%d", cur->block_id(), parent->block_id(), cur->dominator()->block_id(), common_dominator(cur->dominator(), parent)->block_id())); aoqi@0: // Does not hold for exception blocks aoqi@0: assert(cur->number_of_preds() > 1 || cur->is_set(BlockBegin::exception_entry_flag), ""); aoqi@0: cur->set_dominator(common_dominator(cur->dominator(), parent)); aoqi@0: } aoqi@0: aoqi@0: // Additional edge to xhandler of all our successors aoqi@0: // range check elimination needs that the state at the end of a aoqi@0: // block be valid in every block it dominates so cur must dominate aoqi@0: // the exception handlers of its successors. aoqi@0: int num_cur_xhandler = cur->number_of_exception_handlers(); aoqi@0: for (int j = 0; j < num_cur_xhandler; j++) { aoqi@0: BlockBegin* xhandler = cur->exception_handler_at(j); aoqi@0: compute_dominator(xhandler, parent); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: int ComputeLinearScanOrder::compute_weight(BlockBegin* cur) { aoqi@0: BlockBegin* single_sux = NULL; aoqi@0: if (cur->number_of_sux() == 1) { aoqi@0: single_sux = cur->sux_at(0); aoqi@0: } aoqi@0: aoqi@0: // limit loop-depth to 15 bit (only for security reason, it will never be so big) aoqi@0: int weight = (cur->loop_depth() & 0x7FFF) << 16; aoqi@0: aoqi@0: // general macro for short definition of weight flags aoqi@0: // the first instance of INC_WEIGHT_IF has the highest priority aoqi@0: int cur_bit = 15; aoqi@0: #define INC_WEIGHT_IF(condition) if ((condition)) { weight |= (1 << cur_bit); } cur_bit--; aoqi@0: aoqi@0: // this is necessery for the (very rare) case that two successing blocks have aoqi@0: // the same loop depth, but a different loop index (can happen for endless loops aoqi@0: // with exception handlers) aoqi@0: INC_WEIGHT_IF(!cur->is_set(BlockBegin::linear_scan_loop_header_flag)); aoqi@0: aoqi@0: // loop end blocks (blocks that end with a backward branch) are added aoqi@0: // after all other blocks of the loop. aoqi@0: INC_WEIGHT_IF(!cur->is_set(BlockBegin::linear_scan_loop_end_flag)); aoqi@0: aoqi@0: // critical edge split blocks are prefered because than they have a bigger aoqi@0: // proability to be completely empty aoqi@0: INC_WEIGHT_IF(cur->is_set(BlockBegin::critical_edge_split_flag)); aoqi@0: aoqi@0: // exceptions should not be thrown in normal control flow, so these blocks aoqi@0: // are added as late as possible aoqi@0: INC_WEIGHT_IF(cur->end()->as_Throw() == NULL && (single_sux == NULL || single_sux->end()->as_Throw() == NULL)); aoqi@0: INC_WEIGHT_IF(cur->end()->as_Return() == NULL && (single_sux == NULL || single_sux->end()->as_Return() == NULL)); aoqi@0: aoqi@0: // exceptions handlers are added as late as possible aoqi@0: INC_WEIGHT_IF(!cur->is_set(BlockBegin::exception_entry_flag)); aoqi@0: aoqi@0: // guarantee that weight is > 0 aoqi@0: weight |= 1; aoqi@0: aoqi@0: #undef INC_WEIGHT_IF aoqi@0: assert(cur_bit >= 0, "too many flags"); aoqi@0: assert(weight > 0, "weight cannot become negative"); aoqi@0: aoqi@0: return weight; aoqi@0: } aoqi@0: aoqi@0: bool ComputeLinearScanOrder::ready_for_processing(BlockBegin* cur) { aoqi@0: // Discount the edge just traveled. aoqi@0: // When the number drops to zero, all forward branches were processed aoqi@0: if (dec_forward_branches(cur) != 0) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: assert(_linear_scan_order->index_of(cur) == -1, "block already processed (block can be ready only once)"); aoqi@0: assert(_work_list.index_of(cur) == -1, "block already in work-list (block can be ready only once)"); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: void ComputeLinearScanOrder::sort_into_work_list(BlockBegin* cur) { aoqi@0: assert(_work_list.index_of(cur) == -1, "block already in work list"); aoqi@0: aoqi@0: int cur_weight = compute_weight(cur); aoqi@0: aoqi@0: // the linear_scan_number is used to cache the weight of a block aoqi@0: cur->set_linear_scan_number(cur_weight); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: if (StressLinearScan) { aoqi@0: _work_list.insert_before(0, cur); aoqi@0: return; aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: _work_list.append(NULL); // provide space for new element aoqi@0: aoqi@0: int insert_idx = _work_list.length() - 1; aoqi@0: while (insert_idx > 0 && _work_list.at(insert_idx - 1)->linear_scan_number() > cur_weight) { aoqi@0: _work_list.at_put(insert_idx, _work_list.at(insert_idx - 1)); aoqi@0: insert_idx--; aoqi@0: } aoqi@0: _work_list.at_put(insert_idx, cur); aoqi@0: aoqi@0: TRACE_LINEAR_SCAN(3, tty->print_cr("Sorted B%d into worklist. new worklist:", cur->block_id())); aoqi@0: TRACE_LINEAR_SCAN(3, for (int i = 0; i < _work_list.length(); i++) tty->print_cr("%8d B%2d weight:%6x", i, _work_list.at(i)->block_id(), _work_list.at(i)->linear_scan_number())); aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: for (int i = 0; i < _work_list.length(); i++) { aoqi@0: assert(_work_list.at(i)->linear_scan_number() > 0, "weight not set"); aoqi@0: assert(i == 0 || _work_list.at(i - 1)->linear_scan_number() <= _work_list.at(i)->linear_scan_number(), "incorrect order in worklist"); aoqi@0: } aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: void ComputeLinearScanOrder::append_block(BlockBegin* cur) { aoqi@0: TRACE_LINEAR_SCAN(3, tty->print_cr("appending block B%d (weight 0x%6x) to linear-scan order", cur->block_id(), cur->linear_scan_number())); aoqi@0: assert(_linear_scan_order->index_of(cur) == -1, "cannot add the same block twice"); aoqi@0: aoqi@0: // currently, the linear scan order and code emit order are equal. aoqi@0: // therefore the linear_scan_number and the weight of a block must also aoqi@0: // be equal. aoqi@0: cur->set_linear_scan_number(_linear_scan_order->length()); aoqi@0: _linear_scan_order->append(cur); aoqi@0: } aoqi@0: aoqi@0: void ComputeLinearScanOrder::compute_order(BlockBegin* start_block) { aoqi@0: TRACE_LINEAR_SCAN(3, tty->print_cr("----- computing final block order")); aoqi@0: aoqi@0: // the start block is always the first block in the linear scan order aoqi@0: _linear_scan_order = new BlockList(_num_blocks); aoqi@0: append_block(start_block); aoqi@0: aoqi@0: assert(start_block->end()->as_Base() != NULL, "start block must end with Base-instruction"); aoqi@0: BlockBegin* std_entry = ((Base*)start_block->end())->std_entry(); aoqi@0: BlockBegin* osr_entry = ((Base*)start_block->end())->osr_entry(); aoqi@0: aoqi@0: BlockBegin* sux_of_osr_entry = NULL; aoqi@0: if (osr_entry != NULL) { aoqi@0: // special handling for osr entry: aoqi@0: // ignore the edge between the osr entry and its successor for processing aoqi@0: // the osr entry block is added manually below aoqi@0: assert(osr_entry->number_of_sux() == 1, "osr entry must have exactly one successor"); aoqi@0: assert(osr_entry->sux_at(0)->number_of_preds() >= 2, "sucessor of osr entry must have two predecessors (otherwise it is not present in normal control flow"); aoqi@0: aoqi@0: sux_of_osr_entry = osr_entry->sux_at(0); aoqi@0: dec_forward_branches(sux_of_osr_entry); aoqi@0: aoqi@0: compute_dominator(osr_entry, start_block); aoqi@0: _iterative_dominators = true; aoqi@0: } aoqi@0: compute_dominator(std_entry, start_block); aoqi@0: aoqi@0: // start processing with standard entry block aoqi@0: assert(_work_list.is_empty(), "list must be empty before processing"); aoqi@0: aoqi@0: if (ready_for_processing(std_entry)) { aoqi@0: sort_into_work_list(std_entry); aoqi@0: } else { aoqi@0: assert(false, "the std_entry must be ready for processing (otherwise, the method has no start block)"); aoqi@0: } aoqi@0: aoqi@0: do { aoqi@0: BlockBegin* cur = _work_list.pop(); aoqi@0: aoqi@0: if (cur == sux_of_osr_entry) { aoqi@0: // the osr entry block is ignored in normal processing, it is never added to the aoqi@0: // work list. Instead, it is added as late as possible manually here. aoqi@0: append_block(osr_entry); aoqi@0: compute_dominator(cur, osr_entry); aoqi@0: } aoqi@0: append_block(cur); aoqi@0: aoqi@0: int i; aoqi@0: int num_sux = cur->number_of_sux(); aoqi@0: // changed loop order to get "intuitive" order of if- and else-blocks aoqi@0: for (i = 0; i < num_sux; i++) { aoqi@0: BlockBegin* sux = cur->sux_at(i); aoqi@0: compute_dominator(sux, cur); aoqi@0: if (ready_for_processing(sux)) { aoqi@0: sort_into_work_list(sux); aoqi@0: } aoqi@0: } aoqi@0: num_sux = cur->number_of_exception_handlers(); aoqi@0: for (i = 0; i < num_sux; i++) { aoqi@0: BlockBegin* sux = cur->exception_handler_at(i); aoqi@0: if (ready_for_processing(sux)) { aoqi@0: sort_into_work_list(sux); aoqi@0: } aoqi@0: } aoqi@0: } while (_work_list.length() > 0); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool ComputeLinearScanOrder::compute_dominators_iter() { aoqi@0: bool changed = false; aoqi@0: int num_blocks = _linear_scan_order->length(); aoqi@0: aoqi@0: assert(_linear_scan_order->at(0)->dominator() == NULL, "must not have dominator"); aoqi@0: assert(_linear_scan_order->at(0)->number_of_preds() == 0, "must not have predecessors"); aoqi@0: for (int i = 1; i < num_blocks; i++) { aoqi@0: BlockBegin* block = _linear_scan_order->at(i); aoqi@0: aoqi@0: BlockBegin* dominator = block->pred_at(0); aoqi@0: int num_preds = block->number_of_preds(); aoqi@0: aoqi@0: TRACE_LINEAR_SCAN(4, tty->print_cr("DOM: Processing B%d", block->block_id())); aoqi@0: aoqi@0: for (int j = 0; j < num_preds; j++) { aoqi@0: aoqi@0: BlockBegin *pred = block->pred_at(j); aoqi@0: TRACE_LINEAR_SCAN(4, tty->print_cr(" DOM: Subrocessing B%d", pred->block_id())); aoqi@0: aoqi@0: if (block->is_set(BlockBegin::exception_entry_flag)) { aoqi@0: dominator = common_dominator(dominator, pred); aoqi@0: int num_pred_preds = pred->number_of_preds(); aoqi@0: for (int k = 0; k < num_pred_preds; k++) { aoqi@0: dominator = common_dominator(dominator, pred->pred_at(k)); aoqi@0: } aoqi@0: } else { aoqi@0: dominator = common_dominator(dominator, pred); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (dominator != block->dominator()) { aoqi@0: TRACE_LINEAR_SCAN(4, tty->print_cr("DOM: updating dominator of B%d from B%d to B%d", block->block_id(), block->dominator()->block_id(), dominator->block_id())); aoqi@0: aoqi@0: block->set_dominator(dominator); aoqi@0: changed = true; aoqi@0: } aoqi@0: } aoqi@0: return changed; aoqi@0: } aoqi@0: aoqi@0: void ComputeLinearScanOrder::compute_dominators() { aoqi@0: TRACE_LINEAR_SCAN(3, tty->print_cr("----- computing dominators (iterative computation reqired: %d)", _iterative_dominators)); aoqi@0: aoqi@0: // iterative computation of dominators is only required for methods with non-natural loops aoqi@0: // and OSR-methods. For all other methods, the dominators computed when generating the aoqi@0: // linear scan block order are correct. aoqi@0: if (_iterative_dominators) { aoqi@0: do { aoqi@0: TRACE_LINEAR_SCAN(1, tty->print_cr("DOM: next iteration of fix-point calculation")); aoqi@0: } while (compute_dominators_iter()); aoqi@0: } aoqi@0: aoqi@0: // check that dominators are correct aoqi@0: assert(!compute_dominators_iter(), "fix point not reached"); aoqi@0: aoqi@0: // Add Blocks to dominates-Array aoqi@0: int num_blocks = _linear_scan_order->length(); aoqi@0: for (int i = 0; i < num_blocks; i++) { aoqi@0: BlockBegin* block = _linear_scan_order->at(i); aoqi@0: aoqi@0: BlockBegin *dom = block->dominator(); aoqi@0: if (dom) { aoqi@0: assert(dom->dominator_depth() != -1, "Dominator must have been visited before"); aoqi@0: dom->dominates()->append(block); aoqi@0: block->set_dominator_depth(dom->dominator_depth() + 1); aoqi@0: } else { aoqi@0: block->set_dominator_depth(0); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void ComputeLinearScanOrder::print_blocks() { aoqi@0: if (TraceLinearScanLevel >= 2) { aoqi@0: tty->print_cr("----- loop information:"); aoqi@0: for (int block_idx = 0; block_idx < _linear_scan_order->length(); block_idx++) { aoqi@0: BlockBegin* cur = _linear_scan_order->at(block_idx); aoqi@0: aoqi@0: tty->print("%4d: B%2d: ", cur->linear_scan_number(), cur->block_id()); aoqi@0: for (int loop_idx = 0; loop_idx < _num_loops; loop_idx++) { aoqi@0: tty->print ("%d ", is_block_in_loop(loop_idx, cur)); aoqi@0: } aoqi@0: tty->print_cr(" -> loop_index: %2d, loop_depth: %2d", cur->loop_index(), cur->loop_depth()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (TraceLinearScanLevel >= 1) { aoqi@0: tty->print_cr("----- linear-scan block order:"); aoqi@0: for (int block_idx = 0; block_idx < _linear_scan_order->length(); block_idx++) { aoqi@0: BlockBegin* cur = _linear_scan_order->at(block_idx); aoqi@0: tty->print("%4d: B%2d loop: %2d depth: %2d", cur->linear_scan_number(), cur->block_id(), cur->loop_index(), cur->loop_depth()); aoqi@0: aoqi@0: tty->print(cur->is_set(BlockBegin::exception_entry_flag) ? " ex" : " "); aoqi@0: tty->print(cur->is_set(BlockBegin::critical_edge_split_flag) ? " ce" : " "); aoqi@0: tty->print(cur->is_set(BlockBegin::linear_scan_loop_header_flag) ? " lh" : " "); aoqi@0: tty->print(cur->is_set(BlockBegin::linear_scan_loop_end_flag) ? " le" : " "); aoqi@0: aoqi@0: if (cur->dominator() != NULL) { aoqi@0: tty->print(" dom: B%d ", cur->dominator()->block_id()); aoqi@0: } else { aoqi@0: tty->print(" dom: NULL "); aoqi@0: } aoqi@0: aoqi@0: if (cur->number_of_preds() > 0) { aoqi@0: tty->print(" preds: "); aoqi@0: for (int j = 0; j < cur->number_of_preds(); j++) { aoqi@0: BlockBegin* pred = cur->pred_at(j); aoqi@0: tty->print("B%d ", pred->block_id()); aoqi@0: } aoqi@0: } aoqi@0: if (cur->number_of_sux() > 0) { aoqi@0: tty->print(" sux: "); aoqi@0: for (int j = 0; j < cur->number_of_sux(); j++) { aoqi@0: BlockBegin* sux = cur->sux_at(j); aoqi@0: tty->print("B%d ", sux->block_id()); aoqi@0: } aoqi@0: } aoqi@0: if (cur->number_of_exception_handlers() > 0) { aoqi@0: tty->print(" ex: "); aoqi@0: for (int j = 0; j < cur->number_of_exception_handlers(); j++) { aoqi@0: BlockBegin* ex = cur->exception_handler_at(j); aoqi@0: tty->print("B%d ", ex->block_id()); aoqi@0: } aoqi@0: } aoqi@0: tty->cr(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: void ComputeLinearScanOrder::verify() { aoqi@0: assert(_linear_scan_order->length() == _num_blocks, "wrong number of blocks in list"); aoqi@0: aoqi@0: if (StressLinearScan) { aoqi@0: // blocks are scrambled when StressLinearScan is used aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // check that all successors of a block have a higher linear-scan-number aoqi@0: // and that all predecessors of a block have a lower linear-scan-number aoqi@0: // (only backward branches of loops are ignored) aoqi@0: int i; aoqi@0: for (i = 0; i < _linear_scan_order->length(); i++) { aoqi@0: BlockBegin* cur = _linear_scan_order->at(i); aoqi@0: aoqi@0: assert(cur->linear_scan_number() == i, "incorrect linear_scan_number"); aoqi@0: assert(cur->linear_scan_number() >= 0 && cur->linear_scan_number() == _linear_scan_order->index_of(cur), "incorrect linear_scan_number"); aoqi@0: aoqi@0: int j; aoqi@0: for (j = cur->number_of_sux() - 1; j >= 0; j--) { aoqi@0: BlockBegin* sux = cur->sux_at(j); aoqi@0: aoqi@0: assert(sux->linear_scan_number() >= 0 && sux->linear_scan_number() == _linear_scan_order->index_of(sux), "incorrect linear_scan_number"); aoqi@0: if (!sux->is_set(BlockBegin::backward_branch_target_flag)) { aoqi@0: assert(cur->linear_scan_number() < sux->linear_scan_number(), "invalid order"); aoqi@0: } aoqi@0: if (cur->loop_depth() == sux->loop_depth()) { aoqi@0: assert(cur->loop_index() == sux->loop_index() || sux->is_set(BlockBegin::linear_scan_loop_header_flag), "successing blocks with same loop depth must have same loop index"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: for (j = cur->number_of_preds() - 1; j >= 0; j--) { aoqi@0: BlockBegin* pred = cur->pred_at(j); aoqi@0: aoqi@0: assert(pred->linear_scan_number() >= 0 && pred->linear_scan_number() == _linear_scan_order->index_of(pred), "incorrect linear_scan_number"); aoqi@0: if (!cur->is_set(BlockBegin::backward_branch_target_flag)) { aoqi@0: assert(cur->linear_scan_number() > pred->linear_scan_number(), "invalid order"); aoqi@0: } aoqi@0: if (cur->loop_depth() == pred->loop_depth()) { aoqi@0: assert(cur->loop_index() == pred->loop_index() || cur->is_set(BlockBegin::linear_scan_loop_header_flag), "successing blocks with same loop depth must have same loop index"); aoqi@0: } aoqi@0: aoqi@0: assert(cur->dominator()->linear_scan_number() <= cur->pred_at(j)->linear_scan_number(), "dominator must be before predecessors"); aoqi@0: } aoqi@0: aoqi@0: // check dominator aoqi@0: if (i == 0) { aoqi@0: assert(cur->dominator() == NULL, "first block has no dominator"); aoqi@0: } else { aoqi@0: assert(cur->dominator() != NULL, "all but first block must have dominator"); aoqi@0: } aoqi@0: // Assertion does not hold for exception handlers aoqi@0: assert(cur->number_of_preds() != 1 || cur->dominator() == cur->pred_at(0) || cur->is_set(BlockBegin::exception_entry_flag), "Single predecessor must also be dominator"); aoqi@0: } aoqi@0: aoqi@0: // check that all loops are continuous aoqi@0: for (int loop_idx = 0; loop_idx < _num_loops; loop_idx++) { aoqi@0: int block_idx = 0; aoqi@0: assert(!is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx)), "the first block must not be present in any loop"); aoqi@0: aoqi@0: // skip blocks before the loop aoqi@0: while (block_idx < _num_blocks && !is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx))) { aoqi@0: block_idx++; aoqi@0: } aoqi@0: // skip blocks of loop aoqi@0: while (block_idx < _num_blocks && is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx))) { aoqi@0: block_idx++; aoqi@0: } aoqi@0: // after the first non-loop block, there must not be another loop-block aoqi@0: while (block_idx < _num_blocks) { aoqi@0: assert(!is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx)), "loop not continuous in linear-scan order"); aoqi@0: block_idx++; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: aoqi@0: void IR::compute_code() { aoqi@0: assert(is_valid(), "IR must be valid"); aoqi@0: aoqi@0: ComputeLinearScanOrder compute_order(compilation(), start()); aoqi@0: _num_loops = compute_order.num_loops(); aoqi@0: _code = compute_order.linear_scan_order(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void IR::compute_use_counts() { aoqi@0: // make sure all values coming out of this block get evaluated. aoqi@0: int num_blocks = _code->length(); aoqi@0: for (int i = 0; i < num_blocks; i++) { aoqi@0: _code->at(i)->end()->state()->pin_stack_for_linear_scan(); aoqi@0: } aoqi@0: aoqi@0: // compute use counts aoqi@0: UseCountComputer::compute(_code); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void IR::iterate_preorder(BlockClosure* closure) { aoqi@0: assert(is_valid(), "IR must be valid"); aoqi@0: start()->iterate_preorder(closure); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void IR::iterate_postorder(BlockClosure* closure) { aoqi@0: assert(is_valid(), "IR must be valid"); aoqi@0: start()->iterate_postorder(closure); aoqi@0: } aoqi@0: aoqi@0: void IR::iterate_linear_scan_order(BlockClosure* closure) { aoqi@0: linear_scan_order()->iterate_forward(closure); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: class BlockPrinter: public BlockClosure { aoqi@0: private: aoqi@0: InstructionPrinter* _ip; aoqi@0: bool _cfg_only; aoqi@0: bool _live_only; aoqi@0: aoqi@0: public: aoqi@0: BlockPrinter(InstructionPrinter* ip, bool cfg_only, bool live_only = false) { aoqi@0: _ip = ip; aoqi@0: _cfg_only = cfg_only; aoqi@0: _live_only = live_only; aoqi@0: } aoqi@0: aoqi@0: virtual void block_do(BlockBegin* block) { aoqi@0: if (_cfg_only) { aoqi@0: _ip->print_instr(block); tty->cr(); aoqi@0: } else { aoqi@0: block->print_block(*_ip, _live_only); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: void IR::print(BlockBegin* start, bool cfg_only, bool live_only) { aoqi@0: ttyLocker ttyl; aoqi@0: InstructionPrinter ip(!cfg_only); aoqi@0: BlockPrinter bp(&ip, cfg_only, live_only); aoqi@0: start->iterate_preorder(&bp); aoqi@0: tty->cr(); aoqi@0: } aoqi@0: aoqi@0: void IR::print(bool cfg_only, bool live_only) { aoqi@0: if (is_valid()) { aoqi@0: print(start(), cfg_only, live_only); aoqi@0: } else { aoqi@0: tty->print_cr("invalid IR"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: define_array(BlockListArray, BlockList*) aoqi@0: define_stack(BlockListList, BlockListArray) aoqi@0: aoqi@0: class PredecessorValidator : public BlockClosure { aoqi@0: private: aoqi@0: BlockListList* _predecessors; aoqi@0: BlockList* _blocks; aoqi@0: aoqi@0: static int cmp(BlockBegin** a, BlockBegin** b) { aoqi@0: return (*a)->block_id() - (*b)->block_id(); aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: PredecessorValidator(IR* hir) { aoqi@0: ResourceMark rm; aoqi@0: _predecessors = new BlockListList(BlockBegin::number_of_blocks(), NULL); aoqi@0: _blocks = new BlockList(); aoqi@0: aoqi@0: int i; aoqi@0: hir->start()->iterate_preorder(this); aoqi@0: if (hir->code() != NULL) { aoqi@0: assert(hir->code()->length() == _blocks->length(), "must match"); aoqi@0: for (i = 0; i < _blocks->length(); i++) { aoqi@0: assert(hir->code()->contains(_blocks->at(i)), "should be in both lists"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: for (i = 0; i < _blocks->length(); i++) { aoqi@0: BlockBegin* block = _blocks->at(i); aoqi@0: BlockList* preds = _predecessors->at(block->block_id()); aoqi@0: if (preds == NULL) { aoqi@0: assert(block->number_of_preds() == 0, "should be the same"); aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: // clone the pred list so we can mutate it aoqi@0: BlockList* pred_copy = new BlockList(); aoqi@0: int j; aoqi@0: for (j = 0; j < block->number_of_preds(); j++) { aoqi@0: pred_copy->append(block->pred_at(j)); aoqi@0: } aoqi@0: // sort them in the same order aoqi@0: preds->sort(cmp); aoqi@0: pred_copy->sort(cmp); aoqi@0: int length = MIN2(preds->length(), block->number_of_preds()); aoqi@0: for (j = 0; j < block->number_of_preds(); j++) { aoqi@0: assert(preds->at(j) == pred_copy->at(j), "must match"); aoqi@0: } aoqi@0: aoqi@0: assert(preds->length() == block->number_of_preds(), "should be the same"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: virtual void block_do(BlockBegin* block) { aoqi@0: _blocks->append(block); aoqi@0: BlockEnd* be = block->end(); aoqi@0: int n = be->number_of_sux(); aoqi@0: int i; aoqi@0: for (i = 0; i < n; i++) { aoqi@0: BlockBegin* sux = be->sux_at(i); aoqi@0: assert(!sux->is_set(BlockBegin::exception_entry_flag), "must not be xhandler"); aoqi@0: aoqi@0: BlockList* preds = _predecessors->at_grow(sux->block_id(), NULL); aoqi@0: if (preds == NULL) { aoqi@0: preds = new BlockList(); aoqi@0: _predecessors->at_put(sux->block_id(), preds); aoqi@0: } aoqi@0: preds->append(block); aoqi@0: } aoqi@0: aoqi@0: n = block->number_of_exception_handlers(); aoqi@0: for (i = 0; i < n; i++) { aoqi@0: BlockBegin* sux = block->exception_handler_at(i); aoqi@0: assert(sux->is_set(BlockBegin::exception_entry_flag), "must be xhandler"); aoqi@0: aoqi@0: BlockList* preds = _predecessors->at_grow(sux->block_id(), NULL); aoqi@0: if (preds == NULL) { aoqi@0: preds = new BlockList(); aoqi@0: _predecessors->at_put(sux->block_id(), preds); aoqi@0: } aoqi@0: preds->append(block); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: class VerifyBlockBeginField : public BlockClosure { aoqi@0: aoqi@0: public: aoqi@0: aoqi@0: virtual void block_do(BlockBegin *block) { aoqi@0: for ( Instruction *cur = block; cur != NULL; cur = cur->next()) { aoqi@0: assert(cur->block() == block, "Block begin is not correct"); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: void IR::verify() { aoqi@0: #ifdef ASSERT aoqi@0: PredecessorValidator pv(this); aoqi@0: VerifyBlockBeginField verifier; aoqi@0: this->iterate_postorder(&verifier); aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: #endif // PRODUCT aoqi@0: aoqi@0: void SubstitutionResolver::visit(Value* v) { aoqi@0: Value v0 = *v; aoqi@0: if (v0) { aoqi@0: Value vs = v0->subst(); aoqi@0: if (vs != v0) { aoqi@0: *v = v0->subst(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: class SubstitutionChecker: public ValueVisitor { aoqi@0: void visit(Value* v) { aoqi@0: Value v0 = *v; aoqi@0: if (v0) { aoqi@0: Value vs = v0->subst(); aoqi@0: assert(vs == v0, "missed substitution"); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: #endif aoqi@0: aoqi@0: aoqi@0: void SubstitutionResolver::block_do(BlockBegin* block) { aoqi@0: Instruction* last = NULL; aoqi@0: for (Instruction* n = block; n != NULL;) { aoqi@0: n->values_do(this); aoqi@0: // need to remove this instruction from the instruction stream aoqi@0: if (n->subst() != n) { aoqi@0: assert(last != NULL, "must have last"); aoqi@0: last->set_next(n->next()); aoqi@0: } else { aoqi@0: last = n; aoqi@0: } aoqi@0: n = last->next(); aoqi@0: } aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: SubstitutionChecker check_substitute; aoqi@0: if (block->state()) block->state()->values_do(&check_substitute); aoqi@0: block->block_values_do(&check_substitute); aoqi@0: if (block->end() && block->end()->state()) block->end()->state()->values_do(&check_substitute); aoqi@0: #endif aoqi@0: }