Tue, 08 Aug 2017 15:57:29 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #ifndef SHARE_VM_C1_C1_RANGECHECKELIMINATION_HPP |
aoqi@0 | 26 | #define SHARE_VM_C1_C1_RANGECHECKELIMINATION_HPP |
aoqi@0 | 27 | |
aoqi@0 | 28 | #include "c1/c1_Instruction.hpp" |
aoqi@0 | 29 | |
aoqi@0 | 30 | // Base class for range check elimination |
aoqi@0 | 31 | class RangeCheckElimination : AllStatic { |
aoqi@0 | 32 | public: |
aoqi@0 | 33 | static void eliminate(IR *ir); |
aoqi@0 | 34 | }; |
aoqi@0 | 35 | |
aoqi@0 | 36 | // Implementation |
aoqi@0 | 37 | class RangeCheckEliminator VALUE_OBJ_CLASS_SPEC { |
aoqi@0 | 38 | private: |
aoqi@0 | 39 | int _number_of_instructions; |
aoqi@0 | 40 | bool _optimistic; // Insert predicates and deoptimize when they fail |
aoqi@0 | 41 | IR *_ir; |
aoqi@0 | 42 | |
aoqi@0 | 43 | define_array(BlockBeginArray, BlockBegin*) |
aoqi@0 | 44 | define_stack(BlockBeginList, BlockBeginArray) |
aoqi@0 | 45 | define_stack(IntegerStack, intArray) |
aoqi@0 | 46 | define_array(IntegerMap, IntegerStack*) |
aoqi@0 | 47 | |
aoqi@0 | 48 | class Verification : public _ValueObj /*VALUE_OBJ_CLASS_SPEC*/, public BlockClosure { |
aoqi@0 | 49 | private: |
aoqi@0 | 50 | IR *_ir; |
aoqi@0 | 51 | boolArray _used; |
aoqi@0 | 52 | BlockBeginList _current; |
aoqi@0 | 53 | BlockBeginList _successors; |
aoqi@0 | 54 | |
aoqi@0 | 55 | public: |
aoqi@0 | 56 | Verification(IR *ir); |
aoqi@0 | 57 | virtual void block_do(BlockBegin *block); |
aoqi@0 | 58 | bool can_reach(BlockBegin *start, BlockBegin *end, BlockBegin *dont_use = NULL); |
aoqi@0 | 59 | bool dominates(BlockBegin *dominator, BlockBegin *block); |
aoqi@0 | 60 | }; |
aoqi@0 | 61 | |
aoqi@0 | 62 | public: |
aoqi@0 | 63 | // Bounds for an instruction in the form x + c which c integer |
aoqi@0 | 64 | // constant and x another instruction |
aoqi@0 | 65 | class Bound : public CompilationResourceObj { |
aoqi@0 | 66 | private: |
aoqi@0 | 67 | int _upper; |
aoqi@0 | 68 | Value _upper_instr; |
aoqi@0 | 69 | int _lower; |
aoqi@0 | 70 | Value _lower_instr; |
aoqi@0 | 71 | |
aoqi@0 | 72 | public: |
aoqi@0 | 73 | Bound(); |
aoqi@0 | 74 | Bound(Value v); |
aoqi@0 | 75 | Bound(Instruction::Condition cond, Value v, int constant = 0); |
aoqi@0 | 76 | Bound(int lower, Value lower_instr, int upper, Value upper_instr); |
aoqi@0 | 77 | ~Bound(); |
aoqi@0 | 78 | |
aoqi@0 | 79 | #ifdef ASSERT |
aoqi@0 | 80 | void add_assertion(Instruction *instruction, Instruction *position, int i, Value instr, Instruction::Condition cond); |
aoqi@0 | 81 | #endif |
aoqi@0 | 82 | int upper(); |
aoqi@0 | 83 | Value upper_instr(); |
aoqi@0 | 84 | int lower(); |
aoqi@0 | 85 | Value lower_instr(); |
aoqi@0 | 86 | void print(); |
aoqi@0 | 87 | bool check_no_overflow(int const_value); |
aoqi@0 | 88 | void or_op(Bound *b); |
aoqi@0 | 89 | void and_op(Bound *b); |
aoqi@0 | 90 | bool has_upper(); |
aoqi@0 | 91 | bool has_lower(); |
aoqi@0 | 92 | void set_upper(int upper, Value upper_instr); |
aoqi@0 | 93 | void set_lower(int lower, Value lower_instr); |
aoqi@0 | 94 | bool is_smaller(Bound *b); |
aoqi@0 | 95 | void remove_upper(); |
aoqi@0 | 96 | void remove_lower(); |
aoqi@0 | 97 | void add_constant(int value); |
aoqi@0 | 98 | Bound *copy(); |
aoqi@0 | 99 | |
aoqi@0 | 100 | private: |
aoqi@0 | 101 | void init(); |
aoqi@0 | 102 | }; |
aoqi@0 | 103 | |
aoqi@0 | 104 | |
aoqi@0 | 105 | class Visitor : public InstructionVisitor { |
aoqi@0 | 106 | private: |
aoqi@0 | 107 | Bound *_bound; |
aoqi@0 | 108 | RangeCheckEliminator *_rce; |
aoqi@0 | 109 | |
aoqi@0 | 110 | public: |
aoqi@0 | 111 | void set_range_check_eliminator(RangeCheckEliminator *rce) { _rce = rce; } |
aoqi@0 | 112 | Bound *bound() const { return _bound; } |
aoqi@0 | 113 | void clear_bound() { _bound = NULL; } |
aoqi@0 | 114 | |
aoqi@0 | 115 | protected: |
aoqi@0 | 116 | // visitor functions |
aoqi@0 | 117 | void do_Constant (Constant* x); |
aoqi@0 | 118 | void do_IfOp (IfOp* x); |
aoqi@0 | 119 | void do_LogicOp (LogicOp* x); |
aoqi@0 | 120 | void do_ArithmeticOp (ArithmeticOp* x); |
aoqi@0 | 121 | void do_Phi (Phi* x); |
aoqi@0 | 122 | |
aoqi@0 | 123 | void do_StoreField (StoreField* x) { /* nothing to do */ }; |
aoqi@0 | 124 | void do_StoreIndexed (StoreIndexed* x) { /* nothing to do */ }; |
aoqi@0 | 125 | void do_MonitorEnter (MonitorEnter* x) { /* nothing to do */ }; |
aoqi@0 | 126 | void do_MonitorExit (MonitorExit* x) { /* nothing to do */ }; |
aoqi@0 | 127 | void do_Invoke (Invoke* x) { /* nothing to do */ }; |
aoqi@0 | 128 | void do_UnsafePutRaw (UnsafePutRaw* x) { /* nothing to do */ }; |
aoqi@0 | 129 | void do_UnsafePutObject(UnsafePutObject* x) { /* nothing to do */ }; |
aoqi@0 | 130 | void do_Intrinsic (Intrinsic* x) { /* nothing to do */ }; |
aoqi@0 | 131 | void do_Local (Local* x) { /* nothing to do */ }; |
aoqi@0 | 132 | void do_LoadField (LoadField* x) { /* nothing to do */ }; |
aoqi@0 | 133 | void do_ArrayLength (ArrayLength* x) { /* nothing to do */ }; |
aoqi@0 | 134 | void do_LoadIndexed (LoadIndexed* x) { /* nothing to do */ }; |
aoqi@0 | 135 | void do_NegateOp (NegateOp* x) { /* nothing to do */ }; |
aoqi@0 | 136 | void do_ShiftOp (ShiftOp* x) { /* nothing to do */ }; |
aoqi@0 | 137 | void do_CompareOp (CompareOp* x) { /* nothing to do */ }; |
aoqi@0 | 138 | void do_Convert (Convert* x) { /* nothing to do */ }; |
aoqi@0 | 139 | void do_NullCheck (NullCheck* x) { /* nothing to do */ }; |
aoqi@0 | 140 | void do_TypeCast (TypeCast* x) { /* nothing to do */ }; |
aoqi@0 | 141 | void do_NewInstance (NewInstance* x) { /* nothing to do */ }; |
aoqi@0 | 142 | void do_NewTypeArray (NewTypeArray* x) { /* nothing to do */ }; |
aoqi@0 | 143 | void do_NewObjectArray (NewObjectArray* x) { /* nothing to do */ }; |
aoqi@0 | 144 | void do_NewMultiArray (NewMultiArray* x) { /* nothing to do */ }; |
aoqi@0 | 145 | void do_CheckCast (CheckCast* x) { /* nothing to do */ }; |
aoqi@0 | 146 | void do_InstanceOf (InstanceOf* x) { /* nothing to do */ }; |
aoqi@0 | 147 | void do_BlockBegin (BlockBegin* x) { /* nothing to do */ }; |
aoqi@0 | 148 | void do_Goto (Goto* x) { /* nothing to do */ }; |
aoqi@0 | 149 | void do_If (If* x) { /* nothing to do */ }; |
aoqi@0 | 150 | void do_IfInstanceOf (IfInstanceOf* x) { /* nothing to do */ }; |
aoqi@0 | 151 | void do_TableSwitch (TableSwitch* x) { /* nothing to do */ }; |
aoqi@0 | 152 | void do_LookupSwitch (LookupSwitch* x) { /* nothing to do */ }; |
aoqi@0 | 153 | void do_Return (Return* x) { /* nothing to do */ }; |
aoqi@0 | 154 | void do_Throw (Throw* x) { /* nothing to do */ }; |
aoqi@0 | 155 | void do_Base (Base* x) { /* nothing to do */ }; |
aoqi@0 | 156 | void do_OsrEntry (OsrEntry* x) { /* nothing to do */ }; |
aoqi@0 | 157 | void do_ExceptionObject(ExceptionObject* x) { /* nothing to do */ }; |
aoqi@0 | 158 | void do_RoundFP (RoundFP* x) { /* nothing to do */ }; |
aoqi@0 | 159 | void do_UnsafeGetRaw (UnsafeGetRaw* x) { /* nothing to do */ }; |
aoqi@0 | 160 | void do_UnsafeGetObject(UnsafeGetObject* x) { /* nothing to do */ }; |
aoqi@0 | 161 | void do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x) { /* nothing to do */ }; |
aoqi@0 | 162 | void do_UnsafePrefetchRead (UnsafePrefetchRead* x) { /* nothing to do */ }; |
aoqi@0 | 163 | void do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) { /* nothing to do */ }; |
aoqi@0 | 164 | void do_ProfileCall (ProfileCall* x) { /* nothing to do */ }; |
aoqi@0 | 165 | void do_ProfileReturnType (ProfileReturnType* x) { /* nothing to do */ }; |
aoqi@0 | 166 | void do_ProfileInvoke (ProfileInvoke* x) { /* nothing to do */ }; |
aoqi@0 | 167 | void do_RuntimeCall (RuntimeCall* x) { /* nothing to do */ }; |
aoqi@0 | 168 | void do_MemBar (MemBar* x) { /* nothing to do */ }; |
aoqi@0 | 169 | void do_RangeCheckPredicate(RangeCheckPredicate* x) { /* nothing to do */ }; |
aoqi@0 | 170 | #ifdef ASSERT |
aoqi@0 | 171 | void do_Assert (Assert* x) { /* nothing to do */ }; |
aoqi@0 | 172 | #endif |
aoqi@0 | 173 | }; |
aoqi@0 | 174 | |
aoqi@0 | 175 | #ifdef ASSERT |
aoqi@0 | 176 | void add_assertions(Bound *bound, Instruction *instruction, Instruction *position); |
aoqi@0 | 177 | #endif |
aoqi@0 | 178 | |
aoqi@0 | 179 | define_array(BoundArray, Bound *) |
aoqi@0 | 180 | define_stack(BoundStack, BoundArray) |
aoqi@0 | 181 | define_array(BoundMap, BoundStack *) |
aoqi@0 | 182 | define_array(AccessIndexedArray, AccessIndexed *) |
aoqi@0 | 183 | define_stack(AccessIndexedList, AccessIndexedArray) |
aoqi@0 | 184 | define_array(InstructionArray, Instruction *) |
aoqi@0 | 185 | define_stack(InstructionList, InstructionArray) |
aoqi@0 | 186 | |
aoqi@0 | 187 | class AccessIndexedInfo : public CompilationResourceObj { |
aoqi@0 | 188 | public: |
aoqi@0 | 189 | AccessIndexedList *_list; |
aoqi@0 | 190 | int _min; |
aoqi@0 | 191 | int _max; |
aoqi@0 | 192 | }; |
aoqi@0 | 193 | |
aoqi@0 | 194 | define_array(AccessIndexedInfoArray, AccessIndexedInfo *) |
aoqi@0 | 195 | BoundMap _bounds; // Mapping from Instruction's id to current bound |
aoqi@0 | 196 | AccessIndexedInfoArray _access_indexed_info; // Mapping from Instruction's id to AccessIndexedInfo for in block motion |
aoqi@0 | 197 | Visitor _visitor; |
aoqi@0 | 198 | |
aoqi@0 | 199 | public: |
aoqi@0 | 200 | RangeCheckEliminator(IR *ir); |
aoqi@0 | 201 | |
aoqi@0 | 202 | IR *ir() const { return _ir; } |
aoqi@0 | 203 | |
aoqi@0 | 204 | // Pass over the dominator tree to identify blocks where there's an oppportunity for optimization |
aoqi@0 | 205 | bool set_process_block_flags(BlockBegin *block); |
aoqi@0 | 206 | // The core of the optimization work: pass over the dominator tree |
aoqi@0 | 207 | // to propagate bound information, insert predicate out of loops, |
aoqi@0 | 208 | // eliminate bound checks when possible and perform in block motion |
aoqi@0 | 209 | void calc_bounds(BlockBegin *block, BlockBegin *loop_header); |
aoqi@0 | 210 | // reorder bound checks within a block in order to eliminate some of them |
aoqi@0 | 211 | void in_block_motion(BlockBegin *block, AccessIndexedList &accessIndexed, InstructionList &arrays); |
aoqi@0 | 212 | |
aoqi@0 | 213 | // update/access current bound |
aoqi@0 | 214 | void update_bound(IntegerStack &pushed, Value v, Instruction::Condition cond, Value value, int constant); |
aoqi@0 | 215 | void update_bound(IntegerStack &pushed, Value v, Bound *bound); |
aoqi@0 | 216 | Bound *get_bound(Value v); |
aoqi@0 | 217 | |
aoqi@0 | 218 | bool loop_invariant(BlockBegin *loop_header, Instruction *instruction); // check for loop invariance |
aoqi@0 | 219 | void add_access_indexed_info(InstructionList &indices, int i, Value instruction, AccessIndexed *ai); // record indexed access for in block motion |
aoqi@0 | 220 | void remove_range_check(AccessIndexed *ai); // Mark this instructions as not needing a range check |
aoqi@0 | 221 | void add_if_condition(IntegerStack &pushed, Value x, Value y, Instruction::Condition condition); // Update bound for an If |
aoqi@0 | 222 | bool in_array_bound(Bound *bound, Value array); // Check whether bound is known to fall within array |
aoqi@0 | 223 | |
aoqi@0 | 224 | // helper functions to work with predicates |
aoqi@0 | 225 | Instruction* insert_after(Instruction* insert_position, Instruction* instr, int bci); |
aoqi@0 | 226 | Instruction* predicate(Instruction* left, Instruction::Condition cond, Instruction* right, ValueStack* state, Instruction *insert_position, int bci=-1); |
aoqi@0 | 227 | Instruction* predicate_cmp_with_const(Instruction* instr, Instruction::Condition cond, int constant, ValueStack* state, Instruction *insert_position, int bci=1); |
aoqi@0 | 228 | Instruction* predicate_add(Instruction* left, int left_const, Instruction::Condition cond, Instruction* right, ValueStack* state, Instruction *insert_position, int bci=-1); |
aoqi@0 | 229 | Instruction* predicate_add_cmp_with_const(Instruction* left, int left_const, Instruction::Condition cond, int constant, ValueStack* state, Instruction *insert_position, int bci=-1); |
aoqi@0 | 230 | |
aoqi@0 | 231 | void insert_deoptimization(ValueStack *state, Instruction *insert_position, Instruction *array_instr, // Add predicate |
aoqi@0 | 232 | Instruction *length_instruction, Instruction *lower_instr, int lower, |
aoqi@0 | 233 | Instruction *upper_instr, int upper, AccessIndexed *ai); |
aoqi@0 | 234 | bool is_ok_for_deoptimization(Instruction *insert_position, Instruction *array_instr, // Can we safely add a predicate? |
aoqi@0 | 235 | Instruction *length_instr, Instruction *lower_instr, |
aoqi@0 | 236 | int lower, Instruction *upper_instr, int upper); |
aoqi@0 | 237 | void process_if(IntegerStack &pushed, BlockBegin *block, If *cond); // process If Instruction |
aoqi@0 | 238 | void process_access_indexed(BlockBegin *loop_header, BlockBegin *block, AccessIndexed *ai); // process indexed access |
aoqi@0 | 239 | |
aoqi@0 | 240 | void dump_condition_stack(BlockBegin *cur_block); |
aoqi@0 | 241 | static void print_statistics(); |
aoqi@0 | 242 | }; |
aoqi@0 | 243 | |
aoqi@0 | 244 | #endif // SHARE_VM_C1_C1_RANGECHECKELIMINATION_HPP |