Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/c1/c1_RangeCheckElimination.hpp
32285 views
/*1* Copyright (c) 2012, 2013, 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_C1_C1_RANGECHECKELIMINATION_HPP25#define SHARE_VM_C1_C1_RANGECHECKELIMINATION_HPP2627#include "c1/c1_Instruction.hpp"2829// Base class for range check elimination30class RangeCheckElimination : AllStatic {31public:32static void eliminate(IR *ir);33};3435// Implementation36class RangeCheckEliminator VALUE_OBJ_CLASS_SPEC {37private:38int _number_of_instructions;39bool _optimistic; // Insert predicates and deoptimize when they fail40IR *_ir;4142define_array(BlockBeginArray, BlockBegin*)43define_stack(BlockBeginList, BlockBeginArray)44define_stack(IntegerStack, intArray)45define_array(IntegerMap, IntegerStack*)4647class Verification : public _ValueObj /*VALUE_OBJ_CLASS_SPEC*/, public BlockClosure {48private:49IR *_ir;50boolArray _used;51BlockBeginList _current;52BlockBeginList _successors;5354public:55Verification(IR *ir);56virtual void block_do(BlockBegin *block);57bool can_reach(BlockBegin *start, BlockBegin *end, BlockBegin *dont_use = NULL);58bool dominates(BlockBegin *dominator, BlockBegin *block);59};6061public:62// Bounds for an instruction in the form x + c which c integer63// constant and x another instruction64class Bound : public CompilationResourceObj {65private:66int _upper;67Value _upper_instr;68int _lower;69Value _lower_instr;7071public:72Bound();73Bound(Value v);74Bound(Instruction::Condition cond, Value v, int constant = 0);75Bound(int lower, Value lower_instr, int upper, Value upper_instr);76~Bound();7778#ifdef ASSERT79void add_assertion(Instruction *instruction, Instruction *position, int i, Value instr, Instruction::Condition cond);80#endif81int upper();82Value upper_instr();83int lower();84Value lower_instr();85void print();86bool check_no_overflow(int const_value);87void or_op(Bound *b);88void and_op(Bound *b);89bool has_upper();90bool has_lower();91void set_upper(int upper, Value upper_instr);92void set_lower(int lower, Value lower_instr);93bool is_smaller(Bound *b);94void remove_upper();95void remove_lower();96void add_constant(int value);97Bound *copy();9899private:100void init();101};102103104class Visitor : public InstructionVisitor {105private:106Bound *_bound;107RangeCheckEliminator *_rce;108109public:110void set_range_check_eliminator(RangeCheckEliminator *rce) { _rce = rce; }111Bound *bound() const { return _bound; }112void clear_bound() { _bound = NULL; }113114protected:115// visitor functions116void do_Constant (Constant* x);117void do_IfOp (IfOp* x);118void do_LogicOp (LogicOp* x);119void do_ArithmeticOp (ArithmeticOp* x);120void do_Phi (Phi* x);121122void do_StoreField (StoreField* x) { /* nothing to do */ };123void do_StoreIndexed (StoreIndexed* x) { /* nothing to do */ };124void do_MonitorEnter (MonitorEnter* x) { /* nothing to do */ };125void do_MonitorExit (MonitorExit* x) { /* nothing to do */ };126void do_Invoke (Invoke* x) { /* nothing to do */ };127void do_UnsafePutRaw (UnsafePutRaw* x) { /* nothing to do */ };128void do_UnsafePutObject(UnsafePutObject* x) { /* nothing to do */ };129void do_Intrinsic (Intrinsic* x) { /* nothing to do */ };130void do_Local (Local* x) { /* nothing to do */ };131void do_LoadField (LoadField* x) { /* nothing to do */ };132void do_ArrayLength (ArrayLength* x) { /* nothing to do */ };133void do_LoadIndexed (LoadIndexed* x) { /* nothing to do */ };134void do_NegateOp (NegateOp* x) { /* nothing to do */ };135void do_ShiftOp (ShiftOp* x) { /* nothing to do */ };136void do_CompareOp (CompareOp* x) { /* nothing to do */ };137void do_Convert (Convert* x) { /* nothing to do */ };138void do_NullCheck (NullCheck* x) { /* nothing to do */ };139void do_TypeCast (TypeCast* x) { /* nothing to do */ };140void do_NewInstance (NewInstance* x) { /* nothing to do */ };141void do_NewTypeArray (NewTypeArray* x) { /* nothing to do */ };142void do_NewObjectArray (NewObjectArray* x) { /* nothing to do */ };143void do_NewMultiArray (NewMultiArray* x) { /* nothing to do */ };144void do_CheckCast (CheckCast* x) { /* nothing to do */ };145void do_InstanceOf (InstanceOf* x) { /* nothing to do */ };146void do_BlockBegin (BlockBegin* x) { /* nothing to do */ };147void do_Goto (Goto* x) { /* nothing to do */ };148void do_If (If* x) { /* nothing to do */ };149void do_IfInstanceOf (IfInstanceOf* x) { /* nothing to do */ };150void do_TableSwitch (TableSwitch* x) { /* nothing to do */ };151void do_LookupSwitch (LookupSwitch* x) { /* nothing to do */ };152void do_Return (Return* x) { /* nothing to do */ };153void do_Throw (Throw* x) { /* nothing to do */ };154void do_Base (Base* x) { /* nothing to do */ };155void do_OsrEntry (OsrEntry* x) { /* nothing to do */ };156void do_ExceptionObject(ExceptionObject* x) { /* nothing to do */ };157void do_RoundFP (RoundFP* x) { /* nothing to do */ };158void do_UnsafeGetRaw (UnsafeGetRaw* x) { /* nothing to do */ };159void do_UnsafeGetObject(UnsafeGetObject* x) { /* nothing to do */ };160void do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x) { /* nothing to do */ };161void do_UnsafePrefetchRead (UnsafePrefetchRead* x) { /* nothing to do */ };162void do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) { /* nothing to do */ };163void do_ProfileCall (ProfileCall* x) { /* nothing to do */ };164void do_ProfileReturnType (ProfileReturnType* x) { /* nothing to do */ };165void do_ProfileInvoke (ProfileInvoke* x) { /* nothing to do */ };166void do_RuntimeCall (RuntimeCall* x) { /* nothing to do */ };167void do_MemBar (MemBar* x) { /* nothing to do */ };168void do_RangeCheckPredicate(RangeCheckPredicate* x) { /* nothing to do */ };169#ifdef ASSERT170void do_Assert (Assert* x) { /* nothing to do */ };171#endif172};173174#ifdef ASSERT175void add_assertions(Bound *bound, Instruction *instruction, Instruction *position);176#endif177178define_array(BoundArray, Bound *)179define_stack(BoundStack, BoundArray)180define_array(BoundMap, BoundStack *)181define_array(AccessIndexedArray, AccessIndexed *)182define_stack(AccessIndexedList, AccessIndexedArray)183define_array(InstructionArray, Instruction *)184define_stack(InstructionList, InstructionArray)185186class AccessIndexedInfo : public CompilationResourceObj {187public:188AccessIndexedList *_list;189int _min;190int _max;191};192193define_array(AccessIndexedInfoArray, AccessIndexedInfo *)194BoundMap _bounds; // Mapping from Instruction's id to current bound195AccessIndexedInfoArray _access_indexed_info; // Mapping from Instruction's id to AccessIndexedInfo for in block motion196Visitor _visitor;197198public:199RangeCheckEliminator(IR *ir);200201IR *ir() const { return _ir; }202203// Pass over the dominator tree to identify blocks where there's an oppportunity for optimization204bool set_process_block_flags(BlockBegin *block);205// The core of the optimization work: pass over the dominator tree206// to propagate bound information, insert predicate out of loops,207// eliminate bound checks when possible and perform in block motion208void calc_bounds(BlockBegin *block, BlockBegin *loop_header);209// reorder bound checks within a block in order to eliminate some of them210void in_block_motion(BlockBegin *block, AccessIndexedList &accessIndexed, InstructionList &arrays);211212// update/access current bound213void update_bound(IntegerStack &pushed, Value v, Instruction::Condition cond, Value value, int constant);214void update_bound(IntegerStack &pushed, Value v, Bound *bound);215Bound *get_bound(Value v);216217bool loop_invariant(BlockBegin *loop_header, Instruction *instruction); // check for loop invariance218void add_access_indexed_info(InstructionList &indices, int i, Value instruction, AccessIndexed *ai); // record indexed access for in block motion219void remove_range_check(AccessIndexed *ai); // Mark this instructions as not needing a range check220void add_if_condition(IntegerStack &pushed, Value x, Value y, Instruction::Condition condition); // Update bound for an If221bool in_array_bound(Bound *bound, Value array); // Check whether bound is known to fall within array222223// helper functions to work with predicates224Instruction* insert_after(Instruction* insert_position, Instruction* instr, int bci);225Instruction* predicate(Instruction* left, Instruction::Condition cond, Instruction* right, ValueStack* state, Instruction *insert_position, int bci=-1);226Instruction* predicate_cmp_with_const(Instruction* instr, Instruction::Condition cond, int constant, ValueStack* state, Instruction *insert_position, int bci=1);227Instruction* predicate_add(Instruction* left, int left_const, Instruction::Condition cond, Instruction* right, ValueStack* state, Instruction *insert_position, int bci=-1);228Instruction* predicate_add_cmp_with_const(Instruction* left, int left_const, Instruction::Condition cond, int constant, ValueStack* state, Instruction *insert_position, int bci=-1);229230void insert_deoptimization(ValueStack *state, Instruction *insert_position, Instruction *array_instr, // Add predicate231Instruction *length_instruction, Instruction *lower_instr, int lower,232Instruction *upper_instr, int upper, AccessIndexed *ai);233bool is_ok_for_deoptimization(Instruction *insert_position, Instruction *array_instr, // Can we safely add a predicate?234Instruction *length_instr, Instruction *lower_instr,235int lower, Instruction *upper_instr, int upper);236void process_if(IntegerStack &pushed, BlockBegin *block, If *cond); // process If Instruction237void process_access_indexed(BlockBegin *loop_header, BlockBegin *block, AccessIndexed *ai); // process indexed access238239void dump_condition_stack(BlockBegin *cur_block);240static void print_statistics();241};242243#endif // SHARE_VM_C1_C1_RANGECHECKELIMINATION_HPP244245246