Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/oops/generateOopMap.hpp
32285 views
/*1* Copyright (c) 1997, 2014, 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#ifndef SHARE_VM_OOPS_GENERATEOOPMAP_HPP25#define SHARE_VM_OOPS_GENERATEOOPMAP_HPP2627#include "interpreter/bytecodeStream.hpp"28#include "memory/allocation.inline.hpp"29#include "memory/universe.inline.hpp"30#include "oops/method.hpp"31#include "oops/oopsHierarchy.hpp"32#include "runtime/signature.hpp"3334// Forward definition35class GenerateOopMap;36class BasicBlock;37class CellTypeState;38class StackMap;3940// These two should be removed. But requires som code to be cleaned up41#define MAXARGSIZE 256 // This should be enough42#define MAX_LOCAL_VARS 65536 // 16-bit entry4344typedef void (*jmpFct_t)(GenerateOopMap *c, int bcpDelta, int* data);454647// RetTable48//49// Contains maping between jsr targets and there return addresses. One-to-many mapping50//51class RetTableEntry : public ResourceObj {52private:53static int _init_nof_jsrs; // Default size of jsrs list54int _target_bci; // Target PC address of jump (bytecode index)55GrowableArray<intptr_t> * _jsrs; // List of return addresses (bytecode index)56RetTableEntry *_next; // Link to next entry57public:58RetTableEntry(int target, RetTableEntry *next) { _target_bci=target; _jsrs = new GrowableArray<intptr_t>(_init_nof_jsrs); _next = next; }5960// Query61int target_bci() const { return _target_bci; }62int nof_jsrs() const { return _jsrs->length(); }63int jsrs(int i) const { assert(i>=0 && i<nof_jsrs(), "Index out of bounds"); return _jsrs->at(i); }6465// Update entry66void add_jsr (int return_bci) { _jsrs->append(return_bci); }67void add_delta (int bci, int delta);68RetTableEntry * next() const { return _next; }69};707172class RetTable VALUE_OBJ_CLASS_SPEC {73private:74RetTableEntry *_first;75static int _init_nof_entries;7677void add_jsr(int return_bci, int target_bci); // Adds entry to list78public:79RetTable() { _first = NULL; }80void compute_ret_table(methodHandle method);81void update_ret_table(int bci, int delta);82RetTableEntry* find_jsrs_for_target(int targBci);83};8485//86// CellTypeState87//88class CellTypeState VALUE_OBJ_CLASS_SPEC {89private:90unsigned int _state;9192// Masks for separating the BITS and INFO portions of a CellTypeState93enum { info_mask = right_n_bits(28),94bits_mask = (int)(~info_mask) };9596// These constant are used for manipulating the BITS portion of a97// CellTypeState98enum { uninit_bit = (int)(nth_bit(31)),99ref_bit = nth_bit(30),100val_bit = nth_bit(29),101addr_bit = nth_bit(28),102live_bits_mask = (int)(bits_mask & ~uninit_bit) };103104// These constants are used for manipulating the INFO portion of a105// CellTypeState106enum { top_info_bit = nth_bit(27),107not_bottom_info_bit = nth_bit(26),108info_data_mask = right_n_bits(26),109info_conflict = info_mask };110111// Within the INFO data, these values are used to distinguish different112// kinds of references.113enum { ref_not_lock_bit = nth_bit(25), // 0 if this reference is locked as a monitor114ref_slot_bit = nth_bit(24), // 1 if this reference is a "slot" reference,115// 0 if it is a "line" reference.116ref_data_mask = right_n_bits(24) };117118119// These values are used to initialize commonly used CellTypeState120// constants.121enum { bottom_value = 0,122uninit_value = (int)(uninit_bit | info_conflict),123ref_value = ref_bit,124ref_conflict = ref_bit | info_conflict,125val_value = val_bit | info_conflict,126addr_value = addr_bit,127addr_conflict = addr_bit | info_conflict };128129public:130131// Since some C++ constructors generate poor code for declarations of the132// form...133//134// CellTypeState vector[length];135//136// ...we avoid making a constructor for this class. CellTypeState values137// should be constructed using one of the make_* methods:138139static CellTypeState make_any(int state) {140CellTypeState s;141s._state = state;142// Causes SS10 warning.143// assert(s.is_valid_state(), "check to see if CellTypeState is valid");144return s;145}146147static CellTypeState make_bottom() {148return make_any(0);149}150151static CellTypeState make_top() {152return make_any(AllBits);153}154155static CellTypeState make_addr(int bci) {156assert((bci >= 0) && (bci < info_data_mask), "check to see if ret addr is valid");157return make_any(addr_bit | not_bottom_info_bit | (bci & info_data_mask));158}159160static CellTypeState make_slot_ref(int slot_num) {161assert(slot_num >= 0 && slot_num < ref_data_mask, "slot out of range");162return make_any(ref_bit | not_bottom_info_bit | ref_not_lock_bit | ref_slot_bit |163(slot_num & ref_data_mask));164}165166static CellTypeState make_line_ref(int bci) {167assert(bci >= 0 && bci < ref_data_mask, "line out of range");168return make_any(ref_bit | not_bottom_info_bit | ref_not_lock_bit |169(bci & ref_data_mask));170}171172static CellTypeState make_lock_ref(int bci) {173assert(bci >= 0 && bci < ref_data_mask, "line out of range");174return make_any(ref_bit | not_bottom_info_bit | (bci & ref_data_mask));175}176177// Query methods:178bool is_bottom() const { return _state == 0; }179bool is_live() const { return ((_state & live_bits_mask) != 0); }180bool is_valid_state() const {181// Uninitialized and value cells must contain no data in their info field:182if ((can_be_uninit() || can_be_value()) && !is_info_top()) {183return false;184}185// The top bit is only set when all info bits are set:186if (is_info_top() && ((_state & info_mask) != info_mask)) {187return false;188}189// The not_bottom_bit must be set when any other info bit is set:190if (is_info_bottom() && ((_state & info_mask) != 0)) {191return false;192}193return true;194}195196bool is_address() const { return ((_state & bits_mask) == addr_bit); }197bool is_reference() const { return ((_state & bits_mask) == ref_bit); }198bool is_value() const { return ((_state & bits_mask) == val_bit); }199bool is_uninit() const { return ((_state & bits_mask) == (uint)uninit_bit); }200201bool can_be_address() const { return ((_state & addr_bit) != 0); }202bool can_be_reference() const { return ((_state & ref_bit) != 0); }203bool can_be_value() const { return ((_state & val_bit) != 0); }204bool can_be_uninit() const { return ((_state & uninit_bit) != 0); }205206bool is_info_bottom() const { return ((_state & not_bottom_info_bit) == 0); }207bool is_info_top() const { return ((_state & top_info_bit) != 0); }208int get_info() const {209assert((!is_info_top() && !is_info_bottom()),210"check to make sure top/bottom info is not used");211return (_state & info_data_mask);212}213214bool is_good_address() const { return is_address() && !is_info_top(); }215bool is_lock_reference() const {216return ((_state & (bits_mask | top_info_bit | ref_not_lock_bit)) == ref_bit);217}218bool is_nonlock_reference() const {219return ((_state & (bits_mask | top_info_bit | ref_not_lock_bit)) == (ref_bit | ref_not_lock_bit));220}221222bool equal(CellTypeState a) const { return _state == a._state; }223bool equal_kind(CellTypeState a) const {224return (_state & bits_mask) == (a._state & bits_mask);225}226227char to_char() const;228229// Merge230CellTypeState merge (CellTypeState cts, int slot) const;231232// Debugging output233void print(outputStream *os);234235// Default values of common values236static CellTypeState bottom;237static CellTypeState uninit;238static CellTypeState ref;239static CellTypeState value;240static CellTypeState refUninit;241static CellTypeState varUninit;242static CellTypeState top;243static CellTypeState addr;244};245246247//248// BasicBlockStruct249//250class BasicBlock: ResourceObj {251private:252bool _changed; // Reached a fixpoint or not253public:254enum Constants {255_dead_basic_block = -2,256_unreached = -1 // Alive but not yet reached by analysis257// >=0 // Alive and has a merged state258};259260int _bci; // Start of basic block261int _end_bci; // Bci of last instruction in basicblock262int _max_locals; // Determines split between vars and stack263int _max_stack; // Determines split between stack and monitors264CellTypeState* _state; // State (vars, stack) at entry.265int _stack_top; // -1 indicates bottom stack value.266int _monitor_top; // -1 indicates bottom monitor stack value.267268CellTypeState* vars() { return _state; }269CellTypeState* stack() { return _state + _max_locals; }270271bool changed() { return _changed; }272void set_changed(bool s) { _changed = s; }273274bool is_reachable() const { return _stack_top >= 0; } // Analysis has reached this basicblock275276// All basicblocks that are unreachable are going to have a _stack_top == _dead_basic_block.277// This info. is setup in a pre-parse before the real abstract interpretation starts.278bool is_dead() const { return _stack_top == _dead_basic_block; }279bool is_alive() const { return _stack_top != _dead_basic_block; }280void mark_as_alive() { assert(is_dead(), "must be dead"); _stack_top = _unreached; }281};282283284//285// GenerateOopMap286//287// Main class used to compute the pointer-maps in a Method288//289class GenerateOopMap VALUE_OBJ_CLASS_SPEC {290protected:291292// _monitor_top is set to this constant to indicate that a monitor matching293// problem was encountered prior to this point in control flow.294enum { bad_monitors = -1 };295296// Main variables297methodHandle _method; // The method we are examine298RetTable _rt; // Contains the return address mappings299int _max_locals; // Cached value of no. of locals300int _max_stack; // Cached value of max. stack depth301int _max_monitors; // Cached value of max. monitor stack depth302int _has_exceptions; // True, if exceptions exist for method303bool _got_error; // True, if an error occurred during interpretation.304Handle _exception; // Exception if got_error is true.305bool _did_rewriting; // was bytecodes rewritten306bool _did_relocation; // was relocation neccessary307bool _monitor_safe; // The monitors in this method have been determined308// to be safe.309310// Working Cell type state311int _state_len; // Size of states312CellTypeState *_state; // list of states313char *_state_vec_buf; // Buffer used to print a readable version of a state314int _stack_top;315int _monitor_top;316317// Timing and statistics318static elapsedTimer _total_oopmap_time; // Holds cumulative oopmap generation time319static long _total_byte_count; // Holds cumulative number of bytes inspected320321// Cell type methods322void init_state();323void make_context_uninitialized ();324int methodsig_to_effect (Symbol* signature, bool isStatic, CellTypeState* effect);325bool merge_local_state_vectors (CellTypeState* cts, CellTypeState* bbts);326bool merge_monitor_state_vectors(CellTypeState* cts, CellTypeState* bbts);327void copy_state (CellTypeState *dst, CellTypeState *src);328void merge_state_into_bb (BasicBlock *bb);329static void merge_state (GenerateOopMap *gom, int bcidelta, int* data);330void set_var (int localNo, CellTypeState cts);331CellTypeState get_var (int localNo);332CellTypeState pop ();333void push (CellTypeState cts);334CellTypeState monitor_pop ();335void monitor_push (CellTypeState cts);336CellTypeState * vars () { return _state; }337CellTypeState * stack () { return _state+_max_locals; }338CellTypeState * monitors () { return _state+_max_locals+_max_stack; }339340void replace_all_CTS_matches (CellTypeState match,341CellTypeState replace);342void print_states (outputStream *os, CellTypeState *vector, int num);343void print_current_state (outputStream *os,344BytecodeStream *itr,345bool detailed);346void report_monitor_mismatch (const char *msg);347348// Basicblock info349BasicBlock * _basic_blocks; // Array of basicblock info350int _gc_points;351int _bb_count;352BitMap _bb_hdr_bits;353354// Basicblocks methods355void initialize_bb ();356void mark_bbheaders_and_count_gc_points();357bool is_bb_header (int bci) const {358return _bb_hdr_bits.at(bci);359}360int gc_points () const { return _gc_points; }361int bb_count () const { return _bb_count; }362void set_bbmark_bit (int bci) {363_bb_hdr_bits.at_put(bci, true);364}365void clear_bbmark_bit (int bci) {366_bb_hdr_bits.at_put(bci, false);367}368BasicBlock * get_basic_block_at (int bci) const;369BasicBlock * get_basic_block_containing (int bci) const;370void interp_bb (BasicBlock *bb);371void restore_state (BasicBlock *bb);372int next_bb_start_pc (BasicBlock *bb);373void update_basic_blocks (int bci, int delta, int new_method_size);374static void bb_mark_fct (GenerateOopMap *c, int deltaBci, int *data);375376// Dead code detection377void mark_reachable_code();378static void reachable_basicblock (GenerateOopMap *c, int deltaBci, int *data);379380// Interpretation methods (primary)381void do_interpretation ();382void init_basic_blocks ();383void setup_method_entry_state ();384void interp_all ();385386// Interpretation methods (secondary)387void interp1 (BytecodeStream *itr);388void do_exception_edge (BytecodeStream *itr);389void check_type (CellTypeState expected, CellTypeState actual);390void ppstore (CellTypeState *in, int loc_no);391void ppload (CellTypeState *out, int loc_no);392void ppush1 (CellTypeState in);393void ppush (CellTypeState *in);394void ppop1 (CellTypeState out);395void ppop (CellTypeState *out);396void ppop_any (int poplen);397void pp (CellTypeState *in, CellTypeState *out);398void pp_new_ref (CellTypeState *in, int bci);399void ppdupswap (int poplen, const char *out);400void do_ldc (int bci);401void do_astore (int idx);402void do_jsr (int delta);403void do_field (int is_get, int is_static, int idx, int bci);404void do_method (int is_static, int is_interface, int idx, int bci);405void do_multianewarray (int dims, int bci);406void do_monitorenter (int bci);407void do_monitorexit (int bci);408void do_return_monitor_check ();409void do_checkcast ();410CellTypeState *sigchar_to_effect (char sigch, int bci, CellTypeState *out);411int copy_cts (CellTypeState *dst, CellTypeState *src);412413// Error handling414void error_work (const char *format, va_list ap) ATTRIBUTE_PRINTF(2, 0);415void report_error (const char *format, ...) ATTRIBUTE_PRINTF(2, 3);416void verify_error (const char *format, ...) ATTRIBUTE_PRINTF(2, 3);417bool got_error() { return _got_error; }418419// Create result set420bool _report_result;421bool _report_result_for_send; // Unfortunatly, stackmaps for sends are special, so we need some extra422BytecodeStream *_itr_send; // variables to handle them properly.423424void report_result ();425426// Initvars427GrowableArray<intptr_t> * _init_vars;428429void initialize_vars ();430void add_to_ref_init_set (int localNo);431432// Conflicts rewrite logic433bool _conflict; // True, if a conflict occurred during interpretation434int _nof_refval_conflicts; // No. of conflicts that require rewrites435int * _new_var_map;436437void record_refval_conflict (int varNo);438void rewrite_refval_conflicts ();439void rewrite_refval_conflict (int from, int to);440bool rewrite_refval_conflict_inst (BytecodeStream *i, int from, int to);441bool rewrite_load_or_store (BytecodeStream *i, Bytecodes::Code bc, Bytecodes::Code bc0, unsigned int varNo);442443void expand_current_instr (int bci, int ilen, int newIlen, u_char inst_buffer[]);444bool is_astore (BytecodeStream *itr, int *index);445bool is_aload (BytecodeStream *itr, int *index);446447// List of bci's where a return address is on top of the stack448GrowableArray<intptr_t> *_ret_adr_tos;449450bool stack_top_holds_ret_addr (int bci);451void compute_ret_adr_at_TOS ();452void update_ret_adr_at_TOS (int bci, int delta);453454int binsToHold (int no) { return ((no+(BitsPerWord-1))/BitsPerWord); }455char *state_vec_to_string (CellTypeState* vec, int len);456457// Helper method. Can be used in subclasses to fx. calculate gc_points. If the current instuction458// is a control transfer, then calls the jmpFct all possible destinations.459void ret_jump_targets_do (BytecodeStream *bcs, jmpFct_t jmpFct, int varNo,int *data);460bool jump_targets_do (BytecodeStream *bcs, jmpFct_t jmpFct, int *data);461462friend class RelocCallback;463public:464GenerateOopMap(methodHandle method);465466// Compute the map.467void compute_map(TRAPS);468void result_for_basicblock(int bci); // Do a callback on fill_stackmap_for_opcodes for basicblock containing bci469470// Query471int max_locals() const { return _max_locals; }472Method* method() const { return _method(); }473methodHandle method_as_handle() const { return _method; }474475bool did_rewriting() { return _did_rewriting; }476bool did_relocation() { return _did_relocation; }477478static void print_time();479480// Monitor query481bool monitor_safe() { return _monitor_safe; }482483// Specialization methods. Intended use:484// - possible_gc_point must return true for every bci for which the stackmaps must be returned485// - fill_stackmap_prolog is called just before the result is reported. The arguments tells the estimated486// number of gc points487// - fill_stackmap_for_opcodes is called once for each bytecode index in order (0...code_length-1)488// - fill_stackmap_epilog is called after all results has been reported. Note: Since the algorithm does not report489// stackmaps for deadcode, fewer gc_points might have been encounted than assumed during the epilog. It is the490// responsibility of the subclass to count the correct number.491// - fill_init_vars are called once with the result of the init_vars computation492//493// All these methods are used during a call to: compute_map. Note: Non of the return results are valid494// after compute_map returns, since all values are allocated as resource objects.495//496// All virtual method must be implemented in subclasses497virtual bool allow_rewrites () const { return false; }498virtual bool report_results () const { return true; }499virtual bool report_init_vars () const { return true; }500virtual bool possible_gc_point (BytecodeStream *bcs) { ShouldNotReachHere(); return false; }501virtual void fill_stackmap_prolog (int nof_gc_points) { ShouldNotReachHere(); }502virtual void fill_stackmap_epilog () { ShouldNotReachHere(); }503virtual void fill_stackmap_for_opcodes (BytecodeStream *bcs,504CellTypeState* vars,505CellTypeState* stack,506int stackTop) { ShouldNotReachHere(); }507virtual void fill_init_vars (GrowableArray<intptr_t> *init_vars) { ShouldNotReachHere();; }508};509510//511// Subclass of the GenerateOopMap Class that just do rewrites of the method, if needed.512// It does not store any oopmaps.513//514class ResolveOopMapConflicts: public GenerateOopMap {515private:516517bool _must_clear_locals;518519virtual bool report_results() const { return false; }520virtual bool report_init_vars() const { return true; }521virtual bool allow_rewrites() const { return true; }522virtual bool possible_gc_point (BytecodeStream *bcs) { return false; }523virtual void fill_stackmap_prolog (int nof_gc_points) {}524virtual void fill_stackmap_epilog () {}525virtual void fill_stackmap_for_opcodes (BytecodeStream *bcs,526CellTypeState* vars,527CellTypeState* stack,528int stack_top) {}529virtual void fill_init_vars (GrowableArray<intptr_t> *init_vars) { _must_clear_locals = init_vars->length() > 0; }530531#ifndef PRODUCT532// Statistics533static int _nof_invocations;534static int _nof_rewrites;535static int _nof_relocations;536#endif537538public:539ResolveOopMapConflicts(methodHandle method) : GenerateOopMap(method) { _must_clear_locals = false; };540541methodHandle do_potential_rewrite(TRAPS);542bool must_clear_locals() const { return _must_clear_locals; }543};544545546//547// Subclass used by the compiler to generate pairing infomation548//549class GeneratePairingInfo: public GenerateOopMap {550private:551552virtual bool report_results() const { return false; }553virtual bool report_init_vars() const { return false; }554virtual bool allow_rewrites() const { return false; }555virtual bool possible_gc_point (BytecodeStream *bcs) { return false; }556virtual void fill_stackmap_prolog (int nof_gc_points) {}557virtual void fill_stackmap_epilog () {}558virtual void fill_stackmap_for_opcodes (BytecodeStream *bcs,559CellTypeState* vars,560CellTypeState* stack,561int stack_top) {}562virtual void fill_init_vars (GrowableArray<intptr_t> *init_vars) {}563public:564GeneratePairingInfo(methodHandle method) : GenerateOopMap(method) {};565566// Call compute_map(CHECK) to generate info.567};568569#endif // SHARE_VM_OOPS_GENERATEOOPMAP_HPP570571572