Path: blob/master/src/hotspot/share/c1/c1_IR.cpp
64440 views
/*1* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "c1/c1_Compilation.hpp"26#include "c1/c1_FrameMap.hpp"27#include "c1/c1_GraphBuilder.hpp"28#include "c1/c1_IR.hpp"29#include "c1/c1_InstructionPrinter.hpp"30#include "c1/c1_Optimizer.hpp"31#include "compiler/oopMap.hpp"32#include "memory/resourceArea.hpp"33#include "utilities/bitMap.inline.hpp"343536// Implementation of XHandlers37//38// Note: This code could eventually go away if we are39// just using the ciExceptionHandlerStream.4041XHandlers::XHandlers(ciMethod* method) : _list(method->exception_table_length()) {42ciExceptionHandlerStream s(method);43while (!s.is_done()) {44_list.append(new XHandler(s.handler()));45s.next();46}47assert(s.count() == method->exception_table_length(), "exception table lengths inconsistent");48}4950// deep copy of all XHandler contained in list51XHandlers::XHandlers(XHandlers* other) :52_list(other->length())53{54for (int i = 0; i < other->length(); i++) {55_list.append(new XHandler(other->handler_at(i)));56}57}5859// Returns whether a particular exception type can be caught. Also60// returns true if klass is unloaded or any exception handler61// classes are unloaded. type_is_exact indicates whether the throw62// is known to be exactly that class or it might throw a subtype.63bool XHandlers::could_catch(ciInstanceKlass* klass, bool type_is_exact) const {64// the type is unknown so be conservative65if (!klass->is_loaded()) {66return true;67}6869for (int i = 0; i < length(); i++) {70XHandler* handler = handler_at(i);71if (handler->is_catch_all()) {72// catch of ANY73return true;74}75ciInstanceKlass* handler_klass = handler->catch_klass();76// if it's unknown it might be catchable77if (!handler_klass->is_loaded()) {78return true;79}80// if the throw type is definitely a subtype of the catch type81// then it can be caught.82if (klass->is_subtype_of(handler_klass)) {83return true;84}85if (!type_is_exact) {86// If the type isn't exactly known then it can also be caught by87// catch statements where the inexact type is a subtype of the88// catch type.89// given: foo extends bar extends Exception90// throw bar can be caught by catch foo, catch bar, and catch91// Exception, however it can't be caught by any handlers without92// bar in its type hierarchy.93if (handler_klass->is_subtype_of(klass)) {94return true;95}96}97}9899return false;100}101102103bool XHandlers::equals(XHandlers* others) const {104if (others == NULL) return false;105if (length() != others->length()) return false;106107for (int i = 0; i < length(); i++) {108if (!handler_at(i)->equals(others->handler_at(i))) return false;109}110return true;111}112113bool XHandler::equals(XHandler* other) const {114assert(entry_pco() != -1 && other->entry_pco() != -1, "must have entry_pco");115116if (entry_pco() != other->entry_pco()) return false;117if (scope_count() != other->scope_count()) return false;118if (_desc != other->_desc) return false;119120assert(entry_block() == other->entry_block(), "entry_block must be equal when entry_pco is equal");121return true;122}123124125// Implementation of IRScope126BlockBegin* IRScope::build_graph(Compilation* compilation, int osr_bci) {127GraphBuilder gm(compilation, this);128NOT_PRODUCT(if (PrintValueNumbering && Verbose) gm.print_stats());129if (compilation->bailed_out()) return NULL;130return gm.start();131}132133134IRScope::IRScope(Compilation* compilation, IRScope* caller, int caller_bci, ciMethod* method, int osr_bci, bool create_graph)135: _compilation(compilation)136, _callees(2)137, _requires_phi_function(method->max_locals())138{139_caller = caller;140_level = caller == NULL ? 0 : caller->level() + 1;141_method = method;142_xhandlers = new XHandlers(method);143_number_of_locks = 0;144_monitor_pairing_ok = method->has_balanced_monitors();145_wrote_final = false;146_wrote_fields = false;147_wrote_volatile = false;148_start = NULL;149150if (osr_bci != -1) {151// selective creation of phi functions is not possibel in osr-methods152_requires_phi_function.set_range(0, method->max_locals());153}154155assert(method->holder()->is_loaded() , "method holder must be loaded");156157// build graph if monitor pairing is ok158if (create_graph && monitor_pairing_ok()) _start = build_graph(compilation, osr_bci);159}160161162int IRScope::max_stack() const {163int my_max = method()->max_stack();164int callee_max = 0;165for (int i = 0; i < number_of_callees(); i++) {166callee_max = MAX2(callee_max, callee_no(i)->max_stack());167}168return my_max + callee_max;169}170171172bool IRScopeDebugInfo::should_reexecute() {173ciMethod* cur_method = scope()->method();174int cur_bci = bci();175if (cur_method != NULL && cur_bci != SynchronizationEntryBCI) {176Bytecodes::Code code = cur_method->java_code_at_bci(cur_bci);177return Interpreter::bytecode_should_reexecute(code);178} else179return false;180}181182183// Implementation of CodeEmitInfo184185// Stack must be NON-null186CodeEmitInfo::CodeEmitInfo(ValueStack* stack, XHandlers* exception_handlers, bool deoptimize_on_exception)187: _scope_debug_info(NULL)188, _scope(stack->scope())189, _exception_handlers(exception_handlers)190, _oop_map(NULL)191, _stack(stack)192, _is_method_handle_invoke(false)193, _deoptimize_on_exception(deoptimize_on_exception)194, _force_reexecute(false) {195assert(_stack != NULL, "must be non null");196}197198199CodeEmitInfo::CodeEmitInfo(CodeEmitInfo* info, ValueStack* stack)200: _scope_debug_info(NULL)201, _scope(info->_scope)202, _exception_handlers(NULL)203, _oop_map(NULL)204, _stack(stack == NULL ? info->_stack : stack)205, _is_method_handle_invoke(info->_is_method_handle_invoke)206, _deoptimize_on_exception(info->_deoptimize_on_exception)207, _force_reexecute(info->_force_reexecute) {208209// deep copy of exception handlers210if (info->_exception_handlers != NULL) {211_exception_handlers = new XHandlers(info->_exception_handlers);212}213}214215216void CodeEmitInfo::record_debug_info(DebugInformationRecorder* recorder, int pc_offset) {217// record the safepoint before recording the debug info for enclosing scopes218recorder->add_safepoint(pc_offset, _oop_map->deep_copy());219bool reexecute = _force_reexecute || _scope_debug_info->should_reexecute();220_scope_debug_info->record_debug_info(recorder, pc_offset, reexecute, _is_method_handle_invoke);221recorder->end_safepoint(pc_offset);222}223224225void CodeEmitInfo::add_register_oop(LIR_Opr opr) {226assert(_oop_map != NULL, "oop map must already exist");227assert(opr->is_single_cpu(), "should not call otherwise");228229VMReg name = frame_map()->regname(opr);230_oop_map->set_oop(name);231}232233// Mirror the stack size calculation in the deopt code234// How much stack space would we need at this point in the program in235// case of deoptimization?236int CodeEmitInfo::interpreter_frame_size() const {237ValueStack* state = _stack;238int size = 0;239int callee_parameters = 0;240int callee_locals = 0;241int extra_args = state->scope()->method()->max_stack() - state->stack_size();242243while (state != NULL) {244int locks = state->locks_size();245int temps = state->stack_size();246bool is_top_frame = (state == _stack);247ciMethod* method = state->scope()->method();248249int frame_size = BytesPerWord * Interpreter::size_activation(method->max_stack(),250temps + callee_parameters,251extra_args,252locks,253callee_parameters,254callee_locals,255is_top_frame);256size += frame_size;257258callee_parameters = method->size_of_parameters();259callee_locals = method->max_locals();260extra_args = 0;261state = state->caller_state();262}263return size + Deoptimization::last_frame_adjust(0, callee_locals) * BytesPerWord;264}265266// Implementation of IR267268IR::IR(Compilation* compilation, ciMethod* method, int osr_bci) :269_num_loops(0) {270// setup IR fields271_compilation = compilation;272_top_scope = new IRScope(compilation, NULL, -1, method, osr_bci, true);273_code = NULL;274}275276277void IR::optimize_blocks() {278Optimizer opt(this);279if (!compilation()->profile_branches()) {280if (DoCEE) {281opt.eliminate_conditional_expressions();282#ifndef PRODUCT283if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after CEE"); print(true); }284if (PrintIR || PrintIR1 ) { tty->print_cr("IR after CEE"); print(false); }285#endif286}287if (EliminateBlocks) {288opt.eliminate_blocks();289#ifndef PRODUCT290if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after block elimination"); print(true); }291if (PrintIR || PrintIR1 ) { tty->print_cr("IR after block elimination"); print(false); }292#endif293}294}295}296297void IR::eliminate_null_checks() {298Optimizer opt(this);299if (EliminateNullChecks) {300opt.eliminate_null_checks();301#ifndef PRODUCT302if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after null check elimination"); print(true); }303if (PrintIR || PrintIR1 ) { tty->print_cr("IR after null check elimination"); print(false); }304#endif305}306}307308309static int sort_pairs(BlockPair** a, BlockPair** b) {310if ((*a)->from() == (*b)->from()) {311return (*a)->to()->block_id() - (*b)->to()->block_id();312} else {313return (*a)->from()->block_id() - (*b)->from()->block_id();314}315}316317318class CriticalEdgeFinder: public BlockClosure {319BlockPairList blocks;320IR* _ir;321322public:323CriticalEdgeFinder(IR* ir): _ir(ir) {}324void block_do(BlockBegin* bb) {325BlockEnd* be = bb->end();326int nos = be->number_of_sux();327if (nos >= 2) {328for (int i = 0; i < nos; i++) {329BlockBegin* sux = be->sux_at(i);330if (sux->number_of_preds() >= 2) {331blocks.append(new BlockPair(bb, sux));332}333}334}335}336337void split_edges() {338BlockPair* last_pair = NULL;339blocks.sort(sort_pairs);340for (int i = 0; i < blocks.length(); i++) {341BlockPair* pair = blocks.at(i);342if (last_pair != NULL && pair->is_same(last_pair)) continue;343BlockBegin* from = pair->from();344BlockBegin* to = pair->to();345BlockBegin* split = from->insert_block_between(to);346#ifndef PRODUCT347if ((PrintIR || PrintIR1) && Verbose) {348tty->print_cr("Split critical edge B%d -> B%d (new block B%d)",349from->block_id(), to->block_id(), split->block_id());350}351#endif352last_pair = pair;353}354}355};356357void IR::split_critical_edges() {358CriticalEdgeFinder cef(this);359360iterate_preorder(&cef);361cef.split_edges();362}363364365class UseCountComputer: public ValueVisitor, BlockClosure {366private:367void visit(Value* n) {368// Local instructions and Phis for expression stack values at the369// start of basic blocks are not added to the instruction list370if (!(*n)->is_linked() && (*n)->can_be_linked()) {371assert(false, "a node was not appended to the graph");372Compilation::current()->bailout("a node was not appended to the graph");373}374// use n's input if not visited before375if (!(*n)->is_pinned() && !(*n)->has_uses()) {376// note: a) if the instruction is pinned, it will be handled by compute_use_count377// b) if the instruction has uses, it was touched before378// => in both cases we don't need to update n's values379uses_do(n);380}381// use n382(*n)->_use_count++;383}384385Values* worklist;386int depth;387enum {388max_recurse_depth = 20389};390391void uses_do(Value* n) {392depth++;393if (depth > max_recurse_depth) {394// don't allow the traversal to recurse too deeply395worklist->push(*n);396} else {397(*n)->input_values_do(this);398// special handling for some instructions399if ((*n)->as_BlockEnd() != NULL) {400// note on BlockEnd:401// must 'use' the stack only if the method doesn't402// terminate, however, in those cases stack is empty403(*n)->state_values_do(this);404}405}406depth--;407}408409void block_do(BlockBegin* b) {410depth = 0;411// process all pinned nodes as the roots of expression trees412for (Instruction* n = b; n != NULL; n = n->next()) {413if (n->is_pinned()) uses_do(&n);414}415assert(depth == 0, "should have counted back down");416417// now process any unpinned nodes which recursed too deeply418while (worklist->length() > 0) {419Value t = worklist->pop();420if (!t->is_pinned()) {421// compute the use count422uses_do(&t);423424// pin the instruction so that LIRGenerator doesn't recurse425// too deeply during it's evaluation.426t->pin();427}428}429assert(depth == 0, "should have counted back down");430}431432UseCountComputer() {433worklist = new Values();434depth = 0;435}436437public:438static void compute(BlockList* blocks) {439UseCountComputer ucc;440blocks->iterate_backward(&ucc);441}442};443444445// helper macro for short definition of trace-output inside code446#ifdef ASSERT447#define TRACE_LINEAR_SCAN(level, code) \448if (TraceLinearScanLevel >= level) { \449code; \450}451#else452#define TRACE_LINEAR_SCAN(level, code)453#endif454455class ComputeLinearScanOrder : public StackObj {456private:457int _max_block_id; // the highest block_id of a block458int _num_blocks; // total number of blocks (smaller than _max_block_id)459int _num_loops; // total number of loops460bool _iterative_dominators;// method requires iterative computation of dominatiors461462BlockList* _linear_scan_order; // the resulting list of blocks in correct order463464ResourceBitMap _visited_blocks; // used for recursive processing of blocks465ResourceBitMap _active_blocks; // used for recursive processing of blocks466ResourceBitMap _dominator_blocks; // temproary BitMap used for computation of dominator467intArray _forward_branches; // number of incoming forward branches for each block468BlockList _loop_end_blocks; // list of all loop end blocks collected during count_edges469BitMap2D _loop_map; // two-dimensional bit set: a bit is set if a block is contained in a loop470BlockList _work_list; // temporary list (used in mark_loops and compute_order)471BlockList _loop_headers;472473Compilation* _compilation;474475// accessors for _visited_blocks and _active_blocks476void init_visited() { _active_blocks.clear(); _visited_blocks.clear(); }477bool is_visited(BlockBegin* b) const { return _visited_blocks.at(b->block_id()); }478bool is_active(BlockBegin* b) const { return _active_blocks.at(b->block_id()); }479void set_visited(BlockBegin* b) { assert(!is_visited(b), "already set"); _visited_blocks.set_bit(b->block_id()); }480void set_active(BlockBegin* b) { assert(!is_active(b), "already set"); _active_blocks.set_bit(b->block_id()); }481void clear_active(BlockBegin* b) { assert(is_active(b), "not already"); _active_blocks.clear_bit(b->block_id()); }482483// accessors for _forward_branches484void inc_forward_branches(BlockBegin* b) { _forward_branches.at_put(b->block_id(), _forward_branches.at(b->block_id()) + 1); }485int 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()); }486487// accessors for _loop_map488bool is_block_in_loop (int loop_idx, BlockBegin* b) const { return _loop_map.at(loop_idx, b->block_id()); }489void set_block_in_loop (int loop_idx, BlockBegin* b) { _loop_map.set_bit(loop_idx, b->block_id()); }490void clear_block_in_loop(int loop_idx, int block_id) { _loop_map.clear_bit(loop_idx, block_id); }491492// count edges between blocks493void count_edges(BlockBegin* cur, BlockBegin* parent);494495// loop detection496void mark_loops();497void clear_non_natural_loops(BlockBegin* start_block);498void assign_loop_depth(BlockBegin* start_block);499500// computation of final block order501BlockBegin* common_dominator(BlockBegin* a, BlockBegin* b);502void compute_dominator(BlockBegin* cur, BlockBegin* parent);503void compute_dominator_impl(BlockBegin* cur, BlockBegin* parent);504int compute_weight(BlockBegin* cur);505bool ready_for_processing(BlockBegin* cur);506void sort_into_work_list(BlockBegin* b);507void append_block(BlockBegin* cur);508void compute_order(BlockBegin* start_block);509510// fixup of dominators for non-natural loops511bool compute_dominators_iter();512void compute_dominators();513514// debug functions515DEBUG_ONLY(void print_blocks();)516DEBUG_ONLY(void verify();)517518Compilation* compilation() const { return _compilation; }519public:520ComputeLinearScanOrder(Compilation* c, BlockBegin* start_block);521522// accessors for final result523BlockList* linear_scan_order() const { return _linear_scan_order; }524int num_loops() const { return _num_loops; }525};526527528ComputeLinearScanOrder::ComputeLinearScanOrder(Compilation* c, BlockBegin* start_block) :529_max_block_id(BlockBegin::number_of_blocks()),530_num_blocks(0),531_num_loops(0),532_iterative_dominators(false),533_linear_scan_order(NULL), // initialized later with correct size534_visited_blocks(_max_block_id),535_active_blocks(_max_block_id),536_dominator_blocks(_max_block_id),537_forward_branches(_max_block_id, _max_block_id, 0),538_loop_end_blocks(8),539_loop_map(0), // initialized later with correct size540_work_list(8),541_compilation(c)542{543TRACE_LINEAR_SCAN(2, tty->print_cr("***** computing linear-scan block order"));544545count_edges(start_block, NULL);546547if (compilation()->is_profiling()) {548ciMethod *method = compilation()->method();549if (!method->is_accessor()) {550ciMethodData* md = method->method_data_or_null();551assert(md != NULL, "Sanity");552md->set_compilation_stats(_num_loops, _num_blocks);553}554}555556if (_num_loops > 0) {557mark_loops();558clear_non_natural_loops(start_block);559assign_loop_depth(start_block);560}561562compute_order(start_block);563compute_dominators();564565DEBUG_ONLY(print_blocks());566DEBUG_ONLY(verify());567}568569570// Traverse the CFG:571// * count total number of blocks572// * count all incoming edges and backward incoming edges573// * number loop header blocks574// * create a list with all loop end blocks575void ComputeLinearScanOrder::count_edges(BlockBegin* cur, BlockBegin* parent) {576TRACE_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));577assert(cur->dominator() == NULL, "dominator already initialized");578579if (is_active(cur)) {580TRACE_LINEAR_SCAN(3, tty->print_cr("backward branch"));581assert(is_visited(cur), "block must be visisted when block is active");582assert(parent != NULL, "must have parent");583584cur->set(BlockBegin::backward_branch_target_flag);585586// When a loop header is also the start of an exception handler, then the backward branch is587// an exception edge. Because such edges are usually critical edges which cannot be split, the588// loop must be excluded here from processing.589if (cur->is_set(BlockBegin::exception_entry_flag)) {590// Make sure that dominators are correct in this weird situation591_iterative_dominators = true;592return;593}594595cur->set(BlockBegin::linear_scan_loop_header_flag);596parent->set(BlockBegin::linear_scan_loop_end_flag);597598assert(parent->number_of_sux() == 1 && parent->sux_at(0) == cur,599"loop end blocks must have one successor (critical edges are split)");600601_loop_end_blocks.append(parent);602return;603}604605// increment number of incoming forward branches606inc_forward_branches(cur);607608if (is_visited(cur)) {609TRACE_LINEAR_SCAN(3, tty->print_cr("block already visited"));610return;611}612613_num_blocks++;614set_visited(cur);615set_active(cur);616617// recursive call for all successors618int i;619for (i = cur->number_of_sux() - 1; i >= 0; i--) {620count_edges(cur->sux_at(i), cur);621}622for (i = cur->number_of_exception_handlers() - 1; i >= 0; i--) {623count_edges(cur->exception_handler_at(i), cur);624}625626clear_active(cur);627628// Each loop has a unique number.629// When multiple loops are nested, assign_loop_depth assumes that the630// innermost loop has the lowest number. This is guaranteed by setting631// the loop number after the recursive calls for the successors above632// have returned.633if (cur->is_set(BlockBegin::linear_scan_loop_header_flag)) {634assert(cur->loop_index() == -1, "cannot set loop-index twice");635TRACE_LINEAR_SCAN(3, tty->print_cr("Block B%d is loop header of loop %d", cur->block_id(), _num_loops));636637cur->set_loop_index(_num_loops);638_loop_headers.append(cur);639_num_loops++;640}641642TRACE_LINEAR_SCAN(3, tty->print_cr("Finished count_edges for block B%d", cur->block_id()));643}644645646void ComputeLinearScanOrder::mark_loops() {647TRACE_LINEAR_SCAN(3, tty->print_cr("----- marking loops"));648649_loop_map = BitMap2D(_num_loops, _max_block_id);650651for (int i = _loop_end_blocks.length() - 1; i >= 0; i--) {652BlockBegin* loop_end = _loop_end_blocks.at(i);653BlockBegin* loop_start = loop_end->sux_at(0);654int loop_idx = loop_start->loop_index();655656TRACE_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));657assert(loop_end->is_set(BlockBegin::linear_scan_loop_end_flag), "loop end flag must be set");658assert(loop_end->number_of_sux() == 1, "incorrect number of successors");659assert(loop_start->is_set(BlockBegin::linear_scan_loop_header_flag), "loop header flag must be set");660assert(loop_idx >= 0 && loop_idx < _num_loops, "loop index not set");661assert(_work_list.is_empty(), "work list must be empty before processing");662663// add the end-block of the loop to the working list664_work_list.push(loop_end);665set_block_in_loop(loop_idx, loop_end);666do {667BlockBegin* cur = _work_list.pop();668669TRACE_LINEAR_SCAN(3, tty->print_cr(" processing B%d", cur->block_id()));670assert(is_block_in_loop(loop_idx, cur), "bit in loop map must be set when block is in work list");671672// recursive processing of all predecessors ends when start block of loop is reached673if (cur != loop_start && !cur->is_set(BlockBegin::osr_entry_flag)) {674for (int j = cur->number_of_preds() - 1; j >= 0; j--) {675BlockBegin* pred = cur->pred_at(j);676677if (!is_block_in_loop(loop_idx, pred) /*&& !pred->is_set(BlockBeginosr_entry_flag)*/) {678// this predecessor has not been processed yet, so add it to work list679TRACE_LINEAR_SCAN(3, tty->print_cr(" pushing B%d", pred->block_id()));680_work_list.push(pred);681set_block_in_loop(loop_idx, pred);682}683}684}685} while (!_work_list.is_empty());686}687}688689690// check for non-natural loops (loops where the loop header does not dominate691// all other loop blocks = loops with mulitple entries).692// such loops are ignored693void ComputeLinearScanOrder::clear_non_natural_loops(BlockBegin* start_block) {694for (int i = _num_loops - 1; i >= 0; i--) {695if (is_block_in_loop(i, start_block)) {696// loop i contains the entry block of the method697// -> this is not a natural loop, so ignore it698TRACE_LINEAR_SCAN(2, tty->print_cr("Loop %d is non-natural, so it is ignored", i));699700BlockBegin *loop_header = _loop_headers.at(i);701assert(loop_header->is_set(BlockBegin::linear_scan_loop_header_flag), "Must be loop header");702703for (int j = 0; j < loop_header->number_of_preds(); j++) {704BlockBegin *pred = loop_header->pred_at(j);705pred->clear(BlockBegin::linear_scan_loop_end_flag);706}707708loop_header->clear(BlockBegin::linear_scan_loop_header_flag);709710for (int block_id = _max_block_id - 1; block_id >= 0; block_id--) {711clear_block_in_loop(i, block_id);712}713_iterative_dominators = true;714}715}716}717718void ComputeLinearScanOrder::assign_loop_depth(BlockBegin* start_block) {719TRACE_LINEAR_SCAN(3, tty->print_cr("----- computing loop-depth and weight"));720init_visited();721722assert(_work_list.is_empty(), "work list must be empty before processing");723_work_list.append(start_block);724725do {726BlockBegin* cur = _work_list.pop();727728if (!is_visited(cur)) {729set_visited(cur);730TRACE_LINEAR_SCAN(4, tty->print_cr("Computing loop depth for block B%d", cur->block_id()));731732// compute loop-depth and loop-index for the block733assert(cur->loop_depth() == 0, "cannot set loop-depth twice");734int i;735int loop_depth = 0;736int min_loop_idx = -1;737for (i = _num_loops - 1; i >= 0; i--) {738if (is_block_in_loop(i, cur)) {739loop_depth++;740min_loop_idx = i;741}742}743cur->set_loop_depth(loop_depth);744cur->set_loop_index(min_loop_idx);745746// append all unvisited successors to work list747for (i = cur->number_of_sux() - 1; i >= 0; i--) {748_work_list.append(cur->sux_at(i));749}750for (i = cur->number_of_exception_handlers() - 1; i >= 0; i--) {751_work_list.append(cur->exception_handler_at(i));752}753}754} while (!_work_list.is_empty());755}756757758BlockBegin* ComputeLinearScanOrder::common_dominator(BlockBegin* a, BlockBegin* b) {759assert(a != NULL && b != NULL, "must have input blocks");760761_dominator_blocks.clear();762while (a != NULL) {763_dominator_blocks.set_bit(a->block_id());764assert(a->dominator() != NULL || a == _linear_scan_order->at(0), "dominator must be initialized");765a = a->dominator();766}767while (b != NULL && !_dominator_blocks.at(b->block_id())) {768assert(b->dominator() != NULL || b == _linear_scan_order->at(0), "dominator must be initialized");769b = b->dominator();770}771772assert(b != NULL, "could not find dominator");773return b;774}775776void ComputeLinearScanOrder::compute_dominator(BlockBegin* cur, BlockBegin* parent) {777init_visited();778compute_dominator_impl(cur, parent);779}780781void ComputeLinearScanOrder::compute_dominator_impl(BlockBegin* cur, BlockBegin* parent) {782// Mark as visited to avoid recursive calls with same parent783set_visited(cur);784785if (cur->dominator() == NULL) {786TRACE_LINEAR_SCAN(4, tty->print_cr("DOM: initializing dominator of B%d to B%d", cur->block_id(), parent->block_id()));787cur->set_dominator(parent);788789} else if (!(cur->is_set(BlockBegin::linear_scan_loop_header_flag) && parent->is_set(BlockBegin::linear_scan_loop_end_flag))) {790TRACE_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()));791// Does not hold for exception blocks792assert(cur->number_of_preds() > 1 || cur->is_set(BlockBegin::exception_entry_flag), "");793cur->set_dominator(common_dominator(cur->dominator(), parent));794}795796// Additional edge to xhandler of all our successors797// range check elimination needs that the state at the end of a798// block be valid in every block it dominates so cur must dominate799// the exception handlers of its successors.800int num_cur_xhandler = cur->number_of_exception_handlers();801for (int j = 0; j < num_cur_xhandler; j++) {802BlockBegin* xhandler = cur->exception_handler_at(j);803if (!is_visited(xhandler)) {804compute_dominator_impl(xhandler, parent);805}806}807}808809810int ComputeLinearScanOrder::compute_weight(BlockBegin* cur) {811BlockBegin* single_sux = NULL;812if (cur->number_of_sux() == 1) {813single_sux = cur->sux_at(0);814}815816// limit loop-depth to 15 bit (only for security reason, it will never be so big)817int weight = (cur->loop_depth() & 0x7FFF) << 16;818819// general macro for short definition of weight flags820// the first instance of INC_WEIGHT_IF has the highest priority821int cur_bit = 15;822#define INC_WEIGHT_IF(condition) if ((condition)) { weight |= (1 << cur_bit); } cur_bit--;823824// this is necessery for the (very rare) case that two successing blocks have825// the same loop depth, but a different loop index (can happen for endless loops826// with exception handlers)827INC_WEIGHT_IF(!cur->is_set(BlockBegin::linear_scan_loop_header_flag));828829// loop end blocks (blocks that end with a backward branch) are added830// after all other blocks of the loop.831INC_WEIGHT_IF(!cur->is_set(BlockBegin::linear_scan_loop_end_flag));832833// critical edge split blocks are prefered because than they have a bigger834// proability to be completely empty835INC_WEIGHT_IF(cur->is_set(BlockBegin::critical_edge_split_flag));836837// exceptions should not be thrown in normal control flow, so these blocks838// are added as late as possible839INC_WEIGHT_IF(cur->end()->as_Throw() == NULL && (single_sux == NULL || single_sux->end()->as_Throw() == NULL));840INC_WEIGHT_IF(cur->end()->as_Return() == NULL && (single_sux == NULL || single_sux->end()->as_Return() == NULL));841842// exceptions handlers are added as late as possible843INC_WEIGHT_IF(!cur->is_set(BlockBegin::exception_entry_flag));844845// guarantee that weight is > 0846weight |= 1;847848#undef INC_WEIGHT_IF849assert(cur_bit >= 0, "too many flags");850assert(weight > 0, "weight cannot become negative");851852return weight;853}854855bool ComputeLinearScanOrder::ready_for_processing(BlockBegin* cur) {856// Discount the edge just traveled.857// When the number drops to zero, all forward branches were processed858if (dec_forward_branches(cur) != 0) {859return false;860}861862assert(_linear_scan_order->find(cur) == -1, "block already processed (block can be ready only once)");863assert(_work_list.find(cur) == -1, "block already in work-list (block can be ready only once)");864return true;865}866867void ComputeLinearScanOrder::sort_into_work_list(BlockBegin* cur) {868assert(_work_list.find(cur) == -1, "block already in work list");869870int cur_weight = compute_weight(cur);871872// the linear_scan_number is used to cache the weight of a block873cur->set_linear_scan_number(cur_weight);874875#ifndef PRODUCT876if (StressLinearScan) {877_work_list.insert_before(0, cur);878return;879}880#endif881882_work_list.append(NULL); // provide space for new element883884int insert_idx = _work_list.length() - 1;885while (insert_idx > 0 && _work_list.at(insert_idx - 1)->linear_scan_number() > cur_weight) {886_work_list.at_put(insert_idx, _work_list.at(insert_idx - 1));887insert_idx--;888}889_work_list.at_put(insert_idx, cur);890891TRACE_LINEAR_SCAN(3, tty->print_cr("Sorted B%d into worklist. new worklist:", cur->block_id()));892TRACE_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()));893894#ifdef ASSERT895for (int i = 0; i < _work_list.length(); i++) {896assert(_work_list.at(i)->linear_scan_number() > 0, "weight not set");897assert(i == 0 || _work_list.at(i - 1)->linear_scan_number() <= _work_list.at(i)->linear_scan_number(), "incorrect order in worklist");898}899#endif900}901902void ComputeLinearScanOrder::append_block(BlockBegin* cur) {903TRACE_LINEAR_SCAN(3, tty->print_cr("appending block B%d (weight 0x%6x) to linear-scan order", cur->block_id(), cur->linear_scan_number()));904assert(_linear_scan_order->find(cur) == -1, "cannot add the same block twice");905906// currently, the linear scan order and code emit order are equal.907// therefore the linear_scan_number and the weight of a block must also908// be equal.909cur->set_linear_scan_number(_linear_scan_order->length());910_linear_scan_order->append(cur);911}912913void ComputeLinearScanOrder::compute_order(BlockBegin* start_block) {914TRACE_LINEAR_SCAN(3, tty->print_cr("----- computing final block order"));915916// the start block is always the first block in the linear scan order917_linear_scan_order = new BlockList(_num_blocks);918append_block(start_block);919920assert(start_block->end()->as_Base() != NULL, "start block must end with Base-instruction");921BlockBegin* std_entry = ((Base*)start_block->end())->std_entry();922BlockBegin* osr_entry = ((Base*)start_block->end())->osr_entry();923924BlockBegin* sux_of_osr_entry = NULL;925if (osr_entry != NULL) {926// special handling for osr entry:927// ignore the edge between the osr entry and its successor for processing928// the osr entry block is added manually below929assert(osr_entry->number_of_sux() == 1, "osr entry must have exactly one successor");930assert(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");931932sux_of_osr_entry = osr_entry->sux_at(0);933dec_forward_branches(sux_of_osr_entry);934935compute_dominator(osr_entry, start_block);936_iterative_dominators = true;937}938compute_dominator(std_entry, start_block);939940// start processing with standard entry block941assert(_work_list.is_empty(), "list must be empty before processing");942943if (ready_for_processing(std_entry)) {944sort_into_work_list(std_entry);945} else {946assert(false, "the std_entry must be ready for processing (otherwise, the method has no start block)");947}948949do {950BlockBegin* cur = _work_list.pop();951952if (cur == sux_of_osr_entry) {953// the osr entry block is ignored in normal processing, it is never added to the954// work list. Instead, it is added as late as possible manually here.955append_block(osr_entry);956compute_dominator(cur, osr_entry);957}958append_block(cur);959960int i;961int num_sux = cur->number_of_sux();962// changed loop order to get "intuitive" order of if- and else-blocks963for (i = 0; i < num_sux; i++) {964BlockBegin* sux = cur->sux_at(i);965compute_dominator(sux, cur);966if (ready_for_processing(sux)) {967sort_into_work_list(sux);968}969}970num_sux = cur->number_of_exception_handlers();971for (i = 0; i < num_sux; i++) {972BlockBegin* sux = cur->exception_handler_at(i);973if (ready_for_processing(sux)) {974sort_into_work_list(sux);975}976}977} while (_work_list.length() > 0);978}979980981bool ComputeLinearScanOrder::compute_dominators_iter() {982bool changed = false;983int num_blocks = _linear_scan_order->length();984985assert(_linear_scan_order->at(0)->dominator() == NULL, "must not have dominator");986assert(_linear_scan_order->at(0)->number_of_preds() == 0, "must not have predecessors");987for (int i = 1; i < num_blocks; i++) {988BlockBegin* block = _linear_scan_order->at(i);989990BlockBegin* dominator = block->pred_at(0);991int num_preds = block->number_of_preds();992993TRACE_LINEAR_SCAN(4, tty->print_cr("DOM: Processing B%d", block->block_id()));994995for (int j = 0; j < num_preds; j++) {996997BlockBegin *pred = block->pred_at(j);998TRACE_LINEAR_SCAN(4, tty->print_cr(" DOM: Subrocessing B%d", pred->block_id()));9991000if (block->is_set(BlockBegin::exception_entry_flag)) {1001dominator = common_dominator(dominator, pred);1002int num_pred_preds = pred->number_of_preds();1003for (int k = 0; k < num_pred_preds; k++) {1004dominator = common_dominator(dominator, pred->pred_at(k));1005}1006} else {1007dominator = common_dominator(dominator, pred);1008}1009}10101011if (dominator != block->dominator()) {1012TRACE_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()));10131014block->set_dominator(dominator);1015changed = true;1016}1017}1018return changed;1019}10201021void ComputeLinearScanOrder::compute_dominators() {1022TRACE_LINEAR_SCAN(3, tty->print_cr("----- computing dominators (iterative computation reqired: %d)", _iterative_dominators));10231024// iterative computation of dominators is only required for methods with non-natural loops1025// and OSR-methods. For all other methods, the dominators computed when generating the1026// linear scan block order are correct.1027if (_iterative_dominators) {1028do {1029TRACE_LINEAR_SCAN(1, tty->print_cr("DOM: next iteration of fix-point calculation"));1030} while (compute_dominators_iter());1031}10321033// check that dominators are correct1034assert(!compute_dominators_iter(), "fix point not reached");10351036// Add Blocks to dominates-Array1037int num_blocks = _linear_scan_order->length();1038for (int i = 0; i < num_blocks; i++) {1039BlockBegin* block = _linear_scan_order->at(i);10401041BlockBegin *dom = block->dominator();1042if (dom) {1043assert(dom->dominator_depth() != -1, "Dominator must have been visited before");1044dom->dominates()->append(block);1045block->set_dominator_depth(dom->dominator_depth() + 1);1046} else {1047block->set_dominator_depth(0);1048}1049}1050}105110521053#ifdef ASSERT1054void ComputeLinearScanOrder::print_blocks() {1055if (TraceLinearScanLevel >= 2) {1056tty->print_cr("----- loop information:");1057for (int block_idx = 0; block_idx < _linear_scan_order->length(); block_idx++) {1058BlockBegin* cur = _linear_scan_order->at(block_idx);10591060tty->print("%4d: B%2d: ", cur->linear_scan_number(), cur->block_id());1061for (int loop_idx = 0; loop_idx < _num_loops; loop_idx++) {1062tty->print ("%d ", is_block_in_loop(loop_idx, cur));1063}1064tty->print_cr(" -> loop_index: %2d, loop_depth: %2d", cur->loop_index(), cur->loop_depth());1065}1066}10671068if (TraceLinearScanLevel >= 1) {1069tty->print_cr("----- linear-scan block order:");1070for (int block_idx = 0; block_idx < _linear_scan_order->length(); block_idx++) {1071BlockBegin* cur = _linear_scan_order->at(block_idx);1072tty->print("%4d: B%2d loop: %2d depth: %2d", cur->linear_scan_number(), cur->block_id(), cur->loop_index(), cur->loop_depth());10731074tty->print(cur->is_set(BlockBegin::exception_entry_flag) ? " ex" : " ");1075tty->print(cur->is_set(BlockBegin::critical_edge_split_flag) ? " ce" : " ");1076tty->print(cur->is_set(BlockBegin::linear_scan_loop_header_flag) ? " lh" : " ");1077tty->print(cur->is_set(BlockBegin::linear_scan_loop_end_flag) ? " le" : " ");10781079if (cur->dominator() != NULL) {1080tty->print(" dom: B%d ", cur->dominator()->block_id());1081} else {1082tty->print(" dom: NULL ");1083}10841085if (cur->number_of_preds() > 0) {1086tty->print(" preds: ");1087for (int j = 0; j < cur->number_of_preds(); j++) {1088BlockBegin* pred = cur->pred_at(j);1089tty->print("B%d ", pred->block_id());1090}1091}1092if (cur->number_of_sux() > 0) {1093tty->print(" sux: ");1094for (int j = 0; j < cur->number_of_sux(); j++) {1095BlockBegin* sux = cur->sux_at(j);1096tty->print("B%d ", sux->block_id());1097}1098}1099if (cur->number_of_exception_handlers() > 0) {1100tty->print(" ex: ");1101for (int j = 0; j < cur->number_of_exception_handlers(); j++) {1102BlockBegin* ex = cur->exception_handler_at(j);1103tty->print("B%d ", ex->block_id());1104}1105}1106tty->cr();1107}1108}1109}11101111void ComputeLinearScanOrder::verify() {1112assert(_linear_scan_order->length() == _num_blocks, "wrong number of blocks in list");11131114if (StressLinearScan) {1115// blocks are scrambled when StressLinearScan is used1116return;1117}11181119// check that all successors of a block have a higher linear-scan-number1120// and that all predecessors of a block have a lower linear-scan-number1121// (only backward branches of loops are ignored)1122int i;1123for (i = 0; i < _linear_scan_order->length(); i++) {1124BlockBegin* cur = _linear_scan_order->at(i);11251126assert(cur->linear_scan_number() == i, "incorrect linear_scan_number");1127assert(cur->linear_scan_number() >= 0 && cur->linear_scan_number() == _linear_scan_order->find(cur), "incorrect linear_scan_number");11281129int j;1130for (j = cur->number_of_sux() - 1; j >= 0; j--) {1131BlockBegin* sux = cur->sux_at(j);11321133assert(sux->linear_scan_number() >= 0 && sux->linear_scan_number() == _linear_scan_order->find(sux), "incorrect linear_scan_number");1134if (!sux->is_set(BlockBegin::backward_branch_target_flag)) {1135assert(cur->linear_scan_number() < sux->linear_scan_number(), "invalid order");1136}1137if (cur->loop_depth() == sux->loop_depth()) {1138assert(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");1139}1140}11411142for (j = cur->number_of_preds() - 1; j >= 0; j--) {1143BlockBegin* pred = cur->pred_at(j);11441145assert(pred->linear_scan_number() >= 0 && pred->linear_scan_number() == _linear_scan_order->find(pred), "incorrect linear_scan_number");1146if (!cur->is_set(BlockBegin::backward_branch_target_flag)) {1147assert(cur->linear_scan_number() > pred->linear_scan_number(), "invalid order");1148}1149if (cur->loop_depth() == pred->loop_depth()) {1150assert(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");1151}11521153assert(cur->dominator()->linear_scan_number() <= cur->pred_at(j)->linear_scan_number(), "dominator must be before predecessors");1154}11551156// check dominator1157if (i == 0) {1158assert(cur->dominator() == NULL, "first block has no dominator");1159} else {1160assert(cur->dominator() != NULL, "all but first block must have dominator");1161}1162// Assertion does not hold for exception handlers1163assert(cur->number_of_preds() != 1 || cur->dominator() == cur->pred_at(0) || cur->is_set(BlockBegin::exception_entry_flag), "Single predecessor must also be dominator");1164}11651166// check that all loops are continuous1167for (int loop_idx = 0; loop_idx < _num_loops; loop_idx++) {1168int block_idx = 0;1169assert(!is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx)), "the first block must not be present in any loop");11701171// skip blocks before the loop1172while (block_idx < _num_blocks && !is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx))) {1173block_idx++;1174}1175// skip blocks of loop1176while (block_idx < _num_blocks && is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx))) {1177block_idx++;1178}1179// after the first non-loop block, there must not be another loop-block1180while (block_idx < _num_blocks) {1181assert(!is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx)), "loop not continuous in linear-scan order");1182block_idx++;1183}1184}1185}1186#endif // ASSERT118711881189void IR::compute_code() {1190assert(is_valid(), "IR must be valid");11911192ComputeLinearScanOrder compute_order(compilation(), start());1193_num_loops = compute_order.num_loops();1194_code = compute_order.linear_scan_order();1195}119611971198void IR::compute_use_counts() {1199// make sure all values coming out of this block get evaluated.1200int num_blocks = _code->length();1201for (int i = 0; i < num_blocks; i++) {1202_code->at(i)->end()->state()->pin_stack_for_linear_scan();1203}12041205// compute use counts1206UseCountComputer::compute(_code);1207}120812091210void IR::iterate_preorder(BlockClosure* closure) {1211assert(is_valid(), "IR must be valid");1212start()->iterate_preorder(closure);1213}121412151216void IR::iterate_postorder(BlockClosure* closure) {1217assert(is_valid(), "IR must be valid");1218start()->iterate_postorder(closure);1219}12201221void IR::iterate_linear_scan_order(BlockClosure* closure) {1222linear_scan_order()->iterate_forward(closure);1223}122412251226#ifndef PRODUCT1227class BlockPrinter: public BlockClosure {1228private:1229InstructionPrinter* _ip;1230bool _cfg_only;1231bool _live_only;12321233public:1234BlockPrinter(InstructionPrinter* ip, bool cfg_only, bool live_only = false) {1235_ip = ip;1236_cfg_only = cfg_only;1237_live_only = live_only;1238}12391240virtual void block_do(BlockBegin* block) {1241if (_cfg_only) {1242_ip->print_instr(block); tty->cr();1243} else {1244block->print_block(*_ip, _live_only);1245}1246}1247};124812491250void IR::print(BlockBegin* start, bool cfg_only, bool live_only) {1251ttyLocker ttyl;1252InstructionPrinter ip(!cfg_only);1253BlockPrinter bp(&ip, cfg_only, live_only);1254start->iterate_preorder(&bp);1255tty->cr();1256}12571258void IR::print(bool cfg_only, bool live_only) {1259if (is_valid()) {1260print(start(), cfg_only, live_only);1261} else {1262tty->print_cr("invalid IR");1263}1264}126512661267typedef GrowableArray<BlockList*> BlockListList;12681269class PredecessorValidator : public BlockClosure {1270private:1271BlockListList* _predecessors;1272BlockList* _blocks;12731274static int cmp(BlockBegin** a, BlockBegin** b) {1275return (*a)->block_id() - (*b)->block_id();1276}12771278public:1279PredecessorValidator(IR* hir) {1280ResourceMark rm;1281_predecessors = new BlockListList(BlockBegin::number_of_blocks(), BlockBegin::number_of_blocks(), NULL);1282_blocks = new BlockList();12831284int i;1285hir->start()->iterate_preorder(this);1286if (hir->code() != NULL) {1287assert(hir->code()->length() == _blocks->length(), "must match");1288for (i = 0; i < _blocks->length(); i++) {1289assert(hir->code()->contains(_blocks->at(i)), "should be in both lists");1290}1291}12921293for (i = 0; i < _blocks->length(); i++) {1294BlockBegin* block = _blocks->at(i);1295BlockList* preds = _predecessors->at(block->block_id());1296if (preds == NULL) {1297assert(block->number_of_preds() == 0, "should be the same");1298continue;1299}13001301// clone the pred list so we can mutate it1302BlockList* pred_copy = new BlockList();1303int j;1304for (j = 0; j < block->number_of_preds(); j++) {1305pred_copy->append(block->pred_at(j));1306}1307// sort them in the same order1308preds->sort(cmp);1309pred_copy->sort(cmp);1310int length = MIN2(preds->length(), block->number_of_preds());1311for (j = 0; j < block->number_of_preds(); j++) {1312assert(preds->at(j) == pred_copy->at(j), "must match");1313}13141315assert(preds->length() == block->number_of_preds(), "should be the same");1316}1317}13181319virtual void block_do(BlockBegin* block) {1320_blocks->append(block);1321BlockEnd* be = block->end();1322int n = be->number_of_sux();1323int i;1324for (i = 0; i < n; i++) {1325BlockBegin* sux = be->sux_at(i);1326assert(!sux->is_set(BlockBegin::exception_entry_flag), "must not be xhandler");13271328BlockList* preds = _predecessors->at_grow(sux->block_id(), NULL);1329if (preds == NULL) {1330preds = new BlockList();1331_predecessors->at_put(sux->block_id(), preds);1332}1333preds->append(block);1334}13351336n = block->number_of_exception_handlers();1337for (i = 0; i < n; i++) {1338BlockBegin* sux = block->exception_handler_at(i);1339assert(sux->is_set(BlockBegin::exception_entry_flag), "must be xhandler");13401341BlockList* preds = _predecessors->at_grow(sux->block_id(), NULL);1342if (preds == NULL) {1343preds = new BlockList();1344_predecessors->at_put(sux->block_id(), preds);1345}1346preds->append(block);1347}1348}1349};13501351class VerifyBlockBeginField : public BlockClosure {13521353public:13541355virtual void block_do(BlockBegin *block) {1356for ( Instruction *cur = block; cur != NULL; cur = cur->next()) {1357assert(cur->block() == block, "Block begin is not correct");1358}1359}1360};13611362void IR::verify() {1363#ifdef ASSERT1364PredecessorValidator pv(this);1365VerifyBlockBeginField verifier;1366this->iterate_postorder(&verifier);1367#endif1368}13691370#endif // PRODUCT13711372void SubstitutionResolver::visit(Value* v) {1373Value v0 = *v;1374if (v0) {1375Value vs = v0->subst();1376if (vs != v0) {1377*v = v0->subst();1378}1379}1380}13811382#ifdef ASSERT1383class SubstitutionChecker: public ValueVisitor {1384void visit(Value* v) {1385Value v0 = *v;1386if (v0) {1387Value vs = v0->subst();1388assert(vs == v0, "missed substitution");1389}1390}1391};1392#endif139313941395void SubstitutionResolver::block_do(BlockBegin* block) {1396Instruction* last = NULL;1397for (Instruction* n = block; n != NULL;) {1398n->values_do(this);1399// need to remove this instruction from the instruction stream1400if (n->subst() != n) {1401guarantee(last != NULL, "must have last");1402last->set_next(n->next());1403} else {1404last = n;1405}1406n = last->next();1407}14081409#ifdef ASSERT1410SubstitutionChecker check_substitute;1411if (block->state()) block->state()->values_do(&check_substitute);1412block->block_values_do(&check_substitute);1413if (block->end() && block->end()->state()) block->end()->state()->values_do(&check_substitute);1414#endif1415}141614171418