blob: 4fa9f34cfc1f3862091423788a4f07b3d9fba9f4 [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,
419 Primitive::Type result_type) {
420 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
421 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first));
422 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
423}
424
Roland Levillain88cb1752014-10-20 16:36:47 +0100425template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100426void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100427 HInstruction* first = LoadLocal(instruction.VRegB(), type);
428 HInstruction* second = LoadLocal(instruction.VRegC(), type);
429 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100430 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
431}
432
433template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000434void HGraphBuilder::Binop_23x(const Instruction& instruction,
435 Primitive::Type type,
436 uint32_t dex_pc) {
437 HInstruction* first = LoadLocal(instruction.VRegB(), type);
438 HInstruction* second = LoadLocal(instruction.VRegC(), type);
439 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
440 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
441}
442
443template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000444void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
445 Primitive::Type type) {
446 HInstruction* first = LoadLocal(instruction.VRegB(), type);
447 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
448 current_block_->AddInstruction(new (arena_) T(type, first, second));
449 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
450}
451
Calin Juravleddb7df22014-11-25 20:56:51 +0000452void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
453 Primitive::Type type,
454 HCompare::Bias bias) {
455 HInstruction* first = LoadLocal(instruction.VRegB(), type);
456 HInstruction* second = LoadLocal(instruction.VRegC(), type);
457 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias));
458 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
459}
460
Calin Juravle9aec02f2014-11-18 23:06:35 +0000461template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100462void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
463 HInstruction* first = LoadLocal(instruction.VRegA(), type);
464 HInstruction* second = LoadLocal(instruction.VRegB(), type);
465 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100466 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
467}
468
469template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000470void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) {
471 HInstruction* first = LoadLocal(instruction.VRegA(), type);
472 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
473 current_block_->AddInstruction(new (arena_) T(type, first, second));
474 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
475}
476
477template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000478void HGraphBuilder::Binop_12x(const Instruction& instruction,
479 Primitive::Type type,
480 uint32_t dex_pc) {
481 HInstruction* first = LoadLocal(instruction.VRegA(), type);
482 HInstruction* second = LoadLocal(instruction.VRegB(), type);
483 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
484 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
485}
486
487template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100488void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100489 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
490 HInstruction* second = GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100491 if (reverse) {
492 std::swap(first, second);
493 }
494 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
495 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
496}
497
498template<typename T>
499void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100500 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
501 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100502 if (reverse) {
503 std::swap(first, second);
504 }
505 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
506 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
507}
508
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100509void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
510 if (type == Primitive::kPrimVoid) {
511 current_block_->AddInstruction(new (arena_) HReturnVoid());
512 } else {
513 HInstruction* value = LoadLocal(instruction.VRegA(), type);
514 current_block_->AddInstruction(new (arena_) HReturn(value));
515 }
516 current_block_->AddSuccessor(exit_block_);
517 current_block_ = nullptr;
518}
519
520bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000521 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100522 uint32_t method_idx,
523 uint32_t number_of_vreg_arguments,
524 bool is_range,
525 uint32_t* args,
526 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100527 Instruction::Code opcode = instruction.Opcode();
528 InvokeType invoke_type;
529 switch (opcode) {
530 case Instruction::INVOKE_STATIC:
531 case Instruction::INVOKE_STATIC_RANGE:
532 invoke_type = kStatic;
533 break;
534 case Instruction::INVOKE_DIRECT:
535 case Instruction::INVOKE_DIRECT_RANGE:
536 invoke_type = kDirect;
537 break;
538 case Instruction::INVOKE_VIRTUAL:
539 case Instruction::INVOKE_VIRTUAL_RANGE:
540 invoke_type = kVirtual;
541 break;
542 case Instruction::INVOKE_INTERFACE:
543 case Instruction::INVOKE_INTERFACE_RANGE:
544 invoke_type = kInterface;
545 break;
546 case Instruction::INVOKE_SUPER_RANGE:
547 case Instruction::INVOKE_SUPER:
548 invoke_type = kSuper;
549 break;
550 default:
551 LOG(FATAL) << "Unexpected invoke op: " << opcode;
552 return false;
553 }
554
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100555 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
556 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
557 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
558 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100559 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100560 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
561
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000562 MethodReference target_method(dex_file_, method_idx);
563 uintptr_t direct_code;
564 uintptr_t direct_method;
565 int table_index;
566 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000567
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000568 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
569 &optimized_invoke_type, &target_method, &table_index,
570 &direct_code, &direct_method)) {
571 LOG(INFO) << "Did not compile " << PrettyMethod(method_idx, *dex_file_)
572 << " because a method call could not be resolved";
573 return false;
574 }
575 DCHECK(optimized_invoke_type != kSuper);
576
577 HInvoke* invoke = nullptr;
578 if (optimized_invoke_type == kVirtual) {
579 invoke = new (arena_) HInvokeVirtual(
580 arena_, number_of_arguments, return_type, dex_pc, table_index);
581 } else if (optimized_invoke_type == kInterface) {
582 invoke = new (arena_) HInvokeInterface(
583 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100584 } else {
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000585 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic);
586 // Sharpening to kDirect only works if we compile PIC.
587 DCHECK((optimized_invoke_type == invoke_type) || (optimized_invoke_type != kDirect)
588 || compiler_driver_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100589 // Treat invoke-direct like static calls for now.
590 invoke = new (arena_) HInvokeStatic(
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000591 arena_, number_of_arguments, return_type, dex_pc, target_method.dex_method_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100592 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100593
594 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000595 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100596 if (is_instance_call) {
597 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000598 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100599 current_block_->AddInstruction(null_check);
600 temps.Add(null_check);
601 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100602 start_index = 1;
603 }
604
605 uint32_t descriptor_index = 1;
606 uint32_t argument_index = start_index;
607 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
608 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100609 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
610 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100611 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
Calin Juravle225ff812014-11-13 16:46:39 +0000612 << " at " << dex_pc;
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100613 // We do not implement non sequential register pair.
614 return false;
615 }
616 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
617 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100618 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100619 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100620 }
621 }
622
623 DCHECK_EQ(argument_index, number_of_arguments);
624 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100625 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100626 return true;
627}
628
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100629bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000630 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100631 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100632 uint32_t source_or_dest_reg = instruction.VRegA_22c();
633 uint32_t obj_reg = instruction.VRegB_22c();
634 uint16_t field_index = instruction.VRegC_22c();
635
636 ScopedObjectAccess soa(Thread::Current());
637 StackHandleScope<1> hs(soa.Self());
638 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
639 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
640
641 if (resolved_field.Get() == nullptr) {
642 return false;
643 }
644 if (resolved_field->IsVolatile()) {
645 return false;
646 }
647
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100648 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100649
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100650 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000651 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100652 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000653 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100654 HInstruction* null_check = current_block_->GetLastInstruction();
655 // We need one temporary for the null check.
656 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100657 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100658 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
659 null_check,
660 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100661 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100662 resolved_field->GetOffset()));
663 } else {
664 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
665 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100666 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100667 resolved_field->GetOffset()));
668
669 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
670 }
671 return true;
672}
673
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100674
675
676bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000677 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100678 bool is_put) {
679 uint32_t source_or_dest_reg = instruction.VRegA_21c();
680 uint16_t field_index = instruction.VRegB_21c();
681
682 uint32_t storage_index;
683 bool is_referrers_class;
684 bool is_initialized;
685 bool is_volatile;
686 MemberOffset field_offset(0u);
687 Primitive::Type field_type;
688
689 bool fast_path = compiler_driver_->ComputeStaticFieldInfo(field_index,
690 dex_compilation_unit_,
691 is_put,
692 &field_offset,
693 &storage_index,
694 &is_referrers_class,
695 &is_volatile,
696 &is_initialized,
697 &field_type);
698 if (!fast_path) {
699 return false;
700 }
701
702 if (is_volatile) {
703 return false;
704 }
705
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100706 HLoadClass* constant = new (arena_) HLoadClass(
Calin Juravle225ff812014-11-13 16:46:39 +0000707 storage_index, is_referrers_class, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100708 current_block_->AddInstruction(constant);
709
710 HInstruction* cls = constant;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000711 if (!is_initialized) {
Calin Juravle225ff812014-11-13 16:46:39 +0000712 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100713 current_block_->AddInstruction(cls);
714 }
715
716 if (is_put) {
717 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000718 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100719 temps.Add(cls);
720 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
721 DCHECK_EQ(value->GetType(), field_type);
722 current_block_->AddInstruction(
723 new (arena_) HStaticFieldSet(cls, value, field_type, field_offset));
724 } else {
725 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls, field_type, field_offset));
726 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
727 }
728 return true;
729}
730
Calin Juravlebacfec32014-11-14 15:54:36 +0000731void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
732 uint16_t first_vreg,
733 int64_t second_vreg_or_constant,
734 uint32_t dex_pc,
735 Primitive::Type type,
736 bool second_is_constant,
737 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000738 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +0000739
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000740 HInstruction* first = LoadLocal(first_vreg, type);
741 HInstruction* second = nullptr;
742 if (second_is_constant) {
743 if (type == Primitive::kPrimInt) {
744 second = GetIntConstant(second_vreg_or_constant);
745 } else {
746 second = GetLongConstant(second_vreg_or_constant);
747 }
748 } else {
749 second = LoadLocal(second_vreg_or_constant, type);
750 }
751
752 if (!second_is_constant
753 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
754 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
755 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000756 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000757 current_block_->AddInstruction(second);
758 temps.Add(current_block_->GetLastInstruction());
759 }
760
Calin Juravlebacfec32014-11-14 15:54:36 +0000761 if (isDiv) {
762 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
763 } else {
764 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
765 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000766 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +0000767}
768
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100769void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000770 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100771 bool is_put,
772 Primitive::Type anticipated_type) {
773 uint8_t source_or_dest_reg = instruction.VRegA_23x();
774 uint8_t array_reg = instruction.VRegB_23x();
775 uint8_t index_reg = instruction.VRegC_23x();
776
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100777 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000778 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100779
780 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000781 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100782 current_block_->AddInstruction(object);
783 temps.Add(object);
784
785 HInstruction* length = new (arena_) HArrayLength(object);
786 current_block_->AddInstruction(length);
787 temps.Add(length);
788 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +0000789 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100790 current_block_->AddInstruction(index);
791 temps.Add(index);
792 if (is_put) {
793 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
794 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100795 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000796 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100797 } else {
798 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
799 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
800 }
801}
802
Calin Juravle225ff812014-11-13 16:46:39 +0000803void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100804 uint32_t type_index,
805 uint32_t number_of_vreg_arguments,
806 bool is_range,
807 uint32_t* args,
808 uint32_t register_index) {
809 HInstruction* length = GetIntConstant(number_of_vreg_arguments);
Calin Juravle225ff812014-11-13 16:46:39 +0000810 HInstruction* object = new (arena_) HNewArray(length, dex_pc, type_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100811 current_block_->AddInstruction(object);
812
813 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
814 DCHECK_EQ(descriptor[0], '[') << descriptor;
815 char primitive = descriptor[1];
816 DCHECK(primitive == 'I'
817 || primitive == 'L'
818 || primitive == '[') << descriptor;
819 bool is_reference_array = (primitive == 'L') || (primitive == '[');
820 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
821
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000822 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100823 temps.Add(object);
824 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
825 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
826 HInstruction* index = GetIntConstant(i);
827 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000828 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100829 }
830 latest_result_ = object;
831}
832
833template <typename T>
834void HGraphBuilder::BuildFillArrayData(HInstruction* object,
835 const T* data,
836 uint32_t element_count,
837 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +0000838 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100839 for (uint32_t i = 0; i < element_count; ++i) {
840 HInstruction* index = GetIntConstant(i);
841 HInstruction* value = GetIntConstant(data[i]);
842 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000843 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100844 }
845}
846
Calin Juravle225ff812014-11-13 16:46:39 +0000847void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000848 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000849 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000850 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000851 current_block_->AddInstruction(null_check);
852 temps.Add(null_check);
853
854 HInstruction* length = new (arena_) HArrayLength(null_check);
855 current_block_->AddInstruction(length);
856
Calin Juravle225ff812014-11-13 16:46:39 +0000857 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +0000858 const Instruction::ArrayDataPayload* payload =
859 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
860 const uint8_t* data = payload->data;
861 uint32_t element_count = payload->element_count;
862
863 // Implementation of this DEX instruction seems to be that the bounds check is
864 // done before doing any stores.
865 HInstruction* last_index = GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +0000866 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +0000867
868 switch (payload->element_width) {
869 case 1:
870 BuildFillArrayData(null_check,
871 reinterpret_cast<const int8_t*>(data),
872 element_count,
873 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +0000874 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000875 break;
876 case 2:
877 BuildFillArrayData(null_check,
878 reinterpret_cast<const int16_t*>(data),
879 element_count,
880 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +0000881 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000882 break;
883 case 4:
884 BuildFillArrayData(null_check,
885 reinterpret_cast<const int32_t*>(data),
886 element_count,
887 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +0000888 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000889 break;
890 case 8:
891 BuildFillWideArrayData(null_check,
892 reinterpret_cast<const int64_t*>(data),
893 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000894 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000895 break;
896 default:
897 LOG(FATAL) << "Unknown element width for " << payload->element_width;
898 }
899}
900
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100901void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100902 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100903 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000904 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100905 for (uint32_t i = 0; i < element_count; ++i) {
906 HInstruction* index = GetIntConstant(i);
907 HInstruction* value = GetLongConstant(data[i]);
908 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000909 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100910 }
911}
912
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000913bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
914 uint8_t destination,
915 uint8_t reference,
916 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +0000917 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000918 bool type_known_final;
919 bool type_known_abstract;
920 bool is_referrers_class;
921 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
922 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
923 &type_known_final, &type_known_abstract, &is_referrers_class);
924 if (!can_access) {
925 return false;
926 }
927 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000928 HLoadClass* cls = new (arena_) HLoadClass(type_index, is_referrers_class, dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000929 current_block_->AddInstruction(cls);
930 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000931 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000932 temps.Add(cls);
933 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
934 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000935 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000936 UpdateLocal(destination, current_block_->GetLastInstruction());
937 } else {
938 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
939 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000940 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000941 }
942 return true;
943}
944
Andreas Gamped881df52014-11-24 23:28:39 -0800945bool HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
946 SwitchTable table(instruction, dex_pc, false);
947
948 // Value to test against.
949 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
950
951 // Chained cmp-and-branch, starting from starting_key.
952 int32_t starting_key = table.GetEntryAt(0);
953
954 uint16_t num_entries = table.GetNumEntries();
955 // On overflow condition (or zero cases) just punt.
956 if (num_entries == 0 || num_entries == UINT16_MAX) {
957 return false;
958 }
959
960 for (size_t i = 1; i <= num_entries; i++) {
961 int32_t target_offset = table.GetEntryAt(i);
962 PotentiallyAddSuspendCheck(target_offset, dex_pc);
963
964 // The current case's value.
965 HInstruction* this_case_value = GetIntConstant(starting_key + i - 1);
966
967 // Compare value and this_case_value.
968 HEqual* comparison = new (arena_) HEqual(value, this_case_value);
969 current_block_->AddInstruction(comparison);
970 HInstruction* ifinst = new (arena_) HIf(comparison);
971 current_block_->AddInstruction(ifinst);
972
973 // Case hit: use the target offset to determine where to go.
974 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
975 DCHECK(case_target != nullptr);
976 current_block_->AddSuccessor(case_target);
977
978 // Case miss: go to the next case (or default fall-through).
979 // When there is a next case, we use the block stored with the table offset representing this
980 // case (that is where we registered them in ComputeBranchTargets).
981 // When there is no next case, we use the following instruction.
982 // TODO: Peel the last iteration to avoid conditional.
983 if (i < table.GetNumEntries()) {
984 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(i));
985 DCHECK(next_case_target != nullptr);
986 current_block_->AddSuccessor(next_case_target);
987
988 // Need to manually add the block, as there is no dex-pc transition for the cases.
989 graph_->AddBlock(next_case_target);
990
991 current_block_ = next_case_target;
992 } else {
993 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
994 DCHECK(default_target != nullptr);
995 current_block_->AddSuccessor(default_target);
996 current_block_ = nullptr;
997 }
998 }
999 return true;
1000}
1001
Calin Juravle225ff812014-11-13 16:46:39 +00001002void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001003 if (target_offset <= 0) {
1004 // Unconditionnally add a suspend check to backward branches. We can remove
1005 // them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001006 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001007 }
1008}
1009
Calin Juravle225ff812014-11-13 16:46:39 +00001010bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001011 if (current_block_ == nullptr) {
1012 return true; // Dead code
1013 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001014
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001015 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001016 case Instruction::CONST_4: {
1017 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001018 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001019 UpdateLocal(register_index, constant);
1020 break;
1021 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001022
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001023 case Instruction::CONST_16: {
1024 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001025 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
1026 UpdateLocal(register_index, constant);
1027 break;
1028 }
1029
Dave Allison20dfc792014-06-16 20:44:29 -07001030 case Instruction::CONST: {
1031 int32_t register_index = instruction.VRegA();
1032 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
1033 UpdateLocal(register_index, constant);
1034 break;
1035 }
1036
1037 case Instruction::CONST_HIGH16: {
1038 int32_t register_index = instruction.VRegA();
1039 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
1040 UpdateLocal(register_index, constant);
1041 break;
1042 }
1043
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001044 case Instruction::CONST_WIDE_16: {
1045 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001046 // Get 16 bits of constant value, sign extended to 64 bits.
1047 int64_t value = instruction.VRegB_21s();
1048 value <<= 48;
1049 value >>= 48;
1050 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001051 UpdateLocal(register_index, constant);
1052 break;
1053 }
1054
1055 case Instruction::CONST_WIDE_32: {
1056 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001057 // Get 32 bits of constant value, sign extended to 64 bits.
1058 int64_t value = instruction.VRegB_31i();
1059 value <<= 32;
1060 value >>= 32;
1061 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001062 UpdateLocal(register_index, constant);
1063 break;
1064 }
1065
1066 case Instruction::CONST_WIDE: {
1067 int32_t register_index = instruction.VRegA();
1068 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001069 UpdateLocal(register_index, constant);
1070 break;
1071 }
1072
Dave Allison20dfc792014-06-16 20:44:29 -07001073 case Instruction::CONST_WIDE_HIGH16: {
1074 int32_t register_index = instruction.VRegA();
1075 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1076 HLongConstant* constant = GetLongConstant(value);
1077 UpdateLocal(register_index, constant);
1078 break;
1079 }
1080
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001081 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001082 case Instruction::MOVE:
1083 case Instruction::MOVE_FROM16:
1084 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001085 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001086 UpdateLocal(instruction.VRegA(), value);
1087 break;
1088 }
1089
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001090 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001091 case Instruction::MOVE_WIDE:
1092 case Instruction::MOVE_WIDE_FROM16:
1093 case Instruction::MOVE_WIDE_16: {
1094 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1095 UpdateLocal(instruction.VRegA(), value);
1096 break;
1097 }
1098
1099 case Instruction::MOVE_OBJECT:
1100 case Instruction::MOVE_OBJECT_16:
1101 case Instruction::MOVE_OBJECT_FROM16: {
1102 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
1103 UpdateLocal(instruction.VRegA(), value);
1104 break;
1105 }
1106
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001107 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001108 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001109 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001110 }
1111
Dave Allison20dfc792014-06-16 20:44:29 -07001112#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001113 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1114 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001115
Dave Allison20dfc792014-06-16 20:44:29 -07001116 IF_XX(HEqual, EQ);
1117 IF_XX(HNotEqual, NE);
1118 IF_XX(HLessThan, LT);
1119 IF_XX(HLessThanOrEqual, LE);
1120 IF_XX(HGreaterThan, GT);
1121 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001122
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001123 case Instruction::GOTO:
1124 case Instruction::GOTO_16:
1125 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001126 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001127 PotentiallyAddSuspendCheck(offset, dex_pc);
1128 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001129 DCHECK(target != nullptr);
1130 current_block_->AddInstruction(new (arena_) HGoto());
1131 current_block_->AddSuccessor(target);
1132 current_block_ = nullptr;
1133 break;
1134 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001135
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001136 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001137 DCHECK_NE(return_type_, Primitive::kPrimNot);
1138 DCHECK_NE(return_type_, Primitive::kPrimLong);
1139 DCHECK_NE(return_type_, Primitive::kPrimDouble);
1140 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001141 break;
1142 }
1143
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001144 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001145 DCHECK(return_type_ == Primitive::kPrimNot);
1146 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001147 break;
1148 }
1149
1150 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001151 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
1152 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001153 break;
1154 }
1155
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001156 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001157 case Instruction::INVOKE_INTERFACE:
1158 case Instruction::INVOKE_STATIC:
1159 case Instruction::INVOKE_SUPER:
1160 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001161 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001162 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001163 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001164 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001165 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001166 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001167 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001168 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001169 break;
1170 }
1171
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001172 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001173 case Instruction::INVOKE_INTERFACE_RANGE:
1174 case Instruction::INVOKE_STATIC_RANGE:
1175 case Instruction::INVOKE_SUPER_RANGE:
1176 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001177 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001178 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1179 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001180 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001181 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001182 return false;
1183 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001184 break;
1185 }
1186
Roland Levillain88cb1752014-10-20 16:36:47 +01001187 case Instruction::NEG_INT: {
1188 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1189 break;
1190 }
1191
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001192 case Instruction::NEG_LONG: {
1193 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1194 break;
1195 }
1196
Roland Levillain3dbcb382014-10-28 17:30:07 +00001197 case Instruction::NEG_FLOAT: {
1198 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1199 break;
1200 }
1201
1202 case Instruction::NEG_DOUBLE: {
1203 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1204 break;
1205 }
1206
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001207 case Instruction::NOT_INT: {
1208 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1209 break;
1210 }
1211
Roland Levillain70566432014-10-24 16:20:17 +01001212 case Instruction::NOT_LONG: {
1213 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1214 break;
1215 }
1216
Roland Levillaindff1f282014-11-05 14:15:05 +00001217 case Instruction::INT_TO_LONG: {
1218 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong);
1219 break;
1220 }
1221
Roland Levillaincff13742014-11-17 14:32:17 +00001222 case Instruction::INT_TO_FLOAT: {
1223 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat);
1224 break;
1225 }
1226
1227 case Instruction::INT_TO_DOUBLE: {
1228 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble);
1229 break;
1230 }
1231
Roland Levillain946e1432014-11-11 17:35:19 +00001232 case Instruction::LONG_TO_INT: {
1233 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt);
1234 break;
1235 }
1236
Roland Levillain6d0e4832014-11-27 18:31:21 +00001237 case Instruction::LONG_TO_FLOAT: {
1238 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat);
1239 break;
1240 }
1241
Roland Levillain647b9ed2014-11-27 12:06:00 +00001242 case Instruction::LONG_TO_DOUBLE: {
1243 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble);
1244 break;
1245 }
1246
Roland Levillain3f8f9362014-12-02 17:45:01 +00001247 case Instruction::FLOAT_TO_INT: {
1248 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt);
1249 break;
1250 }
1251
Roland Levillain51d3fc42014-11-13 14:11:42 +00001252 case Instruction::INT_TO_BYTE: {
1253 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte);
1254 break;
1255 }
1256
Roland Levillain01a8d712014-11-14 16:27:39 +00001257 case Instruction::INT_TO_SHORT: {
1258 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort);
1259 break;
1260 }
1261
Roland Levillain981e4542014-11-14 11:47:14 +00001262 case Instruction::INT_TO_CHAR: {
1263 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar);
1264 break;
1265 }
1266
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001267 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001268 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001269 break;
1270 }
1271
1272 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001273 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001274 break;
1275 }
1276
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001277 case Instruction::ADD_DOUBLE: {
1278 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1279 break;
1280 }
1281
1282 case Instruction::ADD_FLOAT: {
1283 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1284 break;
1285 }
1286
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001287 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001288 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001289 break;
1290 }
1291
1292 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001293 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001294 break;
1295 }
1296
Calin Juravle096cc022014-10-23 17:01:13 +01001297 case Instruction::SUB_FLOAT: {
1298 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1299 break;
1300 }
1301
1302 case Instruction::SUB_DOUBLE: {
1303 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1304 break;
1305 }
1306
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001307 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001308 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1309 break;
1310 }
1311
Calin Juravle34bacdf2014-10-07 20:23:36 +01001312 case Instruction::MUL_INT: {
1313 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1314 break;
1315 }
1316
1317 case Instruction::MUL_LONG: {
1318 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1319 break;
1320 }
1321
Calin Juravleb5bfa962014-10-21 18:02:24 +01001322 case Instruction::MUL_FLOAT: {
1323 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1324 break;
1325 }
1326
1327 case Instruction::MUL_DOUBLE: {
1328 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1329 break;
1330 }
1331
Calin Juravled0d48522014-11-04 16:40:20 +00001332 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001333 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1334 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001335 break;
1336 }
1337
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001338 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001339 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1340 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001341 break;
1342 }
1343
Calin Juravle7c4954d2014-10-28 16:57:40 +00001344 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001345 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001346 break;
1347 }
1348
1349 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001350 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001351 break;
1352 }
1353
Calin Juravlebacfec32014-11-14 15:54:36 +00001354 case Instruction::REM_INT: {
1355 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1356 dex_pc, Primitive::kPrimInt, false, false);
1357 break;
1358 }
1359
1360 case Instruction::REM_LONG: {
1361 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1362 dex_pc, Primitive::kPrimLong, false, false);
1363 break;
1364 }
1365
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001366 case Instruction::AND_INT: {
1367 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1368 break;
1369 }
1370
1371 case Instruction::AND_LONG: {
1372 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1373 break;
1374 }
1375
Calin Juravle9aec02f2014-11-18 23:06:35 +00001376 case Instruction::SHL_INT: {
1377 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
1378 break;
1379 }
1380
1381 case Instruction::SHL_LONG: {
1382 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
1383 break;
1384 }
1385
1386 case Instruction::SHR_INT: {
1387 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
1388 break;
1389 }
1390
1391 case Instruction::SHR_LONG: {
1392 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
1393 break;
1394 }
1395
1396 case Instruction::USHR_INT: {
1397 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
1398 break;
1399 }
1400
1401 case Instruction::USHR_LONG: {
1402 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
1403 break;
1404 }
1405
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001406 case Instruction::OR_INT: {
1407 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1408 break;
1409 }
1410
1411 case Instruction::OR_LONG: {
1412 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1413 break;
1414 }
1415
1416 case Instruction::XOR_INT: {
1417 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1418 break;
1419 }
1420
1421 case Instruction::XOR_LONG: {
1422 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1423 break;
1424 }
1425
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001426 case Instruction::ADD_LONG_2ADDR: {
1427 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001428 break;
1429 }
1430
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001431 case Instruction::ADD_DOUBLE_2ADDR: {
1432 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1433 break;
1434 }
1435
1436 case Instruction::ADD_FLOAT_2ADDR: {
1437 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
1438 break;
1439 }
1440
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001441 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001442 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
1443 break;
1444 }
1445
1446 case Instruction::SUB_LONG_2ADDR: {
1447 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001448 break;
1449 }
1450
Calin Juravle096cc022014-10-23 17:01:13 +01001451 case Instruction::SUB_FLOAT_2ADDR: {
1452 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
1453 break;
1454 }
1455
1456 case Instruction::SUB_DOUBLE_2ADDR: {
1457 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
1458 break;
1459 }
1460
Calin Juravle34bacdf2014-10-07 20:23:36 +01001461 case Instruction::MUL_INT_2ADDR: {
1462 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
1463 break;
1464 }
1465
1466 case Instruction::MUL_LONG_2ADDR: {
1467 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
1468 break;
1469 }
1470
Calin Juravleb5bfa962014-10-21 18:02:24 +01001471 case Instruction::MUL_FLOAT_2ADDR: {
1472 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
1473 break;
1474 }
1475
1476 case Instruction::MUL_DOUBLE_2ADDR: {
1477 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
1478 break;
1479 }
1480
Calin Juravle865fc882014-11-06 17:09:03 +00001481 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001482 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1483 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00001484 break;
1485 }
1486
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001487 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001488 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1489 dex_pc, Primitive::kPrimLong, false, true);
1490 break;
1491 }
1492
1493 case Instruction::REM_INT_2ADDR: {
1494 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1495 dex_pc, Primitive::kPrimInt, false, false);
1496 break;
1497 }
1498
1499 case Instruction::REM_LONG_2ADDR: {
1500 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1501 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001502 break;
1503 }
1504
Calin Juravle9aec02f2014-11-18 23:06:35 +00001505 case Instruction::SHL_INT_2ADDR: {
1506 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
1507 break;
1508 }
1509
1510 case Instruction::SHL_LONG_2ADDR: {
1511 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
1512 break;
1513 }
1514
1515 case Instruction::SHR_INT_2ADDR: {
1516 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
1517 break;
1518 }
1519
1520 case Instruction::SHR_LONG_2ADDR: {
1521 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
1522 break;
1523 }
1524
1525 case Instruction::USHR_INT_2ADDR: {
1526 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
1527 break;
1528 }
1529
1530 case Instruction::USHR_LONG_2ADDR: {
1531 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
1532 break;
1533 }
1534
Calin Juravle7c4954d2014-10-28 16:57:40 +00001535 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001536 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001537 break;
1538 }
1539
1540 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001541 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001542 break;
1543 }
1544
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001545 case Instruction::AND_INT_2ADDR: {
1546 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
1547 break;
1548 }
1549
1550 case Instruction::AND_LONG_2ADDR: {
1551 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
1552 break;
1553 }
1554
1555 case Instruction::OR_INT_2ADDR: {
1556 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
1557 break;
1558 }
1559
1560 case Instruction::OR_LONG_2ADDR: {
1561 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
1562 break;
1563 }
1564
1565 case Instruction::XOR_INT_2ADDR: {
1566 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
1567 break;
1568 }
1569
1570 case Instruction::XOR_LONG_2ADDR: {
1571 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
1572 break;
1573 }
1574
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001575 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001576 Binop_22s<HAdd>(instruction, false);
1577 break;
1578 }
1579
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001580 case Instruction::AND_INT_LIT16: {
1581 Binop_22s<HAnd>(instruction, false);
1582 break;
1583 }
1584
1585 case Instruction::OR_INT_LIT16: {
1586 Binop_22s<HOr>(instruction, false);
1587 break;
1588 }
1589
1590 case Instruction::XOR_INT_LIT16: {
1591 Binop_22s<HXor>(instruction, false);
1592 break;
1593 }
1594
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001595 case Instruction::RSUB_INT: {
1596 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001597 break;
1598 }
1599
Calin Juravle34bacdf2014-10-07 20:23:36 +01001600 case Instruction::MUL_INT_LIT16: {
1601 Binop_22s<HMul>(instruction, false);
1602 break;
1603 }
1604
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001605 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001606 Binop_22b<HAdd>(instruction, false);
1607 break;
1608 }
1609
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001610 case Instruction::AND_INT_LIT8: {
1611 Binop_22b<HAnd>(instruction, false);
1612 break;
1613 }
1614
1615 case Instruction::OR_INT_LIT8: {
1616 Binop_22b<HOr>(instruction, false);
1617 break;
1618 }
1619
1620 case Instruction::XOR_INT_LIT8: {
1621 Binop_22b<HXor>(instruction, false);
1622 break;
1623 }
1624
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001625 case Instruction::RSUB_INT_LIT8: {
1626 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001627 break;
1628 }
1629
Calin Juravle34bacdf2014-10-07 20:23:36 +01001630 case Instruction::MUL_INT_LIT8: {
1631 Binop_22b<HMul>(instruction, false);
1632 break;
1633 }
1634
Calin Juravled0d48522014-11-04 16:40:20 +00001635 case Instruction::DIV_INT_LIT16:
1636 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001637 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1638 dex_pc, Primitive::kPrimInt, true, true);
1639 break;
1640 }
1641
1642 case Instruction::REM_INT_LIT16:
1643 case Instruction::REM_INT_LIT8: {
1644 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1645 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00001646 break;
1647 }
1648
Calin Juravle9aec02f2014-11-18 23:06:35 +00001649 case Instruction::SHL_INT_LIT8: {
1650 Binop_22b<HShl>(instruction, false);
1651 break;
1652 }
1653
1654 case Instruction::SHR_INT_LIT8: {
1655 Binop_22b<HShr>(instruction, false);
1656 break;
1657 }
1658
1659 case Instruction::USHR_INT_LIT8: {
1660 Binop_22b<HUShr>(instruction, false);
1661 break;
1662 }
1663
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001664 case Instruction::NEW_INSTANCE: {
1665 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001666 new (arena_) HNewInstance(dex_pc, instruction.VRegB_21c()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001667 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1668 break;
1669 }
1670
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001671 case Instruction::NEW_ARRAY: {
1672 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
1673 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001674 new (arena_) HNewArray(length, dex_pc, instruction.VRegC_22c()));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001675 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1676 break;
1677 }
1678
1679 case Instruction::FILLED_NEW_ARRAY: {
1680 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1681 uint32_t type_index = instruction.VRegB_35c();
1682 uint32_t args[5];
1683 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001684 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001685 break;
1686 }
1687
1688 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1689 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1690 uint32_t type_index = instruction.VRegB_3rc();
1691 uint32_t register_index = instruction.VRegC_3rc();
1692 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00001693 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001694 break;
1695 }
1696
1697 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00001698 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001699 break;
1700 }
1701
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001702 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001703 case Instruction::MOVE_RESULT_WIDE:
1704 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001705 UpdateLocal(instruction.VRegA(), latest_result_);
1706 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001707 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001708
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001709 case Instruction::CMP_LONG: {
Calin Juravleddb7df22014-11-25 20:56:51 +00001710 Binop_23x_cmp(instruction, Primitive::kPrimLong, HCompare::kNoBias);
1711 break;
1712 }
1713
1714 case Instruction::CMPG_FLOAT: {
1715 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kGtBias);
1716 break;
1717 }
1718
1719 case Instruction::CMPG_DOUBLE: {
1720 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kGtBias);
1721 break;
1722 }
1723
1724 case Instruction::CMPL_FLOAT: {
1725 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kLtBias);
1726 break;
1727 }
1728
1729 case Instruction::CMPL_DOUBLE: {
1730 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kLtBias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001731 break;
1732 }
1733
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001734 case Instruction::NOP:
1735 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001736
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001737 case Instruction::IGET:
1738 case Instruction::IGET_WIDE:
1739 case Instruction::IGET_OBJECT:
1740 case Instruction::IGET_BOOLEAN:
1741 case Instruction::IGET_BYTE:
1742 case Instruction::IGET_CHAR:
1743 case Instruction::IGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001744 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001745 return false;
1746 }
1747 break;
1748 }
1749
1750 case Instruction::IPUT:
1751 case Instruction::IPUT_WIDE:
1752 case Instruction::IPUT_OBJECT:
1753 case Instruction::IPUT_BOOLEAN:
1754 case Instruction::IPUT_BYTE:
1755 case Instruction::IPUT_CHAR:
1756 case Instruction::IPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001757 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001758 return false;
1759 }
1760 break;
1761 }
1762
1763 case Instruction::SGET:
1764 case Instruction::SGET_WIDE:
1765 case Instruction::SGET_OBJECT:
1766 case Instruction::SGET_BOOLEAN:
1767 case Instruction::SGET_BYTE:
1768 case Instruction::SGET_CHAR:
1769 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001770 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001771 return false;
1772 }
1773 break;
1774 }
1775
1776 case Instruction::SPUT:
1777 case Instruction::SPUT_WIDE:
1778 case Instruction::SPUT_OBJECT:
1779 case Instruction::SPUT_BOOLEAN:
1780 case Instruction::SPUT_BYTE:
1781 case Instruction::SPUT_CHAR:
1782 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001783 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001784 return false;
1785 }
1786 break;
1787 }
1788
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001789#define ARRAY_XX(kind, anticipated_type) \
1790 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001791 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001792 break; \
1793 } \
1794 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001795 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001796 break; \
1797 }
1798
1799 ARRAY_XX(, Primitive::kPrimInt);
1800 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1801 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1802 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1803 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1804 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1805 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1806
Nicolas Geoffray39468442014-09-02 15:17:15 +01001807 case Instruction::ARRAY_LENGTH: {
1808 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001809 // No need for a temporary for the null check, it is the only input of the following
1810 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00001811 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001812 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001813 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1814 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1815 break;
1816 }
1817
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001818 case Instruction::CONST_STRING: {
Calin Juravle225ff812014-11-13 16:46:39 +00001819 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001820 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1821 break;
1822 }
1823
1824 case Instruction::CONST_STRING_JUMBO: {
Calin Juravle225ff812014-11-13 16:46:39 +00001825 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001826 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
1827 break;
1828 }
1829
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001830 case Instruction::CONST_CLASS: {
1831 uint16_t type_index = instruction.VRegB_21c();
1832 bool type_known_final;
1833 bool type_known_abstract;
1834 bool is_referrers_class;
1835 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1836 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
1837 &type_known_final, &type_known_abstract, &is_referrers_class);
1838 if (!can_access) {
1839 return false;
1840 }
1841 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001842 new (arena_) HLoadClass(type_index, is_referrers_class, dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001843 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1844 break;
1845 }
1846
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001847 case Instruction::MOVE_EXCEPTION: {
1848 current_block_->AddInstruction(new (arena_) HLoadException());
1849 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
1850 break;
1851 }
1852
1853 case Instruction::THROW: {
1854 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001855 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001856 // A throw instruction must branch to the exit block.
1857 current_block_->AddSuccessor(exit_block_);
1858 // We finished building this block. Set the current block to null to avoid
1859 // adding dead instructions to it.
1860 current_block_ = nullptr;
1861 break;
1862 }
1863
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001864 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001865 uint8_t destination = instruction.VRegA_22c();
1866 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001867 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00001868 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001869 return false;
1870 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001871 break;
1872 }
1873
1874 case Instruction::CHECK_CAST: {
1875 uint8_t reference = instruction.VRegA_21c();
1876 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00001877 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001878 return false;
1879 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001880 break;
1881 }
1882
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001883 case Instruction::MONITOR_ENTER: {
1884 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1885 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1886 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00001887 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001888 break;
1889 }
1890
1891 case Instruction::MONITOR_EXIT: {
1892 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1893 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1894 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00001895 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001896 break;
1897 }
1898
Andreas Gamped881df52014-11-24 23:28:39 -08001899 case Instruction::PACKED_SWITCH: {
1900 if (!BuildPackedSwitch(instruction, dex_pc)) {
1901 return false;
1902 }
1903 break;
1904 }
1905
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001906 default:
1907 return false;
1908 }
1909 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001910} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001911
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001912HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001913 if (constant0_ != nullptr) {
1914 return constant0_;
1915 }
1916 constant0_ = new(arena_) HIntConstant(0);
1917 entry_block_->AddInstruction(constant0_);
1918 return constant0_;
1919}
1920
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001921HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001922 if (constant1_ != nullptr) {
1923 return constant1_;
1924 }
1925 constant1_ = new(arena_) HIntConstant(1);
1926 entry_block_->AddInstruction(constant1_);
1927 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001928}
1929
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001930HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001931 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001932 case 0: return GetIntConstant0();
1933 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001934 default: {
1935 HIntConstant* instruction = new (arena_) HIntConstant(constant);
1936 entry_block_->AddInstruction(instruction);
1937 return instruction;
1938 }
1939 }
1940}
1941
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001942HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
1943 HLongConstant* instruction = new (arena_) HLongConstant(constant);
1944 entry_block_->AddInstruction(instruction);
1945 return instruction;
1946}
1947
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001948HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
1949 return locals_.Get(register_index);
1950}
1951
1952void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
1953 HLocal* local = GetLocalAt(register_index);
1954 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
1955}
1956
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001957HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001958 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001959 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001960 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001961}
1962
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001963} // namespace art