src/share/vm/c1/c1_IR.cpp

Thu, 24 May 2018 18:41:44 +0800

author
aoqi
date
Thu, 24 May 2018 18:41:44 +0800
changeset 8856
ac27a9c85bea
parent 6876
710a3c8b516e
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "c1/c1_Compilation.hpp"
aoqi@0 27 #include "c1/c1_FrameMap.hpp"
aoqi@0 28 #include "c1/c1_GraphBuilder.hpp"
aoqi@0 29 #include "c1/c1_IR.hpp"
aoqi@0 30 #include "c1/c1_InstructionPrinter.hpp"
aoqi@0 31 #include "c1/c1_Optimizer.hpp"
aoqi@0 32 #include "utilities/bitMap.inline.hpp"
aoqi@0 33
aoqi@0 34
aoqi@0 35 // Implementation of XHandlers
aoqi@0 36 //
aoqi@0 37 // Note: This code could eventually go away if we are
aoqi@0 38 // just using the ciExceptionHandlerStream.
aoqi@0 39
aoqi@0 40 XHandlers::XHandlers(ciMethod* method) : _list(method->exception_table_length()) {
aoqi@0 41 ciExceptionHandlerStream s(method);
aoqi@0 42 while (!s.is_done()) {
aoqi@0 43 _list.append(new XHandler(s.handler()));
aoqi@0 44 s.next();
aoqi@0 45 }
aoqi@0 46 assert(s.count() == method->exception_table_length(), "exception table lengths inconsistent");
aoqi@0 47 }
aoqi@0 48
aoqi@0 49 // deep copy of all XHandler contained in list
aoqi@0 50 XHandlers::XHandlers(XHandlers* other) :
aoqi@0 51 _list(other->length())
aoqi@0 52 {
aoqi@0 53 for (int i = 0; i < other->length(); i++) {
aoqi@0 54 _list.append(new XHandler(other->handler_at(i)));
aoqi@0 55 }
aoqi@0 56 }
aoqi@0 57
aoqi@0 58 // Returns whether a particular exception type can be caught. Also
aoqi@0 59 // returns true if klass is unloaded or any exception handler
aoqi@0 60 // classes are unloaded. type_is_exact indicates whether the throw
aoqi@0 61 // is known to be exactly that class or it might throw a subtype.
aoqi@0 62 bool XHandlers::could_catch(ciInstanceKlass* klass, bool type_is_exact) const {
aoqi@0 63 // the type is unknown so be conservative
aoqi@0 64 if (!klass->is_loaded()) {
aoqi@0 65 return true;
aoqi@0 66 }
aoqi@0 67
aoqi@0 68 for (int i = 0; i < length(); i++) {
aoqi@0 69 XHandler* handler = handler_at(i);
aoqi@0 70 if (handler->is_catch_all()) {
aoqi@0 71 // catch of ANY
aoqi@0 72 return true;
aoqi@0 73 }
aoqi@0 74 ciInstanceKlass* handler_klass = handler->catch_klass();
aoqi@0 75 // if it's unknown it might be catchable
aoqi@0 76 if (!handler_klass->is_loaded()) {
aoqi@0 77 return true;
aoqi@0 78 }
aoqi@0 79 // if the throw type is definitely a subtype of the catch type
aoqi@0 80 // then it can be caught.
aoqi@0 81 if (klass->is_subtype_of(handler_klass)) {
aoqi@0 82 return true;
aoqi@0 83 }
aoqi@0 84 if (!type_is_exact) {
aoqi@0 85 // If the type isn't exactly known then it can also be caught by
aoqi@0 86 // catch statements where the inexact type is a subtype of the
aoqi@0 87 // catch type.
aoqi@0 88 // given: foo extends bar extends Exception
aoqi@0 89 // throw bar can be caught by catch foo, catch bar, and catch
aoqi@0 90 // Exception, however it can't be caught by any handlers without
aoqi@0 91 // bar in its type hierarchy.
aoqi@0 92 if (handler_klass->is_subtype_of(klass)) {
aoqi@0 93 return true;
aoqi@0 94 }
aoqi@0 95 }
aoqi@0 96 }
aoqi@0 97
aoqi@0 98 return false;
aoqi@0 99 }
aoqi@0 100
aoqi@0 101
aoqi@0 102 bool XHandlers::equals(XHandlers* others) const {
aoqi@0 103 if (others == NULL) return false;
aoqi@0 104 if (length() != others->length()) return false;
aoqi@0 105
aoqi@0 106 for (int i = 0; i < length(); i++) {
aoqi@0 107 if (!handler_at(i)->equals(others->handler_at(i))) return false;
aoqi@0 108 }
aoqi@0 109 return true;
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 bool XHandler::equals(XHandler* other) const {
aoqi@0 113 assert(entry_pco() != -1 && other->entry_pco() != -1, "must have entry_pco");
aoqi@0 114
aoqi@0 115 if (entry_pco() != other->entry_pco()) return false;
aoqi@0 116 if (scope_count() != other->scope_count()) return false;
aoqi@0 117 if (_desc != other->_desc) return false;
aoqi@0 118
aoqi@0 119 assert(entry_block() == other->entry_block(), "entry_block must be equal when entry_pco is equal");
aoqi@0 120 return true;
aoqi@0 121 }
aoqi@0 122
aoqi@0 123
aoqi@0 124 // Implementation of IRScope
aoqi@0 125 BlockBegin* IRScope::build_graph(Compilation* compilation, int osr_bci) {
aoqi@0 126 GraphBuilder gm(compilation, this);
aoqi@0 127 NOT_PRODUCT(if (PrintValueNumbering && Verbose) gm.print_stats());
aoqi@0 128 if (compilation->bailed_out()) return NULL;
aoqi@0 129 return gm.start();
aoqi@0 130 }
aoqi@0 131
aoqi@0 132
aoqi@0 133 IRScope::IRScope(Compilation* compilation, IRScope* caller, int caller_bci, ciMethod* method, int osr_bci, bool create_graph)
aoqi@0 134 : _callees(2)
aoqi@0 135 , _compilation(compilation)
aoqi@0 136 , _requires_phi_function(method->max_locals())
aoqi@0 137 {
aoqi@0 138 _caller = caller;
aoqi@0 139 _level = caller == NULL ? 0 : caller->level() + 1;
aoqi@0 140 _method = method;
aoqi@0 141 _xhandlers = new XHandlers(method);
aoqi@0 142 _number_of_locks = 0;
aoqi@0 143 _monitor_pairing_ok = method->has_balanced_monitors();
aoqi@0 144 _wrote_final = false;
aoqi@0 145 _start = NULL;
aoqi@0 146
aoqi@0 147 if (osr_bci == -1) {
aoqi@0 148 _requires_phi_function.clear();
aoqi@0 149 } else {
aoqi@0 150 // selective creation of phi functions is not possibel in osr-methods
aoqi@0 151 _requires_phi_function.set_range(0, method->max_locals());
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 assert(method->holder()->is_loaded() , "method holder must be loaded");
aoqi@0 155
aoqi@0 156 // build graph if monitor pairing is ok
aoqi@0 157 if (create_graph && monitor_pairing_ok()) _start = build_graph(compilation, osr_bci);
aoqi@0 158 }
aoqi@0 159
aoqi@0 160
aoqi@0 161 int IRScope::max_stack() const {
aoqi@0 162 int my_max = method()->max_stack();
aoqi@0 163 int callee_max = 0;
aoqi@0 164 for (int i = 0; i < number_of_callees(); i++) {
aoqi@0 165 callee_max = MAX2(callee_max, callee_no(i)->max_stack());
aoqi@0 166 }
aoqi@0 167 return my_max + callee_max;
aoqi@0 168 }
aoqi@0 169
aoqi@0 170
aoqi@0 171 bool IRScopeDebugInfo::should_reexecute() {
aoqi@0 172 ciMethod* cur_method = scope()->method();
aoqi@0 173 int cur_bci = bci();
aoqi@0 174 if (cur_method != NULL && cur_bci != SynchronizationEntryBCI) {
aoqi@0 175 Bytecodes::Code code = cur_method->java_code_at_bci(cur_bci);
aoqi@0 176 return Interpreter::bytecode_should_reexecute(code);
aoqi@0 177 } else
aoqi@0 178 return false;
aoqi@0 179 }
aoqi@0 180
aoqi@0 181
aoqi@0 182 // Implementation of CodeEmitInfo
aoqi@0 183
aoqi@0 184 // Stack must be NON-null
aoqi@0 185 CodeEmitInfo::CodeEmitInfo(ValueStack* stack, XHandlers* exception_handlers, bool deoptimize_on_exception)
aoqi@0 186 : _scope(stack->scope())
aoqi@0 187 , _scope_debug_info(NULL)
aoqi@0 188 , _oop_map(NULL)
aoqi@0 189 , _stack(stack)
aoqi@0 190 , _exception_handlers(exception_handlers)
aoqi@0 191 , _is_method_handle_invoke(false)
aoqi@0 192 , _deoptimize_on_exception(deoptimize_on_exception) {
aoqi@0 193 assert(_stack != NULL, "must be non null");
aoqi@0 194 }
aoqi@0 195
aoqi@0 196
aoqi@0 197 CodeEmitInfo::CodeEmitInfo(CodeEmitInfo* info, ValueStack* stack)
aoqi@0 198 : _scope(info->_scope)
aoqi@0 199 , _exception_handlers(NULL)
aoqi@0 200 , _scope_debug_info(NULL)
aoqi@0 201 , _oop_map(NULL)
aoqi@0 202 , _stack(stack == NULL ? info->_stack : stack)
aoqi@0 203 , _is_method_handle_invoke(info->_is_method_handle_invoke)
aoqi@0 204 , _deoptimize_on_exception(info->_deoptimize_on_exception) {
aoqi@0 205
aoqi@0 206 // deep copy of exception handlers
aoqi@0 207 if (info->_exception_handlers != NULL) {
aoqi@0 208 _exception_handlers = new XHandlers(info->_exception_handlers);
aoqi@0 209 }
aoqi@0 210 }
aoqi@0 211
aoqi@0 212
aoqi@0 213 void CodeEmitInfo::record_debug_info(DebugInformationRecorder* recorder, int pc_offset) {
aoqi@0 214 // record the safepoint before recording the debug info for enclosing scopes
aoqi@0 215 recorder->add_safepoint(pc_offset, _oop_map->deep_copy());
aoqi@0 216 _scope_debug_info->record_debug_info(recorder, pc_offset, true/*topmost*/, _is_method_handle_invoke);
aoqi@0 217 recorder->end_safepoint(pc_offset);
aoqi@0 218 }
aoqi@0 219
aoqi@0 220
aoqi@0 221 void CodeEmitInfo::add_register_oop(LIR_Opr opr) {
aoqi@0 222 assert(_oop_map != NULL, "oop map must already exist");
aoqi@0 223 assert(opr->is_single_cpu(), "should not call otherwise");
aoqi@0 224
aoqi@0 225 VMReg name = frame_map()->regname(opr);
aoqi@0 226 _oop_map->set_oop(name);
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 // Mirror the stack size calculation in the deopt code
aoqi@0 230 // How much stack space would we need at this point in the program in
aoqi@0 231 // case of deoptimization?
aoqi@0 232 int CodeEmitInfo::interpreter_frame_size() const {
aoqi@0 233 ValueStack* state = _stack;
aoqi@0 234 int size = 0;
aoqi@0 235 int callee_parameters = 0;
aoqi@0 236 int callee_locals = 0;
aoqi@0 237 int extra_args = state->scope()->method()->max_stack() - state->stack_size();
aoqi@0 238
aoqi@0 239 while (state != NULL) {
aoqi@0 240 int locks = state->locks_size();
aoqi@0 241 int temps = state->stack_size();
aoqi@0 242 bool is_top_frame = (state == _stack);
aoqi@0 243 ciMethod* method = state->scope()->method();
aoqi@0 244
aoqi@0 245 int frame_size = BytesPerWord * Interpreter::size_activation(method->max_stack(),
aoqi@0 246 temps + callee_parameters,
aoqi@0 247 extra_args,
aoqi@0 248 locks,
aoqi@0 249 callee_parameters,
aoqi@0 250 callee_locals,
aoqi@0 251 is_top_frame);
aoqi@0 252 size += frame_size;
aoqi@0 253
aoqi@0 254 callee_parameters = method->size_of_parameters();
aoqi@0 255 callee_locals = method->max_locals();
aoqi@0 256 extra_args = 0;
aoqi@0 257 state = state->caller_state();
aoqi@0 258 }
aoqi@0 259 return size + Deoptimization::last_frame_adjust(0, callee_locals) * BytesPerWord;
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 // Implementation of IR
aoqi@0 263
aoqi@0 264 IR::IR(Compilation* compilation, ciMethod* method, int osr_bci) :
aoqi@0 265 _locals_size(in_WordSize(-1))
aoqi@0 266 , _num_loops(0) {
aoqi@0 267 // setup IR fields
aoqi@0 268 _compilation = compilation;
aoqi@0 269 _top_scope = new IRScope(compilation, NULL, -1, method, osr_bci, true);
aoqi@0 270 _code = NULL;
aoqi@0 271 }
aoqi@0 272
aoqi@0 273
aoqi@0 274 void IR::optimize_blocks() {
aoqi@0 275 Optimizer opt(this);
aoqi@0 276 if (!compilation()->profile_branches()) {
aoqi@0 277 if (DoCEE) {
aoqi@0 278 opt.eliminate_conditional_expressions();
aoqi@0 279 #ifndef PRODUCT
aoqi@0 280 if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after CEE"); print(true); }
aoqi@0 281 if (PrintIR || PrintIR1 ) { tty->print_cr("IR after CEE"); print(false); }
aoqi@0 282 #endif
aoqi@0 283 }
aoqi@0 284 if (EliminateBlocks) {
aoqi@0 285 opt.eliminate_blocks();
aoqi@0 286 #ifndef PRODUCT
aoqi@0 287 if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after block elimination"); print(true); }
aoqi@0 288 if (PrintIR || PrintIR1 ) { tty->print_cr("IR after block elimination"); print(false); }
aoqi@0 289 #endif
aoqi@0 290 }
aoqi@0 291 }
aoqi@0 292 }
aoqi@0 293
aoqi@0 294 void IR::eliminate_null_checks() {
aoqi@0 295 Optimizer opt(this);
aoqi@0 296 if (EliminateNullChecks) {
aoqi@0 297 opt.eliminate_null_checks();
aoqi@0 298 #ifndef PRODUCT
aoqi@0 299 if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after null check elimination"); print(true); }
aoqi@0 300 if (PrintIR || PrintIR1 ) { tty->print_cr("IR after null check elimination"); print(false); }
aoqi@0 301 #endif
aoqi@0 302 }
aoqi@0 303 }
aoqi@0 304
aoqi@0 305
aoqi@0 306 static int sort_pairs(BlockPair** a, BlockPair** b) {
aoqi@0 307 if ((*a)->from() == (*b)->from()) {
aoqi@0 308 return (*a)->to()->block_id() - (*b)->to()->block_id();
aoqi@0 309 } else {
aoqi@0 310 return (*a)->from()->block_id() - (*b)->from()->block_id();
aoqi@0 311 }
aoqi@0 312 }
aoqi@0 313
aoqi@0 314
aoqi@0 315 class CriticalEdgeFinder: public BlockClosure {
aoqi@0 316 BlockPairList blocks;
aoqi@0 317 IR* _ir;
aoqi@0 318
aoqi@0 319 public:
aoqi@0 320 CriticalEdgeFinder(IR* ir): _ir(ir) {}
aoqi@0 321 void block_do(BlockBegin* bb) {
aoqi@0 322 BlockEnd* be = bb->end();
aoqi@0 323 int nos = be->number_of_sux();
aoqi@0 324 if (nos >= 2) {
aoqi@0 325 for (int i = 0; i < nos; i++) {
aoqi@0 326 BlockBegin* sux = be->sux_at(i);
aoqi@0 327 if (sux->number_of_preds() >= 2) {
aoqi@0 328 blocks.append(new BlockPair(bb, sux));
aoqi@0 329 }
aoqi@0 330 }
aoqi@0 331 }
aoqi@0 332 }
aoqi@0 333
aoqi@0 334 void split_edges() {
aoqi@0 335 BlockPair* last_pair = NULL;
aoqi@0 336 blocks.sort(sort_pairs);
aoqi@0 337 for (int i = 0; i < blocks.length(); i++) {
aoqi@0 338 BlockPair* pair = blocks.at(i);
aoqi@0 339 if (last_pair != NULL && pair->is_same(last_pair)) continue;
aoqi@0 340 BlockBegin* from = pair->from();
aoqi@0 341 BlockBegin* to = pair->to();
aoqi@0 342 BlockBegin* split = from->insert_block_between(to);
aoqi@0 343 #ifndef PRODUCT
aoqi@0 344 if ((PrintIR || PrintIR1) && Verbose) {
aoqi@0 345 tty->print_cr("Split critical edge B%d -> B%d (new block B%d)",
aoqi@0 346 from->block_id(), to->block_id(), split->block_id());
aoqi@0 347 }
aoqi@0 348 #endif
aoqi@0 349 last_pair = pair;
aoqi@0 350 }
aoqi@0 351 }
aoqi@0 352 };
aoqi@0 353
aoqi@0 354 void IR::split_critical_edges() {
aoqi@0 355 CriticalEdgeFinder cef(this);
aoqi@0 356
aoqi@0 357 iterate_preorder(&cef);
aoqi@0 358 cef.split_edges();
aoqi@0 359 }
aoqi@0 360
aoqi@0 361
aoqi@0 362 class UseCountComputer: public ValueVisitor, BlockClosure {
aoqi@0 363 private:
aoqi@0 364 void visit(Value* n) {
aoqi@0 365 // Local instructions and Phis for expression stack values at the
aoqi@0 366 // start of basic blocks are not added to the instruction list
aoqi@0 367 if (!(*n)->is_linked() && (*n)->can_be_linked()) {
aoqi@0 368 assert(false, "a node was not appended to the graph");
aoqi@0 369 Compilation::current()->bailout("a node was not appended to the graph");
aoqi@0 370 }
aoqi@0 371 // use n's input if not visited before
aoqi@0 372 if (!(*n)->is_pinned() && !(*n)->has_uses()) {
aoqi@0 373 // note: a) if the instruction is pinned, it will be handled by compute_use_count
aoqi@0 374 // b) if the instruction has uses, it was touched before
aoqi@0 375 // => in both cases we don't need to update n's values
aoqi@0 376 uses_do(n);
aoqi@0 377 }
aoqi@0 378 // use n
aoqi@0 379 (*n)->_use_count++;
aoqi@0 380 }
aoqi@0 381
aoqi@0 382 Values* worklist;
aoqi@0 383 int depth;
aoqi@0 384 enum {
aoqi@0 385 max_recurse_depth = 20
aoqi@0 386 };
aoqi@0 387
aoqi@0 388 void uses_do(Value* n) {
aoqi@0 389 depth++;
aoqi@0 390 if (depth > max_recurse_depth) {
aoqi@0 391 // don't allow the traversal to recurse too deeply
aoqi@0 392 worklist->push(*n);
aoqi@0 393 } else {
aoqi@0 394 (*n)->input_values_do(this);
aoqi@0 395 // special handling for some instructions
aoqi@0 396 if ((*n)->as_BlockEnd() != NULL) {
aoqi@0 397 // note on BlockEnd:
aoqi@0 398 // must 'use' the stack only if the method doesn't
aoqi@0 399 // terminate, however, in those cases stack is empty
aoqi@0 400 (*n)->state_values_do(this);
aoqi@0 401 }
aoqi@0 402 }
aoqi@0 403 depth--;
aoqi@0 404 }
aoqi@0 405
aoqi@0 406 void block_do(BlockBegin* b) {
aoqi@0 407 depth = 0;
aoqi@0 408 // process all pinned nodes as the roots of expression trees
aoqi@0 409 for (Instruction* n = b; n != NULL; n = n->next()) {
aoqi@0 410 if (n->is_pinned()) uses_do(&n);
aoqi@0 411 }
aoqi@0 412 assert(depth == 0, "should have counted back down");
aoqi@0 413
aoqi@0 414 // now process any unpinned nodes which recursed too deeply
aoqi@0 415 while (worklist->length() > 0) {
aoqi@0 416 Value t = worklist->pop();
aoqi@0 417 if (!t->is_pinned()) {
aoqi@0 418 // compute the use count
aoqi@0 419 uses_do(&t);
aoqi@0 420
aoqi@0 421 // pin the instruction so that LIRGenerator doesn't recurse
aoqi@0 422 // too deeply during it's evaluation.
aoqi@0 423 t->pin();
aoqi@0 424 }
aoqi@0 425 }
aoqi@0 426 assert(depth == 0, "should have counted back down");
aoqi@0 427 }
aoqi@0 428
aoqi@0 429 UseCountComputer() {
aoqi@0 430 worklist = new Values();
aoqi@0 431 depth = 0;
aoqi@0 432 }
aoqi@0 433
aoqi@0 434 public:
aoqi@0 435 static void compute(BlockList* blocks) {
aoqi@0 436 UseCountComputer ucc;
aoqi@0 437 blocks->iterate_backward(&ucc);
aoqi@0 438 }
aoqi@0 439 };
aoqi@0 440
aoqi@0 441
aoqi@0 442 // helper macro for short definition of trace-output inside code
aoqi@0 443 #ifndef PRODUCT
aoqi@0 444 #define TRACE_LINEAR_SCAN(level, code) \
aoqi@0 445 if (TraceLinearScanLevel >= level) { \
aoqi@0 446 code; \
aoqi@0 447 }
aoqi@0 448 #else
aoqi@0 449 #define TRACE_LINEAR_SCAN(level, code)
aoqi@0 450 #endif
aoqi@0 451
aoqi@0 452 class ComputeLinearScanOrder : public StackObj {
aoqi@0 453 private:
aoqi@0 454 int _max_block_id; // the highest block_id of a block
aoqi@0 455 int _num_blocks; // total number of blocks (smaller than _max_block_id)
aoqi@0 456 int _num_loops; // total number of loops
aoqi@0 457 bool _iterative_dominators;// method requires iterative computation of dominatiors
aoqi@0 458
aoqi@0 459 BlockList* _linear_scan_order; // the resulting list of blocks in correct order
aoqi@0 460
aoqi@0 461 BitMap _visited_blocks; // used for recursive processing of blocks
aoqi@0 462 BitMap _active_blocks; // used for recursive processing of blocks
aoqi@0 463 BitMap _dominator_blocks; // temproary BitMap used for computation of dominator
aoqi@0 464 intArray _forward_branches; // number of incoming forward branches for each block
aoqi@0 465 BlockList _loop_end_blocks; // list of all loop end blocks collected during count_edges
aoqi@0 466 BitMap2D _loop_map; // two-dimensional bit set: a bit is set if a block is contained in a loop
aoqi@0 467 BlockList _work_list; // temporary list (used in mark_loops and compute_order)
aoqi@0 468 BlockList _loop_headers;
aoqi@0 469
aoqi@0 470 Compilation* _compilation;
aoqi@0 471
aoqi@0 472 // accessors for _visited_blocks and _active_blocks
aoqi@0 473 void init_visited() { _active_blocks.clear(); _visited_blocks.clear(); }
aoqi@0 474 bool is_visited(BlockBegin* b) const { return _visited_blocks.at(b->block_id()); }
aoqi@0 475 bool is_active(BlockBegin* b) const { return _active_blocks.at(b->block_id()); }
aoqi@0 476 void set_visited(BlockBegin* b) { assert(!is_visited(b), "already set"); _visited_blocks.set_bit(b->block_id()); }
aoqi@0 477 void set_active(BlockBegin* b) { assert(!is_active(b), "already set"); _active_blocks.set_bit(b->block_id()); }
aoqi@0 478 void clear_active(BlockBegin* b) { assert(is_active(b), "not already"); _active_blocks.clear_bit(b->block_id()); }
aoqi@0 479
aoqi@0 480 // accessors for _forward_branches
aoqi@0 481 void inc_forward_branches(BlockBegin* b) { _forward_branches.at_put(b->block_id(), _forward_branches.at(b->block_id()) + 1); }
aoqi@0 482 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 483
aoqi@0 484 // accessors for _loop_map
aoqi@0 485 bool is_block_in_loop (int loop_idx, BlockBegin* b) const { return _loop_map.at(loop_idx, b->block_id()); }
aoqi@0 486 void set_block_in_loop (int loop_idx, BlockBegin* b) { _loop_map.set_bit(loop_idx, b->block_id()); }
aoqi@0 487 void clear_block_in_loop(int loop_idx, int block_id) { _loop_map.clear_bit(loop_idx, block_id); }
aoqi@0 488
aoqi@0 489 // count edges between blocks
aoqi@0 490 void count_edges(BlockBegin* cur, BlockBegin* parent);
aoqi@0 491
aoqi@0 492 // loop detection
aoqi@0 493 void mark_loops();
aoqi@0 494 void clear_non_natural_loops(BlockBegin* start_block);
aoqi@0 495 void assign_loop_depth(BlockBegin* start_block);
aoqi@0 496
aoqi@0 497 // computation of final block order
aoqi@0 498 BlockBegin* common_dominator(BlockBegin* a, BlockBegin* b);
aoqi@0 499 void compute_dominator(BlockBegin* cur, BlockBegin* parent);
aoqi@0 500 int compute_weight(BlockBegin* cur);
aoqi@0 501 bool ready_for_processing(BlockBegin* cur);
aoqi@0 502 void sort_into_work_list(BlockBegin* b);
aoqi@0 503 void append_block(BlockBegin* cur);
aoqi@0 504 void compute_order(BlockBegin* start_block);
aoqi@0 505
aoqi@0 506 // fixup of dominators for non-natural loops
aoqi@0 507 bool compute_dominators_iter();
aoqi@0 508 void compute_dominators();
aoqi@0 509
aoqi@0 510 // debug functions
aoqi@0 511 NOT_PRODUCT(void print_blocks();)
aoqi@0 512 DEBUG_ONLY(void verify();)
aoqi@0 513
aoqi@0 514 Compilation* compilation() const { return _compilation; }
aoqi@0 515 public:
aoqi@0 516 ComputeLinearScanOrder(Compilation* c, BlockBegin* start_block);
aoqi@0 517
aoqi@0 518 // accessors for final result
aoqi@0 519 BlockList* linear_scan_order() const { return _linear_scan_order; }
aoqi@0 520 int num_loops() const { return _num_loops; }
aoqi@0 521 };
aoqi@0 522
aoqi@0 523
aoqi@0 524 ComputeLinearScanOrder::ComputeLinearScanOrder(Compilation* c, BlockBegin* start_block) :
aoqi@0 525 _max_block_id(BlockBegin::number_of_blocks()),
aoqi@0 526 _num_blocks(0),
aoqi@0 527 _num_loops(0),
aoqi@0 528 _iterative_dominators(false),
aoqi@0 529 _visited_blocks(_max_block_id),
aoqi@0 530 _active_blocks(_max_block_id),
aoqi@0 531 _dominator_blocks(_max_block_id),
aoqi@0 532 _forward_branches(_max_block_id, 0),
aoqi@0 533 _loop_end_blocks(8),
aoqi@0 534 _work_list(8),
aoqi@0 535 _linear_scan_order(NULL), // initialized later with correct size
aoqi@0 536 _loop_map(0, 0), // initialized later with correct size
aoqi@0 537 _compilation(c)
aoqi@0 538 {
aoqi@0 539 TRACE_LINEAR_SCAN(2, tty->print_cr("***** computing linear-scan block order"));
aoqi@0 540
aoqi@0 541 init_visited();
aoqi@0 542 count_edges(start_block, NULL);
aoqi@0 543
aoqi@0 544 if (compilation()->is_profiling()) {
aoqi@0 545 ciMethod *method = compilation()->method();
aoqi@0 546 if (!method->is_accessor()) {
aoqi@0 547 ciMethodData* md = method->method_data_or_null();
aoqi@0 548 assert(md != NULL, "Sanity");
aoqi@0 549 md->set_compilation_stats(_num_loops, _num_blocks);
aoqi@0 550 }
aoqi@0 551 }
aoqi@0 552
aoqi@0 553 if (_num_loops > 0) {
aoqi@0 554 mark_loops();
aoqi@0 555 clear_non_natural_loops(start_block);
aoqi@0 556 assign_loop_depth(start_block);
aoqi@0 557 }
aoqi@0 558
aoqi@0 559 compute_order(start_block);
aoqi@0 560 compute_dominators();
aoqi@0 561
aoqi@0 562 NOT_PRODUCT(print_blocks());
aoqi@0 563 DEBUG_ONLY(verify());
aoqi@0 564 }
aoqi@0 565
aoqi@0 566
aoqi@0 567 // Traverse the CFG:
aoqi@0 568 // * count total number of blocks
aoqi@0 569 // * count all incoming edges and backward incoming edges
aoqi@0 570 // * number loop header blocks
aoqi@0 571 // * create a list with all loop end blocks
aoqi@0 572 void ComputeLinearScanOrder::count_edges(BlockBegin* cur, BlockBegin* parent) {
aoqi@0 573 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 574 assert(cur->dominator() == NULL, "dominator already initialized");
aoqi@0 575
aoqi@0 576 if (is_active(cur)) {
aoqi@0 577 TRACE_LINEAR_SCAN(3, tty->print_cr("backward branch"));
aoqi@0 578 assert(is_visited(cur), "block must be visisted when block is active");
aoqi@0 579 assert(parent != NULL, "must have parent");
aoqi@0 580
aoqi@0 581 cur->set(BlockBegin::linear_scan_loop_header_flag);
aoqi@0 582 cur->set(BlockBegin::backward_branch_target_flag);
aoqi@0 583
aoqi@0 584 parent->set(BlockBegin::linear_scan_loop_end_flag);
aoqi@0 585
aoqi@0 586 // When a loop header is also the start of an exception handler, then the backward branch is
aoqi@0 587 // an exception edge. Because such edges are usually critical edges which cannot be split, the
aoqi@0 588 // loop must be excluded here from processing.
aoqi@0 589 if (cur->is_set(BlockBegin::exception_entry_flag)) {
aoqi@0 590 // Make sure that dominators are correct in this weird situation
aoqi@0 591 _iterative_dominators = true;
aoqi@0 592 return;
aoqi@0 593 }
aoqi@0 594 assert(parent->number_of_sux() == 1 && parent->sux_at(0) == cur,
aoqi@0 595 "loop end blocks must have one successor (critical edges are split)");
aoqi@0 596
aoqi@0 597 _loop_end_blocks.append(parent);
aoqi@0 598 return;
aoqi@0 599 }
aoqi@0 600
aoqi@0 601 // increment number of incoming forward branches
aoqi@0 602 inc_forward_branches(cur);
aoqi@0 603
aoqi@0 604 if (is_visited(cur)) {
aoqi@0 605 TRACE_LINEAR_SCAN(3, tty->print_cr("block already visited"));
aoqi@0 606 return;
aoqi@0 607 }
aoqi@0 608
aoqi@0 609 _num_blocks++;
aoqi@0 610 set_visited(cur);
aoqi@0 611 set_active(cur);
aoqi@0 612
aoqi@0 613 // recursive call for all successors
aoqi@0 614 int i;
aoqi@0 615 for (i = cur->number_of_sux() - 1; i >= 0; i--) {
aoqi@0 616 count_edges(cur->sux_at(i), cur);
aoqi@0 617 }
aoqi@0 618 for (i = cur->number_of_exception_handlers() - 1; i >= 0; i--) {
aoqi@0 619 count_edges(cur->exception_handler_at(i), cur);
aoqi@0 620 }
aoqi@0 621
aoqi@0 622 clear_active(cur);
aoqi@0 623
aoqi@0 624 // Each loop has a unique number.
aoqi@0 625 // When multiple loops are nested, assign_loop_depth assumes that the
aoqi@0 626 // innermost loop has the lowest number. This is guaranteed by setting
aoqi@0 627 // the loop number after the recursive calls for the successors above
aoqi@0 628 // have returned.
aoqi@0 629 if (cur->is_set(BlockBegin::linear_scan_loop_header_flag)) {
aoqi@0 630 assert(cur->loop_index() == -1, "cannot set loop-index twice");
aoqi@0 631 TRACE_LINEAR_SCAN(3, tty->print_cr("Block B%d is loop header of loop %d", cur->block_id(), _num_loops));
aoqi@0 632
aoqi@0 633 cur->set_loop_index(_num_loops);
aoqi@0 634 _loop_headers.append(cur);
aoqi@0 635 _num_loops++;
aoqi@0 636 }
aoqi@0 637
aoqi@0 638 TRACE_LINEAR_SCAN(3, tty->print_cr("Finished count_edges for block B%d", cur->block_id()));
aoqi@0 639 }
aoqi@0 640
aoqi@0 641
aoqi@0 642 void ComputeLinearScanOrder::mark_loops() {
aoqi@0 643 TRACE_LINEAR_SCAN(3, tty->print_cr("----- marking loops"));
aoqi@0 644
aoqi@0 645 _loop_map = BitMap2D(_num_loops, _max_block_id);
aoqi@0 646 _loop_map.clear();
aoqi@0 647
aoqi@0 648 for (int i = _loop_end_blocks.length() - 1; i >= 0; i--) {
aoqi@0 649 BlockBegin* loop_end = _loop_end_blocks.at(i);
aoqi@0 650 BlockBegin* loop_start = loop_end->sux_at(0);
aoqi@0 651 int loop_idx = loop_start->loop_index();
aoqi@0 652
aoqi@0 653 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 654 assert(loop_end->is_set(BlockBegin::linear_scan_loop_end_flag), "loop end flag must be set");
aoqi@0 655 assert(loop_end->number_of_sux() == 1, "incorrect number of successors");
aoqi@0 656 assert(loop_start->is_set(BlockBegin::linear_scan_loop_header_flag), "loop header flag must be set");
aoqi@0 657 assert(loop_idx >= 0 && loop_idx < _num_loops, "loop index not set");
aoqi@0 658 assert(_work_list.is_empty(), "work list must be empty before processing");
aoqi@0 659
aoqi@0 660 // add the end-block of the loop to the working list
aoqi@0 661 _work_list.push(loop_end);
aoqi@0 662 set_block_in_loop(loop_idx, loop_end);
aoqi@0 663 do {
aoqi@0 664 BlockBegin* cur = _work_list.pop();
aoqi@0 665
aoqi@0 666 TRACE_LINEAR_SCAN(3, tty->print_cr(" processing B%d", cur->block_id()));
aoqi@0 667 assert(is_block_in_loop(loop_idx, cur), "bit in loop map must be set when block is in work list");
aoqi@0 668
aoqi@0 669 // recursive processing of all predecessors ends when start block of loop is reached
aoqi@0 670 if (cur != loop_start && !cur->is_set(BlockBegin::osr_entry_flag)) {
aoqi@0 671 for (int j = cur->number_of_preds() - 1; j >= 0; j--) {
aoqi@0 672 BlockBegin* pred = cur->pred_at(j);
aoqi@0 673
aoqi@0 674 if (!is_block_in_loop(loop_idx, pred) /*&& !pred->is_set(BlockBeginosr_entry_flag)*/) {
aoqi@0 675 // this predecessor has not been processed yet, so add it to work list
aoqi@0 676 TRACE_LINEAR_SCAN(3, tty->print_cr(" pushing B%d", pred->block_id()));
aoqi@0 677 _work_list.push(pred);
aoqi@0 678 set_block_in_loop(loop_idx, pred);
aoqi@0 679 }
aoqi@0 680 }
aoqi@0 681 }
aoqi@0 682 } while (!_work_list.is_empty());
aoqi@0 683 }
aoqi@0 684 }
aoqi@0 685
aoqi@0 686
aoqi@0 687 // check for non-natural loops (loops where the loop header does not dominate
aoqi@0 688 // all other loop blocks = loops with mulitple entries).
aoqi@0 689 // such loops are ignored
aoqi@0 690 void ComputeLinearScanOrder::clear_non_natural_loops(BlockBegin* start_block) {
aoqi@0 691 for (int i = _num_loops - 1; i >= 0; i--) {
aoqi@0 692 if (is_block_in_loop(i, start_block)) {
aoqi@0 693 // loop i contains the entry block of the method
aoqi@0 694 // -> this is not a natural loop, so ignore it
aoqi@0 695 TRACE_LINEAR_SCAN(2, tty->print_cr("Loop %d is non-natural, so it is ignored", i));
aoqi@0 696
aoqi@0 697 BlockBegin *loop_header = _loop_headers.at(i);
aoqi@0 698 assert(loop_header->is_set(BlockBegin::linear_scan_loop_header_flag), "Must be loop header");
aoqi@0 699
aoqi@0 700 for (int j = 0; j < loop_header->number_of_preds(); j++) {
aoqi@0 701 BlockBegin *pred = loop_header->pred_at(j);
aoqi@0 702 pred->clear(BlockBegin::linear_scan_loop_end_flag);
aoqi@0 703 }
aoqi@0 704
aoqi@0 705 loop_header->clear(BlockBegin::linear_scan_loop_header_flag);
aoqi@0 706
aoqi@0 707 for (int block_id = _max_block_id - 1; block_id >= 0; block_id--) {
aoqi@0 708 clear_block_in_loop(i, block_id);
aoqi@0 709 }
aoqi@0 710 _iterative_dominators = true;
aoqi@0 711 }
aoqi@0 712 }
aoqi@0 713 }
aoqi@0 714
aoqi@0 715 void ComputeLinearScanOrder::assign_loop_depth(BlockBegin* start_block) {
aoqi@0 716 TRACE_LINEAR_SCAN(3, tty->print_cr("----- computing loop-depth and weight"));
aoqi@0 717 init_visited();
aoqi@0 718
aoqi@0 719 assert(_work_list.is_empty(), "work list must be empty before processing");
aoqi@0 720 _work_list.append(start_block);
aoqi@0 721
aoqi@0 722 do {
aoqi@0 723 BlockBegin* cur = _work_list.pop();
aoqi@0 724
aoqi@0 725 if (!is_visited(cur)) {
aoqi@0 726 set_visited(cur);
aoqi@0 727 TRACE_LINEAR_SCAN(4, tty->print_cr("Computing loop depth for block B%d", cur->block_id()));
aoqi@0 728
aoqi@0 729 // compute loop-depth and loop-index for the block
aoqi@0 730 assert(cur->loop_depth() == 0, "cannot set loop-depth twice");
aoqi@0 731 int i;
aoqi@0 732 int loop_depth = 0;
aoqi@0 733 int min_loop_idx = -1;
aoqi@0 734 for (i = _num_loops - 1; i >= 0; i--) {
aoqi@0 735 if (is_block_in_loop(i, cur)) {
aoqi@0 736 loop_depth++;
aoqi@0 737 min_loop_idx = i;
aoqi@0 738 }
aoqi@0 739 }
aoqi@0 740 cur->set_loop_depth(loop_depth);
aoqi@0 741 cur->set_loop_index(min_loop_idx);
aoqi@0 742
aoqi@0 743 // append all unvisited successors to work list
aoqi@0 744 for (i = cur->number_of_sux() - 1; i >= 0; i--) {
aoqi@0 745 _work_list.append(cur->sux_at(i));
aoqi@0 746 }
aoqi@0 747 for (i = cur->number_of_exception_handlers() - 1; i >= 0; i--) {
aoqi@0 748 _work_list.append(cur->exception_handler_at(i));
aoqi@0 749 }
aoqi@0 750 }
aoqi@0 751 } while (!_work_list.is_empty());
aoqi@0 752 }
aoqi@0 753
aoqi@0 754
aoqi@0 755 BlockBegin* ComputeLinearScanOrder::common_dominator(BlockBegin* a, BlockBegin* b) {
aoqi@0 756 assert(a != NULL && b != NULL, "must have input blocks");
aoqi@0 757
aoqi@0 758 _dominator_blocks.clear();
aoqi@0 759 while (a != NULL) {
aoqi@0 760 _dominator_blocks.set_bit(a->block_id());
aoqi@0 761 assert(a->dominator() != NULL || a == _linear_scan_order->at(0), "dominator must be initialized");
aoqi@0 762 a = a->dominator();
aoqi@0 763 }
aoqi@0 764 while (b != NULL && !_dominator_blocks.at(b->block_id())) {
aoqi@0 765 assert(b->dominator() != NULL || b == _linear_scan_order->at(0), "dominator must be initialized");
aoqi@0 766 b = b->dominator();
aoqi@0 767 }
aoqi@0 768
aoqi@0 769 assert(b != NULL, "could not find dominator");
aoqi@0 770 return b;
aoqi@0 771 }
aoqi@0 772
aoqi@0 773 void ComputeLinearScanOrder::compute_dominator(BlockBegin* cur, BlockBegin* parent) {
aoqi@0 774 if (cur->dominator() == NULL) {
aoqi@0 775 TRACE_LINEAR_SCAN(4, tty->print_cr("DOM: initializing dominator of B%d to B%d", cur->block_id(), parent->block_id()));
aoqi@0 776 cur->set_dominator(parent);
aoqi@0 777
aoqi@0 778 } else if (!(cur->is_set(BlockBegin::linear_scan_loop_header_flag) && parent->is_set(BlockBegin::linear_scan_loop_end_flag))) {
aoqi@0 779 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 780 // Does not hold for exception blocks
aoqi@0 781 assert(cur->number_of_preds() > 1 || cur->is_set(BlockBegin::exception_entry_flag), "");
aoqi@0 782 cur->set_dominator(common_dominator(cur->dominator(), parent));
aoqi@0 783 }
aoqi@0 784
aoqi@0 785 // Additional edge to xhandler of all our successors
aoqi@0 786 // range check elimination needs that the state at the end of a
aoqi@0 787 // block be valid in every block it dominates so cur must dominate
aoqi@0 788 // the exception handlers of its successors.
aoqi@0 789 int num_cur_xhandler = cur->number_of_exception_handlers();
aoqi@0 790 for (int j = 0; j < num_cur_xhandler; j++) {
aoqi@0 791 BlockBegin* xhandler = cur->exception_handler_at(j);
aoqi@0 792 compute_dominator(xhandler, parent);
aoqi@0 793 }
aoqi@0 794 }
aoqi@0 795
aoqi@0 796
aoqi@0 797 int ComputeLinearScanOrder::compute_weight(BlockBegin* cur) {
aoqi@0 798 BlockBegin* single_sux = NULL;
aoqi@0 799 if (cur->number_of_sux() == 1) {
aoqi@0 800 single_sux = cur->sux_at(0);
aoqi@0 801 }
aoqi@0 802
aoqi@0 803 // limit loop-depth to 15 bit (only for security reason, it will never be so big)
aoqi@0 804 int weight = (cur->loop_depth() & 0x7FFF) << 16;
aoqi@0 805
aoqi@0 806 // general macro for short definition of weight flags
aoqi@0 807 // the first instance of INC_WEIGHT_IF has the highest priority
aoqi@0 808 int cur_bit = 15;
aoqi@0 809 #define INC_WEIGHT_IF(condition) if ((condition)) { weight |= (1 << cur_bit); } cur_bit--;
aoqi@0 810
aoqi@0 811 // this is necessery for the (very rare) case that two successing blocks have
aoqi@0 812 // the same loop depth, but a different loop index (can happen for endless loops
aoqi@0 813 // with exception handlers)
aoqi@0 814 INC_WEIGHT_IF(!cur->is_set(BlockBegin::linear_scan_loop_header_flag));
aoqi@0 815
aoqi@0 816 // loop end blocks (blocks that end with a backward branch) are added
aoqi@0 817 // after all other blocks of the loop.
aoqi@0 818 INC_WEIGHT_IF(!cur->is_set(BlockBegin::linear_scan_loop_end_flag));
aoqi@0 819
aoqi@0 820 // critical edge split blocks are prefered because than they have a bigger
aoqi@0 821 // proability to be completely empty
aoqi@0 822 INC_WEIGHT_IF(cur->is_set(BlockBegin::critical_edge_split_flag));
aoqi@0 823
aoqi@0 824 // exceptions should not be thrown in normal control flow, so these blocks
aoqi@0 825 // are added as late as possible
aoqi@0 826 INC_WEIGHT_IF(cur->end()->as_Throw() == NULL && (single_sux == NULL || single_sux->end()->as_Throw() == NULL));
aoqi@0 827 INC_WEIGHT_IF(cur->end()->as_Return() == NULL && (single_sux == NULL || single_sux->end()->as_Return() == NULL));
aoqi@0 828
aoqi@0 829 // exceptions handlers are added as late as possible
aoqi@0 830 INC_WEIGHT_IF(!cur->is_set(BlockBegin::exception_entry_flag));
aoqi@0 831
aoqi@0 832 // guarantee that weight is > 0
aoqi@0 833 weight |= 1;
aoqi@0 834
aoqi@0 835 #undef INC_WEIGHT_IF
aoqi@0 836 assert(cur_bit >= 0, "too many flags");
aoqi@0 837 assert(weight > 0, "weight cannot become negative");
aoqi@0 838
aoqi@0 839 return weight;
aoqi@0 840 }
aoqi@0 841
aoqi@0 842 bool ComputeLinearScanOrder::ready_for_processing(BlockBegin* cur) {
aoqi@0 843 // Discount the edge just traveled.
aoqi@0 844 // When the number drops to zero, all forward branches were processed
aoqi@0 845 if (dec_forward_branches(cur) != 0) {
aoqi@0 846 return false;
aoqi@0 847 }
aoqi@0 848
aoqi@0 849 assert(_linear_scan_order->index_of(cur) == -1, "block already processed (block can be ready only once)");
aoqi@0 850 assert(_work_list.index_of(cur) == -1, "block already in work-list (block can be ready only once)");
aoqi@0 851 return true;
aoqi@0 852 }
aoqi@0 853
aoqi@0 854 void ComputeLinearScanOrder::sort_into_work_list(BlockBegin* cur) {
aoqi@0 855 assert(_work_list.index_of(cur) == -1, "block already in work list");
aoqi@0 856
aoqi@0 857 int cur_weight = compute_weight(cur);
aoqi@0 858
aoqi@0 859 // the linear_scan_number is used to cache the weight of a block
aoqi@0 860 cur->set_linear_scan_number(cur_weight);
aoqi@0 861
aoqi@0 862 #ifndef PRODUCT
aoqi@0 863 if (StressLinearScan) {
aoqi@0 864 _work_list.insert_before(0, cur);
aoqi@0 865 return;
aoqi@0 866 }
aoqi@0 867 #endif
aoqi@0 868
aoqi@0 869 _work_list.append(NULL); // provide space for new element
aoqi@0 870
aoqi@0 871 int insert_idx = _work_list.length() - 1;
aoqi@0 872 while (insert_idx > 0 && _work_list.at(insert_idx - 1)->linear_scan_number() > cur_weight) {
aoqi@0 873 _work_list.at_put(insert_idx, _work_list.at(insert_idx - 1));
aoqi@0 874 insert_idx--;
aoqi@0 875 }
aoqi@0 876 _work_list.at_put(insert_idx, cur);
aoqi@0 877
aoqi@0 878 TRACE_LINEAR_SCAN(3, tty->print_cr("Sorted B%d into worklist. new worklist:", cur->block_id()));
aoqi@0 879 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 880
aoqi@0 881 #ifdef ASSERT
aoqi@0 882 for (int i = 0; i < _work_list.length(); i++) {
aoqi@0 883 assert(_work_list.at(i)->linear_scan_number() > 0, "weight not set");
aoqi@0 884 assert(i == 0 || _work_list.at(i - 1)->linear_scan_number() <= _work_list.at(i)->linear_scan_number(), "incorrect order in worklist");
aoqi@0 885 }
aoqi@0 886 #endif
aoqi@0 887 }
aoqi@0 888
aoqi@0 889 void ComputeLinearScanOrder::append_block(BlockBegin* cur) {
aoqi@0 890 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 891 assert(_linear_scan_order->index_of(cur) == -1, "cannot add the same block twice");
aoqi@0 892
aoqi@0 893 // currently, the linear scan order and code emit order are equal.
aoqi@0 894 // therefore the linear_scan_number and the weight of a block must also
aoqi@0 895 // be equal.
aoqi@0 896 cur->set_linear_scan_number(_linear_scan_order->length());
aoqi@0 897 _linear_scan_order->append(cur);
aoqi@0 898 }
aoqi@0 899
aoqi@0 900 void ComputeLinearScanOrder::compute_order(BlockBegin* start_block) {
aoqi@0 901 TRACE_LINEAR_SCAN(3, tty->print_cr("----- computing final block order"));
aoqi@0 902
aoqi@0 903 // the start block is always the first block in the linear scan order
aoqi@0 904 _linear_scan_order = new BlockList(_num_blocks);
aoqi@0 905 append_block(start_block);
aoqi@0 906
aoqi@0 907 assert(start_block->end()->as_Base() != NULL, "start block must end with Base-instruction");
aoqi@0 908 BlockBegin* std_entry = ((Base*)start_block->end())->std_entry();
aoqi@0 909 BlockBegin* osr_entry = ((Base*)start_block->end())->osr_entry();
aoqi@0 910
aoqi@0 911 BlockBegin* sux_of_osr_entry = NULL;
aoqi@0 912 if (osr_entry != NULL) {
aoqi@0 913 // special handling for osr entry:
aoqi@0 914 // ignore the edge between the osr entry and its successor for processing
aoqi@0 915 // the osr entry block is added manually below
aoqi@0 916 assert(osr_entry->number_of_sux() == 1, "osr entry must have exactly one successor");
aoqi@0 917 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 918
aoqi@0 919 sux_of_osr_entry = osr_entry->sux_at(0);
aoqi@0 920 dec_forward_branches(sux_of_osr_entry);
aoqi@0 921
aoqi@0 922 compute_dominator(osr_entry, start_block);
aoqi@0 923 _iterative_dominators = true;
aoqi@0 924 }
aoqi@0 925 compute_dominator(std_entry, start_block);
aoqi@0 926
aoqi@0 927 // start processing with standard entry block
aoqi@0 928 assert(_work_list.is_empty(), "list must be empty before processing");
aoqi@0 929
aoqi@0 930 if (ready_for_processing(std_entry)) {
aoqi@0 931 sort_into_work_list(std_entry);
aoqi@0 932 } else {
aoqi@0 933 assert(false, "the std_entry must be ready for processing (otherwise, the method has no start block)");
aoqi@0 934 }
aoqi@0 935
aoqi@0 936 do {
aoqi@0 937 BlockBegin* cur = _work_list.pop();
aoqi@0 938
aoqi@0 939 if (cur == sux_of_osr_entry) {
aoqi@0 940 // the osr entry block is ignored in normal processing, it is never added to the
aoqi@0 941 // work list. Instead, it is added as late as possible manually here.
aoqi@0 942 append_block(osr_entry);
aoqi@0 943 compute_dominator(cur, osr_entry);
aoqi@0 944 }
aoqi@0 945 append_block(cur);
aoqi@0 946
aoqi@0 947 int i;
aoqi@0 948 int num_sux = cur->number_of_sux();
aoqi@0 949 // changed loop order to get "intuitive" order of if- and else-blocks
aoqi@0 950 for (i = 0; i < num_sux; i++) {
aoqi@0 951 BlockBegin* sux = cur->sux_at(i);
aoqi@0 952 compute_dominator(sux, cur);
aoqi@0 953 if (ready_for_processing(sux)) {
aoqi@0 954 sort_into_work_list(sux);
aoqi@0 955 }
aoqi@0 956 }
aoqi@0 957 num_sux = cur->number_of_exception_handlers();
aoqi@0 958 for (i = 0; i < num_sux; i++) {
aoqi@0 959 BlockBegin* sux = cur->exception_handler_at(i);
aoqi@0 960 if (ready_for_processing(sux)) {
aoqi@0 961 sort_into_work_list(sux);
aoqi@0 962 }
aoqi@0 963 }
aoqi@0 964 } while (_work_list.length() > 0);
aoqi@0 965 }
aoqi@0 966
aoqi@0 967
aoqi@0 968 bool ComputeLinearScanOrder::compute_dominators_iter() {
aoqi@0 969 bool changed = false;
aoqi@0 970 int num_blocks = _linear_scan_order->length();
aoqi@0 971
aoqi@0 972 assert(_linear_scan_order->at(0)->dominator() == NULL, "must not have dominator");
aoqi@0 973 assert(_linear_scan_order->at(0)->number_of_preds() == 0, "must not have predecessors");
aoqi@0 974 for (int i = 1; i < num_blocks; i++) {
aoqi@0 975 BlockBegin* block = _linear_scan_order->at(i);
aoqi@0 976
aoqi@0 977 BlockBegin* dominator = block->pred_at(0);
aoqi@0 978 int num_preds = block->number_of_preds();
aoqi@0 979
aoqi@0 980 TRACE_LINEAR_SCAN(4, tty->print_cr("DOM: Processing B%d", block->block_id()));
aoqi@0 981
aoqi@0 982 for (int j = 0; j < num_preds; j++) {
aoqi@0 983
aoqi@0 984 BlockBegin *pred = block->pred_at(j);
aoqi@0 985 TRACE_LINEAR_SCAN(4, tty->print_cr(" DOM: Subrocessing B%d", pred->block_id()));
aoqi@0 986
aoqi@0 987 if (block->is_set(BlockBegin::exception_entry_flag)) {
aoqi@0 988 dominator = common_dominator(dominator, pred);
aoqi@0 989 int num_pred_preds = pred->number_of_preds();
aoqi@0 990 for (int k = 0; k < num_pred_preds; k++) {
aoqi@0 991 dominator = common_dominator(dominator, pred->pred_at(k));
aoqi@0 992 }
aoqi@0 993 } else {
aoqi@0 994 dominator = common_dominator(dominator, pred);
aoqi@0 995 }
aoqi@0 996 }
aoqi@0 997
aoqi@0 998 if (dominator != block->dominator()) {
aoqi@0 999 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 1000
aoqi@0 1001 block->set_dominator(dominator);
aoqi@0 1002 changed = true;
aoqi@0 1003 }
aoqi@0 1004 }
aoqi@0 1005 return changed;
aoqi@0 1006 }
aoqi@0 1007
aoqi@0 1008 void ComputeLinearScanOrder::compute_dominators() {
aoqi@0 1009 TRACE_LINEAR_SCAN(3, tty->print_cr("----- computing dominators (iterative computation reqired: %d)", _iterative_dominators));
aoqi@0 1010
aoqi@0 1011 // iterative computation of dominators is only required for methods with non-natural loops
aoqi@0 1012 // and OSR-methods. For all other methods, the dominators computed when generating the
aoqi@0 1013 // linear scan block order are correct.
aoqi@0 1014 if (_iterative_dominators) {
aoqi@0 1015 do {
aoqi@0 1016 TRACE_LINEAR_SCAN(1, tty->print_cr("DOM: next iteration of fix-point calculation"));
aoqi@0 1017 } while (compute_dominators_iter());
aoqi@0 1018 }
aoqi@0 1019
aoqi@0 1020 // check that dominators are correct
aoqi@0 1021 assert(!compute_dominators_iter(), "fix point not reached");
aoqi@0 1022
aoqi@0 1023 // Add Blocks to dominates-Array
aoqi@0 1024 int num_blocks = _linear_scan_order->length();
aoqi@0 1025 for (int i = 0; i < num_blocks; i++) {
aoqi@0 1026 BlockBegin* block = _linear_scan_order->at(i);
aoqi@0 1027
aoqi@0 1028 BlockBegin *dom = block->dominator();
aoqi@0 1029 if (dom) {
aoqi@0 1030 assert(dom->dominator_depth() != -1, "Dominator must have been visited before");
aoqi@0 1031 dom->dominates()->append(block);
aoqi@0 1032 block->set_dominator_depth(dom->dominator_depth() + 1);
aoqi@0 1033 } else {
aoqi@0 1034 block->set_dominator_depth(0);
aoqi@0 1035 }
aoqi@0 1036 }
aoqi@0 1037 }
aoqi@0 1038
aoqi@0 1039
aoqi@0 1040 #ifndef PRODUCT
aoqi@0 1041 void ComputeLinearScanOrder::print_blocks() {
aoqi@0 1042 if (TraceLinearScanLevel >= 2) {
aoqi@0 1043 tty->print_cr("----- loop information:");
aoqi@0 1044 for (int block_idx = 0; block_idx < _linear_scan_order->length(); block_idx++) {
aoqi@0 1045 BlockBegin* cur = _linear_scan_order->at(block_idx);
aoqi@0 1046
aoqi@0 1047 tty->print("%4d: B%2d: ", cur->linear_scan_number(), cur->block_id());
aoqi@0 1048 for (int loop_idx = 0; loop_idx < _num_loops; loop_idx++) {
aoqi@0 1049 tty->print ("%d ", is_block_in_loop(loop_idx, cur));
aoqi@0 1050 }
aoqi@0 1051 tty->print_cr(" -> loop_index: %2d, loop_depth: %2d", cur->loop_index(), cur->loop_depth());
aoqi@0 1052 }
aoqi@0 1053 }
aoqi@0 1054
aoqi@0 1055 if (TraceLinearScanLevel >= 1) {
aoqi@0 1056 tty->print_cr("----- linear-scan block order:");
aoqi@0 1057 for (int block_idx = 0; block_idx < _linear_scan_order->length(); block_idx++) {
aoqi@0 1058 BlockBegin* cur = _linear_scan_order->at(block_idx);
aoqi@0 1059 tty->print("%4d: B%2d loop: %2d depth: %2d", cur->linear_scan_number(), cur->block_id(), cur->loop_index(), cur->loop_depth());
aoqi@0 1060
aoqi@0 1061 tty->print(cur->is_set(BlockBegin::exception_entry_flag) ? " ex" : " ");
aoqi@0 1062 tty->print(cur->is_set(BlockBegin::critical_edge_split_flag) ? " ce" : " ");
aoqi@0 1063 tty->print(cur->is_set(BlockBegin::linear_scan_loop_header_flag) ? " lh" : " ");
aoqi@0 1064 tty->print(cur->is_set(BlockBegin::linear_scan_loop_end_flag) ? " le" : " ");
aoqi@0 1065
aoqi@0 1066 if (cur->dominator() != NULL) {
aoqi@0 1067 tty->print(" dom: B%d ", cur->dominator()->block_id());
aoqi@0 1068 } else {
aoqi@0 1069 tty->print(" dom: NULL ");
aoqi@0 1070 }
aoqi@0 1071
aoqi@0 1072 if (cur->number_of_preds() > 0) {
aoqi@0 1073 tty->print(" preds: ");
aoqi@0 1074 for (int j = 0; j < cur->number_of_preds(); j++) {
aoqi@0 1075 BlockBegin* pred = cur->pred_at(j);
aoqi@0 1076 tty->print("B%d ", pred->block_id());
aoqi@0 1077 }
aoqi@0 1078 }
aoqi@0 1079 if (cur->number_of_sux() > 0) {
aoqi@0 1080 tty->print(" sux: ");
aoqi@0 1081 for (int j = 0; j < cur->number_of_sux(); j++) {
aoqi@0 1082 BlockBegin* sux = cur->sux_at(j);
aoqi@0 1083 tty->print("B%d ", sux->block_id());
aoqi@0 1084 }
aoqi@0 1085 }
aoqi@0 1086 if (cur->number_of_exception_handlers() > 0) {
aoqi@0 1087 tty->print(" ex: ");
aoqi@0 1088 for (int j = 0; j < cur->number_of_exception_handlers(); j++) {
aoqi@0 1089 BlockBegin* ex = cur->exception_handler_at(j);
aoqi@0 1090 tty->print("B%d ", ex->block_id());
aoqi@0 1091 }
aoqi@0 1092 }
aoqi@0 1093 tty->cr();
aoqi@0 1094 }
aoqi@0 1095 }
aoqi@0 1096 }
aoqi@0 1097 #endif
aoqi@0 1098
aoqi@0 1099 #ifdef ASSERT
aoqi@0 1100 void ComputeLinearScanOrder::verify() {
aoqi@0 1101 assert(_linear_scan_order->length() == _num_blocks, "wrong number of blocks in list");
aoqi@0 1102
aoqi@0 1103 if (StressLinearScan) {
aoqi@0 1104 // blocks are scrambled when StressLinearScan is used
aoqi@0 1105 return;
aoqi@0 1106 }
aoqi@0 1107
aoqi@0 1108 // check that all successors of a block have a higher linear-scan-number
aoqi@0 1109 // and that all predecessors of a block have a lower linear-scan-number
aoqi@0 1110 // (only backward branches of loops are ignored)
aoqi@0 1111 int i;
aoqi@0 1112 for (i = 0; i < _linear_scan_order->length(); i++) {
aoqi@0 1113 BlockBegin* cur = _linear_scan_order->at(i);
aoqi@0 1114
aoqi@0 1115 assert(cur->linear_scan_number() == i, "incorrect linear_scan_number");
aoqi@0 1116 assert(cur->linear_scan_number() >= 0 && cur->linear_scan_number() == _linear_scan_order->index_of(cur), "incorrect linear_scan_number");
aoqi@0 1117
aoqi@0 1118 int j;
aoqi@0 1119 for (j = cur->number_of_sux() - 1; j >= 0; j--) {
aoqi@0 1120 BlockBegin* sux = cur->sux_at(j);
aoqi@0 1121
aoqi@0 1122 assert(sux->linear_scan_number() >= 0 && sux->linear_scan_number() == _linear_scan_order->index_of(sux), "incorrect linear_scan_number");
aoqi@0 1123 if (!sux->is_set(BlockBegin::backward_branch_target_flag)) {
aoqi@0 1124 assert(cur->linear_scan_number() < sux->linear_scan_number(), "invalid order");
aoqi@0 1125 }
aoqi@0 1126 if (cur->loop_depth() == sux->loop_depth()) {
aoqi@0 1127 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 1128 }
aoqi@0 1129 }
aoqi@0 1130
aoqi@0 1131 for (j = cur->number_of_preds() - 1; j >= 0; j--) {
aoqi@0 1132 BlockBegin* pred = cur->pred_at(j);
aoqi@0 1133
aoqi@0 1134 assert(pred->linear_scan_number() >= 0 && pred->linear_scan_number() == _linear_scan_order->index_of(pred), "incorrect linear_scan_number");
aoqi@0 1135 if (!cur->is_set(BlockBegin::backward_branch_target_flag)) {
aoqi@0 1136 assert(cur->linear_scan_number() > pred->linear_scan_number(), "invalid order");
aoqi@0 1137 }
aoqi@0 1138 if (cur->loop_depth() == pred->loop_depth()) {
aoqi@0 1139 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 1140 }
aoqi@0 1141
aoqi@0 1142 assert(cur->dominator()->linear_scan_number() <= cur->pred_at(j)->linear_scan_number(), "dominator must be before predecessors");
aoqi@0 1143 }
aoqi@0 1144
aoqi@0 1145 // check dominator
aoqi@0 1146 if (i == 0) {
aoqi@0 1147 assert(cur->dominator() == NULL, "first block has no dominator");
aoqi@0 1148 } else {
aoqi@0 1149 assert(cur->dominator() != NULL, "all but first block must have dominator");
aoqi@0 1150 }
aoqi@0 1151 // Assertion does not hold for exception handlers
aoqi@0 1152 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 1153 }
aoqi@0 1154
aoqi@0 1155 // check that all loops are continuous
aoqi@0 1156 for (int loop_idx = 0; loop_idx < _num_loops; loop_idx++) {
aoqi@0 1157 int block_idx = 0;
aoqi@0 1158 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 1159
aoqi@0 1160 // skip blocks before the loop
aoqi@0 1161 while (block_idx < _num_blocks && !is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx))) {
aoqi@0 1162 block_idx++;
aoqi@0 1163 }
aoqi@0 1164 // skip blocks of loop
aoqi@0 1165 while (block_idx < _num_blocks && is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx))) {
aoqi@0 1166 block_idx++;
aoqi@0 1167 }
aoqi@0 1168 // after the first non-loop block, there must not be another loop-block
aoqi@0 1169 while (block_idx < _num_blocks) {
aoqi@0 1170 assert(!is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx)), "loop not continuous in linear-scan order");
aoqi@0 1171 block_idx++;
aoqi@0 1172 }
aoqi@0 1173 }
aoqi@0 1174 }
aoqi@0 1175 #endif
aoqi@0 1176
aoqi@0 1177
aoqi@0 1178 void IR::compute_code() {
aoqi@0 1179 assert(is_valid(), "IR must be valid");
aoqi@0 1180
aoqi@0 1181 ComputeLinearScanOrder compute_order(compilation(), start());
aoqi@0 1182 _num_loops = compute_order.num_loops();
aoqi@0 1183 _code = compute_order.linear_scan_order();
aoqi@0 1184 }
aoqi@0 1185
aoqi@0 1186
aoqi@0 1187 void IR::compute_use_counts() {
aoqi@0 1188 // make sure all values coming out of this block get evaluated.
aoqi@0 1189 int num_blocks = _code->length();
aoqi@0 1190 for (int i = 0; i < num_blocks; i++) {
aoqi@0 1191 _code->at(i)->end()->state()->pin_stack_for_linear_scan();
aoqi@0 1192 }
aoqi@0 1193
aoqi@0 1194 // compute use counts
aoqi@0 1195 UseCountComputer::compute(_code);
aoqi@0 1196 }
aoqi@0 1197
aoqi@0 1198
aoqi@0 1199 void IR::iterate_preorder(BlockClosure* closure) {
aoqi@0 1200 assert(is_valid(), "IR must be valid");
aoqi@0 1201 start()->iterate_preorder(closure);
aoqi@0 1202 }
aoqi@0 1203
aoqi@0 1204
aoqi@0 1205 void IR::iterate_postorder(BlockClosure* closure) {
aoqi@0 1206 assert(is_valid(), "IR must be valid");
aoqi@0 1207 start()->iterate_postorder(closure);
aoqi@0 1208 }
aoqi@0 1209
aoqi@0 1210 void IR::iterate_linear_scan_order(BlockClosure* closure) {
aoqi@0 1211 linear_scan_order()->iterate_forward(closure);
aoqi@0 1212 }
aoqi@0 1213
aoqi@0 1214
aoqi@0 1215 #ifndef PRODUCT
aoqi@0 1216 class BlockPrinter: public BlockClosure {
aoqi@0 1217 private:
aoqi@0 1218 InstructionPrinter* _ip;
aoqi@0 1219 bool _cfg_only;
aoqi@0 1220 bool _live_only;
aoqi@0 1221
aoqi@0 1222 public:
aoqi@0 1223 BlockPrinter(InstructionPrinter* ip, bool cfg_only, bool live_only = false) {
aoqi@0 1224 _ip = ip;
aoqi@0 1225 _cfg_only = cfg_only;
aoqi@0 1226 _live_only = live_only;
aoqi@0 1227 }
aoqi@0 1228
aoqi@0 1229 virtual void block_do(BlockBegin* block) {
aoqi@0 1230 if (_cfg_only) {
aoqi@0 1231 _ip->print_instr(block); tty->cr();
aoqi@0 1232 } else {
aoqi@0 1233 block->print_block(*_ip, _live_only);
aoqi@0 1234 }
aoqi@0 1235 }
aoqi@0 1236 };
aoqi@0 1237
aoqi@0 1238
aoqi@0 1239 void IR::print(BlockBegin* start, bool cfg_only, bool live_only) {
aoqi@0 1240 ttyLocker ttyl;
aoqi@0 1241 InstructionPrinter ip(!cfg_only);
aoqi@0 1242 BlockPrinter bp(&ip, cfg_only, live_only);
aoqi@0 1243 start->iterate_preorder(&bp);
aoqi@0 1244 tty->cr();
aoqi@0 1245 }
aoqi@0 1246
aoqi@0 1247 void IR::print(bool cfg_only, bool live_only) {
aoqi@0 1248 if (is_valid()) {
aoqi@0 1249 print(start(), cfg_only, live_only);
aoqi@0 1250 } else {
aoqi@0 1251 tty->print_cr("invalid IR");
aoqi@0 1252 }
aoqi@0 1253 }
aoqi@0 1254
aoqi@0 1255
aoqi@0 1256 define_array(BlockListArray, BlockList*)
aoqi@0 1257 define_stack(BlockListList, BlockListArray)
aoqi@0 1258
aoqi@0 1259 class PredecessorValidator : public BlockClosure {
aoqi@0 1260 private:
aoqi@0 1261 BlockListList* _predecessors;
aoqi@0 1262 BlockList* _blocks;
aoqi@0 1263
aoqi@0 1264 static int cmp(BlockBegin** a, BlockBegin** b) {
aoqi@0 1265 return (*a)->block_id() - (*b)->block_id();
aoqi@0 1266 }
aoqi@0 1267
aoqi@0 1268 public:
aoqi@0 1269 PredecessorValidator(IR* hir) {
aoqi@0 1270 ResourceMark rm;
aoqi@0 1271 _predecessors = new BlockListList(BlockBegin::number_of_blocks(), NULL);
aoqi@0 1272 _blocks = new BlockList();
aoqi@0 1273
aoqi@0 1274 int i;
aoqi@0 1275 hir->start()->iterate_preorder(this);
aoqi@0 1276 if (hir->code() != NULL) {
aoqi@0 1277 assert(hir->code()->length() == _blocks->length(), "must match");
aoqi@0 1278 for (i = 0; i < _blocks->length(); i++) {
aoqi@0 1279 assert(hir->code()->contains(_blocks->at(i)), "should be in both lists");
aoqi@0 1280 }
aoqi@0 1281 }
aoqi@0 1282
aoqi@0 1283 for (i = 0; i < _blocks->length(); i++) {
aoqi@0 1284 BlockBegin* block = _blocks->at(i);
aoqi@0 1285 BlockList* preds = _predecessors->at(block->block_id());
aoqi@0 1286 if (preds == NULL) {
aoqi@0 1287 assert(block->number_of_preds() == 0, "should be the same");
aoqi@0 1288 continue;
aoqi@0 1289 }
aoqi@0 1290
aoqi@0 1291 // clone the pred list so we can mutate it
aoqi@0 1292 BlockList* pred_copy = new BlockList();
aoqi@0 1293 int j;
aoqi@0 1294 for (j = 0; j < block->number_of_preds(); j++) {
aoqi@0 1295 pred_copy->append(block->pred_at(j));
aoqi@0 1296 }
aoqi@0 1297 // sort them in the same order
aoqi@0 1298 preds->sort(cmp);
aoqi@0 1299 pred_copy->sort(cmp);
aoqi@0 1300 int length = MIN2(preds->length(), block->number_of_preds());
aoqi@0 1301 for (j = 0; j < block->number_of_preds(); j++) {
aoqi@0 1302 assert(preds->at(j) == pred_copy->at(j), "must match");
aoqi@0 1303 }
aoqi@0 1304
aoqi@0 1305 assert(preds->length() == block->number_of_preds(), "should be the same");
aoqi@0 1306 }
aoqi@0 1307 }
aoqi@0 1308
aoqi@0 1309 virtual void block_do(BlockBegin* block) {
aoqi@0 1310 _blocks->append(block);
aoqi@0 1311 BlockEnd* be = block->end();
aoqi@0 1312 int n = be->number_of_sux();
aoqi@0 1313 int i;
aoqi@0 1314 for (i = 0; i < n; i++) {
aoqi@0 1315 BlockBegin* sux = be->sux_at(i);
aoqi@0 1316 assert(!sux->is_set(BlockBegin::exception_entry_flag), "must not be xhandler");
aoqi@0 1317
aoqi@0 1318 BlockList* preds = _predecessors->at_grow(sux->block_id(), NULL);
aoqi@0 1319 if (preds == NULL) {
aoqi@0 1320 preds = new BlockList();
aoqi@0 1321 _predecessors->at_put(sux->block_id(), preds);
aoqi@0 1322 }
aoqi@0 1323 preds->append(block);
aoqi@0 1324 }
aoqi@0 1325
aoqi@0 1326 n = block->number_of_exception_handlers();
aoqi@0 1327 for (i = 0; i < n; i++) {
aoqi@0 1328 BlockBegin* sux = block->exception_handler_at(i);
aoqi@0 1329 assert(sux->is_set(BlockBegin::exception_entry_flag), "must be xhandler");
aoqi@0 1330
aoqi@0 1331 BlockList* preds = _predecessors->at_grow(sux->block_id(), NULL);
aoqi@0 1332 if (preds == NULL) {
aoqi@0 1333 preds = new BlockList();
aoqi@0 1334 _predecessors->at_put(sux->block_id(), preds);
aoqi@0 1335 }
aoqi@0 1336 preds->append(block);
aoqi@0 1337 }
aoqi@0 1338 }
aoqi@0 1339 };
aoqi@0 1340
aoqi@0 1341 class VerifyBlockBeginField : public BlockClosure {
aoqi@0 1342
aoqi@0 1343 public:
aoqi@0 1344
aoqi@0 1345 virtual void block_do(BlockBegin *block) {
aoqi@0 1346 for ( Instruction *cur = block; cur != NULL; cur = cur->next()) {
aoqi@0 1347 assert(cur->block() == block, "Block begin is not correct");
aoqi@0 1348 }
aoqi@0 1349 }
aoqi@0 1350 };
aoqi@0 1351
aoqi@0 1352 void IR::verify() {
aoqi@0 1353 #ifdef ASSERT
aoqi@0 1354 PredecessorValidator pv(this);
aoqi@0 1355 VerifyBlockBeginField verifier;
aoqi@0 1356 this->iterate_postorder(&verifier);
aoqi@0 1357 #endif
aoqi@0 1358 }
aoqi@0 1359
aoqi@0 1360 #endif // PRODUCT
aoqi@0 1361
aoqi@0 1362 void SubstitutionResolver::visit(Value* v) {
aoqi@0 1363 Value v0 = *v;
aoqi@0 1364 if (v0) {
aoqi@0 1365 Value vs = v0->subst();
aoqi@0 1366 if (vs != v0) {
aoqi@0 1367 *v = v0->subst();
aoqi@0 1368 }
aoqi@0 1369 }
aoqi@0 1370 }
aoqi@0 1371
aoqi@0 1372 #ifdef ASSERT
aoqi@0 1373 class SubstitutionChecker: public ValueVisitor {
aoqi@0 1374 void visit(Value* v) {
aoqi@0 1375 Value v0 = *v;
aoqi@0 1376 if (v0) {
aoqi@0 1377 Value vs = v0->subst();
aoqi@0 1378 assert(vs == v0, "missed substitution");
aoqi@0 1379 }
aoqi@0 1380 }
aoqi@0 1381 };
aoqi@0 1382 #endif
aoqi@0 1383
aoqi@0 1384
aoqi@0 1385 void SubstitutionResolver::block_do(BlockBegin* block) {
aoqi@0 1386 Instruction* last = NULL;
aoqi@0 1387 for (Instruction* n = block; n != NULL;) {
aoqi@0 1388 n->values_do(this);
aoqi@0 1389 // need to remove this instruction from the instruction stream
aoqi@0 1390 if (n->subst() != n) {
aoqi@0 1391 assert(last != NULL, "must have last");
aoqi@0 1392 last->set_next(n->next());
aoqi@0 1393 } else {
aoqi@0 1394 last = n;
aoqi@0 1395 }
aoqi@0 1396 n = last->next();
aoqi@0 1397 }
aoqi@0 1398
aoqi@0 1399 #ifdef ASSERT
aoqi@0 1400 SubstitutionChecker check_substitute;
aoqi@0 1401 if (block->state()) block->state()->values_do(&check_substitute);
aoqi@0 1402 block->block_values_do(&check_substitute);
aoqi@0 1403 if (block->end() && block->end()->state()) block->end()->state()->values_do(&check_substitute);
aoqi@0 1404 #endif
aoqi@0 1405 }

mercurial