blob: c0068b2331cfb0bc60b457866b2745e3673d4709 [file] [log] [blame]
buzbee2502e002012-12-31 16:05:53 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
buzbee311ca162013-02-28 15:56:43 -080017#include "local_value_numbering.h"
buzbee2502e002012-12-31 16:05:53 -080018
Vladimir Markobe0e5462014-02-26 11:24:15 +000019#include "mir_field_info.h"
Vladimir Markof59f18b2014-02-17 15:53:57 +000020#include "mir_graph.h"
21
buzbee2502e002012-12-31 16:05:53 -080022namespace art {
23
Vladimir Markof59f18b2014-02-17 15:53:57 +000024uint16_t LocalValueNumbering::GetFieldId(const DexFile* dex_file, uint16_t field_idx) {
25 FieldReference key = { dex_file, field_idx };
26 auto it = field_index_map_.find(key);
27 if (it != field_index_map_.end()) {
28 return it->second;
29 }
30 uint16_t id = field_index_map_.size();
31 field_index_map_.Put(key, id);
32 return id;
33}
34
35void LocalValueNumbering::AdvanceGlobalMemory() {
36 // See AdvanceMemoryVersion() for explanation.
37 global_memory_version_ = next_memory_version_;
38 ++next_memory_version_;
39}
40
41uint16_t LocalValueNumbering::GetMemoryVersion(uint16_t base, uint16_t field, uint16_t type) {
42 // See AdvanceMemoryVersion() for explanation.
43 MemoryVersionKey key = { base, field, type };
44 MemoryVersionMap::iterator it = memory_version_map_.find(key);
45 uint16_t memory_version = (it != memory_version_map_.end()) ? it->second : 0u;
46 if (base != NO_VALUE && non_aliasing_refs_.find(base) == non_aliasing_refs_.end()) {
47 // Check modifications by potentially aliased access.
48 MemoryVersionKey aliased_access_key = { NO_VALUE, field, type };
49 auto aa_it = memory_version_map_.find(aliased_access_key);
50 if (aa_it != memory_version_map_.end() && aa_it->second > memory_version) {
51 memory_version = aa_it->second;
52 }
53 memory_version = std::max(memory_version, global_memory_version_);
54 } else if (base != NO_VALUE) {
55 // Ignore global_memory_version_ for access via unique references.
56 } else {
57 memory_version = std::max(memory_version, global_memory_version_);
58 }
59 return memory_version;
60};
61
62uint16_t LocalValueNumbering::AdvanceMemoryVersion(uint16_t base, uint16_t field, uint16_t type) {
63 // When we read the same value from memory, we want to assign the same value name to it.
64 // However, we need to be careful not to assign the same value name if the memory location
65 // may have been written to between the reads. To avoid that we do "memory versioning".
66 //
67 // For each write to a memory location (instance field, static field, array element) we assign
68 // a new memory version number to the location identified by the value name of the base register,
69 // the field id and type, or "{ base, field, type }". For static fields the "base" is NO_VALUE
70 // since they are not accessed via a reference. For arrays the "field" is NO_VALUE since they
71 // don't have a field id.
72 //
73 // To account for the possibility of aliased access to the same memory location via different
74 // "base", we also store the memory version number with the key "{ NO_VALUE, field, type }"
75 // if "base" is an aliasing reference and check it in GetMemoryVersion() on reads via
76 // aliasing references. A global memory version is set for method calls as a method can
77 // potentially write to any memory location accessed via an aliasing reference.
78
79 uint16_t result = next_memory_version_;
80 ++next_memory_version_;
81 MemoryVersionKey key = { base, field, type };
82 memory_version_map_.Overwrite(key, result);
83 if (base != NO_VALUE && non_aliasing_refs_.find(base) == non_aliasing_refs_.end()) {
84 // Advance memory version for aliased access.
85 MemoryVersionKey aliased_access_key = { NO_VALUE, field, type };
86 memory_version_map_.Overwrite(aliased_access_key, result);
87 }
88 return result;
89};
90
91uint16_t LocalValueNumbering::MarkNonAliasingNonNull(MIR* mir) {
92 uint16_t res = GetOperandValue(mir->ssa_rep->defs[0]);
93 SetOperandValue(mir->ssa_rep->defs[0], res);
94 DCHECK(null_checked_.find(res) == null_checked_.end());
95 null_checked_.insert(res);
96 non_aliasing_refs_.insert(res);
97 return res;
98}
99
100void LocalValueNumbering::MakeArgsAliasing(MIR* mir) {
101 for (size_t i = 0u, count = mir->ssa_rep->num_uses; i != count; ++i) {
102 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[i]);
103 non_aliasing_refs_.erase(reg);
104 }
105}
106
107void LocalValueNumbering::HandleNullCheck(MIR* mir, uint16_t reg) {
108 if (null_checked_.find(reg) != null_checked_.end()) {
109 if (cu_->verbose) {
110 LOG(INFO) << "Removing null check for 0x" << std::hex << mir->offset;
111 }
112 mir->optimization_flags |= MIR_IGNORE_NULL_CHECK;
113 } else {
114 null_checked_.insert(reg);
115 }
116}
117
118void LocalValueNumbering::HandleRangeCheck(MIR* mir, uint16_t array, uint16_t index) {
119 if (ValueExists(ARRAY_REF, array, index, NO_VALUE)) {
120 if (cu_->verbose) {
121 LOG(INFO) << "Removing range check for 0x" << std::hex << mir->offset;
122 }
123 mir->optimization_flags |= MIR_IGNORE_RANGE_CHECK;
124 }
125 // Use side effect to note range check completed.
126 (void)LookupValue(ARRAY_REF, array, index, NO_VALUE);
127}
128
129void LocalValueNumbering::HandlePutObject(MIR* mir) {
130 // If we're storing a non-aliasing reference, stop tracking it as non-aliasing now.
131 uint16_t base = GetOperandValue(mir->ssa_rep->uses[0]);
132 non_aliasing_refs_.erase(base);
133}
buzbee2502e002012-12-31 16:05:53 -0800134
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700135uint16_t LocalValueNumbering::GetValueNumber(MIR* mir) {
buzbee2502e002012-12-31 16:05:53 -0800136 uint16_t res = NO_VALUE;
137 uint16_t opcode = mir->dalvikInsn.opcode;
138 switch (opcode) {
139 case Instruction::NOP:
140 case Instruction::RETURN_VOID:
141 case Instruction::RETURN:
142 case Instruction::RETURN_OBJECT:
143 case Instruction::RETURN_WIDE:
144 case Instruction::MONITOR_ENTER:
145 case Instruction::MONITOR_EXIT:
146 case Instruction::GOTO:
147 case Instruction::GOTO_16:
148 case Instruction::GOTO_32:
149 case Instruction::CHECK_CAST:
150 case Instruction::THROW:
151 case Instruction::FILL_ARRAY_DATA:
buzbee2502e002012-12-31 16:05:53 -0800152 case Instruction::PACKED_SWITCH:
153 case Instruction::SPARSE_SWITCH:
154 case Instruction::IF_EQ:
155 case Instruction::IF_NE:
156 case Instruction::IF_LT:
157 case Instruction::IF_GE:
158 case Instruction::IF_GT:
159 case Instruction::IF_LE:
160 case Instruction::IF_EQZ:
161 case Instruction::IF_NEZ:
162 case Instruction::IF_LTZ:
163 case Instruction::IF_GEZ:
164 case Instruction::IF_GTZ:
165 case Instruction::IF_LEZ:
buzbee2502e002012-12-31 16:05:53 -0800166 case kMirOpFusedCmplFloat:
167 case kMirOpFusedCmpgFloat:
168 case kMirOpFusedCmplDouble:
169 case kMirOpFusedCmpgDouble:
170 case kMirOpFusedCmpLong:
171 // Nothing defined - take no action.
172 break;
173
Vladimir Markof59f18b2014-02-17 15:53:57 +0000174 case Instruction::FILLED_NEW_ARRAY:
175 case Instruction::FILLED_NEW_ARRAY_RANGE:
176 // Nothing defined but the result will be unique and non-null.
177 if (mir->next != nullptr && mir->next->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) {
178 MarkNonAliasingNonNull(mir->next);
179 // The MOVE_RESULT_OBJECT will be processed next and we'll return the value name then.
180 }
181 MakeArgsAliasing(mir);
182 break;
183
184 case Instruction::INVOKE_DIRECT:
185 case Instruction::INVOKE_DIRECT_RANGE:
186 case Instruction::INVOKE_VIRTUAL:
187 case Instruction::INVOKE_VIRTUAL_RANGE:
188 case Instruction::INVOKE_SUPER:
189 case Instruction::INVOKE_SUPER_RANGE:
190 case Instruction::INVOKE_INTERFACE:
191 case Instruction::INVOKE_INTERFACE_RANGE: {
192 // Nothing defined but handle the null check.
193 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[0]);
194 HandleNullCheck(mir, reg);
195 }
196 // Intentional fall-through.
197 case Instruction::INVOKE_STATIC:
198 case Instruction::INVOKE_STATIC_RANGE:
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000199 if ((mir->optimization_flags & MIR_INLINED) == 0) {
200 AdvanceGlobalMemory();
201 MakeArgsAliasing(mir);
202 }
Vladimir Markof59f18b2014-02-17 15:53:57 +0000203 break;
204
buzbee2502e002012-12-31 16:05:53 -0800205 case Instruction::MOVE_RESULT:
206 case Instruction::MOVE_RESULT_OBJECT:
207 case Instruction::INSTANCE_OF:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000208 // 1 result, treat as unique each time, use result s_reg - will be unique.
209 res = GetOperandValue(mir->ssa_rep->defs[0]);
210 SetOperandValue(mir->ssa_rep->defs[0], res);
211 break;
212 case Instruction::MOVE_EXCEPTION:
buzbee2502e002012-12-31 16:05:53 -0800213 case Instruction::NEW_INSTANCE:
214 case Instruction::CONST_STRING:
215 case Instruction::CONST_STRING_JUMBO:
216 case Instruction::CONST_CLASS:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000217 case Instruction::NEW_ARRAY:
Vladimir Markob3e527b2014-04-04 12:37:07 +0100218 // 1 result, treat as unique each time, use result s_reg - will be unique.
219 res = MarkNonAliasingNonNull(mir);
buzbee2502e002012-12-31 16:05:53 -0800220 break;
Vladimir Markof59f18b2014-02-17 15:53:57 +0000221 case Instruction::MOVE_RESULT_WIDE:
Vladimir Markob3e527b2014-04-04 12:37:07 +0100222 // 1 wide result, treat as unique each time, use result s_reg - will be unique.
223 res = GetOperandValueWide(mir->ssa_rep->defs[0]);
224 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -0800225 break;
226
227 case kMirOpPhi:
228 /*
229 * Because we'll only see phi nodes at the beginning of an extended basic block,
230 * we can ignore them. Revisit if we shift to global value numbering.
231 */
232 break;
233
234 case Instruction::MOVE:
235 case Instruction::MOVE_OBJECT:
236 case Instruction::MOVE_16:
237 case Instruction::MOVE_OBJECT_16:
238 case Instruction::MOVE_FROM16:
239 case Instruction::MOVE_OBJECT_FROM16:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000240 case kMirOpCopy:
241 // Just copy value number of source to value number of result.
242 res = GetOperandValue(mir->ssa_rep->uses[0]);
243 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -0800244 break;
245
246 case Instruction::MOVE_WIDE:
247 case Instruction::MOVE_WIDE_16:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000248 case Instruction::MOVE_WIDE_FROM16:
249 // Just copy value number of source to value number of result.
250 res = GetOperandValueWide(mir->ssa_rep->uses[0]);
251 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -0800252 break;
253
254 case Instruction::CONST:
255 case Instruction::CONST_4:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000256 case Instruction::CONST_16:
257 res = LookupValue(Instruction::CONST, Low16Bits(mir->dalvikInsn.vB),
258 High16Bits(mir->dalvikInsn.vB >> 16), 0);
259 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -0800260 break;
261
Vladimir Markof59f18b2014-02-17 15:53:57 +0000262 case Instruction::CONST_HIGH16:
263 res = LookupValue(Instruction::CONST, 0, mir->dalvikInsn.vB, 0);
264 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -0800265 break;
266
267 case Instruction::CONST_WIDE_16:
268 case Instruction::CONST_WIDE_32: {
269 uint16_t low_res = LookupValue(Instruction::CONST, Low16Bits(mir->dalvikInsn.vB),
270 High16Bits(mir->dalvikInsn.vB >> 16), 1);
271 uint16_t high_res;
272 if (mir->dalvikInsn.vB & 0x80000000) {
273 high_res = LookupValue(Instruction::CONST, 0xffff, 0xffff, 2);
274 } else {
275 high_res = LookupValue(Instruction::CONST, 0, 0, 2);
276 }
Vladimir Markof59f18b2014-02-17 15:53:57 +0000277 res = LookupValue(Instruction::CONST, low_res, high_res, 3);
278 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -0800279 }
280 break;
281
282 case Instruction::CONST_WIDE: {
283 uint32_t low_word = Low32Bits(mir->dalvikInsn.vB_wide);
284 uint32_t high_word = High32Bits(mir->dalvikInsn.vB_wide);
285 uint16_t low_res = LookupValue(Instruction::CONST, Low16Bits(low_word),
286 High16Bits(low_word), 1);
287 uint16_t high_res = LookupValue(Instruction::CONST, Low16Bits(high_word),
288 High16Bits(high_word), 2);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000289 res = LookupValue(Instruction::CONST, low_res, high_res, 3);
buzbee2502e002012-12-31 16:05:53 -0800290 SetOperandValueWide(mir->ssa_rep->defs[0], res);
291 }
292 break;
293
294 case Instruction::CONST_WIDE_HIGH16: {
295 uint16_t low_res = LookupValue(Instruction::CONST, 0, 0, 1);
296 uint16_t high_res = LookupValue(Instruction::CONST, 0, Low16Bits(mir->dalvikInsn.vB), 2);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000297 res = LookupValue(Instruction::CONST, low_res, high_res, 3);
buzbee2502e002012-12-31 16:05:53 -0800298 SetOperandValueWide(mir->ssa_rep->defs[0], res);
299 }
300 break;
301
302 case Instruction::ARRAY_LENGTH:
303 case Instruction::NEG_INT:
304 case Instruction::NOT_INT:
305 case Instruction::NEG_FLOAT:
306 case Instruction::INT_TO_BYTE:
307 case Instruction::INT_TO_SHORT:
308 case Instruction::INT_TO_CHAR:
309 case Instruction::INT_TO_FLOAT:
310 case Instruction::FLOAT_TO_INT: {
311 // res = op + 1 operand
312 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000313 res = LookupValue(opcode, operand1, NO_VALUE, NO_VALUE);
buzbee2502e002012-12-31 16:05:53 -0800314 SetOperandValue(mir->ssa_rep->defs[0], res);
315 }
316 break;
317
318 case Instruction::LONG_TO_FLOAT:
319 case Instruction::LONG_TO_INT:
320 case Instruction::DOUBLE_TO_FLOAT:
321 case Instruction::DOUBLE_TO_INT: {
322 // res = op + 1 wide operand
323 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000324 res = LookupValue(opcode, operand1, NO_VALUE, NO_VALUE);
buzbee2502e002012-12-31 16:05:53 -0800325 SetOperandValue(mir->ssa_rep->defs[0], res);
326 }
327 break;
328
329
330 case Instruction::DOUBLE_TO_LONG:
331 case Instruction::LONG_TO_DOUBLE:
332 case Instruction::NEG_LONG:
333 case Instruction::NOT_LONG:
334 case Instruction::NEG_DOUBLE: {
335 // wide res = op + 1 wide operand
336 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000337 res = LookupValue(opcode, operand1, NO_VALUE, NO_VALUE);
buzbee2502e002012-12-31 16:05:53 -0800338 SetOperandValueWide(mir->ssa_rep->defs[0], res);
339 }
340 break;
341
342 case Instruction::FLOAT_TO_DOUBLE:
343 case Instruction::FLOAT_TO_LONG:
344 case Instruction::INT_TO_DOUBLE:
345 case Instruction::INT_TO_LONG: {
346 // wide res = op + 1 operand
347 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000348 res = LookupValue(opcode, operand1, NO_VALUE, NO_VALUE);
buzbee2502e002012-12-31 16:05:53 -0800349 SetOperandValueWide(mir->ssa_rep->defs[0], res);
350 }
351 break;
352
353 case Instruction::CMPL_DOUBLE:
354 case Instruction::CMPG_DOUBLE:
355 case Instruction::CMP_LONG: {
356 // res = op + 2 wide operands
357 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
358 uint16_t operand2 = GetOperandValueWide(mir->ssa_rep->uses[2]);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000359 res = LookupValue(opcode, operand1, operand2, NO_VALUE);
buzbee2502e002012-12-31 16:05:53 -0800360 SetOperandValue(mir->ssa_rep->defs[0], res);
361 }
362 break;
363
364 case Instruction::CMPG_FLOAT:
365 case Instruction::CMPL_FLOAT:
366 case Instruction::ADD_INT:
367 case Instruction::ADD_INT_2ADDR:
368 case Instruction::MUL_INT:
369 case Instruction::MUL_INT_2ADDR:
370 case Instruction::AND_INT:
371 case Instruction::AND_INT_2ADDR:
372 case Instruction::OR_INT:
373 case Instruction::OR_INT_2ADDR:
374 case Instruction::XOR_INT:
375 case Instruction::XOR_INT_2ADDR:
376 case Instruction::SUB_INT:
377 case Instruction::SUB_INT_2ADDR:
378 case Instruction::DIV_INT:
379 case Instruction::DIV_INT_2ADDR:
380 case Instruction::REM_INT:
381 case Instruction::REM_INT_2ADDR:
382 case Instruction::SHL_INT:
383 case Instruction::SHL_INT_2ADDR:
384 case Instruction::SHR_INT:
385 case Instruction::SHR_INT_2ADDR:
386 case Instruction::USHR_INT:
387 case Instruction::USHR_INT_2ADDR: {
388 // res = op + 2 operands
389 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
390 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[1]);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000391 res = LookupValue(opcode, operand1, operand2, NO_VALUE);
buzbee2502e002012-12-31 16:05:53 -0800392 SetOperandValue(mir->ssa_rep->defs[0], res);
393 }
394 break;
395
396 case Instruction::ADD_LONG:
397 case Instruction::SUB_LONG:
398 case Instruction::MUL_LONG:
399 case Instruction::DIV_LONG:
400 case Instruction::REM_LONG:
401 case Instruction::AND_LONG:
402 case Instruction::OR_LONG:
403 case Instruction::XOR_LONG:
404 case Instruction::ADD_LONG_2ADDR:
405 case Instruction::SUB_LONG_2ADDR:
406 case Instruction::MUL_LONG_2ADDR:
407 case Instruction::DIV_LONG_2ADDR:
408 case Instruction::REM_LONG_2ADDR:
409 case Instruction::AND_LONG_2ADDR:
410 case Instruction::OR_LONG_2ADDR:
411 case Instruction::XOR_LONG_2ADDR:
412 case Instruction::ADD_DOUBLE:
413 case Instruction::SUB_DOUBLE:
414 case Instruction::MUL_DOUBLE:
415 case Instruction::DIV_DOUBLE:
416 case Instruction::REM_DOUBLE:
417 case Instruction::ADD_DOUBLE_2ADDR:
418 case Instruction::SUB_DOUBLE_2ADDR:
419 case Instruction::MUL_DOUBLE_2ADDR:
420 case Instruction::DIV_DOUBLE_2ADDR:
421 case Instruction::REM_DOUBLE_2ADDR: {
422 // wide res = op + 2 wide operands
423 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
424 uint16_t operand2 = GetOperandValueWide(mir->ssa_rep->uses[2]);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000425 res = LookupValue(opcode, operand1, operand2, NO_VALUE);
buzbee2502e002012-12-31 16:05:53 -0800426 SetOperandValueWide(mir->ssa_rep->defs[0], res);
427 }
428 break;
429
430 case Instruction::SHL_LONG:
431 case Instruction::SHR_LONG:
432 case Instruction::USHR_LONG:
433 case Instruction::SHL_LONG_2ADDR:
434 case Instruction::SHR_LONG_2ADDR:
435 case Instruction::USHR_LONG_2ADDR: {
436 // wide res = op + 1 wide operand + 1 operand
437 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
438 uint16_t operand2 = GetOperandValueWide(mir->ssa_rep->uses[2]);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000439 res = LookupValue(opcode, operand1, operand2, NO_VALUE);
buzbee2502e002012-12-31 16:05:53 -0800440 SetOperandValueWide(mir->ssa_rep->defs[0], res);
441 }
442 break;
443
444 case Instruction::ADD_FLOAT:
445 case Instruction::SUB_FLOAT:
446 case Instruction::MUL_FLOAT:
447 case Instruction::DIV_FLOAT:
448 case Instruction::REM_FLOAT:
449 case Instruction::ADD_FLOAT_2ADDR:
450 case Instruction::SUB_FLOAT_2ADDR:
451 case Instruction::MUL_FLOAT_2ADDR:
452 case Instruction::DIV_FLOAT_2ADDR:
453 case Instruction::REM_FLOAT_2ADDR: {
454 // res = op + 2 operands
455 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
456 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[1]);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000457 res = LookupValue(opcode, operand1, operand2, NO_VALUE);
buzbee2502e002012-12-31 16:05:53 -0800458 SetOperandValue(mir->ssa_rep->defs[0], res);
459 }
460 break;
461
462 case Instruction::RSUB_INT:
463 case Instruction::ADD_INT_LIT16:
464 case Instruction::MUL_INT_LIT16:
465 case Instruction::DIV_INT_LIT16:
466 case Instruction::REM_INT_LIT16:
467 case Instruction::AND_INT_LIT16:
468 case Instruction::OR_INT_LIT16:
469 case Instruction::XOR_INT_LIT16:
470 case Instruction::ADD_INT_LIT8:
471 case Instruction::RSUB_INT_LIT8:
472 case Instruction::MUL_INT_LIT8:
473 case Instruction::DIV_INT_LIT8:
474 case Instruction::REM_INT_LIT8:
475 case Instruction::AND_INT_LIT8:
476 case Instruction::OR_INT_LIT8:
477 case Instruction::XOR_INT_LIT8:
478 case Instruction::SHL_INT_LIT8:
479 case Instruction::SHR_INT_LIT8:
480 case Instruction::USHR_INT_LIT8: {
nikolay serdjukee40aa42014-03-25 12:21:29 +0700481 // Same as res = op + 2 operands, except use vC as operand 2
buzbee2502e002012-12-31 16:05:53 -0800482 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
nikolay serdjukee40aa42014-03-25 12:21:29 +0700483 uint16_t operand2 = LookupValue(Instruction::CONST, mir->dalvikInsn.vC, 0, 0);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000484 res = LookupValue(opcode, operand1, operand2, NO_VALUE);
buzbee2502e002012-12-31 16:05:53 -0800485 SetOperandValue(mir->ssa_rep->defs[0], res);
486 }
487 break;
488
buzbee2502e002012-12-31 16:05:53 -0800489 case Instruction::AGET_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000490 case Instruction::AGET:
491 case Instruction::AGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -0800492 case Instruction::AGET_BOOLEAN:
493 case Instruction::AGET_BYTE:
494 case Instruction::AGET_CHAR:
495 case Instruction::AGET_SHORT: {
Vladimir Markof59f18b2014-02-17 15:53:57 +0000496 uint16_t type = opcode - Instruction::AGET;
buzbee2502e002012-12-31 16:05:53 -0800497 uint16_t array = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000498 HandleNullCheck(mir, array);
buzbee2502e002012-12-31 16:05:53 -0800499 uint16_t index = GetOperandValue(mir->ssa_rep->uses[1]);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000500 HandleRangeCheck(mir, array, index);
buzbee2502e002012-12-31 16:05:53 -0800501 // Establish value number for loaded register. Note use of memory version.
Vladimir Markof59f18b2014-02-17 15:53:57 +0000502 uint16_t memory_version = GetMemoryVersion(array, NO_VALUE, type);
buzbee2502e002012-12-31 16:05:53 -0800503 uint16_t res = LookupValue(ARRAY_REF, array, index, memory_version);
504 if (opcode == Instruction::AGET_WIDE) {
505 SetOperandValueWide(mir->ssa_rep->defs[0], res);
506 } else {
507 SetOperandValue(mir->ssa_rep->defs[0], res);
508 }
509 }
510 break;
511
buzbee2502e002012-12-31 16:05:53 -0800512 case Instruction::APUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000513 HandlePutObject(mir);
514 // Intentional fall-through.
515 case Instruction::APUT:
516 case Instruction::APUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -0800517 case Instruction::APUT_BYTE:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000518 case Instruction::APUT_BOOLEAN:
519 case Instruction::APUT_SHORT:
520 case Instruction::APUT_CHAR: {
521 uint16_t type = opcode - Instruction::APUT;
buzbee2502e002012-12-31 16:05:53 -0800522 int array_idx = (opcode == Instruction::APUT_WIDE) ? 2 : 1;
523 int index_idx = array_idx + 1;
524 uint16_t array = GetOperandValue(mir->ssa_rep->uses[array_idx]);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000525 HandleNullCheck(mir, array);
buzbee2502e002012-12-31 16:05:53 -0800526 uint16_t index = GetOperandValue(mir->ssa_rep->uses[index_idx]);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000527 HandleRangeCheck(mir, array, index);
buzbee2502e002012-12-31 16:05:53 -0800528 // Rev the memory version
Vladimir Markof59f18b2014-02-17 15:53:57 +0000529 AdvanceMemoryVersion(array, NO_VALUE, type);
buzbee2502e002012-12-31 16:05:53 -0800530 }
531 break;
532
533 case Instruction::IGET_OBJECT:
buzbee2502e002012-12-31 16:05:53 -0800534 case Instruction::IGET:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000535 case Instruction::IGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -0800536 case Instruction::IGET_BOOLEAN:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000537 case Instruction::IGET_BYTE:
538 case Instruction::IGET_CHAR:
539 case Instruction::IGET_SHORT: {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000540 uint16_t type = opcode - Instruction::IGET;
buzbee2502e002012-12-31 16:05:53 -0800541 uint16_t base = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000542 HandleNullCheck(mir, base);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000543 const MirFieldInfo& field_info = cu_->mir_graph->GetIFieldLoweringInfo(mir);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000544 uint16_t memory_version;
545 uint16_t field_id;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000546 if (!field_info.IsResolved() || field_info.IsVolatile()) {
547 // Volatile fields always get a new memory version; field id is irrelevant.
548 // Unresolved fields may be volatile, so handle them as such to be safe.
549 field_id = 0u;
550 memory_version = next_memory_version_;
551 ++next_memory_version_;
552 } else {
553 DCHECK(field_info.IsResolved());
554 field_id = GetFieldId(field_info.DeclaringDexFile(), field_info.DeclaringFieldIndex());
555 memory_version = std::max(unresolved_ifield_version_[type],
556 GetMemoryVersion(base, field_id, type));
557 }
buzbee2502e002012-12-31 16:05:53 -0800558 if (opcode == Instruction::IGET_WIDE) {
Vladimir Markof59f18b2014-02-17 15:53:57 +0000559 res = LookupValue(Instruction::IGET_WIDE, base, field_id, memory_version);
buzbee2502e002012-12-31 16:05:53 -0800560 SetOperandValueWide(mir->ssa_rep->defs[0], res);
561 } else {
Vladimir Markof59f18b2014-02-17 15:53:57 +0000562 res = LookupValue(Instruction::IGET, base, field_id, memory_version);
buzbee2502e002012-12-31 16:05:53 -0800563 SetOperandValue(mir->ssa_rep->defs[0], res);
564 }
565 }
566 break;
567
buzbee2502e002012-12-31 16:05:53 -0800568 case Instruction::IPUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000569 HandlePutObject(mir);
570 // Intentional fall-through.
buzbee2502e002012-12-31 16:05:53 -0800571 case Instruction::IPUT:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000572 case Instruction::IPUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -0800573 case Instruction::IPUT_BOOLEAN:
574 case Instruction::IPUT_BYTE:
575 case Instruction::IPUT_CHAR:
576 case Instruction::IPUT_SHORT: {
Vladimir Markof59f18b2014-02-17 15:53:57 +0000577 uint16_t type = opcode - Instruction::IPUT;
buzbee2502e002012-12-31 16:05:53 -0800578 int base_reg = (opcode == Instruction::IPUT_WIDE) ? 2 : 1;
579 uint16_t base = GetOperandValue(mir->ssa_rep->uses[base_reg]);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000580 HandleNullCheck(mir, base);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000581 const MirFieldInfo& field_info = cu_->mir_graph->GetIFieldLoweringInfo(mir);
582 if (!field_info.IsResolved()) {
583 // Unresolved fields always alias with everything of the same type.
584 unresolved_ifield_version_[type] = next_memory_version_;
585 ++next_memory_version_;
586 } else if (field_info.IsVolatile()) {
587 // Nothing to do, resolved volatile fields always get a new memory version anyway and
588 // can't alias with resolved non-volatile fields.
589 } else {
590 AdvanceMemoryVersion(base, GetFieldId(field_info.DeclaringDexFile(),
591 field_info.DeclaringFieldIndex()), type);
592 }
buzbee2502e002012-12-31 16:05:53 -0800593 }
594 break;
595
596 case Instruction::SGET_OBJECT:
597 case Instruction::SGET:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000598 case Instruction::SGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -0800599 case Instruction::SGET_BOOLEAN:
600 case Instruction::SGET_BYTE:
601 case Instruction::SGET_CHAR:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000602 case Instruction::SGET_SHORT: {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000603 uint16_t type = opcode - Instruction::SGET;
604 const MirFieldInfo& field_info = cu_->mir_graph->GetSFieldLoweringInfo(mir);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000605 uint16_t memory_version;
606 uint16_t field_id;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000607 if (!field_info.IsResolved() || field_info.IsVolatile()) {
608 // Volatile fields always get a new memory version; field id is irrelevant.
609 // Unresolved fields may be volatile, so handle them as such to be safe.
610 field_id = 0u;
611 memory_version = next_memory_version_;
612 ++next_memory_version_;
613 } else {
614 DCHECK(field_info.IsResolved());
615 field_id = GetFieldId(field_info.DeclaringDexFile(), field_info.DeclaringFieldIndex());
616 memory_version = std::max(unresolved_sfield_version_[type],
617 GetMemoryVersion(NO_VALUE, field_id, type));
618 }
buzbee2502e002012-12-31 16:05:53 -0800619 if (opcode == Instruction::SGET_WIDE) {
Vladimir Markof59f18b2014-02-17 15:53:57 +0000620 res = LookupValue(Instruction::SGET_WIDE, NO_VALUE, field_id, memory_version);
buzbee2502e002012-12-31 16:05:53 -0800621 SetOperandValueWide(mir->ssa_rep->defs[0], res);
622 } else {
Vladimir Markof59f18b2014-02-17 15:53:57 +0000623 res = LookupValue(Instruction::SGET, NO_VALUE, field_id, memory_version);
buzbee2502e002012-12-31 16:05:53 -0800624 SetOperandValue(mir->ssa_rep->defs[0], res);
625 }
626 }
627 break;
628
629 case Instruction::SPUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000630 HandlePutObject(mir);
631 // Intentional fall-through.
buzbee2502e002012-12-31 16:05:53 -0800632 case Instruction::SPUT:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000633 case Instruction::SPUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -0800634 case Instruction::SPUT_BOOLEAN:
635 case Instruction::SPUT_BYTE:
636 case Instruction::SPUT_CHAR:
Vladimir Markof59f18b2014-02-17 15:53:57 +0000637 case Instruction::SPUT_SHORT: {
638 uint16_t type = opcode - Instruction::SPUT;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000639 const MirFieldInfo& field_info = cu_->mir_graph->GetSFieldLoweringInfo(mir);
640 if (!field_info.IsResolved()) {
641 // Unresolved fields always alias with everything of the same type.
642 unresolved_sfield_version_[type] = next_memory_version_;
643 ++next_memory_version_;
644 } else if (field_info.IsVolatile()) {
645 // Nothing to do, resolved volatile fields always get a new memory version anyway and
646 // can't alias with resolved non-volatile fields.
647 } else {
648 AdvanceMemoryVersion(NO_VALUE, GetFieldId(field_info.DeclaringDexFile(),
649 field_info.DeclaringFieldIndex()), type);
650 }
buzbee2502e002012-12-31 16:05:53 -0800651 }
652 break;
buzbee2502e002012-12-31 16:05:53 -0800653 }
654 return res;
655}
656
657} // namespace art