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