Path: blob/master/src/hotspot/share/oops/generateOopMap.hpp
40951 views
/*1* Copyright (c) 1997, 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#ifndef SHARE_OOPS_GENERATEOOPMAP_HPP25#define SHARE_OOPS_GENERATEOOPMAP_HPP2627#include "interpreter/bytecodeStream.hpp"28#include "memory/allocation.hpp"29#include "oops/method.hpp"30#include "oops/oopsHierarchy.hpp"31#include "runtime/signature.hpp"32#include "utilities/bitMap.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);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 {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(const methodHandle& method);81void update_ret_table(int bci, int delta);82RetTableEntry* find_jsrs_for_target(int targBci);83};8485//86// CellTypeState87//88class CellTypeState {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 {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 uint64_t _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;352ResourceBitMap _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);363BasicBlock * get_basic_block_at (int bci) const;364BasicBlock * get_basic_block_containing (int bci) const;365void interp_bb (BasicBlock *bb);366void restore_state (BasicBlock *bb);367int next_bb_start_pc (BasicBlock *bb);368void update_basic_blocks (int bci, int delta, int new_method_size);369static void bb_mark_fct (GenerateOopMap *c, int deltaBci, int *data);370371// Dead code detection372void mark_reachable_code();373static void reachable_basicblock (GenerateOopMap *c, int deltaBci, int *data);374375// Interpretation methods (primary)376void do_interpretation ();377void init_basic_blocks ();378void setup_method_entry_state ();379void interp_all ();380381// Interpretation methods (secondary)382void interp1 (BytecodeStream *itr);383void do_exception_edge (BytecodeStream *itr);384void check_type (CellTypeState expected, CellTypeState actual);385void ppstore (CellTypeState *in, int loc_no);386void ppload (CellTypeState *out, int loc_no);387void ppush1 (CellTypeState in);388void ppush (CellTypeState *in);389void ppop1 (CellTypeState out);390void ppop (CellTypeState *out);391void ppop_any (int poplen);392void pp (CellTypeState *in, CellTypeState *out);393void pp_new_ref (CellTypeState *in, int bci);394void ppdupswap (int poplen, const char *out);395void do_ldc (int bci);396void do_astore (int idx);397void do_jsr (int delta);398void do_field (int is_get, int is_static, int idx, int bci);399void do_method (int is_static, int is_interface, int idx, int bci);400void do_multianewarray (int dims, int bci);401void do_monitorenter (int bci);402void do_monitorexit (int bci);403void do_return_monitor_check ();404void do_checkcast ();405CellTypeState *signature_to_effect (const Symbol* sig, int bci, CellTypeState *out);406int copy_cts (CellTypeState *dst, CellTypeState *src);407408// Error handling409void error_work (const char *format, va_list ap) ATTRIBUTE_PRINTF(2, 0);410void report_error (const char *format, ...) ATTRIBUTE_PRINTF(2, 3);411void verify_error (const char *format, ...) ATTRIBUTE_PRINTF(2, 3);412bool got_error() { return _got_error; }413414// Create result set415bool _report_result;416bool _report_result_for_send; // Unfortunatly, stackmaps for sends are special, so we need some extra417BytecodeStream *_itr_send; // variables to handle them properly.418419void report_result ();420421// Initvars422GrowableArray<intptr_t> * _init_vars;423424void initialize_vars ();425void add_to_ref_init_set (int localNo);426427// Conflicts rewrite logic428bool _conflict; // True, if a conflict occurred during interpretation429int _nof_refval_conflicts; // No. of conflicts that require rewrites430int * _new_var_map;431432void record_refval_conflict (int varNo);433void rewrite_refval_conflicts ();434void rewrite_refval_conflict (int from, int to);435bool rewrite_refval_conflict_inst (BytecodeStream *i, int from, int to);436bool rewrite_load_or_store (BytecodeStream *i, Bytecodes::Code bc, Bytecodes::Code bc0, unsigned int varNo);437438void expand_current_instr (int bci, int ilen, int newIlen, u_char inst_buffer[]);439bool is_astore (BytecodeStream *itr, int *index);440bool is_aload (BytecodeStream *itr, int *index);441442// List of bci's where a return address is on top of the stack443GrowableArray<intptr_t> *_ret_adr_tos;444445bool stack_top_holds_ret_addr (int bci);446void compute_ret_adr_at_TOS ();447void update_ret_adr_at_TOS (int bci, int delta);448449int binsToHold (int no) { return ((no+(BitsPerWord-1))/BitsPerWord); }450char *state_vec_to_string (CellTypeState* vec, int len);451452// Helper method. Can be used in subclasses to fx. calculate gc_points. If the current instuction453// is a control transfer, then calls the jmpFct all possible destinations.454void ret_jump_targets_do (BytecodeStream *bcs, jmpFct_t jmpFct, int varNo,int *data);455bool jump_targets_do (BytecodeStream *bcs, jmpFct_t jmpFct, int *data);456457friend class RelocCallback;458public:459GenerateOopMap(const methodHandle& method);460461// Compute the map - returns true on success and false on error.462bool compute_map(Thread* current);463// Returns the exception related to any error, if the map was computed by a suitable JavaThread.464Handle exception() { return _exception; }465466void result_for_basicblock(int bci); // Do a callback on fill_stackmap_for_opcodes for basicblock containing bci467468// Query469int max_locals() const { return _max_locals; }470Method* method() const { return _method(); }471methodHandle method_as_handle() const { return _method; }472473bool did_rewriting() { return _did_rewriting; }474bool did_relocation() { return _did_relocation; }475476static void print_time();477478// Monitor query479bool monitor_safe() { return _monitor_safe; }480481// Specialization methods. Intended use:482// - possible_gc_point must return true for every bci for which the stackmaps must be returned483// - fill_stackmap_prolog is called just before the result is reported. The arguments tells the estimated484// number of gc points485// - fill_stackmap_for_opcodes is called once for each bytecode index in order (0...code_length-1)486// - fill_stackmap_epilog is called after all results has been reported. Note: Since the algorithm does not report487// stackmaps for deadcode, fewer gc_points might have been encounted than assumed during the epilog. It is the488// responsibility of the subclass to count the correct number.489// - fill_init_vars are called once with the result of the init_vars computation490//491// All these methods are used during a call to: compute_map. Note: Non of the return results are valid492// after compute_map returns, since all values are allocated as resource objects.493//494// All virtual method must be implemented in subclasses495virtual bool allow_rewrites () const { return false; }496virtual bool report_results () const { return true; }497virtual bool report_init_vars () const { return true; }498virtual bool possible_gc_point (BytecodeStream *bcs) { ShouldNotReachHere(); return false; }499virtual void fill_stackmap_prolog (int nof_gc_points) { ShouldNotReachHere(); }500virtual void fill_stackmap_epilog () { ShouldNotReachHere(); }501virtual void fill_stackmap_for_opcodes (BytecodeStream *bcs,502CellTypeState* vars,503CellTypeState* stack,504int stackTop) { ShouldNotReachHere(); }505virtual void fill_init_vars (GrowableArray<intptr_t> *init_vars) { ShouldNotReachHere();; }506};507508//509// Subclass of the GenerateOopMap Class that just do rewrites of the method, if needed.510// It does not store any oopmaps.511//512class ResolveOopMapConflicts: public GenerateOopMap {513private:514515bool _must_clear_locals;516517virtual bool report_results() const { return false; }518virtual bool report_init_vars() const { return true; }519virtual bool allow_rewrites() const { return true; }520virtual bool possible_gc_point (BytecodeStream *bcs) { return false; }521virtual void fill_stackmap_prolog (int nof_gc_points) {}522virtual void fill_stackmap_epilog () {}523virtual void fill_stackmap_for_opcodes (BytecodeStream *bcs,524CellTypeState* vars,525CellTypeState* stack,526int stack_top) {}527virtual void fill_init_vars (GrowableArray<intptr_t> *init_vars) { _must_clear_locals = init_vars->length() > 0; }528529#ifndef PRODUCT530// Statistics531static int _nof_invocations;532static int _nof_rewrites;533static int _nof_relocations;534#endif535536public:537ResolveOopMapConflicts(const methodHandle& method) : GenerateOopMap(method) { _must_clear_locals = false; };538539methodHandle do_potential_rewrite(TRAPS);540bool must_clear_locals() const { return _must_clear_locals; }541};542543544//545// Subclass used by the compiler to generate pairing infomation546//547class GeneratePairingInfo: public GenerateOopMap {548private:549550virtual bool report_results() const { return false; }551virtual bool report_init_vars() const { return false; }552virtual bool allow_rewrites() const { return false; }553virtual bool possible_gc_point (BytecodeStream *bcs) { return false; }554virtual void fill_stackmap_prolog (int nof_gc_points) {}555virtual void fill_stackmap_epilog () {}556virtual void fill_stackmap_for_opcodes (BytecodeStream *bcs,557CellTypeState* vars,558CellTypeState* stack,559int stack_top) {}560virtual void fill_init_vars (GrowableArray<intptr_t> *init_vars) {}561public:562GeneratePairingInfo(const methodHandle& method) : GenerateOopMap(method) {};563564// Call compute_map() to generate info.565};566567#endif // SHARE_OOPS_GENERATEOOPMAP_HPP568569570