blob: fe32da05e7297ae1b41bd2d73f8c7e0ea1ff286d [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
91 int32_t GetEntryAt(size_t index) const {
92 DCHECK_LE(index, static_cast<size_t>(sparse_ ? num_entries_ - 1 : num_entries_));
93 return values_[index];
94 }
95
96 uint32_t GetDexPcForIndex(size_t index) const {
97 DCHECK_LE(index, static_cast<size_t>(sparse_ ? num_entries_ - 1 : num_entries_));
98 return dex_pc_ +
99 (reinterpret_cast<const int16_t*>(values_ + index) -
100 reinterpret_cast<const int16_t*>(&instruction_));
101 }
102
103 private:
104 const Instruction& instruction_;
105 const uint32_t dex_pc_;
106
107 // Whether this is a sparse-switch table (or a packed-switch one).
108 const bool sparse_;
109
110 // This can't be const as it needs to be computed off of the given instruction, and complicated
111 // expressions in the initializer list seemed very ugly.
112 uint16_t num_entries_;
113
114 const int32_t* values_;
115
116 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
117};
118
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100119void HGraphBuilder::InitializeLocals(uint16_t count) {
120 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000121 locals_.SetSize(count);
122 for (int i = 0; i < count; i++) {
123 HLocal* local = new (arena_) HLocal(i);
124 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000125 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000126 }
127}
128
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000129void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100130 // dex_compilation_unit_ is null only when unit testing.
131 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000132 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100133 }
134
135 graph_->SetNumberOfInVRegs(number_of_parameters);
136 const char* shorty = dex_compilation_unit_->GetShorty();
137 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100138 int parameter_index = 0;
139
140 if (!dex_compilation_unit_->IsStatic()) {
141 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100142 HParameterValue* parameter =
143 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100144 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100145 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100146 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100147 number_of_parameters--;
148 }
149
150 uint32_t pos = 1;
151 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100152 HParameterValue* parameter =
153 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
154 entry_block_->AddInstruction(parameter);
155 HLocal* local = GetLocalAt(locals_index++);
156 // Store the parameter value in the local that the dex code will use
157 // to reference that parameter.
158 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
159 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
160 || (parameter->GetType() == Primitive::kPrimDouble);
161 if (is_wide) {
162 i++;
163 locals_index++;
164 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100165 }
166 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100167}
168
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100169template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000170void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 int32_t target_offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +0000172 PotentiallyAddSuspendCheck(target_offset, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100173 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
174 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700175 T* comparison = new (arena_) T(first, second);
176 current_block_->AddInstruction(comparison);
177 HInstruction* ifinst = new (arena_) HIf(comparison);
178 current_block_->AddInstruction(ifinst);
Calin Juravle225ff812014-11-13 16:46:39 +0000179 HBasicBlock* target = FindBlockStartingAt(dex_pc + target_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700180 DCHECK(target != nullptr);
181 current_block_->AddSuccessor(target);
Calin Juravle225ff812014-11-13 16:46:39 +0000182 target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
Dave Allison20dfc792014-06-16 20:44:29 -0700183 DCHECK(target != nullptr);
184 current_block_->AddSuccessor(target);
185 current_block_ = nullptr;
186}
187
188template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000189void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000190 int32_t target_offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +0000191 PotentiallyAddSuspendCheck(target_offset, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700192 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
193 T* comparison = new (arena_) T(value, GetIntConstant(0));
194 current_block_->AddInstruction(comparison);
195 HInstruction* ifinst = new (arena_) HIf(comparison);
196 current_block_->AddInstruction(ifinst);
Calin Juravle225ff812014-11-13 16:46:39 +0000197 HBasicBlock* target = FindBlockStartingAt(dex_pc + target_offset);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100198 DCHECK(target != nullptr);
199 current_block_->AddSuccessor(target);
Calin Juravle225ff812014-11-13 16:46:39 +0000200 target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100201 DCHECK(target != nullptr);
202 current_block_->AddSuccessor(target);
203 current_block_ = nullptr;
204}
205
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000206static bool ShouldSkipCompilation(const CompilerDriver& compiler_driver,
207 const DexCompilationUnit& dex_compilation_unit,
208 size_t number_of_dex_instructions,
209 size_t number_of_blocks ATTRIBUTE_UNUSED,
210 size_t number_of_branches) {
211 const CompilerOptions& compiler_options = compiler_driver.GetCompilerOptions();
212 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
213 if (compiler_filter == CompilerOptions::kEverything) {
214 return false;
215 }
216
217 if (compiler_options.IsHugeMethod(number_of_dex_instructions)) {
218 LOG(INFO) << "Skip compilation of huge method "
219 << PrettyMethod(dex_compilation_unit.GetDexMethodIndex(),
220 *dex_compilation_unit.GetDexFile())
221 << ": " << number_of_dex_instructions << " dex instructions";
222 return true;
223 }
224
225 // If it's large and contains no branches, it's likely to be machine generated initialization.
226 if (compiler_options.IsLargeMethod(number_of_dex_instructions) && (number_of_branches == 0)) {
227 LOG(INFO) << "Skip compilation of large method with no branch "
228 << PrettyMethod(dex_compilation_unit.GetDexMethodIndex(),
229 *dex_compilation_unit.GetDexFile())
230 << ": " << number_of_dex_instructions << " dex instructions";
231 return true;
232 }
233
234 return false;
235}
236
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000237HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000238 const uint16_t* code_ptr = code_item.insns_;
239 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100240 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000241
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000242 // Setup the graph with the entry block and exit block.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000243 graph_ = new (arena_) HGraph(arena_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100244 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000245 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100246 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000247 graph_->SetEntryBlock(entry_block_);
248 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000249
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000250 InitializeLocals(code_item.registers_size_);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100251 graph_->UpdateMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000252
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000253 // Compute the number of dex instructions, blocks, and branches. We will
254 // check these values against limits given to the compiler.
255 size_t number_of_dex_instructions = 0;
256 size_t number_of_blocks = 0;
257 size_t number_of_branches = 0;
258
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000259 // To avoid splitting blocks, we compute ahead of time the instructions that
260 // start a new block, and create these blocks.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000261 ComputeBranchTargets(
262 code_ptr, code_end, &number_of_dex_instructions, &number_of_blocks, &number_of_branches);
263
264 // Note that the compiler driver is null when unit testing.
265 if (compiler_driver_ != nullptr) {
266 if (ShouldSkipCompilation(*compiler_driver_,
267 *dex_compilation_unit_,
268 number_of_dex_instructions,
269 number_of_blocks,
270 number_of_branches)) {
271 return nullptr;
272 }
273 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000274
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000275 // Also create blocks for catch handlers.
276 if (code_item.tries_size_ != 0) {
277 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
278 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
279 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
280 CatchHandlerIterator iterator(handlers_ptr);
281 for (; iterator.HasNext(); iterator.Next()) {
282 uint32_t address = iterator.GetHandlerAddress();
283 HBasicBlock* block = FindBlockStartingAt(address);
284 if (block == nullptr) {
285 block = new (arena_) HBasicBlock(graph_, address);
286 branch_targets_.Put(address, block);
287 }
288 block->SetIsCatchBlock();
289 }
290 handlers_ptr = iterator.EndDataPointer();
291 }
292 }
293
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000294 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100295
Calin Juravle225ff812014-11-13 16:46:39 +0000296 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000297 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000298 // Update the current block if dex_pc starts a new block.
299 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000300 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle225ff812014-11-13 16:46:39 +0000301 if (!AnalyzeDexInstruction(instruction, dex_pc)) return nullptr;
302 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000303 code_ptr += instruction.SizeInCodeUnits();
304 }
305
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000306 // Add the exit block at the end to give it the highest id.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307 graph_->AddBlock(exit_block_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000308 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000309 // Add the suspend check to the entry block.
310 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000311 entry_block_->AddInstruction(new (arena_) HGoto());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000312 return graph_;
313}
314
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) {
316 HBasicBlock* block = FindBlockStartingAt(index);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000317 if (block == nullptr) {
318 return;
319 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000320
321 if (current_block_ != nullptr) {
322 // Branching instructions clear current_block, so we know
323 // the last instruction of the current block is not a branching
324 // instruction. We add an unconditional goto to the found block.
325 current_block_->AddInstruction(new (arena_) HGoto());
326 current_block_->AddSuccessor(block);
327 }
328 graph_->AddBlock(block);
329 current_block_ = block;
330}
331
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000332void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
333 const uint16_t* code_end,
334 size_t* number_of_dex_instructions,
335 size_t* number_of_blocks,
336 size_t* number_of_branches) {
Andreas Gamped881df52014-11-24 23:28:39 -0800337 // TODO: Support sparse-switch instructions.
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000338 branch_targets_.SetSize(code_end - code_ptr);
339
340 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100341 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000342 branch_targets_.Put(0, block);
343 entry_block_->AddSuccessor(block);
344
345 // Iterate over all instructions and find branching instructions. Create blocks for
346 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800347 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000348 while (code_ptr < code_end) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000349 (*number_of_dex_instructions)++;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000350 const Instruction& instruction = *Instruction::At(code_ptr);
351 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000352 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000353 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000354 // Create a block for the target instruction.
355 if (FindBlockStartingAt(target) == nullptr) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100356 block = new (arena_) HBasicBlock(graph_, target);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000357 branch_targets_.Put(target, block);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000358 (*number_of_blocks)++;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000359 }
Calin Juravle225ff812014-11-13 16:46:39 +0000360 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000361 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000362 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_pc) == nullptr)) {
363 block = new (arena_) HBasicBlock(graph_, dex_pc);
364 branch_targets_.Put(dex_pc, block);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000365 (*number_of_blocks)++;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000366 }
Andreas Gamped881df52014-11-24 23:28:39 -0800367 } else if (instruction.Opcode() == Instruction::PACKED_SWITCH) {
368 SwitchTable table(instruction, dex_pc, false);
369
370 uint16_t num_entries = table.GetNumEntries();
371
372 // Entry @0: starting key. Use a larger loop counter type to avoid overflow issues.
373 for (size_t i = 1; i <= num_entries; ++i) {
374 // The target of the case.
375 uint32_t target = dex_pc + table.GetEntryAt(i);
376 if (FindBlockStartingAt(target) == nullptr) {
377 block = new (arena_) HBasicBlock(graph_, target);
378 branch_targets_.Put(target, block);
379 (*number_of_blocks)++;
380 }
381
382 // The next case gets its own block.
383 if (i < num_entries) {
384 block = new (arena_) HBasicBlock(graph_, target);
385 branch_targets_.Put(table.GetDexPcForIndex(i), block);
386 (*number_of_blocks)++;
387 }
388 }
389
390 // Fall-through. Add a block if there is more code afterwards.
391 dex_pc += instruction.SizeInCodeUnits();
392 code_ptr += instruction.SizeInCodeUnits();
393 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_pc) == nullptr)) {
394 block = new (arena_) HBasicBlock(graph_, dex_pc);
395 branch_targets_.Put(dex_pc, block);
396 (*number_of_blocks)++;
397 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000398 } else {
399 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000400 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000401 }
402 }
403}
404
405HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const {
406 DCHECK_GE(index, 0);
407 return branch_targets_.Get(index);
408}
409
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100410template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100411void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
412 HInstruction* first = LoadLocal(instruction.VRegB(), type);
413 current_block_->AddInstruction(new (arena_) T(type, first));
414 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
415}
416
Roland Levillaindff1f282014-11-05 14:15:05 +0000417void HGraphBuilder::Conversion_12x(const Instruction& instruction,
418 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000419 Primitive::Type result_type,
420 uint32_t dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +0000421 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Roland Levillain624279f2014-12-04 11:54:28 +0000422 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Roland Levillaindff1f282014-11-05 14:15:05 +0000423 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
424}
425
Roland Levillain88cb1752014-10-20 16:36:47 +0100426template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100427void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100428 HInstruction* first = LoadLocal(instruction.VRegB(), type);
429 HInstruction* second = LoadLocal(instruction.VRegC(), type);
430 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100431 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
432}
433
434template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000435void HGraphBuilder::Binop_23x(const Instruction& instruction,
436 Primitive::Type type,
437 uint32_t dex_pc) {
438 HInstruction* first = LoadLocal(instruction.VRegB(), type);
439 HInstruction* second = LoadLocal(instruction.VRegC(), type);
440 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
441 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
442}
443
444template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000445void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
446 Primitive::Type type) {
447 HInstruction* first = LoadLocal(instruction.VRegB(), type);
448 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
449 current_block_->AddInstruction(new (arena_) T(type, first, second));
450 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
451}
452
Calin Juravleddb7df22014-11-25 20:56:51 +0000453void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
454 Primitive::Type type,
455 HCompare::Bias bias) {
456 HInstruction* first = LoadLocal(instruction.VRegB(), type);
457 HInstruction* second = LoadLocal(instruction.VRegC(), type);
458 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias));
459 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
460}
461
Calin Juravle9aec02f2014-11-18 23:06:35 +0000462template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100463void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
464 HInstruction* first = LoadLocal(instruction.VRegA(), type);
465 HInstruction* second = LoadLocal(instruction.VRegB(), type);
466 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100467 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
468}
469
470template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000471void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) {
472 HInstruction* first = LoadLocal(instruction.VRegA(), type);
473 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
474 current_block_->AddInstruction(new (arena_) T(type, first, second));
475 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
476}
477
478template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000479void HGraphBuilder::Binop_12x(const Instruction& instruction,
480 Primitive::Type type,
481 uint32_t dex_pc) {
482 HInstruction* first = LoadLocal(instruction.VRegA(), type);
483 HInstruction* second = LoadLocal(instruction.VRegB(), type);
484 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
485 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
486}
487
488template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100489void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100490 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
491 HInstruction* second = GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100492 if (reverse) {
493 std::swap(first, second);
494 }
495 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
496 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
497}
498
499template<typename T>
500void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100501 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
502 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100503 if (reverse) {
504 std::swap(first, second);
505 }
506 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
507 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
508}
509
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100510void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
511 if (type == Primitive::kPrimVoid) {
512 current_block_->AddInstruction(new (arena_) HReturnVoid());
513 } else {
514 HInstruction* value = LoadLocal(instruction.VRegA(), type);
515 current_block_->AddInstruction(new (arena_) HReturn(value));
516 }
517 current_block_->AddSuccessor(exit_block_);
518 current_block_ = nullptr;
519}
520
521bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000522 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100523 uint32_t method_idx,
524 uint32_t number_of_vreg_arguments,
525 bool is_range,
526 uint32_t* args,
527 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100528 Instruction::Code opcode = instruction.Opcode();
529 InvokeType invoke_type;
530 switch (opcode) {
531 case Instruction::INVOKE_STATIC:
532 case Instruction::INVOKE_STATIC_RANGE:
533 invoke_type = kStatic;
534 break;
535 case Instruction::INVOKE_DIRECT:
536 case Instruction::INVOKE_DIRECT_RANGE:
537 invoke_type = kDirect;
538 break;
539 case Instruction::INVOKE_VIRTUAL:
540 case Instruction::INVOKE_VIRTUAL_RANGE:
541 invoke_type = kVirtual;
542 break;
543 case Instruction::INVOKE_INTERFACE:
544 case Instruction::INVOKE_INTERFACE_RANGE:
545 invoke_type = kInterface;
546 break;
547 case Instruction::INVOKE_SUPER_RANGE:
548 case Instruction::INVOKE_SUPER:
549 invoke_type = kSuper;
550 break;
551 default:
552 LOG(FATAL) << "Unexpected invoke op: " << opcode;
553 return false;
554 }
555
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100556 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
557 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
558 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
559 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100560 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100561 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
562
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000563 MethodReference target_method(dex_file_, method_idx);
564 uintptr_t direct_code;
565 uintptr_t direct_method;
566 int table_index;
567 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000568
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000569 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
570 &optimized_invoke_type, &target_method, &table_index,
571 &direct_code, &direct_method)) {
572 LOG(INFO) << "Did not compile " << PrettyMethod(method_idx, *dex_file_)
573 << " because a method call could not be resolved";
574 return false;
575 }
576 DCHECK(optimized_invoke_type != kSuper);
577
578 HInvoke* invoke = nullptr;
579 if (optimized_invoke_type == kVirtual) {
580 invoke = new (arena_) HInvokeVirtual(
581 arena_, number_of_arguments, return_type, dex_pc, table_index);
582 } else if (optimized_invoke_type == kInterface) {
583 invoke = new (arena_) HInvokeInterface(
584 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100585 } else {
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000586 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic);
587 // Sharpening to kDirect only works if we compile PIC.
588 DCHECK((optimized_invoke_type == invoke_type) || (optimized_invoke_type != kDirect)
589 || compiler_driver_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100590 // Treat invoke-direct like static calls for now.
591 invoke = new (arena_) HInvokeStatic(
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000592 arena_, number_of_arguments, return_type, dex_pc, target_method.dex_method_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100593 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100594
595 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000596 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100597 if (is_instance_call) {
598 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000599 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100600 current_block_->AddInstruction(null_check);
601 temps.Add(null_check);
602 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100603 start_index = 1;
604 }
605
606 uint32_t descriptor_index = 1;
607 uint32_t argument_index = start_index;
608 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
609 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100610 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
611 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100612 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
Calin Juravle225ff812014-11-13 16:46:39 +0000613 << " at " << dex_pc;
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100614 // We do not implement non sequential register pair.
615 return false;
616 }
617 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
618 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100619 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100620 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100621 }
622 }
623
624 DCHECK_EQ(argument_index, number_of_arguments);
625 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100626 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100627 return true;
628}
629
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100630bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000631 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100632 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100633 uint32_t source_or_dest_reg = instruction.VRegA_22c();
634 uint32_t obj_reg = instruction.VRegB_22c();
635 uint16_t field_index = instruction.VRegC_22c();
636
637 ScopedObjectAccess soa(Thread::Current());
638 StackHandleScope<1> hs(soa.Self());
639 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
640 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
641
642 if (resolved_field.Get() == nullptr) {
643 return false;
644 }
645 if (resolved_field->IsVolatile()) {
646 return false;
647 }
648
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100649 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100650
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100651 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000652 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100653 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000654 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100655 HInstruction* null_check = current_block_->GetLastInstruction();
656 // We need one temporary for the null check.
657 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100658 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100659 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
660 null_check,
661 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100662 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100663 resolved_field->GetOffset()));
664 } else {
665 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
666 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100667 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100668 resolved_field->GetOffset()));
669
670 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
671 }
672 return true;
673}
674
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100675
676
677bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000678 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100679 bool is_put) {
680 uint32_t source_or_dest_reg = instruction.VRegA_21c();
681 uint16_t field_index = instruction.VRegB_21c();
682
683 uint32_t storage_index;
684 bool is_referrers_class;
685 bool is_initialized;
686 bool is_volatile;
687 MemberOffset field_offset(0u);
688 Primitive::Type field_type;
689
690 bool fast_path = compiler_driver_->ComputeStaticFieldInfo(field_index,
691 dex_compilation_unit_,
692 is_put,
693 &field_offset,
694 &storage_index,
695 &is_referrers_class,
696 &is_volatile,
697 &is_initialized,
698 &field_type);
699 if (!fast_path) {
700 return false;
701 }
702
703 if (is_volatile) {
704 return false;
705 }
706
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100707 HLoadClass* constant = new (arena_) HLoadClass(
Calin Juravle225ff812014-11-13 16:46:39 +0000708 storage_index, is_referrers_class, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100709 current_block_->AddInstruction(constant);
710
711 HInstruction* cls = constant;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000712 if (!is_initialized) {
Calin Juravle225ff812014-11-13 16:46:39 +0000713 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100714 current_block_->AddInstruction(cls);
715 }
716
717 if (is_put) {
718 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000719 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100720 temps.Add(cls);
721 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
722 DCHECK_EQ(value->GetType(), field_type);
723 current_block_->AddInstruction(
724 new (arena_) HStaticFieldSet(cls, value, field_type, field_offset));
725 } else {
726 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls, field_type, field_offset));
727 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
728 }
729 return true;
730}
731
Calin Juravlebacfec32014-11-14 15:54:36 +0000732void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
733 uint16_t first_vreg,
734 int64_t second_vreg_or_constant,
735 uint32_t dex_pc,
736 Primitive::Type type,
737 bool second_is_constant,
738 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000739 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +0000740
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000741 HInstruction* first = LoadLocal(first_vreg, type);
742 HInstruction* second = nullptr;
743 if (second_is_constant) {
744 if (type == Primitive::kPrimInt) {
745 second = GetIntConstant(second_vreg_or_constant);
746 } else {
747 second = GetLongConstant(second_vreg_or_constant);
748 }
749 } else {
750 second = LoadLocal(second_vreg_or_constant, type);
751 }
752
753 if (!second_is_constant
754 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
755 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
756 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000757 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000758 current_block_->AddInstruction(second);
759 temps.Add(current_block_->GetLastInstruction());
760 }
761
Calin Juravlebacfec32014-11-14 15:54:36 +0000762 if (isDiv) {
763 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
764 } else {
765 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
766 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000767 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +0000768}
769
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100770void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000771 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100772 bool is_put,
773 Primitive::Type anticipated_type) {
774 uint8_t source_or_dest_reg = instruction.VRegA_23x();
775 uint8_t array_reg = instruction.VRegB_23x();
776 uint8_t index_reg = instruction.VRegC_23x();
777
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100778 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000779 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100780
781 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000782 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100783 current_block_->AddInstruction(object);
784 temps.Add(object);
785
786 HInstruction* length = new (arena_) HArrayLength(object);
787 current_block_->AddInstruction(length);
788 temps.Add(length);
789 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +0000790 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100791 current_block_->AddInstruction(index);
792 temps.Add(index);
793 if (is_put) {
794 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
795 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100796 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000797 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100798 } else {
799 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
800 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
801 }
802}
803
Calin Juravle225ff812014-11-13 16:46:39 +0000804void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100805 uint32_t type_index,
806 uint32_t number_of_vreg_arguments,
807 bool is_range,
808 uint32_t* args,
809 uint32_t register_index) {
810 HInstruction* length = GetIntConstant(number_of_vreg_arguments);
Calin Juravle225ff812014-11-13 16:46:39 +0000811 HInstruction* object = new (arena_) HNewArray(length, dex_pc, type_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100812 current_block_->AddInstruction(object);
813
814 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
815 DCHECK_EQ(descriptor[0], '[') << descriptor;
816 char primitive = descriptor[1];
817 DCHECK(primitive == 'I'
818 || primitive == 'L'
819 || primitive == '[') << descriptor;
820 bool is_reference_array = (primitive == 'L') || (primitive == '[');
821 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
822
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000823 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100824 temps.Add(object);
825 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
826 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
827 HInstruction* index = GetIntConstant(i);
828 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000829 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100830 }
831 latest_result_ = object;
832}
833
834template <typename T>
835void HGraphBuilder::BuildFillArrayData(HInstruction* object,
836 const T* data,
837 uint32_t element_count,
838 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +0000839 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100840 for (uint32_t i = 0; i < element_count; ++i) {
841 HInstruction* index = GetIntConstant(i);
842 HInstruction* value = GetIntConstant(data[i]);
843 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000844 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100845 }
846}
847
Calin Juravle225ff812014-11-13 16:46:39 +0000848void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000849 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000850 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000851 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000852 current_block_->AddInstruction(null_check);
853 temps.Add(null_check);
854
855 HInstruction* length = new (arena_) HArrayLength(null_check);
856 current_block_->AddInstruction(length);
857
Calin Juravle225ff812014-11-13 16:46:39 +0000858 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +0000859 const Instruction::ArrayDataPayload* payload =
860 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
861 const uint8_t* data = payload->data;
862 uint32_t element_count = payload->element_count;
863
864 // Implementation of this DEX instruction seems to be that the bounds check is
865 // done before doing any stores.
866 HInstruction* last_index = GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +0000867 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +0000868
869 switch (payload->element_width) {
870 case 1:
871 BuildFillArrayData(null_check,
872 reinterpret_cast<const int8_t*>(data),
873 element_count,
874 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +0000875 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000876 break;
877 case 2:
878 BuildFillArrayData(null_check,
879 reinterpret_cast<const int16_t*>(data),
880 element_count,
881 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +0000882 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000883 break;
884 case 4:
885 BuildFillArrayData(null_check,
886 reinterpret_cast<const int32_t*>(data),
887 element_count,
888 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +0000889 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000890 break;
891 case 8:
892 BuildFillWideArrayData(null_check,
893 reinterpret_cast<const int64_t*>(data),
894 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000895 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000896 break;
897 default:
898 LOG(FATAL) << "Unknown element width for " << payload->element_width;
899 }
900}
901
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100902void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100903 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100904 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000905 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100906 for (uint32_t i = 0; i < element_count; ++i) {
907 HInstruction* index = GetIntConstant(i);
908 HInstruction* value = GetLongConstant(data[i]);
909 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000910 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100911 }
912}
913
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000914bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
915 uint8_t destination,
916 uint8_t reference,
917 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +0000918 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000919 bool type_known_final;
920 bool type_known_abstract;
921 bool is_referrers_class;
922 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
923 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
924 &type_known_final, &type_known_abstract, &is_referrers_class);
925 if (!can_access) {
926 return false;
927 }
928 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000929 HLoadClass* cls = new (arena_) HLoadClass(type_index, is_referrers_class, dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000930 current_block_->AddInstruction(cls);
931 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000932 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000933 temps.Add(cls);
934 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
935 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000936 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000937 UpdateLocal(destination, current_block_->GetLastInstruction());
938 } else {
939 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
940 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000941 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000942 }
943 return true;
944}
945
Andreas Gamped881df52014-11-24 23:28:39 -0800946bool HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
947 SwitchTable table(instruction, dex_pc, false);
948
949 // Value to test against.
950 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
951
952 // Chained cmp-and-branch, starting from starting_key.
953 int32_t starting_key = table.GetEntryAt(0);
954
955 uint16_t num_entries = table.GetNumEntries();
956 // On overflow condition (or zero cases) just punt.
957 if (num_entries == 0 || num_entries == UINT16_MAX) {
958 return false;
959 }
960
961 for (size_t i = 1; i <= num_entries; i++) {
962 int32_t target_offset = table.GetEntryAt(i);
963 PotentiallyAddSuspendCheck(target_offset, dex_pc);
964
965 // The current case's value.
966 HInstruction* this_case_value = GetIntConstant(starting_key + i - 1);
967
968 // Compare value and this_case_value.
969 HEqual* comparison = new (arena_) HEqual(value, this_case_value);
970 current_block_->AddInstruction(comparison);
971 HInstruction* ifinst = new (arena_) HIf(comparison);
972 current_block_->AddInstruction(ifinst);
973
974 // Case hit: use the target offset to determine where to go.
975 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
976 DCHECK(case_target != nullptr);
977 current_block_->AddSuccessor(case_target);
978
979 // Case miss: go to the next case (or default fall-through).
980 // When there is a next case, we use the block stored with the table offset representing this
981 // case (that is where we registered them in ComputeBranchTargets).
982 // When there is no next case, we use the following instruction.
983 // TODO: Peel the last iteration to avoid conditional.
984 if (i < table.GetNumEntries()) {
985 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(i));
986 DCHECK(next_case_target != nullptr);
987 current_block_->AddSuccessor(next_case_target);
988
989 // Need to manually add the block, as there is no dex-pc transition for the cases.
990 graph_->AddBlock(next_case_target);
991
992 current_block_ = next_case_target;
993 } else {
994 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
995 DCHECK(default_target != nullptr);
996 current_block_->AddSuccessor(default_target);
997 current_block_ = nullptr;
998 }
999 }
1000 return true;
1001}
1002
Calin Juravle225ff812014-11-13 16:46:39 +00001003void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001004 if (target_offset <= 0) {
1005 // Unconditionnally add a suspend check to backward branches. We can remove
1006 // them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001007 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001008 }
1009}
1010
Calin Juravle225ff812014-11-13 16:46:39 +00001011bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001012 if (current_block_ == nullptr) {
1013 return true; // Dead code
1014 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001015
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001016 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001017 case Instruction::CONST_4: {
1018 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001019 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001020 UpdateLocal(register_index, constant);
1021 break;
1022 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001023
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001024 case Instruction::CONST_16: {
1025 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001026 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
1027 UpdateLocal(register_index, constant);
1028 break;
1029 }
1030
Dave Allison20dfc792014-06-16 20:44:29 -07001031 case Instruction::CONST: {
1032 int32_t register_index = instruction.VRegA();
1033 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
1034 UpdateLocal(register_index, constant);
1035 break;
1036 }
1037
1038 case Instruction::CONST_HIGH16: {
1039 int32_t register_index = instruction.VRegA();
1040 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
1041 UpdateLocal(register_index, constant);
1042 break;
1043 }
1044
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001045 case Instruction::CONST_WIDE_16: {
1046 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001047 // Get 16 bits of constant value, sign extended to 64 bits.
1048 int64_t value = instruction.VRegB_21s();
1049 value <<= 48;
1050 value >>= 48;
1051 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001052 UpdateLocal(register_index, constant);
1053 break;
1054 }
1055
1056 case Instruction::CONST_WIDE_32: {
1057 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001058 // Get 32 bits of constant value, sign extended to 64 bits.
1059 int64_t value = instruction.VRegB_31i();
1060 value <<= 32;
1061 value >>= 32;
1062 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001063 UpdateLocal(register_index, constant);
1064 break;
1065 }
1066
1067 case Instruction::CONST_WIDE: {
1068 int32_t register_index = instruction.VRegA();
1069 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001070 UpdateLocal(register_index, constant);
1071 break;
1072 }
1073
Dave Allison20dfc792014-06-16 20:44:29 -07001074 case Instruction::CONST_WIDE_HIGH16: {
1075 int32_t register_index = instruction.VRegA();
1076 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1077 HLongConstant* constant = GetLongConstant(value);
1078 UpdateLocal(register_index, constant);
1079 break;
1080 }
1081
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001082 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001083 case Instruction::MOVE:
1084 case Instruction::MOVE_FROM16:
1085 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001086 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001087 UpdateLocal(instruction.VRegA(), value);
1088 break;
1089 }
1090
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001091 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001092 case Instruction::MOVE_WIDE:
1093 case Instruction::MOVE_WIDE_FROM16:
1094 case Instruction::MOVE_WIDE_16: {
1095 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1096 UpdateLocal(instruction.VRegA(), value);
1097 break;
1098 }
1099
1100 case Instruction::MOVE_OBJECT:
1101 case Instruction::MOVE_OBJECT_16:
1102 case Instruction::MOVE_OBJECT_FROM16: {
1103 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
1104 UpdateLocal(instruction.VRegA(), value);
1105 break;
1106 }
1107
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001108 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001109 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001110 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001111 }
1112
Dave Allison20dfc792014-06-16 20:44:29 -07001113#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001114 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1115 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001116
Dave Allison20dfc792014-06-16 20:44:29 -07001117 IF_XX(HEqual, EQ);
1118 IF_XX(HNotEqual, NE);
1119 IF_XX(HLessThan, LT);
1120 IF_XX(HLessThanOrEqual, LE);
1121 IF_XX(HGreaterThan, GT);
1122 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001123
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001124 case Instruction::GOTO:
1125 case Instruction::GOTO_16:
1126 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001127 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001128 PotentiallyAddSuspendCheck(offset, dex_pc);
1129 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001130 DCHECK(target != nullptr);
1131 current_block_->AddInstruction(new (arena_) HGoto());
1132 current_block_->AddSuccessor(target);
1133 current_block_ = nullptr;
1134 break;
1135 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001136
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001137 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001138 DCHECK_NE(return_type_, Primitive::kPrimNot);
1139 DCHECK_NE(return_type_, Primitive::kPrimLong);
1140 DCHECK_NE(return_type_, Primitive::kPrimDouble);
1141 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001142 break;
1143 }
1144
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001145 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001146 DCHECK(return_type_ == Primitive::kPrimNot);
1147 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001148 break;
1149 }
1150
1151 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001152 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
1153 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001154 break;
1155 }
1156
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001157 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001158 case Instruction::INVOKE_INTERFACE:
1159 case Instruction::INVOKE_STATIC:
1160 case Instruction::INVOKE_SUPER:
1161 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001162 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001163 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001164 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001165 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001166 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001167 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001168 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001169 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001170 break;
1171 }
1172
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001173 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001174 case Instruction::INVOKE_INTERFACE_RANGE:
1175 case Instruction::INVOKE_STATIC_RANGE:
1176 case Instruction::INVOKE_SUPER_RANGE:
1177 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001178 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001179 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1180 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001181 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001182 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001183 return false;
1184 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001185 break;
1186 }
1187
Roland Levillain88cb1752014-10-20 16:36:47 +01001188 case Instruction::NEG_INT: {
1189 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1190 break;
1191 }
1192
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001193 case Instruction::NEG_LONG: {
1194 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1195 break;
1196 }
1197
Roland Levillain3dbcb382014-10-28 17:30:07 +00001198 case Instruction::NEG_FLOAT: {
1199 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1200 break;
1201 }
1202
1203 case Instruction::NEG_DOUBLE: {
1204 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1205 break;
1206 }
1207
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001208 case Instruction::NOT_INT: {
1209 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1210 break;
1211 }
1212
Roland Levillain70566432014-10-24 16:20:17 +01001213 case Instruction::NOT_LONG: {
1214 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1215 break;
1216 }
1217
Roland Levillaindff1f282014-11-05 14:15:05 +00001218 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001219 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001220 break;
1221 }
1222
Roland Levillaincff13742014-11-17 14:32:17 +00001223 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001224 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001225 break;
1226 }
1227
1228 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001229 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001230 break;
1231 }
1232
Roland Levillain946e1432014-11-11 17:35:19 +00001233 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001234 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001235 break;
1236 }
1237
Roland Levillain6d0e4832014-11-27 18:31:21 +00001238 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001239 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001240 break;
1241 }
1242
Roland Levillain647b9ed2014-11-27 12:06:00 +00001243 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001244 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001245 break;
1246 }
1247
Roland Levillain3f8f9362014-12-02 17:45:01 +00001248 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001249 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1250 break;
1251 }
1252
1253 case Instruction::FLOAT_TO_LONG: {
1254 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001255 break;
1256 }
1257
Roland Levillain8964e2b2014-12-04 12:10:50 +00001258 case Instruction::FLOAT_TO_DOUBLE: {
1259 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1260 break;
1261 }
1262
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001263 case Instruction::DOUBLE_TO_INT: {
1264 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1265 break;
1266 }
1267
1268 case Instruction::DOUBLE_TO_LONG: {
1269 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1270 break;
1271 }
1272
Roland Levillain8964e2b2014-12-04 12:10:50 +00001273 case Instruction::DOUBLE_TO_FLOAT: {
1274 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1275 break;
1276 }
1277
Roland Levillain51d3fc42014-11-13 14:11:42 +00001278 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001279 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001280 break;
1281 }
1282
Roland Levillain01a8d712014-11-14 16:27:39 +00001283 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001284 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001285 break;
1286 }
1287
Roland Levillain981e4542014-11-14 11:47:14 +00001288 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001289 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001290 break;
1291 }
1292
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001293 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001294 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001295 break;
1296 }
1297
1298 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001299 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001300 break;
1301 }
1302
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001303 case Instruction::ADD_DOUBLE: {
1304 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1305 break;
1306 }
1307
1308 case Instruction::ADD_FLOAT: {
1309 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1310 break;
1311 }
1312
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001313 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001314 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001315 break;
1316 }
1317
1318 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001319 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001320 break;
1321 }
1322
Calin Juravle096cc022014-10-23 17:01:13 +01001323 case Instruction::SUB_FLOAT: {
1324 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1325 break;
1326 }
1327
1328 case Instruction::SUB_DOUBLE: {
1329 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1330 break;
1331 }
1332
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001333 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001334 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1335 break;
1336 }
1337
Calin Juravle34bacdf2014-10-07 20:23:36 +01001338 case Instruction::MUL_INT: {
1339 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1340 break;
1341 }
1342
1343 case Instruction::MUL_LONG: {
1344 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1345 break;
1346 }
1347
Calin Juravleb5bfa962014-10-21 18:02:24 +01001348 case Instruction::MUL_FLOAT: {
1349 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1350 break;
1351 }
1352
1353 case Instruction::MUL_DOUBLE: {
1354 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1355 break;
1356 }
1357
Calin Juravled0d48522014-11-04 16:40:20 +00001358 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001359 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1360 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001361 break;
1362 }
1363
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001364 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001365 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1366 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001367 break;
1368 }
1369
Calin Juravle7c4954d2014-10-28 16:57:40 +00001370 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001371 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001372 break;
1373 }
1374
1375 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001376 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001377 break;
1378 }
1379
Calin Juravlebacfec32014-11-14 15:54:36 +00001380 case Instruction::REM_INT: {
1381 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1382 dex_pc, Primitive::kPrimInt, false, false);
1383 break;
1384 }
1385
1386 case Instruction::REM_LONG: {
1387 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1388 dex_pc, Primitive::kPrimLong, false, false);
1389 break;
1390 }
1391
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001392 case Instruction::AND_INT: {
1393 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1394 break;
1395 }
1396
1397 case Instruction::AND_LONG: {
1398 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1399 break;
1400 }
1401
Calin Juravle9aec02f2014-11-18 23:06:35 +00001402 case Instruction::SHL_INT: {
1403 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
1404 break;
1405 }
1406
1407 case Instruction::SHL_LONG: {
1408 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
1409 break;
1410 }
1411
1412 case Instruction::SHR_INT: {
1413 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
1414 break;
1415 }
1416
1417 case Instruction::SHR_LONG: {
1418 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
1419 break;
1420 }
1421
1422 case Instruction::USHR_INT: {
1423 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
1424 break;
1425 }
1426
1427 case Instruction::USHR_LONG: {
1428 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
1429 break;
1430 }
1431
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001432 case Instruction::OR_INT: {
1433 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1434 break;
1435 }
1436
1437 case Instruction::OR_LONG: {
1438 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1439 break;
1440 }
1441
1442 case Instruction::XOR_INT: {
1443 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1444 break;
1445 }
1446
1447 case Instruction::XOR_LONG: {
1448 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1449 break;
1450 }
1451
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001452 case Instruction::ADD_LONG_2ADDR: {
1453 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001454 break;
1455 }
1456
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001457 case Instruction::ADD_DOUBLE_2ADDR: {
1458 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1459 break;
1460 }
1461
1462 case Instruction::ADD_FLOAT_2ADDR: {
1463 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
1464 break;
1465 }
1466
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001467 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001468 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
1469 break;
1470 }
1471
1472 case Instruction::SUB_LONG_2ADDR: {
1473 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001474 break;
1475 }
1476
Calin Juravle096cc022014-10-23 17:01:13 +01001477 case Instruction::SUB_FLOAT_2ADDR: {
1478 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
1479 break;
1480 }
1481
1482 case Instruction::SUB_DOUBLE_2ADDR: {
1483 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
1484 break;
1485 }
1486
Calin Juravle34bacdf2014-10-07 20:23:36 +01001487 case Instruction::MUL_INT_2ADDR: {
1488 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
1489 break;
1490 }
1491
1492 case Instruction::MUL_LONG_2ADDR: {
1493 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
1494 break;
1495 }
1496
Calin Juravleb5bfa962014-10-21 18:02:24 +01001497 case Instruction::MUL_FLOAT_2ADDR: {
1498 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
1499 break;
1500 }
1501
1502 case Instruction::MUL_DOUBLE_2ADDR: {
1503 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
1504 break;
1505 }
1506
Calin Juravle865fc882014-11-06 17:09:03 +00001507 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001508 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1509 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00001510 break;
1511 }
1512
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001513 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001514 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1515 dex_pc, Primitive::kPrimLong, false, true);
1516 break;
1517 }
1518
1519 case Instruction::REM_INT_2ADDR: {
1520 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1521 dex_pc, Primitive::kPrimInt, false, false);
1522 break;
1523 }
1524
1525 case Instruction::REM_LONG_2ADDR: {
1526 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1527 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001528 break;
1529 }
1530
Calin Juravle9aec02f2014-11-18 23:06:35 +00001531 case Instruction::SHL_INT_2ADDR: {
1532 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
1533 break;
1534 }
1535
1536 case Instruction::SHL_LONG_2ADDR: {
1537 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
1538 break;
1539 }
1540
1541 case Instruction::SHR_INT_2ADDR: {
1542 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
1543 break;
1544 }
1545
1546 case Instruction::SHR_LONG_2ADDR: {
1547 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
1548 break;
1549 }
1550
1551 case Instruction::USHR_INT_2ADDR: {
1552 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
1553 break;
1554 }
1555
1556 case Instruction::USHR_LONG_2ADDR: {
1557 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
1558 break;
1559 }
1560
Calin Juravle7c4954d2014-10-28 16:57:40 +00001561 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001562 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001563 break;
1564 }
1565
1566 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001567 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001568 break;
1569 }
1570
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001571 case Instruction::AND_INT_2ADDR: {
1572 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
1573 break;
1574 }
1575
1576 case Instruction::AND_LONG_2ADDR: {
1577 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
1578 break;
1579 }
1580
1581 case Instruction::OR_INT_2ADDR: {
1582 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
1583 break;
1584 }
1585
1586 case Instruction::OR_LONG_2ADDR: {
1587 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
1588 break;
1589 }
1590
1591 case Instruction::XOR_INT_2ADDR: {
1592 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
1593 break;
1594 }
1595
1596 case Instruction::XOR_LONG_2ADDR: {
1597 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
1598 break;
1599 }
1600
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001601 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001602 Binop_22s<HAdd>(instruction, false);
1603 break;
1604 }
1605
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001606 case Instruction::AND_INT_LIT16: {
1607 Binop_22s<HAnd>(instruction, false);
1608 break;
1609 }
1610
1611 case Instruction::OR_INT_LIT16: {
1612 Binop_22s<HOr>(instruction, false);
1613 break;
1614 }
1615
1616 case Instruction::XOR_INT_LIT16: {
1617 Binop_22s<HXor>(instruction, false);
1618 break;
1619 }
1620
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001621 case Instruction::RSUB_INT: {
1622 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001623 break;
1624 }
1625
Calin Juravle34bacdf2014-10-07 20:23:36 +01001626 case Instruction::MUL_INT_LIT16: {
1627 Binop_22s<HMul>(instruction, false);
1628 break;
1629 }
1630
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001631 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001632 Binop_22b<HAdd>(instruction, false);
1633 break;
1634 }
1635
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001636 case Instruction::AND_INT_LIT8: {
1637 Binop_22b<HAnd>(instruction, false);
1638 break;
1639 }
1640
1641 case Instruction::OR_INT_LIT8: {
1642 Binop_22b<HOr>(instruction, false);
1643 break;
1644 }
1645
1646 case Instruction::XOR_INT_LIT8: {
1647 Binop_22b<HXor>(instruction, false);
1648 break;
1649 }
1650
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001651 case Instruction::RSUB_INT_LIT8: {
1652 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001653 break;
1654 }
1655
Calin Juravle34bacdf2014-10-07 20:23:36 +01001656 case Instruction::MUL_INT_LIT8: {
1657 Binop_22b<HMul>(instruction, false);
1658 break;
1659 }
1660
Calin Juravled0d48522014-11-04 16:40:20 +00001661 case Instruction::DIV_INT_LIT16:
1662 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001663 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1664 dex_pc, Primitive::kPrimInt, true, true);
1665 break;
1666 }
1667
1668 case Instruction::REM_INT_LIT16:
1669 case Instruction::REM_INT_LIT8: {
1670 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1671 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00001672 break;
1673 }
1674
Calin Juravle9aec02f2014-11-18 23:06:35 +00001675 case Instruction::SHL_INT_LIT8: {
1676 Binop_22b<HShl>(instruction, false);
1677 break;
1678 }
1679
1680 case Instruction::SHR_INT_LIT8: {
1681 Binop_22b<HShr>(instruction, false);
1682 break;
1683 }
1684
1685 case Instruction::USHR_INT_LIT8: {
1686 Binop_22b<HUShr>(instruction, false);
1687 break;
1688 }
1689
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001690 case Instruction::NEW_INSTANCE: {
1691 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001692 new (arena_) HNewInstance(dex_pc, instruction.VRegB_21c()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001693 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1694 break;
1695 }
1696
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001697 case Instruction::NEW_ARRAY: {
1698 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
1699 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001700 new (arena_) HNewArray(length, dex_pc, instruction.VRegC_22c()));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001701 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1702 break;
1703 }
1704
1705 case Instruction::FILLED_NEW_ARRAY: {
1706 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1707 uint32_t type_index = instruction.VRegB_35c();
1708 uint32_t args[5];
1709 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001710 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001711 break;
1712 }
1713
1714 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1715 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1716 uint32_t type_index = instruction.VRegB_3rc();
1717 uint32_t register_index = instruction.VRegC_3rc();
1718 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00001719 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001720 break;
1721 }
1722
1723 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00001724 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001725 break;
1726 }
1727
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001728 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001729 case Instruction::MOVE_RESULT_WIDE:
1730 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001731 UpdateLocal(instruction.VRegA(), latest_result_);
1732 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001733 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001734
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001735 case Instruction::CMP_LONG: {
Calin Juravleddb7df22014-11-25 20:56:51 +00001736 Binop_23x_cmp(instruction, Primitive::kPrimLong, HCompare::kNoBias);
1737 break;
1738 }
1739
1740 case Instruction::CMPG_FLOAT: {
1741 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kGtBias);
1742 break;
1743 }
1744
1745 case Instruction::CMPG_DOUBLE: {
1746 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kGtBias);
1747 break;
1748 }
1749
1750 case Instruction::CMPL_FLOAT: {
1751 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kLtBias);
1752 break;
1753 }
1754
1755 case Instruction::CMPL_DOUBLE: {
1756 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kLtBias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001757 break;
1758 }
1759
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001760 case Instruction::NOP:
1761 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001762
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001763 case Instruction::IGET:
1764 case Instruction::IGET_WIDE:
1765 case Instruction::IGET_OBJECT:
1766 case Instruction::IGET_BOOLEAN:
1767 case Instruction::IGET_BYTE:
1768 case Instruction::IGET_CHAR:
1769 case Instruction::IGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001770 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001771 return false;
1772 }
1773 break;
1774 }
1775
1776 case Instruction::IPUT:
1777 case Instruction::IPUT_WIDE:
1778 case Instruction::IPUT_OBJECT:
1779 case Instruction::IPUT_BOOLEAN:
1780 case Instruction::IPUT_BYTE:
1781 case Instruction::IPUT_CHAR:
1782 case Instruction::IPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001783 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001784 return false;
1785 }
1786 break;
1787 }
1788
1789 case Instruction::SGET:
1790 case Instruction::SGET_WIDE:
1791 case Instruction::SGET_OBJECT:
1792 case Instruction::SGET_BOOLEAN:
1793 case Instruction::SGET_BYTE:
1794 case Instruction::SGET_CHAR:
1795 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001796 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001797 return false;
1798 }
1799 break;
1800 }
1801
1802 case Instruction::SPUT:
1803 case Instruction::SPUT_WIDE:
1804 case Instruction::SPUT_OBJECT:
1805 case Instruction::SPUT_BOOLEAN:
1806 case Instruction::SPUT_BYTE:
1807 case Instruction::SPUT_CHAR:
1808 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001809 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001810 return false;
1811 }
1812 break;
1813 }
1814
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001815#define ARRAY_XX(kind, anticipated_type) \
1816 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001817 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001818 break; \
1819 } \
1820 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001821 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001822 break; \
1823 }
1824
1825 ARRAY_XX(, Primitive::kPrimInt);
1826 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1827 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1828 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1829 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1830 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1831 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1832
Nicolas Geoffray39468442014-09-02 15:17:15 +01001833 case Instruction::ARRAY_LENGTH: {
1834 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001835 // No need for a temporary for the null check, it is the only input of the following
1836 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00001837 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001838 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001839 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1840 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1841 break;
1842 }
1843
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001844 case Instruction::CONST_STRING: {
Calin Juravle225ff812014-11-13 16:46:39 +00001845 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001846 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1847 break;
1848 }
1849
1850 case Instruction::CONST_STRING_JUMBO: {
Calin Juravle225ff812014-11-13 16:46:39 +00001851 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001852 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
1853 break;
1854 }
1855
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001856 case Instruction::CONST_CLASS: {
1857 uint16_t type_index = instruction.VRegB_21c();
1858 bool type_known_final;
1859 bool type_known_abstract;
1860 bool is_referrers_class;
1861 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1862 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
1863 &type_known_final, &type_known_abstract, &is_referrers_class);
1864 if (!can_access) {
1865 return false;
1866 }
1867 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001868 new (arena_) HLoadClass(type_index, is_referrers_class, dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001869 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1870 break;
1871 }
1872
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001873 case Instruction::MOVE_EXCEPTION: {
1874 current_block_->AddInstruction(new (arena_) HLoadException());
1875 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
1876 break;
1877 }
1878
1879 case Instruction::THROW: {
1880 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001881 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001882 // A throw instruction must branch to the exit block.
1883 current_block_->AddSuccessor(exit_block_);
1884 // We finished building this block. Set the current block to null to avoid
1885 // adding dead instructions to it.
1886 current_block_ = nullptr;
1887 break;
1888 }
1889
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001890 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001891 uint8_t destination = instruction.VRegA_22c();
1892 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001893 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00001894 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001895 return false;
1896 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001897 break;
1898 }
1899
1900 case Instruction::CHECK_CAST: {
1901 uint8_t reference = instruction.VRegA_21c();
1902 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00001903 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001904 return false;
1905 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001906 break;
1907 }
1908
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001909 case Instruction::MONITOR_ENTER: {
1910 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1911 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1912 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00001913 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001914 break;
1915 }
1916
1917 case Instruction::MONITOR_EXIT: {
1918 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1919 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1920 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00001921 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001922 break;
1923 }
1924
Andreas Gamped881df52014-11-24 23:28:39 -08001925 case Instruction::PACKED_SWITCH: {
1926 if (!BuildPackedSwitch(instruction, dex_pc)) {
1927 return false;
1928 }
1929 break;
1930 }
1931
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001932 default:
1933 return false;
1934 }
1935 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001936} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001937
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001938HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001939 if (constant0_ != nullptr) {
1940 return constant0_;
1941 }
1942 constant0_ = new(arena_) HIntConstant(0);
1943 entry_block_->AddInstruction(constant0_);
1944 return constant0_;
1945}
1946
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001947HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001948 if (constant1_ != nullptr) {
1949 return constant1_;
1950 }
1951 constant1_ = new(arena_) HIntConstant(1);
1952 entry_block_->AddInstruction(constant1_);
1953 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001954}
1955
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001956HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001957 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001958 case 0: return GetIntConstant0();
1959 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001960 default: {
1961 HIntConstant* instruction = new (arena_) HIntConstant(constant);
1962 entry_block_->AddInstruction(instruction);
1963 return instruction;
1964 }
1965 }
1966}
1967
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001968HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
1969 HLongConstant* instruction = new (arena_) HLongConstant(constant);
1970 entry_block_->AddInstruction(instruction);
1971 return instruction;
1972}
1973
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001974HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
1975 return locals_.Get(register_index);
1976}
1977
1978void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
1979 HLocal* local = GetLocalAt(register_index);
1980 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
1981}
1982
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001983HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001984 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001985 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001986 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001987}
1988
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001989} // namespace art