blob: ca72f3f2425243d20594f3c298d43e3c7e60a98a [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002 * Copyright (C) 2014 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
Nicolas Geoffraye5038322014-07-04 09:41:32 +010017#include "builder.h"
18
Andreas Gamped881df52014-11-24 23:28:39 -080019#include "base/logging.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010020#include "class_linker.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000021#include "dex_file.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000022#include "dex_file-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "dex_instruction.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000024#include "dex_instruction-inl.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010025#include "driver/compiler_driver-inl.h"
26#include "mirror/art_field.h"
27#include "mirror/art_field-inl.h"
28#include "mirror/class_loader.h"
29#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000031#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010032#include "scoped_thread_state_change.h"
33#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000034
35namespace art {
36
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010037/**
38 * Helper class to add HTemporary instructions. This class is used when
39 * converting a DEX instruction to multiple HInstruction, and where those
40 * instructions do not die at the following instruction, but instead spans
41 * multiple instructions.
42 */
43class Temporaries : public ValueObject {
44 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000045 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010046
47 void Add(HInstruction* instruction) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +000048 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000050
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010051 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000052
53 size_t offset;
54 if (instruction->GetType() == Primitive::kPrimLong
55 || instruction->GetType() == Primitive::kPrimDouble) {
56 offset = 2;
57 } else {
58 offset = 1;
59 }
60 index_ += offset;
61
62 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010063 }
64
65 private:
66 HGraph* const graph_;
67
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010068 // Current index in the temporary stack, updated by `Add`.
69 size_t index_;
70};
71
Andreas Gamped881df52014-11-24 23:28:39 -080072class SwitchTable : public ValueObject {
73 public:
74 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
75 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
76 int32_t table_offset = instruction.VRegB_31t();
77 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
78 if (sparse) {
79 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
80 } else {
81 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
82 }
83 num_entries_ = table[1];
84 values_ = reinterpret_cast<const int32_t*>(&table[2]);
85 }
86
87 uint16_t GetNumEntries() const {
88 return num_entries_;
89 }
90
Andreas Gampee4d4d322014-12-04 09:09:57 -080091 void CheckIndex(size_t index) const {
92 if (sparse_) {
93 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
94 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
95 } else {
96 // In a packed table, we have the starting key and num_entries_ values.
97 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
98 }
99 }
100
Andreas Gamped881df52014-11-24 23:28:39 -0800101 int32_t GetEntryAt(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800102 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800103 return values_[index];
104 }
105
106 uint32_t GetDexPcForIndex(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800107 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800108 return dex_pc_ +
109 (reinterpret_cast<const int16_t*>(values_ + index) -
110 reinterpret_cast<const int16_t*>(&instruction_));
111 }
112
Andreas Gampee4d4d322014-12-04 09:09:57 -0800113 // Index of the first value in the table.
114 size_t GetFirstValueIndex() const {
115 if (sparse_) {
116 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
117 return num_entries_;
118 } else {
119 // In a packed table, we have the starting key and num_entries_ values.
120 return 1;
121 }
122 }
123
Andreas Gamped881df52014-11-24 23:28:39 -0800124 private:
125 const Instruction& instruction_;
126 const uint32_t dex_pc_;
127
128 // Whether this is a sparse-switch table (or a packed-switch one).
129 const bool sparse_;
130
131 // This can't be const as it needs to be computed off of the given instruction, and complicated
132 // expressions in the initializer list seemed very ugly.
133 uint16_t num_entries_;
134
135 const int32_t* values_;
136
137 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
138};
139
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100140void HGraphBuilder::InitializeLocals(uint16_t count) {
141 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000142 locals_.SetSize(count);
143 for (int i = 0; i < count; i++) {
144 HLocal* local = new (arena_) HLocal(i);
145 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000146 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000147 }
148}
149
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000150void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100151 // dex_compilation_unit_ is null only when unit testing.
152 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000153 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100154 }
155
156 graph_->SetNumberOfInVRegs(number_of_parameters);
157 const char* shorty = dex_compilation_unit_->GetShorty();
158 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100159 int parameter_index = 0;
160
161 if (!dex_compilation_unit_->IsStatic()) {
162 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100163 HParameterValue* parameter =
164 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100165 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100166 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100167 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100168 number_of_parameters--;
169 }
170
171 uint32_t pos = 1;
172 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100173 HParameterValue* parameter =
174 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
175 entry_block_->AddInstruction(parameter);
176 HLocal* local = GetLocalAt(locals_index++);
177 // Store the parameter value in the local that the dex code will use
178 // to reference that parameter.
179 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
180 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
181 || (parameter->GetType() == Primitive::kPrimDouble);
182 if (is_wide) {
183 i++;
184 locals_index++;
185 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100186 }
187 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100188}
189
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100190template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000191void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000192 int32_t target_offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +0000193 PotentiallyAddSuspendCheck(target_offset, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100194 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
195 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700196 T* comparison = new (arena_) T(first, second);
197 current_block_->AddInstruction(comparison);
198 HInstruction* ifinst = new (arena_) HIf(comparison);
199 current_block_->AddInstruction(ifinst);
Calin Juravle225ff812014-11-13 16:46:39 +0000200 HBasicBlock* target = FindBlockStartingAt(dex_pc + target_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700201 DCHECK(target != nullptr);
202 current_block_->AddSuccessor(target);
Calin Juravle225ff812014-11-13 16:46:39 +0000203 target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
Dave Allison20dfc792014-06-16 20:44:29 -0700204 DCHECK(target != nullptr);
205 current_block_->AddSuccessor(target);
206 current_block_ = nullptr;
207}
208
209template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000210void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000211 int32_t target_offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +0000212 PotentiallyAddSuspendCheck(target_offset, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700213 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
214 T* comparison = new (arena_) T(value, GetIntConstant(0));
215 current_block_->AddInstruction(comparison);
216 HInstruction* ifinst = new (arena_) HIf(comparison);
217 current_block_->AddInstruction(ifinst);
Calin Juravle225ff812014-11-13 16:46:39 +0000218 HBasicBlock* target = FindBlockStartingAt(dex_pc + target_offset);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100219 DCHECK(target != nullptr);
220 current_block_->AddSuccessor(target);
Calin Juravle225ff812014-11-13 16:46:39 +0000221 target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100222 DCHECK(target != nullptr);
223 current_block_->AddSuccessor(target);
224 current_block_ = nullptr;
225}
226
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000227static bool ShouldSkipCompilation(const CompilerDriver& compiler_driver,
228 const DexCompilationUnit& dex_compilation_unit,
229 size_t number_of_dex_instructions,
230 size_t number_of_blocks ATTRIBUTE_UNUSED,
231 size_t number_of_branches) {
232 const CompilerOptions& compiler_options = compiler_driver.GetCompilerOptions();
233 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
234 if (compiler_filter == CompilerOptions::kEverything) {
235 return false;
236 }
237
238 if (compiler_options.IsHugeMethod(number_of_dex_instructions)) {
239 LOG(INFO) << "Skip compilation of huge method "
240 << PrettyMethod(dex_compilation_unit.GetDexMethodIndex(),
241 *dex_compilation_unit.GetDexFile())
242 << ": " << number_of_dex_instructions << " dex instructions";
243 return true;
244 }
245
246 // If it's large and contains no branches, it's likely to be machine generated initialization.
247 if (compiler_options.IsLargeMethod(number_of_dex_instructions) && (number_of_branches == 0)) {
248 LOG(INFO) << "Skip compilation of large method with no branch "
249 << PrettyMethod(dex_compilation_unit.GetDexMethodIndex(),
250 *dex_compilation_unit.GetDexFile())
251 << ": " << number_of_dex_instructions << " dex instructions";
252 return true;
253 }
254
255 return false;
256}
257
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000258HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000259 const uint16_t* code_ptr = code_item.insns_;
260 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100261 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000262
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000263 // Setup the graph with the entry block and exit block.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000264 graph_ = new (arena_) HGraph(arena_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100265 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000266 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100267 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000268 graph_->SetEntryBlock(entry_block_);
269 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000270
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000271 InitializeLocals(code_item.registers_size_);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100272 graph_->UpdateMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000273
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000274 // Compute the number of dex instructions, blocks, and branches. We will
275 // check these values against limits given to the compiler.
276 size_t number_of_dex_instructions = 0;
277 size_t number_of_blocks = 0;
278 size_t number_of_branches = 0;
279
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000280 // To avoid splitting blocks, we compute ahead of time the instructions that
281 // start a new block, and create these blocks.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000282 ComputeBranchTargets(
283 code_ptr, code_end, &number_of_dex_instructions, &number_of_blocks, &number_of_branches);
284
285 // Note that the compiler driver is null when unit testing.
286 if (compiler_driver_ != nullptr) {
287 if (ShouldSkipCompilation(*compiler_driver_,
288 *dex_compilation_unit_,
289 number_of_dex_instructions,
290 number_of_blocks,
291 number_of_branches)) {
292 return nullptr;
293 }
294 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000295
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000296 // Also create blocks for catch handlers.
297 if (code_item.tries_size_ != 0) {
298 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
299 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
300 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
301 CatchHandlerIterator iterator(handlers_ptr);
302 for (; iterator.HasNext(); iterator.Next()) {
303 uint32_t address = iterator.GetHandlerAddress();
304 HBasicBlock* block = FindBlockStartingAt(address);
305 if (block == nullptr) {
306 block = new (arena_) HBasicBlock(graph_, address);
307 branch_targets_.Put(address, block);
308 }
309 block->SetIsCatchBlock();
310 }
311 handlers_ptr = iterator.EndDataPointer();
312 }
313 }
314
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000315 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100316
Calin Juravle225ff812014-11-13 16:46:39 +0000317 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000318 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000319 // Update the current block if dex_pc starts a new block.
320 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000321 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle225ff812014-11-13 16:46:39 +0000322 if (!AnalyzeDexInstruction(instruction, dex_pc)) return nullptr;
323 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000324 code_ptr += instruction.SizeInCodeUnits();
325 }
326
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000327 // Add the exit block at the end to give it the highest id.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000328 graph_->AddBlock(exit_block_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000329 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000330 // Add the suspend check to the entry block.
331 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000332 entry_block_->AddInstruction(new (arena_) HGoto());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000333 return graph_;
334}
335
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000336void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) {
337 HBasicBlock* block = FindBlockStartingAt(index);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000338 if (block == nullptr) {
339 return;
340 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000341
342 if (current_block_ != nullptr) {
343 // Branching instructions clear current_block, so we know
344 // the last instruction of the current block is not a branching
345 // instruction. We add an unconditional goto to the found block.
346 current_block_->AddInstruction(new (arena_) HGoto());
347 current_block_->AddSuccessor(block);
348 }
349 graph_->AddBlock(block);
350 current_block_ = block;
351}
352
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000353void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
354 const uint16_t* code_end,
355 size_t* number_of_dex_instructions,
356 size_t* number_of_blocks,
357 size_t* number_of_branches) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000358 branch_targets_.SetSize(code_end - code_ptr);
359
360 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100361 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000362 branch_targets_.Put(0, block);
363 entry_block_->AddSuccessor(block);
364
365 // Iterate over all instructions and find branching instructions. Create blocks for
366 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800367 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000368 while (code_ptr < code_end) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000369 (*number_of_dex_instructions)++;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000370 const Instruction& instruction = *Instruction::At(code_ptr);
371 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000372 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000373 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000374 // Create a block for the target instruction.
375 if (FindBlockStartingAt(target) == nullptr) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100376 block = new (arena_) HBasicBlock(graph_, target);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000377 branch_targets_.Put(target, block);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000378 (*number_of_blocks)++;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000379 }
Calin Juravle225ff812014-11-13 16:46:39 +0000380 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000381 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000382 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_pc) == nullptr)) {
383 block = new (arena_) HBasicBlock(graph_, dex_pc);
384 branch_targets_.Put(dex_pc, block);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000385 (*number_of_blocks)++;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000386 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800387 } else if (instruction.IsSwitch()) {
388 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800389
390 uint16_t num_entries = table.GetNumEntries();
391
Andreas Gampee4d4d322014-12-04 09:09:57 -0800392 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
393 // entry at index 0 is the first key, and values are after *all* keys.
394 size_t offset = table.GetFirstValueIndex();
395
396 // Use a larger loop counter type to avoid overflow issues.
397 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800398 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800399 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
Andreas Gamped881df52014-11-24 23:28:39 -0800400 if (FindBlockStartingAt(target) == nullptr) {
401 block = new (arena_) HBasicBlock(graph_, target);
402 branch_targets_.Put(target, block);
403 (*number_of_blocks)++;
404 }
405
406 // The next case gets its own block.
407 if (i < num_entries) {
408 block = new (arena_) HBasicBlock(graph_, target);
409 branch_targets_.Put(table.GetDexPcForIndex(i), block);
410 (*number_of_blocks)++;
411 }
412 }
413
414 // Fall-through. Add a block if there is more code afterwards.
415 dex_pc += instruction.SizeInCodeUnits();
416 code_ptr += instruction.SizeInCodeUnits();
417 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_pc) == nullptr)) {
418 block = new (arena_) HBasicBlock(graph_, dex_pc);
419 branch_targets_.Put(dex_pc, block);
420 (*number_of_blocks)++;
421 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000422 } else {
423 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000424 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000425 }
426 }
427}
428
429HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const {
430 DCHECK_GE(index, 0);
431 return branch_targets_.Get(index);
432}
433
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100434template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100435void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
436 HInstruction* first = LoadLocal(instruction.VRegB(), type);
437 current_block_->AddInstruction(new (arena_) T(type, first));
438 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
439}
440
Roland Levillaindff1f282014-11-05 14:15:05 +0000441void HGraphBuilder::Conversion_12x(const Instruction& instruction,
442 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000443 Primitive::Type result_type,
444 uint32_t dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +0000445 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Roland Levillain624279f2014-12-04 11:54:28 +0000446 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Roland Levillaindff1f282014-11-05 14:15:05 +0000447 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
448}
449
Roland Levillain88cb1752014-10-20 16:36:47 +0100450template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100451void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100452 HInstruction* first = LoadLocal(instruction.VRegB(), type);
453 HInstruction* second = LoadLocal(instruction.VRegC(), type);
454 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100455 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
456}
457
458template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000459void HGraphBuilder::Binop_23x(const Instruction& instruction,
460 Primitive::Type type,
461 uint32_t dex_pc) {
462 HInstruction* first = LoadLocal(instruction.VRegB(), type);
463 HInstruction* second = LoadLocal(instruction.VRegC(), type);
464 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
465 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
466}
467
468template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000469void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
470 Primitive::Type type) {
471 HInstruction* first = LoadLocal(instruction.VRegB(), type);
472 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
473 current_block_->AddInstruction(new (arena_) T(type, first, second));
474 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
475}
476
Calin Juravleddb7df22014-11-25 20:56:51 +0000477void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
478 Primitive::Type type,
479 HCompare::Bias bias) {
480 HInstruction* first = LoadLocal(instruction.VRegB(), type);
481 HInstruction* second = LoadLocal(instruction.VRegC(), type);
482 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias));
483 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
484}
485
Calin Juravle9aec02f2014-11-18 23:06:35 +0000486template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100487void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
488 HInstruction* first = LoadLocal(instruction.VRegA(), type);
489 HInstruction* second = LoadLocal(instruction.VRegB(), type);
490 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100491 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
492}
493
494template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000495void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) {
496 HInstruction* first = LoadLocal(instruction.VRegA(), type);
497 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
498 current_block_->AddInstruction(new (arena_) T(type, first, second));
499 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
500}
501
502template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000503void HGraphBuilder::Binop_12x(const Instruction& instruction,
504 Primitive::Type type,
505 uint32_t dex_pc) {
506 HInstruction* first = LoadLocal(instruction.VRegA(), type);
507 HInstruction* second = LoadLocal(instruction.VRegB(), type);
508 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
509 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
510}
511
512template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100513void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100514 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
515 HInstruction* second = GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100516 if (reverse) {
517 std::swap(first, second);
518 }
519 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
520 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
521}
522
523template<typename T>
524void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100525 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
526 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100527 if (reverse) {
528 std::swap(first, second);
529 }
530 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
531 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
532}
533
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100534void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
535 if (type == Primitive::kPrimVoid) {
536 current_block_->AddInstruction(new (arena_) HReturnVoid());
537 } else {
538 HInstruction* value = LoadLocal(instruction.VRegA(), type);
539 current_block_->AddInstruction(new (arena_) HReturn(value));
540 }
541 current_block_->AddSuccessor(exit_block_);
542 current_block_ = nullptr;
543}
544
545bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000546 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100547 uint32_t method_idx,
548 uint32_t number_of_vreg_arguments,
549 bool is_range,
550 uint32_t* args,
551 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100552 Instruction::Code opcode = instruction.Opcode();
553 InvokeType invoke_type;
554 switch (opcode) {
555 case Instruction::INVOKE_STATIC:
556 case Instruction::INVOKE_STATIC_RANGE:
557 invoke_type = kStatic;
558 break;
559 case Instruction::INVOKE_DIRECT:
560 case Instruction::INVOKE_DIRECT_RANGE:
561 invoke_type = kDirect;
562 break;
563 case Instruction::INVOKE_VIRTUAL:
564 case Instruction::INVOKE_VIRTUAL_RANGE:
565 invoke_type = kVirtual;
566 break;
567 case Instruction::INVOKE_INTERFACE:
568 case Instruction::INVOKE_INTERFACE_RANGE:
569 invoke_type = kInterface;
570 break;
571 case Instruction::INVOKE_SUPER_RANGE:
572 case Instruction::INVOKE_SUPER:
573 invoke_type = kSuper;
574 break;
575 default:
576 LOG(FATAL) << "Unexpected invoke op: " << opcode;
577 return false;
578 }
579
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100580 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
581 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
582 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
583 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100584 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100585 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
586
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000587 MethodReference target_method(dex_file_, method_idx);
588 uintptr_t direct_code;
589 uintptr_t direct_method;
590 int table_index;
591 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000592
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000593 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
594 &optimized_invoke_type, &target_method, &table_index,
595 &direct_code, &direct_method)) {
596 LOG(INFO) << "Did not compile " << PrettyMethod(method_idx, *dex_file_)
597 << " because a method call could not be resolved";
598 return false;
599 }
600 DCHECK(optimized_invoke_type != kSuper);
601
602 HInvoke* invoke = nullptr;
603 if (optimized_invoke_type == kVirtual) {
604 invoke = new (arena_) HInvokeVirtual(
605 arena_, number_of_arguments, return_type, dex_pc, table_index);
606 } else if (optimized_invoke_type == kInterface) {
607 invoke = new (arena_) HInvokeInterface(
608 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100609 } else {
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000610 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic);
611 // Sharpening to kDirect only works if we compile PIC.
612 DCHECK((optimized_invoke_type == invoke_type) || (optimized_invoke_type != kDirect)
613 || compiler_driver_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100614 // Treat invoke-direct like static calls for now.
615 invoke = new (arena_) HInvokeStatic(
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000616 arena_, number_of_arguments, return_type, dex_pc, target_method.dex_method_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100617 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100618
619 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000620 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100621 if (is_instance_call) {
622 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000623 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100624 current_block_->AddInstruction(null_check);
625 temps.Add(null_check);
626 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100627 start_index = 1;
628 }
629
630 uint32_t descriptor_index = 1;
631 uint32_t argument_index = start_index;
632 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
633 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100634 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
635 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100636 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
Calin Juravle225ff812014-11-13 16:46:39 +0000637 << " at " << dex_pc;
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100638 // We do not implement non sequential register pair.
639 return false;
640 }
641 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
642 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100643 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100644 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100645 }
646 }
647
648 DCHECK_EQ(argument_index, number_of_arguments);
649 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100650 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100651 return true;
652}
653
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100654bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000655 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100656 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100657 uint32_t source_or_dest_reg = instruction.VRegA_22c();
658 uint32_t obj_reg = instruction.VRegB_22c();
659 uint16_t field_index = instruction.VRegC_22c();
660
661 ScopedObjectAccess soa(Thread::Current());
662 StackHandleScope<1> hs(soa.Self());
663 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
664 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
665
666 if (resolved_field.Get() == nullptr) {
667 return false;
668 }
669 if (resolved_field->IsVolatile()) {
670 return false;
671 }
672
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100673 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100674
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100675 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000676 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100677 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000678 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100679 HInstruction* null_check = current_block_->GetLastInstruction();
680 // We need one temporary for the null check.
681 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100682 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100683 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
684 null_check,
685 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100686 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100687 resolved_field->GetOffset()));
688 } else {
689 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
690 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100691 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100692 resolved_field->GetOffset()));
693
694 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
695 }
696 return true;
697}
698
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100699
700
701bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000702 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100703 bool is_put) {
704 uint32_t source_or_dest_reg = instruction.VRegA_21c();
705 uint16_t field_index = instruction.VRegB_21c();
706
707 uint32_t storage_index;
708 bool is_referrers_class;
709 bool is_initialized;
710 bool is_volatile;
711 MemberOffset field_offset(0u);
712 Primitive::Type field_type;
713
714 bool fast_path = compiler_driver_->ComputeStaticFieldInfo(field_index,
715 dex_compilation_unit_,
716 is_put,
717 &field_offset,
718 &storage_index,
719 &is_referrers_class,
720 &is_volatile,
721 &is_initialized,
722 &field_type);
723 if (!fast_path) {
724 return false;
725 }
726
727 if (is_volatile) {
728 return false;
729 }
730
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100731 HLoadClass* constant = new (arena_) HLoadClass(
Calin Juravle225ff812014-11-13 16:46:39 +0000732 storage_index, is_referrers_class, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100733 current_block_->AddInstruction(constant);
734
735 HInstruction* cls = constant;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000736 if (!is_initialized) {
Calin Juravle225ff812014-11-13 16:46:39 +0000737 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100738 current_block_->AddInstruction(cls);
739 }
740
741 if (is_put) {
742 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000743 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100744 temps.Add(cls);
745 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
746 DCHECK_EQ(value->GetType(), field_type);
747 current_block_->AddInstruction(
748 new (arena_) HStaticFieldSet(cls, value, field_type, field_offset));
749 } else {
750 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls, field_type, field_offset));
751 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
752 }
753 return true;
754}
755
Calin Juravlebacfec32014-11-14 15:54:36 +0000756void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
757 uint16_t first_vreg,
758 int64_t second_vreg_or_constant,
759 uint32_t dex_pc,
760 Primitive::Type type,
761 bool second_is_constant,
762 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000763 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +0000764
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000765 HInstruction* first = LoadLocal(first_vreg, type);
766 HInstruction* second = nullptr;
767 if (second_is_constant) {
768 if (type == Primitive::kPrimInt) {
769 second = GetIntConstant(second_vreg_or_constant);
770 } else {
771 second = GetLongConstant(second_vreg_or_constant);
772 }
773 } else {
774 second = LoadLocal(second_vreg_or_constant, type);
775 }
776
777 if (!second_is_constant
778 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
779 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
780 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000781 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000782 current_block_->AddInstruction(second);
783 temps.Add(current_block_->GetLastInstruction());
784 }
785
Calin Juravlebacfec32014-11-14 15:54:36 +0000786 if (isDiv) {
787 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
788 } else {
789 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
790 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000791 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +0000792}
793
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100794void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000795 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100796 bool is_put,
797 Primitive::Type anticipated_type) {
798 uint8_t source_or_dest_reg = instruction.VRegA_23x();
799 uint8_t array_reg = instruction.VRegB_23x();
800 uint8_t index_reg = instruction.VRegC_23x();
801
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100802 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000803 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100804
805 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000806 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100807 current_block_->AddInstruction(object);
808 temps.Add(object);
809
810 HInstruction* length = new (arena_) HArrayLength(object);
811 current_block_->AddInstruction(length);
812 temps.Add(length);
813 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +0000814 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100815 current_block_->AddInstruction(index);
816 temps.Add(index);
817 if (is_put) {
818 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
819 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100820 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000821 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100822 } else {
823 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
824 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
825 }
826}
827
Calin Juravle225ff812014-11-13 16:46:39 +0000828void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100829 uint32_t type_index,
830 uint32_t number_of_vreg_arguments,
831 bool is_range,
832 uint32_t* args,
833 uint32_t register_index) {
834 HInstruction* length = GetIntConstant(number_of_vreg_arguments);
Calin Juravle225ff812014-11-13 16:46:39 +0000835 HInstruction* object = new (arena_) HNewArray(length, dex_pc, type_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100836 current_block_->AddInstruction(object);
837
838 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
839 DCHECK_EQ(descriptor[0], '[') << descriptor;
840 char primitive = descriptor[1];
841 DCHECK(primitive == 'I'
842 || primitive == 'L'
843 || primitive == '[') << descriptor;
844 bool is_reference_array = (primitive == 'L') || (primitive == '[');
845 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
846
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000847 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100848 temps.Add(object);
849 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
850 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
851 HInstruction* index = GetIntConstant(i);
852 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000853 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100854 }
855 latest_result_ = object;
856}
857
858template <typename T>
859void HGraphBuilder::BuildFillArrayData(HInstruction* object,
860 const T* data,
861 uint32_t element_count,
862 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +0000863 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100864 for (uint32_t i = 0; i < element_count; ++i) {
865 HInstruction* index = GetIntConstant(i);
866 HInstruction* value = GetIntConstant(data[i]);
867 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000868 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100869 }
870}
871
Calin Juravle225ff812014-11-13 16:46:39 +0000872void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000873 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000874 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000875 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000876 current_block_->AddInstruction(null_check);
877 temps.Add(null_check);
878
879 HInstruction* length = new (arena_) HArrayLength(null_check);
880 current_block_->AddInstruction(length);
881
Calin Juravle225ff812014-11-13 16:46:39 +0000882 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +0000883 const Instruction::ArrayDataPayload* payload =
884 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
885 const uint8_t* data = payload->data;
886 uint32_t element_count = payload->element_count;
887
888 // Implementation of this DEX instruction seems to be that the bounds check is
889 // done before doing any stores.
890 HInstruction* last_index = GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +0000891 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +0000892
893 switch (payload->element_width) {
894 case 1:
895 BuildFillArrayData(null_check,
896 reinterpret_cast<const int8_t*>(data),
897 element_count,
898 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +0000899 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000900 break;
901 case 2:
902 BuildFillArrayData(null_check,
903 reinterpret_cast<const int16_t*>(data),
904 element_count,
905 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +0000906 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000907 break;
908 case 4:
909 BuildFillArrayData(null_check,
910 reinterpret_cast<const int32_t*>(data),
911 element_count,
912 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +0000913 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000914 break;
915 case 8:
916 BuildFillWideArrayData(null_check,
917 reinterpret_cast<const int64_t*>(data),
918 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000919 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000920 break;
921 default:
922 LOG(FATAL) << "Unknown element width for " << payload->element_width;
923 }
924}
925
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100926void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100927 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100928 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000929 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100930 for (uint32_t i = 0; i < element_count; ++i) {
931 HInstruction* index = GetIntConstant(i);
932 HInstruction* value = GetLongConstant(data[i]);
933 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000934 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100935 }
936}
937
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000938bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
939 uint8_t destination,
940 uint8_t reference,
941 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +0000942 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000943 bool type_known_final;
944 bool type_known_abstract;
945 bool is_referrers_class;
946 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
947 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
948 &type_known_final, &type_known_abstract, &is_referrers_class);
949 if (!can_access) {
950 return false;
951 }
952 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000953 HLoadClass* cls = new (arena_) HLoadClass(type_index, is_referrers_class, dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000954 current_block_->AddInstruction(cls);
955 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000956 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000957 temps.Add(cls);
958 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
959 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000960 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000961 UpdateLocal(destination, current_block_->GetLastInstruction());
962 } else {
963 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
964 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000965 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000966 }
967 return true;
968}
969
Andreas Gamped881df52014-11-24 23:28:39 -0800970bool HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
971 SwitchTable table(instruction, dex_pc, false);
972
973 // Value to test against.
974 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
975
Andreas Gampee4d4d322014-12-04 09:09:57 -0800976 uint16_t num_entries = table.GetNumEntries();
977 // There should be at least one entry here.
978 DCHECK_GT(num_entries, 0U);
979
Andreas Gamped881df52014-11-24 23:28:39 -0800980 // Chained cmp-and-branch, starting from starting_key.
981 int32_t starting_key = table.GetEntryAt(0);
982
Andreas Gamped881df52014-11-24 23:28:39 -0800983 for (size_t i = 1; i <= num_entries; i++) {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800984 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value, starting_key + i - 1,
985 table.GetEntryAt(i), dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800986 }
987 return true;
988}
989
Andreas Gampee4d4d322014-12-04 09:09:57 -0800990bool HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
991 SwitchTable table(instruction, dex_pc, true);
992
993 // Value to test against.
994 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
995
996 uint16_t num_entries = table.GetNumEntries();
997 // There should be at least one entry here.
998 DCHECK_GT(num_entries, 0U);
999
1000 for (size_t i = 0; i < num_entries; i++) {
1001 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1002 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1003 }
1004 return true;
1005}
1006
1007void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1008 bool is_last_case, const SwitchTable& table,
1009 HInstruction* value, int32_t case_value_int,
1010 int32_t target_offset, uint32_t dex_pc) {
1011 PotentiallyAddSuspendCheck(target_offset, dex_pc);
1012
1013 // The current case's value.
1014 HInstruction* this_case_value = GetIntConstant(case_value_int);
1015
1016 // Compare value and this_case_value.
1017 HEqual* comparison = new (arena_) HEqual(value, this_case_value);
1018 current_block_->AddInstruction(comparison);
1019 HInstruction* ifinst = new (arena_) HIf(comparison);
1020 current_block_->AddInstruction(ifinst);
1021
1022 // Case hit: use the target offset to determine where to go.
1023 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1024 DCHECK(case_target != nullptr);
1025 current_block_->AddSuccessor(case_target);
1026
1027 // Case miss: go to the next case (or default fall-through).
1028 // When there is a next case, we use the block stored with the table offset representing this
1029 // case (that is where we registered them in ComputeBranchTargets).
1030 // When there is no next case, we use the following instruction.
1031 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1032 if (!is_last_case) {
1033 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1034 DCHECK(next_case_target != nullptr);
1035 current_block_->AddSuccessor(next_case_target);
1036
1037 // Need to manually add the block, as there is no dex-pc transition for the cases.
1038 graph_->AddBlock(next_case_target);
1039
1040 current_block_ = next_case_target;
1041 } else {
1042 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1043 DCHECK(default_target != nullptr);
1044 current_block_->AddSuccessor(default_target);
1045 current_block_ = nullptr;
1046 }
1047}
1048
Calin Juravle225ff812014-11-13 16:46:39 +00001049void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001050 if (target_offset <= 0) {
1051 // Unconditionnally add a suspend check to backward branches. We can remove
1052 // them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001053 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001054 }
1055}
1056
Calin Juravle225ff812014-11-13 16:46:39 +00001057bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001058 if (current_block_ == nullptr) {
1059 return true; // Dead code
1060 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001061
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001062 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001063 case Instruction::CONST_4: {
1064 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001065 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001066 UpdateLocal(register_index, constant);
1067 break;
1068 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001069
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001070 case Instruction::CONST_16: {
1071 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
1073 UpdateLocal(register_index, constant);
1074 break;
1075 }
1076
Dave Allison20dfc792014-06-16 20:44:29 -07001077 case Instruction::CONST: {
1078 int32_t register_index = instruction.VRegA();
1079 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
1080 UpdateLocal(register_index, constant);
1081 break;
1082 }
1083
1084 case Instruction::CONST_HIGH16: {
1085 int32_t register_index = instruction.VRegA();
1086 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
1087 UpdateLocal(register_index, constant);
1088 break;
1089 }
1090
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091 case Instruction::CONST_WIDE_16: {
1092 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001093 // Get 16 bits of constant value, sign extended to 64 bits.
1094 int64_t value = instruction.VRegB_21s();
1095 value <<= 48;
1096 value >>= 48;
1097 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098 UpdateLocal(register_index, constant);
1099 break;
1100 }
1101
1102 case Instruction::CONST_WIDE_32: {
1103 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001104 // Get 32 bits of constant value, sign extended to 64 bits.
1105 int64_t value = instruction.VRegB_31i();
1106 value <<= 32;
1107 value >>= 32;
1108 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001109 UpdateLocal(register_index, constant);
1110 break;
1111 }
1112
1113 case Instruction::CONST_WIDE: {
1114 int32_t register_index = instruction.VRegA();
1115 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001116 UpdateLocal(register_index, constant);
1117 break;
1118 }
1119
Dave Allison20dfc792014-06-16 20:44:29 -07001120 case Instruction::CONST_WIDE_HIGH16: {
1121 int32_t register_index = instruction.VRegA();
1122 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1123 HLongConstant* constant = GetLongConstant(value);
1124 UpdateLocal(register_index, constant);
1125 break;
1126 }
1127
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001128 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001129 case Instruction::MOVE:
1130 case Instruction::MOVE_FROM16:
1131 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001132 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001133 UpdateLocal(instruction.VRegA(), value);
1134 break;
1135 }
1136
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001137 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001138 case Instruction::MOVE_WIDE:
1139 case Instruction::MOVE_WIDE_FROM16:
1140 case Instruction::MOVE_WIDE_16: {
1141 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1142 UpdateLocal(instruction.VRegA(), value);
1143 break;
1144 }
1145
1146 case Instruction::MOVE_OBJECT:
1147 case Instruction::MOVE_OBJECT_16:
1148 case Instruction::MOVE_OBJECT_FROM16: {
1149 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
1150 UpdateLocal(instruction.VRegA(), value);
1151 break;
1152 }
1153
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001154 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001155 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001156 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001157 }
1158
Dave Allison20dfc792014-06-16 20:44:29 -07001159#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001160 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1161 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001162
Dave Allison20dfc792014-06-16 20:44:29 -07001163 IF_XX(HEqual, EQ);
1164 IF_XX(HNotEqual, NE);
1165 IF_XX(HLessThan, LT);
1166 IF_XX(HLessThanOrEqual, LE);
1167 IF_XX(HGreaterThan, GT);
1168 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001169
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001170 case Instruction::GOTO:
1171 case Instruction::GOTO_16:
1172 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001173 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001174 PotentiallyAddSuspendCheck(offset, dex_pc);
1175 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001176 DCHECK(target != nullptr);
1177 current_block_->AddInstruction(new (arena_) HGoto());
1178 current_block_->AddSuccessor(target);
1179 current_block_ = nullptr;
1180 break;
1181 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001182
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001183 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001184 DCHECK_NE(return_type_, Primitive::kPrimNot);
1185 DCHECK_NE(return_type_, Primitive::kPrimLong);
1186 DCHECK_NE(return_type_, Primitive::kPrimDouble);
1187 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001188 break;
1189 }
1190
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001191 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001192 DCHECK(return_type_ == Primitive::kPrimNot);
1193 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001194 break;
1195 }
1196
1197 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001198 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
1199 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001200 break;
1201 }
1202
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001203 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001204 case Instruction::INVOKE_INTERFACE:
1205 case Instruction::INVOKE_STATIC:
1206 case Instruction::INVOKE_SUPER:
1207 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001208 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001209 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001210 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001211 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001212 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001213 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001214 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001215 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001216 break;
1217 }
1218
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001219 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001220 case Instruction::INVOKE_INTERFACE_RANGE:
1221 case Instruction::INVOKE_STATIC_RANGE:
1222 case Instruction::INVOKE_SUPER_RANGE:
1223 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001224 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001225 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1226 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001227 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001228 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001229 return false;
1230 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001231 break;
1232 }
1233
Roland Levillain88cb1752014-10-20 16:36:47 +01001234 case Instruction::NEG_INT: {
1235 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1236 break;
1237 }
1238
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001239 case Instruction::NEG_LONG: {
1240 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1241 break;
1242 }
1243
Roland Levillain3dbcb382014-10-28 17:30:07 +00001244 case Instruction::NEG_FLOAT: {
1245 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1246 break;
1247 }
1248
1249 case Instruction::NEG_DOUBLE: {
1250 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1251 break;
1252 }
1253
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001254 case Instruction::NOT_INT: {
1255 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1256 break;
1257 }
1258
Roland Levillain70566432014-10-24 16:20:17 +01001259 case Instruction::NOT_LONG: {
1260 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1261 break;
1262 }
1263
Roland Levillaindff1f282014-11-05 14:15:05 +00001264 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001265 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001266 break;
1267 }
1268
Roland Levillaincff13742014-11-17 14:32:17 +00001269 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001270 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001271 break;
1272 }
1273
1274 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001275 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001276 break;
1277 }
1278
Roland Levillain946e1432014-11-11 17:35:19 +00001279 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001280 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001281 break;
1282 }
1283
Roland Levillain6d0e4832014-11-27 18:31:21 +00001284 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001285 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001286 break;
1287 }
1288
Roland Levillain647b9ed2014-11-27 12:06:00 +00001289 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001290 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001291 break;
1292 }
1293
Roland Levillain3f8f9362014-12-02 17:45:01 +00001294 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001295 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1296 break;
1297 }
1298
1299 case Instruction::FLOAT_TO_LONG: {
1300 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001301 break;
1302 }
1303
Roland Levillain8964e2b2014-12-04 12:10:50 +00001304 case Instruction::FLOAT_TO_DOUBLE: {
1305 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1306 break;
1307 }
1308
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001309 case Instruction::DOUBLE_TO_INT: {
1310 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1311 break;
1312 }
1313
1314 case Instruction::DOUBLE_TO_LONG: {
1315 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1316 break;
1317 }
1318
Roland Levillain8964e2b2014-12-04 12:10:50 +00001319 case Instruction::DOUBLE_TO_FLOAT: {
1320 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1321 break;
1322 }
1323
Roland Levillain51d3fc42014-11-13 14:11:42 +00001324 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001325 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001326 break;
1327 }
1328
Roland Levillain01a8d712014-11-14 16:27:39 +00001329 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001330 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001331 break;
1332 }
1333
Roland Levillain981e4542014-11-14 11:47:14 +00001334 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001335 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001336 break;
1337 }
1338
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001339 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001340 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001341 break;
1342 }
1343
1344 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001345 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001346 break;
1347 }
1348
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001349 case Instruction::ADD_DOUBLE: {
1350 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1351 break;
1352 }
1353
1354 case Instruction::ADD_FLOAT: {
1355 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1356 break;
1357 }
1358
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001359 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001360 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001361 break;
1362 }
1363
1364 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001365 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001366 break;
1367 }
1368
Calin Juravle096cc022014-10-23 17:01:13 +01001369 case Instruction::SUB_FLOAT: {
1370 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1371 break;
1372 }
1373
1374 case Instruction::SUB_DOUBLE: {
1375 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1376 break;
1377 }
1378
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001379 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001380 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1381 break;
1382 }
1383
Calin Juravle34bacdf2014-10-07 20:23:36 +01001384 case Instruction::MUL_INT: {
1385 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1386 break;
1387 }
1388
1389 case Instruction::MUL_LONG: {
1390 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1391 break;
1392 }
1393
Calin Juravleb5bfa962014-10-21 18:02:24 +01001394 case Instruction::MUL_FLOAT: {
1395 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1396 break;
1397 }
1398
1399 case Instruction::MUL_DOUBLE: {
1400 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1401 break;
1402 }
1403
Calin Juravled0d48522014-11-04 16:40:20 +00001404 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001405 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1406 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001407 break;
1408 }
1409
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001410 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001411 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1412 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001413 break;
1414 }
1415
Calin Juravle7c4954d2014-10-28 16:57:40 +00001416 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001417 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001418 break;
1419 }
1420
1421 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001422 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001423 break;
1424 }
1425
Calin Juravlebacfec32014-11-14 15:54:36 +00001426 case Instruction::REM_INT: {
1427 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1428 dex_pc, Primitive::kPrimInt, false, false);
1429 break;
1430 }
1431
1432 case Instruction::REM_LONG: {
1433 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1434 dex_pc, Primitive::kPrimLong, false, false);
1435 break;
1436 }
1437
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001438 case Instruction::AND_INT: {
1439 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1440 break;
1441 }
1442
1443 case Instruction::AND_LONG: {
1444 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1445 break;
1446 }
1447
Calin Juravle9aec02f2014-11-18 23:06:35 +00001448 case Instruction::SHL_INT: {
1449 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
1450 break;
1451 }
1452
1453 case Instruction::SHL_LONG: {
1454 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
1455 break;
1456 }
1457
1458 case Instruction::SHR_INT: {
1459 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
1460 break;
1461 }
1462
1463 case Instruction::SHR_LONG: {
1464 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
1465 break;
1466 }
1467
1468 case Instruction::USHR_INT: {
1469 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
1470 break;
1471 }
1472
1473 case Instruction::USHR_LONG: {
1474 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
1475 break;
1476 }
1477
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001478 case Instruction::OR_INT: {
1479 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1480 break;
1481 }
1482
1483 case Instruction::OR_LONG: {
1484 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1485 break;
1486 }
1487
1488 case Instruction::XOR_INT: {
1489 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1490 break;
1491 }
1492
1493 case Instruction::XOR_LONG: {
1494 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1495 break;
1496 }
1497
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001498 case Instruction::ADD_LONG_2ADDR: {
1499 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001500 break;
1501 }
1502
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001503 case Instruction::ADD_DOUBLE_2ADDR: {
1504 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1505 break;
1506 }
1507
1508 case Instruction::ADD_FLOAT_2ADDR: {
1509 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
1510 break;
1511 }
1512
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001513 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001514 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
1515 break;
1516 }
1517
1518 case Instruction::SUB_LONG_2ADDR: {
1519 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001520 break;
1521 }
1522
Calin Juravle096cc022014-10-23 17:01:13 +01001523 case Instruction::SUB_FLOAT_2ADDR: {
1524 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
1525 break;
1526 }
1527
1528 case Instruction::SUB_DOUBLE_2ADDR: {
1529 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
1530 break;
1531 }
1532
Calin Juravle34bacdf2014-10-07 20:23:36 +01001533 case Instruction::MUL_INT_2ADDR: {
1534 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
1535 break;
1536 }
1537
1538 case Instruction::MUL_LONG_2ADDR: {
1539 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
1540 break;
1541 }
1542
Calin Juravleb5bfa962014-10-21 18:02:24 +01001543 case Instruction::MUL_FLOAT_2ADDR: {
1544 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
1545 break;
1546 }
1547
1548 case Instruction::MUL_DOUBLE_2ADDR: {
1549 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
1550 break;
1551 }
1552
Calin Juravle865fc882014-11-06 17:09:03 +00001553 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001554 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1555 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00001556 break;
1557 }
1558
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001559 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001560 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1561 dex_pc, Primitive::kPrimLong, false, true);
1562 break;
1563 }
1564
1565 case Instruction::REM_INT_2ADDR: {
1566 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1567 dex_pc, Primitive::kPrimInt, false, false);
1568 break;
1569 }
1570
1571 case Instruction::REM_LONG_2ADDR: {
1572 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1573 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001574 break;
1575 }
1576
Calin Juravle9aec02f2014-11-18 23:06:35 +00001577 case Instruction::SHL_INT_2ADDR: {
1578 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
1579 break;
1580 }
1581
1582 case Instruction::SHL_LONG_2ADDR: {
1583 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
1584 break;
1585 }
1586
1587 case Instruction::SHR_INT_2ADDR: {
1588 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
1589 break;
1590 }
1591
1592 case Instruction::SHR_LONG_2ADDR: {
1593 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
1594 break;
1595 }
1596
1597 case Instruction::USHR_INT_2ADDR: {
1598 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
1599 break;
1600 }
1601
1602 case Instruction::USHR_LONG_2ADDR: {
1603 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
1604 break;
1605 }
1606
Calin Juravle7c4954d2014-10-28 16:57:40 +00001607 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001608 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001609 break;
1610 }
1611
1612 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001613 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001614 break;
1615 }
1616
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001617 case Instruction::AND_INT_2ADDR: {
1618 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
1619 break;
1620 }
1621
1622 case Instruction::AND_LONG_2ADDR: {
1623 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
1624 break;
1625 }
1626
1627 case Instruction::OR_INT_2ADDR: {
1628 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
1629 break;
1630 }
1631
1632 case Instruction::OR_LONG_2ADDR: {
1633 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
1634 break;
1635 }
1636
1637 case Instruction::XOR_INT_2ADDR: {
1638 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
1639 break;
1640 }
1641
1642 case Instruction::XOR_LONG_2ADDR: {
1643 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
1644 break;
1645 }
1646
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001647 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001648 Binop_22s<HAdd>(instruction, false);
1649 break;
1650 }
1651
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001652 case Instruction::AND_INT_LIT16: {
1653 Binop_22s<HAnd>(instruction, false);
1654 break;
1655 }
1656
1657 case Instruction::OR_INT_LIT16: {
1658 Binop_22s<HOr>(instruction, false);
1659 break;
1660 }
1661
1662 case Instruction::XOR_INT_LIT16: {
1663 Binop_22s<HXor>(instruction, false);
1664 break;
1665 }
1666
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001667 case Instruction::RSUB_INT: {
1668 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001669 break;
1670 }
1671
Calin Juravle34bacdf2014-10-07 20:23:36 +01001672 case Instruction::MUL_INT_LIT16: {
1673 Binop_22s<HMul>(instruction, false);
1674 break;
1675 }
1676
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001677 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001678 Binop_22b<HAdd>(instruction, false);
1679 break;
1680 }
1681
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001682 case Instruction::AND_INT_LIT8: {
1683 Binop_22b<HAnd>(instruction, false);
1684 break;
1685 }
1686
1687 case Instruction::OR_INT_LIT8: {
1688 Binop_22b<HOr>(instruction, false);
1689 break;
1690 }
1691
1692 case Instruction::XOR_INT_LIT8: {
1693 Binop_22b<HXor>(instruction, false);
1694 break;
1695 }
1696
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001697 case Instruction::RSUB_INT_LIT8: {
1698 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001699 break;
1700 }
1701
Calin Juravle34bacdf2014-10-07 20:23:36 +01001702 case Instruction::MUL_INT_LIT8: {
1703 Binop_22b<HMul>(instruction, false);
1704 break;
1705 }
1706
Calin Juravled0d48522014-11-04 16:40:20 +00001707 case Instruction::DIV_INT_LIT16:
1708 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001709 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1710 dex_pc, Primitive::kPrimInt, true, true);
1711 break;
1712 }
1713
1714 case Instruction::REM_INT_LIT16:
1715 case Instruction::REM_INT_LIT8: {
1716 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1717 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00001718 break;
1719 }
1720
Calin Juravle9aec02f2014-11-18 23:06:35 +00001721 case Instruction::SHL_INT_LIT8: {
1722 Binop_22b<HShl>(instruction, false);
1723 break;
1724 }
1725
1726 case Instruction::SHR_INT_LIT8: {
1727 Binop_22b<HShr>(instruction, false);
1728 break;
1729 }
1730
1731 case Instruction::USHR_INT_LIT8: {
1732 Binop_22b<HUShr>(instruction, false);
1733 break;
1734 }
1735
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001736 case Instruction::NEW_INSTANCE: {
1737 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001738 new (arena_) HNewInstance(dex_pc, instruction.VRegB_21c()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001739 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1740 break;
1741 }
1742
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001743 case Instruction::NEW_ARRAY: {
1744 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
1745 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001746 new (arena_) HNewArray(length, dex_pc, instruction.VRegC_22c()));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001747 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1748 break;
1749 }
1750
1751 case Instruction::FILLED_NEW_ARRAY: {
1752 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1753 uint32_t type_index = instruction.VRegB_35c();
1754 uint32_t args[5];
1755 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001756 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001757 break;
1758 }
1759
1760 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1761 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1762 uint32_t type_index = instruction.VRegB_3rc();
1763 uint32_t register_index = instruction.VRegC_3rc();
1764 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00001765 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001766 break;
1767 }
1768
1769 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00001770 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001771 break;
1772 }
1773
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001774 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001775 case Instruction::MOVE_RESULT_WIDE:
1776 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001777 UpdateLocal(instruction.VRegA(), latest_result_);
1778 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001779 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001780
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001781 case Instruction::CMP_LONG: {
Calin Juravleddb7df22014-11-25 20:56:51 +00001782 Binop_23x_cmp(instruction, Primitive::kPrimLong, HCompare::kNoBias);
1783 break;
1784 }
1785
1786 case Instruction::CMPG_FLOAT: {
1787 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kGtBias);
1788 break;
1789 }
1790
1791 case Instruction::CMPG_DOUBLE: {
1792 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kGtBias);
1793 break;
1794 }
1795
1796 case Instruction::CMPL_FLOAT: {
1797 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kLtBias);
1798 break;
1799 }
1800
1801 case Instruction::CMPL_DOUBLE: {
1802 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kLtBias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001803 break;
1804 }
1805
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001806 case Instruction::NOP:
1807 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001808
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001809 case Instruction::IGET:
1810 case Instruction::IGET_WIDE:
1811 case Instruction::IGET_OBJECT:
1812 case Instruction::IGET_BOOLEAN:
1813 case Instruction::IGET_BYTE:
1814 case Instruction::IGET_CHAR:
1815 case Instruction::IGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001816 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001817 return false;
1818 }
1819 break;
1820 }
1821
1822 case Instruction::IPUT:
1823 case Instruction::IPUT_WIDE:
1824 case Instruction::IPUT_OBJECT:
1825 case Instruction::IPUT_BOOLEAN:
1826 case Instruction::IPUT_BYTE:
1827 case Instruction::IPUT_CHAR:
1828 case Instruction::IPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001829 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001830 return false;
1831 }
1832 break;
1833 }
1834
1835 case Instruction::SGET:
1836 case Instruction::SGET_WIDE:
1837 case Instruction::SGET_OBJECT:
1838 case Instruction::SGET_BOOLEAN:
1839 case Instruction::SGET_BYTE:
1840 case Instruction::SGET_CHAR:
1841 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001842 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001843 return false;
1844 }
1845 break;
1846 }
1847
1848 case Instruction::SPUT:
1849 case Instruction::SPUT_WIDE:
1850 case Instruction::SPUT_OBJECT:
1851 case Instruction::SPUT_BOOLEAN:
1852 case Instruction::SPUT_BYTE:
1853 case Instruction::SPUT_CHAR:
1854 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001855 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001856 return false;
1857 }
1858 break;
1859 }
1860
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001861#define ARRAY_XX(kind, anticipated_type) \
1862 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001863 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001864 break; \
1865 } \
1866 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001867 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001868 break; \
1869 }
1870
1871 ARRAY_XX(, Primitive::kPrimInt);
1872 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1873 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1874 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1875 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1876 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1877 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1878
Nicolas Geoffray39468442014-09-02 15:17:15 +01001879 case Instruction::ARRAY_LENGTH: {
1880 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001881 // No need for a temporary for the null check, it is the only input of the following
1882 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00001883 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001884 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001885 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1886 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1887 break;
1888 }
1889
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001890 case Instruction::CONST_STRING: {
Calin Juravle225ff812014-11-13 16:46:39 +00001891 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001892 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1893 break;
1894 }
1895
1896 case Instruction::CONST_STRING_JUMBO: {
Calin Juravle225ff812014-11-13 16:46:39 +00001897 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001898 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
1899 break;
1900 }
1901
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001902 case Instruction::CONST_CLASS: {
1903 uint16_t type_index = instruction.VRegB_21c();
1904 bool type_known_final;
1905 bool type_known_abstract;
1906 bool is_referrers_class;
1907 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1908 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
1909 &type_known_final, &type_known_abstract, &is_referrers_class);
1910 if (!can_access) {
1911 return false;
1912 }
1913 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001914 new (arena_) HLoadClass(type_index, is_referrers_class, dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001915 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1916 break;
1917 }
1918
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001919 case Instruction::MOVE_EXCEPTION: {
1920 current_block_->AddInstruction(new (arena_) HLoadException());
1921 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
1922 break;
1923 }
1924
1925 case Instruction::THROW: {
1926 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001927 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001928 // A throw instruction must branch to the exit block.
1929 current_block_->AddSuccessor(exit_block_);
1930 // We finished building this block. Set the current block to null to avoid
1931 // adding dead instructions to it.
1932 current_block_ = nullptr;
1933 break;
1934 }
1935
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001936 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001937 uint8_t destination = instruction.VRegA_22c();
1938 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001939 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00001940 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001941 return false;
1942 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001943 break;
1944 }
1945
1946 case Instruction::CHECK_CAST: {
1947 uint8_t reference = instruction.VRegA_21c();
1948 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00001949 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001950 return false;
1951 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001952 break;
1953 }
1954
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001955 case Instruction::MONITOR_ENTER: {
1956 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1957 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1958 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00001959 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001960 break;
1961 }
1962
1963 case Instruction::MONITOR_EXIT: {
1964 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1965 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1966 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00001967 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001968 break;
1969 }
1970
Andreas Gamped881df52014-11-24 23:28:39 -08001971 case Instruction::PACKED_SWITCH: {
1972 if (!BuildPackedSwitch(instruction, dex_pc)) {
1973 return false;
1974 }
1975 break;
1976 }
1977
Andreas Gampee4d4d322014-12-04 09:09:57 -08001978 case Instruction::SPARSE_SWITCH: {
1979 if (!BuildSparseSwitch(instruction, dex_pc)) {
1980 return false;
1981 }
1982 break;
1983 }
1984
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001985 default:
1986 return false;
1987 }
1988 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001989} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001990
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001991HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001992 if (constant0_ != nullptr) {
1993 return constant0_;
1994 }
1995 constant0_ = new(arena_) HIntConstant(0);
1996 entry_block_->AddInstruction(constant0_);
1997 return constant0_;
1998}
1999
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002000HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002001 if (constant1_ != nullptr) {
2002 return constant1_;
2003 }
2004 constant1_ = new(arena_) HIntConstant(1);
2005 entry_block_->AddInstruction(constant1_);
2006 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002007}
2008
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002009HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002010 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002011 case 0: return GetIntConstant0();
2012 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002013 default: {
2014 HIntConstant* instruction = new (arena_) HIntConstant(constant);
2015 entry_block_->AddInstruction(instruction);
2016 return instruction;
2017 }
2018 }
2019}
2020
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002021HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
2022 HLongConstant* instruction = new (arena_) HLongConstant(constant);
2023 entry_block_->AddInstruction(instruction);
2024 return instruction;
2025}
2026
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002027HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
2028 return locals_.Get(register_index);
2029}
2030
2031void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
2032 HLocal* local = GetLocalAt(register_index);
2033 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
2034}
2035
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002036HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002037 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002038 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002039 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002040}
2041
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002042} // namespace art