Path: blob/master/src/hotspot/share/c1/c1_RangeCheckElimination.hpp
40930 views
/*1* Copyright (c) 2012, 2019, 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_C1_C1_RANGECHECKELIMINATION_HPP25#define SHARE_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 {37private:38int _number_of_instructions;39bool _optimistic; // Insert predicates and deoptimize when they fail40IR *_ir;4142typedef GrowableArray<BlockBegin*> BlockBeginList;43typedef GrowableArray<int> IntegerStack;4445#ifdef ASSERT46class Verification : public BlockClosure {47// RangeCheckEliminator::Verification should never get instatiated on the heap.48private:49void* operator new(size_t size) throw();50void* operator new[](size_t size) throw();51void operator delete(void* p) { ShouldNotReachHere(); }52void operator delete[](void* p) { ShouldNotReachHere(); }5354bool can_reach(BlockBegin *start, BlockBegin *end, BlockBegin *dont_use = NULL);55bool dominates(BlockBegin *dominator, BlockBegin *block);56bool is_backbranch_from_xhandler(BlockBegin* block);5758IR *_ir;59boolArray _used;60BlockBeginList _current;61BlockBeginList _successors;6263public:64Verification(IR *ir);65virtual void block_do(BlockBegin *block);66};67#endif6869public:70// Bounds for an instruction in the form x + c which c integer71// constant and x another instruction72class Bound : public CompilationResourceObj {73private:74int _upper;75Value _upper_instr;76int _lower;77Value _lower_instr;7879public:80Bound();81Bound(Value v);82Bound(Instruction::Condition cond, Value v, int constant = 0);83Bound(int lower, Value lower_instr, int upper, Value upper_instr);84~Bound();8586#ifdef ASSERT87void add_assertion(Instruction *instruction, Instruction *position, int i, Value instr, Instruction::Condition cond);88#endif89int upper();90Value upper_instr();91int lower();92Value lower_instr();93void print();94bool check_no_overflow(int const_value);95void or_op(Bound *b);96void and_op(Bound *b);97bool has_upper();98bool has_lower();99void set_upper(int upper, Value upper_instr);100void set_lower(int lower, Value lower_instr);101bool is_smaller(Bound *b);102void remove_upper();103void remove_lower();104void add_constant(int value);105Bound *copy();106};107108109class Visitor : public InstructionVisitor {110private:111Bound *_bound;112RangeCheckEliminator *_rce;113114public:115void set_range_check_eliminator(RangeCheckEliminator *rce) { _rce = rce; }116Bound *bound() const { return _bound; }117void clear_bound() { _bound = NULL; }118119protected:120// visitor functions121void do_Constant (Constant* x);122void do_IfOp (IfOp* x);123void do_LogicOp (LogicOp* x);124void do_ArithmeticOp (ArithmeticOp* x);125void do_Phi (Phi* x);126127void do_StoreField (StoreField* x) { /* nothing to do */ };128void do_StoreIndexed (StoreIndexed* x) { /* nothing to do */ };129void do_MonitorEnter (MonitorEnter* x) { /* nothing to do */ };130void do_MonitorExit (MonitorExit* x) { /* nothing to do */ };131void do_Invoke (Invoke* x) { /* nothing to do */ };132void do_UnsafePutRaw (UnsafePutRaw* x) { /* nothing to do */ };133void do_UnsafePutObject(UnsafePutObject* x) { /* nothing to do */ };134void do_Intrinsic (Intrinsic* x) { /* nothing to do */ };135void do_Local (Local* x) { /* nothing to do */ };136void do_LoadField (LoadField* x) { /* nothing to do */ };137void do_ArrayLength (ArrayLength* x) { /* nothing to do */ };138void do_LoadIndexed (LoadIndexed* x) { /* nothing to do */ };139void do_NegateOp (NegateOp* x) { /* nothing to do */ };140void do_ShiftOp (ShiftOp* x) { /* nothing to do */ };141void do_CompareOp (CompareOp* x) { /* nothing to do */ };142void do_Convert (Convert* x) { /* nothing to do */ };143void do_NullCheck (NullCheck* x) { /* nothing to do */ };144void do_TypeCast (TypeCast* x) { /* nothing to do */ };145void do_NewInstance (NewInstance* x) { /* nothing to do */ };146void do_NewTypeArray (NewTypeArray* x) { /* nothing to do */ };147void do_NewObjectArray (NewObjectArray* x) { /* nothing to do */ };148void do_NewMultiArray (NewMultiArray* x) { /* nothing to do */ };149void do_CheckCast (CheckCast* x) { /* nothing to do */ };150void do_InstanceOf (InstanceOf* x) { /* nothing to do */ };151void do_BlockBegin (BlockBegin* x) { /* nothing to do */ };152void do_Goto (Goto* x) { /* nothing to do */ };153void do_If (If* x) { /* nothing to do */ };154void do_TableSwitch (TableSwitch* x) { /* nothing to do */ };155void do_LookupSwitch (LookupSwitch* x) { /* nothing to do */ };156void do_Return (Return* x) { /* nothing to do */ };157void do_Throw (Throw* x) { /* nothing to do */ };158void do_Base (Base* x) { /* nothing to do */ };159void do_OsrEntry (OsrEntry* x) { /* nothing to do */ };160void do_ExceptionObject(ExceptionObject* x) { /* nothing to do */ };161void do_RoundFP (RoundFP* x) { /* nothing to do */ };162void do_UnsafeGetRaw (UnsafeGetRaw* x) { /* nothing to do */ };163void do_UnsafeGetObject(UnsafeGetObject* x) { /* nothing to do */ };164void do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x) { /* nothing to do */ };165void do_ProfileCall (ProfileCall* x) { /* nothing to do */ };166void do_ProfileReturnType (ProfileReturnType* x) { /* nothing to do */ };167void do_ProfileInvoke (ProfileInvoke* x) { /* nothing to do */ };168void do_RuntimeCall (RuntimeCall* x) { /* nothing to do */ };169void do_MemBar (MemBar* x) { /* nothing to do */ };170void do_RangeCheckPredicate(RangeCheckPredicate* x) { /* nothing to do */ };171#ifdef ASSERT172void do_Assert (Assert* x) { /* nothing to do */ };173#endif174};175176#ifdef ASSERT177void add_assertions(Bound *bound, Instruction *instruction, Instruction *position);178#endif179180typedef GrowableArray<Bound*> BoundStack;181typedef GrowableArray<BoundStack*> BoundMap;182typedef GrowableArray<AccessIndexed*> AccessIndexedList;183typedef GrowableArray<Instruction*> InstructionList;184185class AccessIndexedInfo : public CompilationResourceObj {186public:187AccessIndexedList *_list;188int _min;189int _max;190};191192typedef GrowableArray<AccessIndexedInfo*> AccessIndexedInfoArray;193BoundMap _bounds; // Mapping from Instruction's id to current bound194AccessIndexedInfoArray _access_indexed_info; // Mapping from Instruction's id to AccessIndexedInfo for in block motion195Visitor _visitor;196197public:198RangeCheckEliminator(IR *ir);199200IR *ir() const { return _ir; }201202// Pass over the dominator tree to identify blocks where there's an oppportunity for optimization203bool set_process_block_flags(BlockBegin *block);204// The core of the optimization work: pass over the dominator tree205// to propagate bound information, insert predicate out of loops,206// eliminate bound checks when possible and perform in block motion207void calc_bounds(BlockBegin *block, BlockBegin *loop_header);208// reorder bound checks within a block in order to eliminate some of them209void in_block_motion(BlockBegin *block, AccessIndexedList &accessIndexed, InstructionList &arrays);210211// update/access current bound212void update_bound(IntegerStack &pushed, Value v, Instruction::Condition cond, Value value, int constant);213void update_bound(IntegerStack &pushed, Value v, Bound *bound);214Bound *get_bound(Value v);215216bool loop_invariant(BlockBegin *loop_header, Instruction *instruction); // check for loop invariance217void add_access_indexed_info(InstructionList &indices, int i, Value instruction, AccessIndexed *ai); // record indexed access for in block motion218void remove_range_check(AccessIndexed *ai); // Mark this instructions as not needing a range check219void add_if_condition(IntegerStack &pushed, Value x, Value y, Instruction::Condition condition); // Update bound for an If220bool in_array_bound(Bound *bound, Value array); // Check whether bound is known to fall within array221222// helper functions to work with predicates223Instruction* insert_after(Instruction* insert_position, Instruction* instr, int bci);224Instruction* predicate(Instruction* left, Instruction::Condition cond, Instruction* right, ValueStack* state, Instruction *insert_position, int bci=-1);225Instruction* predicate_cmp_with_const(Instruction* instr, Instruction::Condition cond, int constant, ValueStack* state, Instruction *insert_position, int bci=1);226Instruction* predicate_add(Instruction* left, int left_const, Instruction::Condition cond, Instruction* right, ValueStack* state, Instruction *insert_position, int bci=-1);227Instruction* predicate_add_cmp_with_const(Instruction* left, int left_const, Instruction::Condition cond, int constant, ValueStack* state, Instruction *insert_position, int bci=-1);228229void insert_deoptimization(ValueStack *state, Instruction *insert_position, Instruction *array_instr, // Add predicate230Instruction *length_instruction, Instruction *lower_instr, int lower,231Instruction *upper_instr, int upper, AccessIndexed *ai);232bool is_ok_for_deoptimization(Instruction *insert_position, Instruction *array_instr, // Can we safely add a predicate?233Instruction *length_instr, Instruction *lower_instr,234int lower, Instruction *upper_instr, int upper);235void process_if(IntegerStack &pushed, BlockBegin *block, If *cond); // process If Instruction236void process_access_indexed(BlockBegin *loop_header, BlockBegin *block, AccessIndexed *ai); // process indexed access237238void dump_condition_stack(BlockBegin *cur_block);239static void print_statistics();240};241242#endif // SHARE_C1_C1_RANGECHECKELIMINATION_HPP243244245