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