1 #ifdef USE_PRAGMA_IDENT_SRC
2 #pragma ident "@(#)bcEscapeAnalyzer.cpp 1.7 07/05/17 15:49:50 JVM"
3 #endif
4 /*
5 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 *
8 * This code is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 only, as
10 * published by the Free Software Foundation.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
24 * have any questions.
25 *
90 short _max_stack;
91 bool _initialized;
92 ArgumentMap empty_map;
93
94 StateInfo() {
95 empty_map.clear();
96 }
97
98 ArgumentMap raw_pop() { assert(_stack_height > 0, "stack underflow"); return _stack[--_stack_height]; }
99 ArgumentMap apop() { return raw_pop(); }
100 void spop() { raw_pop(); }
101 void lpop() { spop(); spop(); }
102 void raw_push(ArgumentMap i) { assert(_stack_height < _max_stack, "stack overflow"); _stack[_stack_height++] = i; }
103 void apush(ArgumentMap i) { raw_push(i); }
104 void spush() { raw_push(empty_map); }
105 void lpush() { spush(); spush(); }
106
107 };
108
109 void BCEscapeAnalyzer::set_returned(ArgumentMap vars) {
110 for (int i = 0; i <= _arg_size; i++) {
111 if (vars.contains(i))
112 _arg_returned.set_bit(i);
113 }
114 _return_local = _return_local && !(vars.contains_unknown() || vars.contains_allocated());
115 _return_allocated = _return_allocated && vars.contains_allocated() && !(vars.contains_unknown() || vars.contains_vars());
116 }
117
118
119 // return true if any element of vars is an argument
120 bool BCEscapeAnalyzer::is_argument(ArgumentMap vars) {
121 for (int i = 0; i <= _arg_size; i++) {
122 if (vars.contains(i))
123 return true;
124 }
125 return false;
126 }
127
128 // return true if any element of vars is an arg_stack argument
129 bool BCEscapeAnalyzer::is_arg_stack(ArgumentMap vars){
130 if (_conservative)
131 return true;
132 for (int i = 0; i <= _arg_size; i++) {
133 if (vars.contains(i) && _arg_stack.at(i))
134 return true;
135 }
136 return false;
137 }
138
139 void BCEscapeAnalyzer::clear_bits(ArgumentMap vars, BitMap &bm) {
140 for (int i = 0; i <= _arg_size; i++) {
141 if (vars.contains(i)) {
142 bm.clear_bit(i);
143 }
144 }
145 }
146 void BCEscapeAnalyzer::set_method_escape(ArgumentMap vars) {
147 clear_bits(vars, _arg_local);
148 }
149
150 void BCEscapeAnalyzer::set_global_escape(ArgumentMap vars) {
151 clear_bits(vars, _arg_local);
152 clear_bits(vars, _arg_stack);
153 if (vars.contains_allocated())
154 _allocated_escapes = true;
155 }
156
157 void BCEscapeAnalyzer::set_dirty(ArgumentMap vars) {
158 clear_bits(vars, _dirty);
159 }
160
161 bool BCEscapeAnalyzer::is_recursive_call(ciMethod* callee) {
162 for (BCEscapeAnalyzer* scope = this; scope != NULL; scope = scope->_parent) {
163 if (scope->method() == callee) {
164 return true;
165 }
166 }
167 return false;
168 }
169
170 void BCEscapeAnalyzer::invoke(StateInfo &state, Bytecodes::Code code, ciMethod* target, ciKlass* holder) {
171 int i;
172
173 // retrieve information about the callee
174 ciInstanceKlass* klass = target->holder();
175 ciInstanceKlass* calling_klass = method()->holder();
176 ciInstanceKlass* callee_holder = ciEnv::get_instance_klass_for_declared_method_holder(holder);
177 ciInstanceKlass* actual_recv = callee_holder;
178
179 // compute size of arguments
180 int arg_size = target->arg_size();
181 if (!target->is_loaded() && code == Bytecodes::_invokestatic) {
182 arg_size--;
183 }
184 int arg_base = MAX2(state._stack_height - arg_size, 0);
185
186 // direct recursive calls are skipped if they can be bound statically without introducing
187 // dependencies and if parameters are passed at the same position as in the current method
188 // other calls are skipped if there are no unescaped arguments passed to them
189 bool directly_recursive = (method() == target) &&
190 (code != Bytecodes::_invokevirtual || target->is_final_method() || state._stack[arg_base] .is_empty());
191
192 // check if analysis of callee can safely be skipped
193 bool skip_callee = true;
194 for (i = state._stack_height - 1; i >= arg_base && skip_callee; i--) {
195 ArgumentMap arg = state._stack[i];
196 skip_callee = !is_argument(arg) || !is_arg_stack(arg) || (directly_recursive && arg.is_singleton(i - arg_base));
197 }
198 if (skip_callee) {
199 TRACE_BCEA(3, tty->print_cr("[EA] skipping method %s::%s", holder->name()->as_utf8(), target->name()->as_utf8()));
200 for (i = 0; i < arg_size; i++) {
201 set_method_escape(state.raw_pop());
202 }
203 return;
204 }
205
206 // determine actual method (use CHA if necessary)
207 ciMethod* inline_target = NULL;
208 if (target->is_loaded() && klass->is_loaded()
209 && (klass->is_initialized() || klass->is_interface() && target->holder()->is_initialized())
210 && target->will_link(klass, callee_holder, code)) {
211 if (code == Bytecodes::_invokestatic
212 || code == Bytecodes::_invokespecial
213 || code == Bytecodes::_invokevirtual && target->is_final_method()) {
214 inline_target = target;
215 } else {
216 inline_target = target->find_monomorphic_target(calling_klass, callee_holder, actual_recv);
217 }
218 }
219
220 if (inline_target != NULL && !is_recursive_call(inline_target)) {
221 // analyze callee
222 BCEscapeAnalyzer analyzer(inline_target, this);
223
224 // adjust escape state of actual parameters
225 bool must_record_dependencies = false;
226 for (i = arg_size - 1; i >= 0; i--) {
227 ArgumentMap arg = state.raw_pop();
228 if (!is_argument(arg))
229 continue;
230 if (!is_arg_stack(arg)) {
231 // arguments have already been recognized as escaping
232 } else if (analyzer.is_arg_stack(i) && !analyzer.is_arg_returned(i)) {
233 set_method_escape(arg);
234 must_record_dependencies = true;
235 } else {
236 set_global_escape(arg);
237 }
238 }
239
240 // record dependencies if at least one parameter retained stack-allocatable
241 if (must_record_dependencies) {
242 if (code == Bytecodes::_invokeinterface || code == Bytecodes::_invokevirtual && !target->is_final_method()) {
243 _dependencies.append(actual_recv);
244 _dependencies.append(inline_target);
245 }
246 _dependencies.appendAll(analyzer.dependencies());
247 }
248 } else {
249 TRACE_BCEA(1, tty->print_cr("[EA] virtual method %s is not monomorphic.",
250 target->name()->as_utf8()));
251 // conservatively mark all actual parameters as escaping globally
252 for (i = 0; i < arg_size; i++) {
253 ArgumentMap arg = state.raw_pop();
254 if (!is_argument(arg))
255 continue;
256 set_global_escape(arg);
257 }
258 }
259 }
260
261 bool BCEscapeAnalyzer::contains(uint arg_set1, uint arg_set2) {
262 return ((~arg_set1) | arg_set2) == 0;
263 }
264
265
266 void BCEscapeAnalyzer::iterate_one_block(ciBlock *blk, StateInfo &state, GrowableArray<ciBlock *> &successors) {
267
268 blk->set_processed();
269 ciBytecodeStream s(method());
270 int limit_bci = blk->limit_bci();
271 bool fall_through = false;
272 ArgumentMap allocated_obj;
273 allocated_obj.add_allocated();
274 ArgumentMap unknown_obj;
275 unknown_obj.add_unknown();
276 ArgumentMap empty_map;
277
407 break;
408 case Bytecodes::_astore_1:
409 state._vars[1] = state.apop();
410 break;
411 case Bytecodes::_astore_2:
412 state._vars[2] = state.apop();
413 break;
414 case Bytecodes::_astore_3:
415 state._vars[3] = state.apop();
416 break;
417 case Bytecodes::_iastore:
418 case Bytecodes::_fastore:
419 case Bytecodes::_bastore:
420 case Bytecodes::_castore:
421 case Bytecodes::_sastore:
422 {
423 state.spop();
424 state.spop();
425 ArgumentMap arr = state.apop();
426 set_method_escape(arr);
427 break;
428 }
429 case Bytecodes::_lastore:
430 case Bytecodes::_dastore:
431 {
432 state.lpop();
433 state.spop();
434 ArgumentMap arr = state.apop();
435 set_method_escape(arr);
436 break;
437 }
438 case Bytecodes::_aastore:
439 {
440 set_global_escape(state.apop());
441 state.spop();
442 ArgumentMap arr = state.apop();
443 break;
444 }
445 case Bytecodes::_pop:
446 state.raw_pop();
447 break;
448 case Bytecodes::_pop2:
449 state.raw_pop();
450 state.raw_pop();
451 break;
452 case Bytecodes::_dup:
453 { ArgumentMap w1 = state.raw_pop();
454 state.raw_push(w1);
455 state.raw_push(w1);
456 }
457 break;
458 case Bytecodes::_dup_x1:
459 { ArgumentMap w1 = state.raw_pop();
460 ArgumentMap w2 = state.raw_pop();
461 state.raw_push(w1);
462 state.raw_push(w2);
748 } else {
749 state.lpush();
750 }
751 }
752 break;
753 case Bytecodes::_putstatic:
754 case Bytecodes::_putfield:
755 { bool will_link;
756 ciField* field = s.get_field(will_link);
757 BasicType field_type = field->type()->basic_type();
758 if (field_type == T_OBJECT || field_type == T_ARRAY) {
759 set_global_escape(state.apop());
760 } else if (type2size[field_type] == 1) {
761 state.spop();
762 } else {
763 state.lpop();
764 }
765 if (s.cur_bc() != Bytecodes::_putstatic) {
766 ArgumentMap p = state.apop();
767 set_method_escape(p);
768 }
769 }
770 break;
771 case Bytecodes::_invokevirtual:
772 case Bytecodes::_invokespecial:
773 case Bytecodes::_invokestatic:
774 case Bytecodes::_invokeinterface:
775 { bool will_link;
776 ciMethod* target = s.get_method(will_link);
777 ciKlass* holder = s.get_declared_method_holder();
778 invoke(state, s.cur_bc(), target, holder);
779 ciType* return_type = target->return_type();
780 if (!return_type->is_primitive_type()) {
781 state.apush(unknown_obj);
782 } else if (return_type->is_one_word()) {
783 state.spush();
784 } else if (return_type->is_two_word()) {
785 state.lpush();
786 }
787 }
858 break;
859 }
860 case Bytecodes::_breakpoint:
861 break;
862 default:
863 ShouldNotReachHere();
864 break;
865 }
866
867 }
868 if (fall_through) {
869 int fall_through_bci = s.cur_bci();
870 if (fall_through_bci < _method->code_size()) {
871 assert(_methodBlocks->is_block_start(fall_through_bci), "must fall through to block start.");
872 successors.push(_methodBlocks->block_containing(fall_through_bci));
873 }
874 }
875 }
876
877 void BCEscapeAnalyzer::merge_block_states(StateInfo *blockstates, ciBlock *dest, StateInfo *s_state) {
878 StateInfo *d_state = blockstates+dest->index();
879 int nlocals = _method->max_locals();
880
881 // exceptions may cause transfer of control to handlers in the middle of a
882 // block, so we don't merge the incoming state of exception handlers
883 if (dest->is_handler())
884 return;
885 if (!d_state->_initialized ) {
886 // destination not initialized, just copy
887 for (int i = 0; i < nlocals; i++) {
888 d_state->_vars[i] = s_state->_vars[i];
889 }
890 for (int i = 0; i < s_state->_stack_height; i++) {
891 d_state->_stack[i] = s_state->_stack[i];
892 }
893 d_state->_stack_height = s_state->_stack_height;
894 d_state->_max_stack = s_state->_max_stack;
895 d_state->_initialized = true;
896 } else if (!dest->processed()) {
897 // we have not yet walked the bytecodes of dest, we can merge
898 // the states
902 }
903 for (int i = 0; i < s_state->_stack_height; i++) {
904 d_state->_stack[i].set_union(s_state->_stack[i]);
905 }
906 } else {
907 // the bytecodes of dest have already been processed, mark any
908 // arguments in the source state which are not in the dest state
909 // as global escape.
910 // Future refinement: we only need to mark these variable to the
911 // maximum escape of any variables in dest state
912 assert(d_state->_stack_height == s_state->_stack_height, "computed stack heights must match");
913 ArgumentMap extra_vars;
914 for (int i = 0; i < nlocals; i++) {
915 ArgumentMap t;
916 t = s_state->_vars[i];
917 t.set_difference(d_state->_vars[i]);
918 extra_vars.set_union(t);
919 }
920 for (int i = 0; i < s_state->_stack_height; i++) {
921 ArgumentMap t;
922 t.clear();
923 t = s_state->_stack[i];
924 t.set_difference(d_state->_stack[i]);
925 extra_vars.set_union(t);
926 }
927 set_global_escape(extra_vars);
928 }
929 }
930
931 void BCEscapeAnalyzer::iterate_blocks(Arena *arena) {
932 int numblocks = _methodBlocks->num_blocks();
933 int stkSize = _method->max_stack();
934 int numLocals = _method->max_locals();
935 StateInfo state;
936
937 int datacount = (numblocks + 1) * (stkSize + numLocals);
938 int datasize = datacount * sizeof(ArgumentMap);
939 StateInfo *blockstates = (StateInfo *) arena->Amalloc(_methodBlocks->num_blocks() * sizeof(StateInfo));
940 ArgumentMap *statedata = (ArgumentMap *) arena->Amalloc(datasize);
941 for (int i = 0; i < datacount; i++) ::new ((void*)&statedata[i]) ArgumentMap();
942 ArgumentMap *dp = statedata;
943 state._vars = dp;
944 dp += numLocals;
945 state._stack = dp;
946 dp += stkSize;
947 state._initialized = false;
948 state._max_stack = stkSize;
949 for (int i = 0; i < numblocks; i++) {
950 blockstates[i]._vars = dp;
951 dp += numLocals;
952 blockstates[i]._stack = dp;
953 dp += stkSize;
954 blockstates[i]._initialized = false;
955 blockstates[i]._stack_height = 0;
956 blockstates[i]._max_stack = stkSize;
957 }
958 GrowableArray<ciBlock *> worklist(arena, numblocks / 4, 0, NULL);
959 GrowableArray<ciBlock *> successors(arena, 4, 0, NULL);
960
961 _methodBlocks->clear_processed();
962
963 // initialize block 0 state from method signature
964 ArgumentMap allVars; // all oop arguments to method
965 ciSignature* sig = method()->signature();
966 int j = 0;
967 if (!method()->is_static()) {
968 // record information for "this"
969 blockstates[0]._vars[j].set(j);
970 allVars.add(j);
971 j++;
972 }
973 for (int i = 0; i < sig->count(); i++) {
974 ciType* t = sig->type_at(i);
975 if (!t->is_primitive_type()) {
976 blockstates[0]._vars[j].set(j);
977 allVars.add(j);
978 }
979 j += t->size();
980 }
981 blockstates[0]._initialized = true;
982 assert(j == _arg_size, "just checking");
983
984 ArgumentMap unknown_map;
985 unknown_map.add_unknown();
986
987 worklist.push(_methodBlocks->block_containing(0));
988 while(worklist.length() > 0) {
989 ciBlock *blk = worklist.pop();
990 StateInfo *blkState = blockstates+blk->index();
991 if (blk->is_handler() || blk->is_ret_target()) {
992 // for an exception handler or a target of a ret instruction, we assume the worst case,
993 // that any variable or stack slot could contain any argument
994 for (int i = 0; i < numLocals; i++) {
995 state._vars[i] = allVars;
996 }
997 if (blk->is_handler()) {
998 state._stack_height = 1;
999 } else {
1000 state._stack_height = blkState->_stack_height;
1001 }
1002 for (int i = 0; i < state._stack_height; i++) {
1003 state._stack[i] = allVars;
1004 }
1005 } else {
1006 for (int i = 0; i < numLocals; i++) {
1007 state._vars[i] = blkState->_vars[i];
1008 }
1009 for (int i = 0; i < blkState->_stack_height; i++) {
1010 state._stack[i] = blkState->_stack[i];
1011 }
1012 state._stack_height = blkState->_stack_height;
1013 }
1014 iterate_one_block(blk, state, successors);
1015 // if this block has any exception handlers, push them
1016 // onto successor list
1017 if (blk->has_handler()) {
1018 DEBUG_ONLY(int handler_count = 0;)
1019 int blk_start = blk->start_bci();
1020 int blk_end = blk->limit_bci();
1021 for (int i = 0; i < numblocks; i++) {
1022 ciBlock *b = _methodBlocks->block(i);
1039 if (!succ->processed())
1040 worklist.push(succ);
1041 }
1042 }
1043 }
1044
1045 bool BCEscapeAnalyzer::do_analysis() {
1046 Arena* arena = CURRENT_ENV->arena();
1047 // identify basic blocks
1048 _methodBlocks = _method->get_method_blocks();
1049
1050 iterate_blocks(arena);
1051 // TEMPORARY
1052 return true;
1053 }
1054
1055 vmIntrinsics::ID BCEscapeAnalyzer::known_intrinsic() {
1056 vmIntrinsics::ID iid = method()->intrinsic_id();
1057
1058 if (iid == vmIntrinsics::_getClass ||
1059 iid == vmIntrinsics::_hashCode)
1060 return iid;
1061 else
1062 return vmIntrinsics::_none;
1063 }
1064
1065 bool BCEscapeAnalyzer::compute_escape_for_intrinsic(vmIntrinsics::ID iid) {
1066 ArgumentMap empty;
1067 empty.clear();
1068 switch (iid) {
1069 case vmIntrinsics::_getClass:
1070 _return_local = false;
1071 break;
1072 case vmIntrinsics::_hashCode:
1073 // initialized state is correct
1074 break;
1075 default:
1076 assert(false, "unexpected intrinsic");
1077 }
1078 return true;
1079 }
1080
1081 void BCEscapeAnalyzer::initialize() {
1082 int i;
1083
1084 // clear escape information (method may have been deoptimized)
1085 methodData()->clear_escape_info();
1086
1087 // initialize escape state of object parameters
1088 ciSignature* sig = method()->signature();
1089 int j = 0;
1090 if (!method()->is_static()) {
1091 _arg_local.set_bit(0);
1095 for (i = 0; i < sig->count(); i++) {
1096 ciType* t = sig->type_at(i);
1097 if (!t->is_primitive_type()) {
1098 _arg_local.set_bit(j);
1099 _arg_stack.set_bit(j);
1100 }
1101 j += t->size();
1102 }
1103 assert(j == _arg_size, "just checking");
1104
1105 // start with optimistic assumption
1106 ciType *rt = _method->return_type();
1107 if (rt->is_primitive_type()) {
1108 _return_local = false;
1109 _return_allocated = false;
1110 } else {
1111 _return_local = true;
1112 _return_allocated = true;
1113 }
1114 _allocated_escapes = false;
1115 }
1116
1117 void BCEscapeAnalyzer::clear_escape_info() {
1118 ciSignature* sig = method()->signature();
1119 int arg_count = sig->count();
1120 ArgumentMap var;
1121 for (int i = 0; i < arg_count; i++) {
1122 var.clear();
1123 var.set(i);
1124 set_global_escape(var);
1125 }
1126 _arg_local.clear();
1127 _arg_stack.clear();
1128 _arg_returned.clear();
1129 _return_local = false;
1130 _return_allocated = false;
1131 _allocated_escapes = true;
1132 }
1133
1134
1135 void BCEscapeAnalyzer::compute_escape_info() {
1136 int i;
1137 assert(!methodData()->has_escape_info(), "do not overwrite escape info");
1138
1139 vmIntrinsics::ID iid = known_intrinsic();
1140
1141 // check if method can be analyzed
1142 if (iid == vmIntrinsics::_none && (method()->is_abstract() || method()->is_native() || !method()->holder()->is_initialized()
1143 || _level > MaxBCEAEstimateLevel
1144 || method()->code_size() > MaxBCEAEstimateSize)) {
1145 if (BCEATraceLevel >= 1) {
1146 tty->print("Skipping method because: ");
1147 if (method()->is_abstract())
1148 tty->print_cr("method is abstract.");
1149 else if (method()->is_native())
1150 tty->print_cr("method is native.");
1151 else if (!method()->holder()->is_initialized())
1159 else
1160 ShouldNotReachHere();
1161 }
1162 clear_escape_info();
1163
1164 return;
1165 }
1166
1167 if (BCEATraceLevel >= 1) {
1168 tty->print("[EA] estimating escape information for");
1169 if (iid != vmIntrinsics::_none)
1170 tty->print(" intrinsic");
1171 method()->print_short_name();
1172 tty->print_cr(" (%d bytes)", method()->code_size());
1173 }
1174
1175 bool success;
1176
1177 initialize();
1178
1179 // do not scan method if it has no object parameters
1180 if (_arg_local.is_empty()) {
1181 methodData()->set_eflag(methodDataOopDesc::estimated);
1182 return;
1183 }
1184
1185 if (iid != vmIntrinsics::_none)
1186 success = compute_escape_for_intrinsic(iid);
1187 else {
1188 success = do_analysis();
1189 }
1190
1191 // dump result of bytecode analysis
1192 #ifndef PRODUCT
1193 if (BCEATraceLevel >= 3) {
1194 tty->print("[EA] estimated escape information for");
1195 if (iid != vmIntrinsics::_none)
1196 tty->print(" intrinsic");
1197 method()->print_short_name();
1198 tty->print_cr(has_dependencies() ? " (not stored)" : "");
1199 tty->print(" non-escaping args: ");
1200 _arg_local.print_on(tty);
1201 tty->print(" stack-allocatable args: ");
1202 _arg_stack.print_on(tty);
1203 if (_return_local) {
1204 tty->print(" returned args: ");
1205 _arg_returned.print_on(tty);
1206 } else if (is_return_allocated()) {
1207 tty->print_cr(" allocated return values");
1208 } else {
1209 tty->print_cr(" non-local return values");
1210 }
1211 tty->cr();
1212 tty->print(" flags: ");
1213 if (_return_allocated)
1214 tty->print(" return_allocated");
1215 tty->cr();
1216 }
1217
1218 #endif
1219 // don't store interprocedural escape information if it introduces dependencies
1220 // or if method data is empty
1221 //
1222 if (!has_dependencies() && !methodData()->is_empty()) {
1223 for (i = 0; i < _arg_size; i++) {
1224 if (_arg_local.at(i)) {
1225 assert(_arg_stack.at(i), "inconsistent escape info");
1226 methodData()->set_arg_local(i);
1227 methodData()->set_arg_stack(i);
1228 } else if (_arg_stack.at(i)) {
1229 methodData()->set_arg_stack(i);
1230 }
1231 if (_arg_returned.at(i)) {
1232 methodData()->set_arg_returned(i);
1233 }
1234 }
1235 if (_return_local) {
1236 methodData()->set_eflag(methodDataOopDesc::return_local);
1237 }
1238 methodData()->set_eflag(methodDataOopDesc::estimated);
1239 }
1240 }
1241
1242 void BCEscapeAnalyzer::read_escape_info() {
1243 assert(methodData()->has_escape_info(), "no escape info available");
1244
1245 // read escape information from method descriptor
1246 for (int i = 0; i < _arg_size; i++) {
1247 _arg_local.at_put(i, methodData()->is_arg_local(i));
1248 _arg_stack.at_put(i, methodData()->is_arg_stack(i));
1249 _arg_returned.at_put(i, methodData()->is_arg_returned(i));
1250 }
1251 _return_local = methodData()->eflag_set(methodDataOopDesc::return_local);
1252
1253 // dump result of loaded escape information
1254 #ifndef PRODUCT
1255 if (BCEATraceLevel >= 4) {
1256 tty->print(" non-escaping args: ");
1257 _arg_local.print_on(tty);
1258 tty->print(" stack-allocatable args: ");
1259 _arg_stack.print_on(tty);
1260 if (_return_local) {
1261 tty->print(" returned args: ");
1262 _arg_returned.print_on(tty);
1263 } else {
1264 tty->print_cr(" non-local return values");
1265 }
1266 tty->print(" modified args: ");
1267 tty->cr();
1268 }
1269 #endif
1270
1271 }
1272
1273
1274 BCEscapeAnalyzer::BCEscapeAnalyzer(ciMethod* method, BCEscapeAnalyzer* parent)
1275 : _conservative(method == NULL || !EstimateArgEscape)
1276 , _method(method)
1277 , _methodData(method ? method->method_data() : NULL)
1278 , _arg_size(method ? method->arg_size() : 0)
1279 , _stack()
1280 , _arg_local(_arg_size)
1281 , _arg_stack(_arg_size)
1282 , _arg_returned(_arg_size)
1283 , _dirty(_arg_size)
1284 , _return_local(false)
1285 , _return_allocated(false)
1286 , _allocated_escapes(false)
1287 , _dependencies()
1288 , _parent(parent)
1289 , _level(parent == NULL ? 0 : parent->level() + 1) {
1290 if (!_conservative) {
1291 _arg_local.clear();
1292 _arg_stack.clear();
1293 _arg_returned.clear();
1294 _dirty.clear();
1295 Arena* arena = CURRENT_ENV->arena();
1296
1297 if (methodData() == NULL)
1298 return;
1299 bool printit = _method->should_print_assembly();
1300 if (methodData()->has_escape_info()) {
1301 TRACE_BCEA(2, tty->print_cr("[EA] Reading previous results for %s.%s",
1302 method->holder()->name()->as_utf8(),
1303 method->name()->as_utf8()));
1304 read_escape_info();
1305 } else {
1306 TRACE_BCEA(2, tty->print_cr("[EA] computing results for %s.%s",
1307 method->holder()->name()->as_utf8(),
1308 method->name()->as_utf8()));
1309
1310 compute_escape_info();
1311 methodData()->update_escape_info();
1312 }
1313 }
1314 }
1315
1316 void BCEscapeAnalyzer::copy_dependencies(Dependencies *deps) {
1317 if(!has_dependencies())
1318 return;
1319 for (int i = 0; i < _dependencies.length(); i+=2) {
1320 ciKlass *k = _dependencies[i]->as_klass();
1321 ciMethod *m = _dependencies[i+1]->as_method();
1322 deps->assert_unique_concrete_method(k, m);
1323 }
1324 }
1325
|
1 #ifdef USE_PRAGMA_IDENT_SRC
2 #pragma ident "@(#)bcEscapeAnalyzer.cpp 1.7 07/05/17 15:49:50 JVM"
3 #endif
4 /*
5 * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 *
8 * This code is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 only, as
10 * published by the Free Software Foundation.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
24 * have any questions.
25 *
90 short _max_stack;
91 bool _initialized;
92 ArgumentMap empty_map;
93
94 StateInfo() {
95 empty_map.clear();
96 }
97
98 ArgumentMap raw_pop() { assert(_stack_height > 0, "stack underflow"); return _stack[--_stack_height]; }
99 ArgumentMap apop() { return raw_pop(); }
100 void spop() { raw_pop(); }
101 void lpop() { spop(); spop(); }
102 void raw_push(ArgumentMap i) { assert(_stack_height < _max_stack, "stack overflow"); _stack[_stack_height++] = i; }
103 void apush(ArgumentMap i) { raw_push(i); }
104 void spush() { raw_push(empty_map); }
105 void lpush() { spush(); spush(); }
106
107 };
108
109 void BCEscapeAnalyzer::set_returned(ArgumentMap vars) {
110 for (int i = 0; i < _arg_size; i++) {
111 if (vars.contains(i))
112 _arg_returned.set_bit(i);
113 }
114 _return_local = _return_local && !(vars.contains_unknown() || vars.contains_allocated());
115 _return_allocated = _return_allocated && vars.contains_allocated() && !(vars.contains_unknown() || vars.contains_vars());
116 }
117
118 // return true if any element of vars is an argument
119 bool BCEscapeAnalyzer::is_argument(ArgumentMap vars) {
120 for (int i = 0; i < _arg_size; i++) {
121 if (vars.contains(i))
122 return true;
123 }
124 return false;
125 }
126
127 // return true if any element of vars is an arg_stack argument
128 bool BCEscapeAnalyzer::is_arg_stack(ArgumentMap vars){
129 if (_conservative)
130 return true;
131 for (int i = 0; i < _arg_size; i++) {
132 if (vars.contains(i) && _arg_stack.at(i))
133 return true;
134 }
135 return false;
136 }
137
138 void BCEscapeAnalyzer::clear_bits(ArgumentMap vars, BitMap &bm) {
139 for (int i = 0; i < _arg_size; i++) {
140 if (vars.contains(i)) {
141 bm.clear_bit(i);
142 }
143 }
144 }
145
146 void BCEscapeAnalyzer::set_method_escape(ArgumentMap vars) {
147 clear_bits(vars, _arg_local);
148 }
149
150 void BCEscapeAnalyzer::set_global_escape(ArgumentMap vars) {
151 clear_bits(vars, _arg_local);
152 clear_bits(vars, _arg_stack);
153 if (vars.contains_allocated())
154 _allocated_escapes = true;
155 }
156
157 void BCEscapeAnalyzer::set_dirty(ArgumentMap vars) {
158 clear_bits(vars, _dirty);
159 }
160
161 void BCEscapeAnalyzer::set_modified(ArgumentMap vars, int offs, int size) {
162
163 for (int i = 0; i < _arg_size; i++) {
164 if (vars.contains(i)) {
165 set_arg_modified(i, offs, size);
166 }
167 }
168 if (vars.contains_unknown())
169 _unknown_modified = true;
170 }
171
172 bool BCEscapeAnalyzer::is_recursive_call(ciMethod* callee) {
173 for (BCEscapeAnalyzer* scope = this; scope != NULL; scope = scope->_parent) {
174 if (scope->method() == callee) {
175 return true;
176 }
177 }
178 return false;
179 }
180
181 bool BCEscapeAnalyzer::is_arg_modified(int arg, int offset, int size_in_bytes) {
182 if (offset == OFFSET_ANY)
183 return _arg_modified[arg] != 0;
184 assert(arg >= 0 && arg < _arg_size, "must be an argument.");
185 bool modified = false;
186 int l = offset / HeapWordSize;
187 int h = round_to(offset + size_in_bytes, HeapWordSize) / HeapWordSize;
188 if (l > ARG_OFFSET_MAX)
189 l = ARG_OFFSET_MAX;
190 if (h > ARG_OFFSET_MAX+1)
191 h = ARG_OFFSET_MAX + 1;
192 for (int i = l; i < h; i++) {
193 modified = modified || (_arg_modified[arg] & (1 << i)) != 0;
194 }
195 return modified;
196 }
197
198 void BCEscapeAnalyzer::set_arg_modified(int arg, int offset, int size_in_bytes) {
199 if (offset == OFFSET_ANY) {
200 _arg_modified[arg] = (uint) -1;
201 return;
202 }
203 assert(arg >= 0 && arg < _arg_size, "must be an argument.");
204 int l = offset / HeapWordSize;
205 int h = round_to(offset + size_in_bytes, HeapWordSize) / HeapWordSize;
206 if (l > ARG_OFFSET_MAX)
207 l = ARG_OFFSET_MAX;
208 if (h > ARG_OFFSET_MAX+1)
209 h = ARG_OFFSET_MAX + 1;
210 for (int i = l; i < h; i++) {
211 _arg_modified[arg] |= (1 << i);
212 }
213 }
214
215 void BCEscapeAnalyzer::invoke(StateInfo &state, Bytecodes::Code code, ciMethod* target, ciKlass* holder) {
216 int i;
217
218 // retrieve information about the callee
219 ciInstanceKlass* klass = target->holder();
220 ciInstanceKlass* calling_klass = method()->holder();
221 ciInstanceKlass* callee_holder = ciEnv::get_instance_klass_for_declared_method_holder(holder);
222 ciInstanceKlass* actual_recv = callee_holder;
223
224 // some methods are obviously bindable without any type checks so
225 // convert them directly to an invokespecial.
226 if (target->is_loaded() && !target->is_abstract() &&
227 target->can_be_statically_bound() && code == Bytecodes::_invokevirtual) {
228 code = Bytecodes::_invokespecial;
229 }
230
231 // compute size of arguments
232 int arg_size = target->arg_size();
233 if (!target->is_loaded() && code == Bytecodes::_invokestatic) {
234 arg_size--;
235 }
236 int arg_base = MAX2(state._stack_height - arg_size, 0);
237
238 // direct recursive calls are skipped if they can be bound statically without introducing
239 // dependencies and if parameters are passed at the same position as in the current method
240 // other calls are skipped if there are no unescaped arguments passed to them
241 bool directly_recursive = (method() == target) &&
242 (code != Bytecodes::_invokevirtual || target->is_final_method() || state._stack[arg_base] .is_empty());
243
244 // check if analysis of callee can safely be skipped
245 bool skip_callee = true;
246 for (i = state._stack_height - 1; i >= arg_base && skip_callee; i--) {
247 ArgumentMap arg = state._stack[i];
248 skip_callee = !is_argument(arg) || !is_arg_stack(arg) || (directly_recursive && arg.is_singleton(i - arg_base));
249 }
250 if (skip_callee) {
251 TRACE_BCEA(3, tty->print_cr("[EA] skipping method %s::%s", holder->name()->as_utf8(), target->name()->as_utf8()));
252 for (i = 0; i < arg_size; i++) {
253 set_method_escape(state.raw_pop());
254 }
255 _unknown_modified = true; // assume the worst since we don't analyze the called method
256 return;
257 }
258
259 // determine actual method (use CHA if necessary)
260 ciMethod* inline_target = NULL;
261 if (target->is_loaded() && klass->is_loaded()
262 && (klass->is_initialized() || klass->is_interface() && target->holder()->is_initialized())
263 && target->will_link(klass, callee_holder, code)) {
264 if (code == Bytecodes::_invokestatic
265 || code == Bytecodes::_invokespecial
266 || code == Bytecodes::_invokevirtual && target->is_final_method()) {
267 inline_target = target;
268 } else {
269 inline_target = target->find_monomorphic_target(calling_klass, callee_holder, actual_recv);
270 }
271 }
272
273 if (inline_target != NULL && !is_recursive_call(inline_target)) {
274 // analyze callee
275 BCEscapeAnalyzer analyzer(inline_target, this);
276
277 // adjust escape state of actual parameters
278 bool must_record_dependencies = false;
279 for (i = arg_size - 1; i >= 0; i--) {
280 ArgumentMap arg = state.raw_pop();
281 if (!is_argument(arg))
282 continue;
283 for (int j = 0; j < _arg_size; j++) {
284 if (arg.contains(j)) {
285 _arg_modified[j] |= analyzer._arg_modified[i];
286 }
287 }
288 if (!is_arg_stack(arg)) {
289 // arguments have already been recognized as escaping
290 } else if (analyzer.is_arg_stack(i) && !analyzer.is_arg_returned(i)) {
291 set_method_escape(arg);
292 must_record_dependencies = true;
293 } else {
294 set_global_escape(arg);
295 }
296 }
297 _unknown_modified = _unknown_modified || analyzer.has_non_arg_side_affects();
298
299 // record dependencies if at least one parameter retained stack-allocatable
300 if (must_record_dependencies) {
301 if (code == Bytecodes::_invokeinterface || code == Bytecodes::_invokevirtual && !target->is_final_method()) {
302 _dependencies.append(actual_recv);
303 _dependencies.append(inline_target);
304 }
305 _dependencies.appendAll(analyzer.dependencies());
306 }
307 } else {
308 TRACE_BCEA(1, tty->print_cr("[EA] virtual method %s is not monomorphic.",
309 target->name()->as_utf8()));
310 // conservatively mark all actual parameters as escaping globally
311 for (i = 0; i < arg_size; i++) {
312 ArgumentMap arg = state.raw_pop();
313 if (!is_argument(arg))
314 continue;
315 set_modified(arg, OFFSET_ANY, type2size[T_INT]*HeapWordSize);
316 set_global_escape(arg);
317 }
318 _unknown_modified = true; // assume the worst since we don't know the called method
319 }
320 }
321
322 bool BCEscapeAnalyzer::contains(uint arg_set1, uint arg_set2) {
323 return ((~arg_set1) | arg_set2) == 0;
324 }
325
326
327 void BCEscapeAnalyzer::iterate_one_block(ciBlock *blk, StateInfo &state, GrowableArray<ciBlock *> &successors) {
328
329 blk->set_processed();
330 ciBytecodeStream s(method());
331 int limit_bci = blk->limit_bci();
332 bool fall_through = false;
333 ArgumentMap allocated_obj;
334 allocated_obj.add_allocated();
335 ArgumentMap unknown_obj;
336 unknown_obj.add_unknown();
337 ArgumentMap empty_map;
338
468 break;
469 case Bytecodes::_astore_1:
470 state._vars[1] = state.apop();
471 break;
472 case Bytecodes::_astore_2:
473 state._vars[2] = state.apop();
474 break;
475 case Bytecodes::_astore_3:
476 state._vars[3] = state.apop();
477 break;
478 case Bytecodes::_iastore:
479 case Bytecodes::_fastore:
480 case Bytecodes::_bastore:
481 case Bytecodes::_castore:
482 case Bytecodes::_sastore:
483 {
484 state.spop();
485 state.spop();
486 ArgumentMap arr = state.apop();
487 set_method_escape(arr);
488 set_modified(arr, OFFSET_ANY, type2size[T_INT]*HeapWordSize);
489 break;
490 }
491 case Bytecodes::_lastore:
492 case Bytecodes::_dastore:
493 {
494 state.lpop();
495 state.spop();
496 ArgumentMap arr = state.apop();
497 set_method_escape(arr);
498 set_modified(arr, OFFSET_ANY, type2size[T_LONG]*HeapWordSize);
499 break;
500 }
501 case Bytecodes::_aastore:
502 {
503 set_global_escape(state.apop());
504 state.spop();
505 ArgumentMap arr = state.apop();
506 set_modified(arr, OFFSET_ANY, type2size[T_OBJECT]*HeapWordSize);
507 break;
508 }
509 case Bytecodes::_pop:
510 state.raw_pop();
511 break;
512 case Bytecodes::_pop2:
513 state.raw_pop();
514 state.raw_pop();
515 break;
516 case Bytecodes::_dup:
517 { ArgumentMap w1 = state.raw_pop();
518 state.raw_push(w1);
519 state.raw_push(w1);
520 }
521 break;
522 case Bytecodes::_dup_x1:
523 { ArgumentMap w1 = state.raw_pop();
524 ArgumentMap w2 = state.raw_pop();
525 state.raw_push(w1);
526 state.raw_push(w2);
812 } else {
813 state.lpush();
814 }
815 }
816 break;
817 case Bytecodes::_putstatic:
818 case Bytecodes::_putfield:
819 { bool will_link;
820 ciField* field = s.get_field(will_link);
821 BasicType field_type = field->type()->basic_type();
822 if (field_type == T_OBJECT || field_type == T_ARRAY) {
823 set_global_escape(state.apop());
824 } else if (type2size[field_type] == 1) {
825 state.spop();
826 } else {
827 state.lpop();
828 }
829 if (s.cur_bc() != Bytecodes::_putstatic) {
830 ArgumentMap p = state.apop();
831 set_method_escape(p);
832 set_modified(p, will_link ? field->offset() : OFFSET_ANY, type2size[field_type]*HeapWordSize);
833 }
834 }
835 break;
836 case Bytecodes::_invokevirtual:
837 case Bytecodes::_invokespecial:
838 case Bytecodes::_invokestatic:
839 case Bytecodes::_invokeinterface:
840 { bool will_link;
841 ciMethod* target = s.get_method(will_link);
842 ciKlass* holder = s.get_declared_method_holder();
843 invoke(state, s.cur_bc(), target, holder);
844 ciType* return_type = target->return_type();
845 if (!return_type->is_primitive_type()) {
846 state.apush(unknown_obj);
847 } else if (return_type->is_one_word()) {
848 state.spush();
849 } else if (return_type->is_two_word()) {
850 state.lpush();
851 }
852 }
923 break;
924 }
925 case Bytecodes::_breakpoint:
926 break;
927 default:
928 ShouldNotReachHere();
929 break;
930 }
931
932 }
933 if (fall_through) {
934 int fall_through_bci = s.cur_bci();
935 if (fall_through_bci < _method->code_size()) {
936 assert(_methodBlocks->is_block_start(fall_through_bci), "must fall through to block start.");
937 successors.push(_methodBlocks->block_containing(fall_through_bci));
938 }
939 }
940 }
941
942 void BCEscapeAnalyzer::merge_block_states(StateInfo *blockstates, ciBlock *dest, StateInfo *s_state) {
943 StateInfo *d_state = blockstates + dest->index();
944 int nlocals = _method->max_locals();
945
946 // exceptions may cause transfer of control to handlers in the middle of a
947 // block, so we don't merge the incoming state of exception handlers
948 if (dest->is_handler())
949 return;
950 if (!d_state->_initialized ) {
951 // destination not initialized, just copy
952 for (int i = 0; i < nlocals; i++) {
953 d_state->_vars[i] = s_state->_vars[i];
954 }
955 for (int i = 0; i < s_state->_stack_height; i++) {
956 d_state->_stack[i] = s_state->_stack[i];
957 }
958 d_state->_stack_height = s_state->_stack_height;
959 d_state->_max_stack = s_state->_max_stack;
960 d_state->_initialized = true;
961 } else if (!dest->processed()) {
962 // we have not yet walked the bytecodes of dest, we can merge
963 // the states
967 }
968 for (int i = 0; i < s_state->_stack_height; i++) {
969 d_state->_stack[i].set_union(s_state->_stack[i]);
970 }
971 } else {
972 // the bytecodes of dest have already been processed, mark any
973 // arguments in the source state which are not in the dest state
974 // as global escape.
975 // Future refinement: we only need to mark these variable to the
976 // maximum escape of any variables in dest state
977 assert(d_state->_stack_height == s_state->_stack_height, "computed stack heights must match");
978 ArgumentMap extra_vars;
979 for (int i = 0; i < nlocals; i++) {
980 ArgumentMap t;
981 t = s_state->_vars[i];
982 t.set_difference(d_state->_vars[i]);
983 extra_vars.set_union(t);
984 }
985 for (int i = 0; i < s_state->_stack_height; i++) {
986 ArgumentMap t;
987 //extra_vars |= !d_state->_vars[i] & s_state->_vars[i];
988 t.clear();
989 t = s_state->_stack[i];
990 t.set_difference(d_state->_stack[i]);
991 extra_vars.set_union(t);
992 }
993 set_global_escape(extra_vars);
994 }
995 }
996
997 void BCEscapeAnalyzer::iterate_blocks(Arena *arena) {
998 int numblocks = _methodBlocks->num_blocks();
999 int stkSize = _method->max_stack();
1000 int numLocals = _method->max_locals();
1001 StateInfo state;
1002
1003 int datacount = (numblocks + 1) * (stkSize + numLocals);
1004 int datasize = datacount * sizeof(ArgumentMap);
1005 StateInfo *blockstates = (StateInfo *) arena->Amalloc(numblocks * sizeof(StateInfo));
1006 ArgumentMap *statedata = (ArgumentMap *) arena->Amalloc(datasize);
1007 for (int i = 0; i < datacount; i++) ::new ((void*)&statedata[i]) ArgumentMap();
1008 ArgumentMap *dp = statedata;
1009 state._vars = dp;
1010 dp += numLocals;
1011 state._stack = dp;
1012 dp += stkSize;
1013 state._initialized = false;
1014 state._max_stack = stkSize;
1015 for (int i = 0; i < numblocks; i++) {
1016 blockstates[i]._vars = dp;
1017 dp += numLocals;
1018 blockstates[i]._stack = dp;
1019 dp += stkSize;
1020 blockstates[i]._initialized = false;
1021 blockstates[i]._stack_height = 0;
1022 blockstates[i]._max_stack = stkSize;
1023 }
1024 GrowableArray<ciBlock *> worklist(arena, numblocks / 4, 0, NULL);
1025 GrowableArray<ciBlock *> successors(arena, 4, 0, NULL);
1026
1027 _methodBlocks->clear_processed();
1028
1029 // initialize block 0 state from method signature
1030 ArgumentMap allVars; // all oop arguments to method
1031 ciSignature* sig = method()->signature();
1032 int j = 0;
1033 ciBlock* first_blk = _methodBlocks->block_containing(0);
1034 int fb_i = first_blk->index();
1035 if (!method()->is_static()) {
1036 // record information for "this"
1037 blockstates[fb_i]._vars[j].set(j);
1038 allVars.add(j);
1039 j++;
1040 }
1041 for (int i = 0; i < sig->count(); i++) {
1042 ciType* t = sig->type_at(i);
1043 if (!t->is_primitive_type()) {
1044 blockstates[fb_i]._vars[j].set(j);
1045 allVars.add(j);
1046 }
1047 j += t->size();
1048 }
1049 blockstates[fb_i]._initialized = true;
1050 assert(j == _arg_size, "just checking");
1051
1052 ArgumentMap unknown_map;
1053 unknown_map.add_unknown();
1054
1055 worklist.push(first_blk);
1056 while(worklist.length() > 0) {
1057 ciBlock *blk = worklist.pop();
1058 StateInfo *blkState = blockstates + blk->index();
1059 if (blk->is_handler() || blk->is_ret_target()) {
1060 // for an exception handler or a target of a ret instruction, we assume the worst case,
1061 // that any variable could contain any argument
1062 for (int i = 0; i < numLocals; i++) {
1063 state._vars[i] = allVars;
1064 }
1065 if (blk->is_handler()) {
1066 state._stack_height = 1;
1067 } else {
1068 state._stack_height = blkState->_stack_height;
1069 }
1070 for (int i = 0; i < state._stack_height; i++) {
1071 // ??? should this be unknown_map ???
1072 state._stack[i] = allVars;
1073 }
1074 } else {
1075 for (int i = 0; i < numLocals; i++) {
1076 state._vars[i] = blkState->_vars[i];
1077 }
1078 for (int i = 0; i < blkState->_stack_height; i++) {
1079 state._stack[i] = blkState->_stack[i];
1080 }
1081 state._stack_height = blkState->_stack_height;
1082 }
1083 iterate_one_block(blk, state, successors);
1084 // if this block has any exception handlers, push them
1085 // onto successor list
1086 if (blk->has_handler()) {
1087 DEBUG_ONLY(int handler_count = 0;)
1088 int blk_start = blk->start_bci();
1089 int blk_end = blk->limit_bci();
1090 for (int i = 0; i < numblocks; i++) {
1091 ciBlock *b = _methodBlocks->block(i);
1108 if (!succ->processed())
1109 worklist.push(succ);
1110 }
1111 }
1112 }
1113
1114 bool BCEscapeAnalyzer::do_analysis() {
1115 Arena* arena = CURRENT_ENV->arena();
1116 // identify basic blocks
1117 _methodBlocks = _method->get_method_blocks();
1118
1119 iterate_blocks(arena);
1120 // TEMPORARY
1121 return true;
1122 }
1123
1124 vmIntrinsics::ID BCEscapeAnalyzer::known_intrinsic() {
1125 vmIntrinsics::ID iid = method()->intrinsic_id();
1126
1127 if (iid == vmIntrinsics::_getClass ||
1128 iid == vmIntrinsics::_fillInStackTrace ||
1129 iid == vmIntrinsics::_hashCode)
1130 return iid;
1131 else
1132 return vmIntrinsics::_none;
1133 }
1134
1135 bool BCEscapeAnalyzer::compute_escape_for_intrinsic(vmIntrinsics::ID iid) {
1136 ArgumentMap arg;
1137 arg.clear();
1138 switch (iid) {
1139 case vmIntrinsics::_getClass:
1140 _return_local = false;
1141 break;
1142 case vmIntrinsics::_fillInStackTrace:
1143 arg.set(0); // 'this'
1144 set_returned(arg);
1145 break;
1146 case vmIntrinsics::_hashCode:
1147 // initialized state is correct
1148 break;
1149 default:
1150 assert(false, "unexpected intrinsic");
1151 }
1152 return true;
1153 }
1154
1155 void BCEscapeAnalyzer::initialize() {
1156 int i;
1157
1158 // clear escape information (method may have been deoptimized)
1159 methodData()->clear_escape_info();
1160
1161 // initialize escape state of object parameters
1162 ciSignature* sig = method()->signature();
1163 int j = 0;
1164 if (!method()->is_static()) {
1165 _arg_local.set_bit(0);
1169 for (i = 0; i < sig->count(); i++) {
1170 ciType* t = sig->type_at(i);
1171 if (!t->is_primitive_type()) {
1172 _arg_local.set_bit(j);
1173 _arg_stack.set_bit(j);
1174 }
1175 j += t->size();
1176 }
1177 assert(j == _arg_size, "just checking");
1178
1179 // start with optimistic assumption
1180 ciType *rt = _method->return_type();
1181 if (rt->is_primitive_type()) {
1182 _return_local = false;
1183 _return_allocated = false;
1184 } else {
1185 _return_local = true;
1186 _return_allocated = true;
1187 }
1188 _allocated_escapes = false;
1189 _unknown_modified = false;
1190 }
1191
1192 void BCEscapeAnalyzer::clear_escape_info() {
1193 ciSignature* sig = method()->signature();
1194 int arg_count = sig->count();
1195 ArgumentMap var;
1196 if (!method()->is_static()) {
1197 arg_count++; // allow for "this"
1198 }
1199 for (int i = 0; i < arg_count; i++) {
1200 set_arg_modified(i, OFFSET_ANY, 4);
1201 var.clear();
1202 var.set(i);
1203 set_modified(var, OFFSET_ANY, 4);
1204 set_global_escape(var);
1205 }
1206 _arg_local.clear();
1207 _arg_stack.clear();
1208 _arg_returned.clear();
1209 _return_local = false;
1210 _return_allocated = false;
1211 _allocated_escapes = true;
1212 _unknown_modified = true;
1213 }
1214
1215
1216 void BCEscapeAnalyzer::compute_escape_info() {
1217 int i;
1218 assert(!methodData()->has_escape_info(), "do not overwrite escape info");
1219
1220 vmIntrinsics::ID iid = known_intrinsic();
1221
1222 // check if method can be analyzed
1223 if (iid == vmIntrinsics::_none && (method()->is_abstract() || method()->is_native() || !method()->holder()->is_initialized()
1224 || _level > MaxBCEAEstimateLevel
1225 || method()->code_size() > MaxBCEAEstimateSize)) {
1226 if (BCEATraceLevel >= 1) {
1227 tty->print("Skipping method because: ");
1228 if (method()->is_abstract())
1229 tty->print_cr("method is abstract.");
1230 else if (method()->is_native())
1231 tty->print_cr("method is native.");
1232 else if (!method()->holder()->is_initialized())
1240 else
1241 ShouldNotReachHere();
1242 }
1243 clear_escape_info();
1244
1245 return;
1246 }
1247
1248 if (BCEATraceLevel >= 1) {
1249 tty->print("[EA] estimating escape information for");
1250 if (iid != vmIntrinsics::_none)
1251 tty->print(" intrinsic");
1252 method()->print_short_name();
1253 tty->print_cr(" (%d bytes)", method()->code_size());
1254 }
1255
1256 bool success;
1257
1258 initialize();
1259
1260 // Do not scan method if it has no object parameters and
1261 // does not returns an object (_return_allocated is set in initialize()).
1262 if (_arg_local.is_empty() && !_return_allocated) {
1263 // Clear all info since method's bytecode was not analysed and
1264 // set pessimistic escape information.
1265 clear_escape_info();
1266 methodData()->set_eflag(methodDataOopDesc::allocated_escapes);
1267 methodData()->set_eflag(methodDataOopDesc::unknown_modified);
1268 methodData()->set_eflag(methodDataOopDesc::estimated);
1269 return;
1270 }
1271
1272 if (iid != vmIntrinsics::_none)
1273 success = compute_escape_for_intrinsic(iid);
1274 else {
1275 success = do_analysis();
1276 }
1277
1278 // don't store interprocedural escape information if it introduces
1279 // dependencies or if method data is empty
1280 //
1281 if (!has_dependencies() && !methodData()->is_empty()) {
1282 for (i = 0; i < _arg_size; i++) {
1283 if (_arg_local.at(i)) {
1284 assert(_arg_stack.at(i), "inconsistent escape info");
1285 methodData()->set_arg_local(i);
1286 methodData()->set_arg_stack(i);
1287 } else if (_arg_stack.at(i)) {
1288 methodData()->set_arg_stack(i);
1289 }
1290 if (_arg_returned.at(i)) {
1291 methodData()->set_arg_returned(i);
1292 }
1293 methodData()->set_arg_modified(i, _arg_modified[i]);
1294 }
1295 if (_return_local) {
1296 methodData()->set_eflag(methodDataOopDesc::return_local);
1297 }
1298 if (_return_allocated) {
1299 methodData()->set_eflag(methodDataOopDesc::return_allocated);
1300 }
1301 if (_allocated_escapes) {
1302 methodData()->set_eflag(methodDataOopDesc::allocated_escapes);
1303 }
1304 if (_unknown_modified) {
1305 methodData()->set_eflag(methodDataOopDesc::unknown_modified);
1306 }
1307 methodData()->set_eflag(methodDataOopDesc::estimated);
1308 }
1309 }
1310
1311 void BCEscapeAnalyzer::read_escape_info() {
1312 assert(methodData()->has_escape_info(), "no escape info available");
1313
1314 // read escape information from method descriptor
1315 for (int i = 0; i < _arg_size; i++) {
1316 _arg_local.at_put(i, methodData()->is_arg_local(i));
1317 _arg_stack.at_put(i, methodData()->is_arg_stack(i));
1318 _arg_returned.at_put(i, methodData()->is_arg_returned(i));
1319 _arg_modified[i] = methodData()->arg_modified(i);
1320 }
1321 _return_local = methodData()->eflag_set(methodDataOopDesc::return_local);
1322 _return_allocated = methodData()->eflag_set(methodDataOopDesc::return_allocated);
1323 _allocated_escapes = methodData()->eflag_set(methodDataOopDesc::allocated_escapes);
1324 _unknown_modified = methodData()->eflag_set(methodDataOopDesc::unknown_modified);
1325
1326 }
1327
1328 #ifndef PRODUCT
1329 void BCEscapeAnalyzer::dump() {
1330 tty->print("[EA] estimated escape information for");
1331 method()->print_short_name();
1332 tty->print_cr(has_dependencies() ? " (not stored)" : "");
1333 tty->print(" non-escaping args: ");
1334 _arg_local.print_on(tty);
1335 tty->print(" stack-allocatable args: ");
1336 _arg_stack.print_on(tty);
1337 if (_return_local) {
1338 tty->print(" returned args: ");
1339 _arg_returned.print_on(tty);
1340 } else if (is_return_allocated()) {
1341 tty->print_cr(" return allocated value");
1342 } else {
1343 tty->print_cr(" return non-local value");
1344 }
1345 tty->print(" modified args: ");
1346 for (int i = 0; i < _arg_size; i++) {
1347 if (_arg_modified[i] == 0)
1348 tty->print(" 0");
1349 else
1350 tty->print(" 0x%x", _arg_modified[i]);
1351 }
1352 tty->cr();
1353 tty->print(" flags: ");
1354 if (_return_allocated)
1355 tty->print(" return_allocated");
1356 if (_allocated_escapes)
1357 tty->print(" allocated_escapes");
1358 if (_unknown_modified)
1359 tty->print(" unknown_modified");
1360 tty->cr();
1361 }
1362 #endif
1363
1364 BCEscapeAnalyzer::BCEscapeAnalyzer(ciMethod* method, BCEscapeAnalyzer* parent)
1365 : _conservative(method == NULL || !EstimateArgEscape)
1366 , _method(method)
1367 , _methodData(method ? method->method_data() : NULL)
1368 , _arg_size(method ? method->arg_size() : 0)
1369 , _stack()
1370 , _arg_local(_arg_size)
1371 , _arg_stack(_arg_size)
1372 , _arg_returned(_arg_size)
1373 , _dirty(_arg_size)
1374 , _return_local(false)
1375 , _return_allocated(false)
1376 , _allocated_escapes(false)
1377 , _unknown_modified(false)
1378 , _dependencies()
1379 , _parent(parent)
1380 , _level(parent == NULL ? 0 : parent->level() + 1) {
1381 if (!_conservative) {
1382 _arg_local.clear();
1383 _arg_stack.clear();
1384 _arg_returned.clear();
1385 _dirty.clear();
1386 Arena* arena = CURRENT_ENV->arena();
1387 _arg_modified = (uint *) arena->Amalloc(_arg_size * sizeof(uint));
1388 Copy::zero_to_bytes(_arg_modified, _arg_size * sizeof(uint));
1389
1390 if (methodData() == NULL)
1391 return;
1392 bool printit = _method->should_print_assembly();
1393 if (methodData()->has_escape_info()) {
1394 TRACE_BCEA(2, tty->print_cr("[EA] Reading previous results for %s.%s",
1395 method->holder()->name()->as_utf8(),
1396 method->name()->as_utf8()));
1397 read_escape_info();
1398 } else {
1399 TRACE_BCEA(2, tty->print_cr("[EA] computing results for %s.%s",
1400 method->holder()->name()->as_utf8(),
1401 method->name()->as_utf8()));
1402
1403 compute_escape_info();
1404 methodData()->update_escape_info();
1405 }
1406 #ifndef PRODUCT
1407 if (BCEATraceLevel >= 3) {
1408 // dump escape information
1409 dump();
1410 }
1411 #endif
1412 }
1413 }
1414
1415 void BCEscapeAnalyzer::copy_dependencies(Dependencies *deps) {
1416 if(!has_dependencies())
1417 return;
1418 for (int i = 0; i < _dependencies.length(); i+=2) {
1419 ciKlass *k = _dependencies[i]->as_klass();
1420 ciMethod *m = _dependencies[i+1]->as_method();
1421 deps->assert_unique_concrete_method(k, m);
1422 }
1423 }
1424
|