blob: a883bd01492a779652f64818910c294015b4509f [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070019#include "art_field-inl.h"
Andreas Gamped881df52014-11-24 23:28:39 -080020#include "base/logging.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010021#include "class_linker.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080022#include "dex/verified_method.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000023#include "dex_file-inl.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000024#include "dex_instruction-inl.h"
Roland Levillain4c0eb422015-04-24 16:43:49 +010025#include "dex/verified_method.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010026#include "driver/compiler_driver-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000027#include "driver/compiler_options.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010028#include "mirror/class_loader.h"
29#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000031#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010032#include "scoped_thread_state_change.h"
33#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000034
35namespace art {
36
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010037/**
38 * Helper class to add HTemporary instructions. This class is used when
39 * converting a DEX instruction to multiple HInstruction, and where those
40 * instructions do not die at the following instruction, but instead spans
41 * multiple instructions.
42 */
43class Temporaries : public ValueObject {
44 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000045 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010046
47 void Add(HInstruction* instruction) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +000048 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000050
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010051 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000052
53 size_t offset;
54 if (instruction->GetType() == Primitive::kPrimLong
55 || instruction->GetType() == Primitive::kPrimDouble) {
56 offset = 2;
57 } else {
58 offset = 1;
59 }
60 index_ += offset;
61
62 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010063 }
64
65 private:
66 HGraph* const graph_;
67
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010068 // Current index in the temporary stack, updated by `Add`.
69 size_t index_;
70};
71
Andreas Gamped881df52014-11-24 23:28:39 -080072class SwitchTable : public ValueObject {
73 public:
74 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
75 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
76 int32_t table_offset = instruction.VRegB_31t();
77 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
78 if (sparse) {
79 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
80 } else {
81 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
82 }
83 num_entries_ = table[1];
84 values_ = reinterpret_cast<const int32_t*>(&table[2]);
85 }
86
87 uint16_t GetNumEntries() const {
88 return num_entries_;
89 }
90
Andreas Gampee4d4d322014-12-04 09:09:57 -080091 void CheckIndex(size_t index) const {
92 if (sparse_) {
93 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
94 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
95 } else {
96 // In a packed table, we have the starting key and num_entries_ values.
97 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
98 }
99 }
100
Andreas Gamped881df52014-11-24 23:28:39 -0800101 int32_t GetEntryAt(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800102 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800103 return values_[index];
104 }
105
106 uint32_t GetDexPcForIndex(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800107 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800108 return dex_pc_ +
109 (reinterpret_cast<const int16_t*>(values_ + index) -
110 reinterpret_cast<const int16_t*>(&instruction_));
111 }
112
Andreas Gampee4d4d322014-12-04 09:09:57 -0800113 // Index of the first value in the table.
114 size_t GetFirstValueIndex() const {
115 if (sparse_) {
116 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
117 return num_entries_;
118 } else {
119 // In a packed table, we have the starting key and num_entries_ values.
120 return 1;
121 }
122 }
123
Andreas Gamped881df52014-11-24 23:28:39 -0800124 private:
125 const Instruction& instruction_;
126 const uint32_t dex_pc_;
127
128 // Whether this is a sparse-switch table (or a packed-switch one).
129 const bool sparse_;
130
131 // This can't be const as it needs to be computed off of the given instruction, and complicated
132 // expressions in the initializer list seemed very ugly.
133 uint16_t num_entries_;
134
135 const int32_t* values_;
136
137 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
138};
139
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100140void HGraphBuilder::InitializeLocals(uint16_t count) {
141 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000142 locals_.SetSize(count);
143 for (int i = 0; i < count; i++) {
144 HLocal* local = new (arena_) HLocal(i);
145 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000146 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000147 }
148}
149
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000150void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100151 // dex_compilation_unit_ is null only when unit testing.
152 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000153 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100154 }
155
156 graph_->SetNumberOfInVRegs(number_of_parameters);
157 const char* shorty = dex_compilation_unit_->GetShorty();
158 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100159 int parameter_index = 0;
160
161 if (!dex_compilation_unit_->IsStatic()) {
162 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100163 HParameterValue* parameter =
Calin Juravle10e244f2015-01-26 18:54:32 +0000164 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot, true);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100165 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100166 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100167 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100168 number_of_parameters--;
169 }
170
171 uint32_t pos = 1;
172 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100173 HParameterValue* parameter =
174 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
175 entry_block_->AddInstruction(parameter);
176 HLocal* local = GetLocalAt(locals_index++);
177 // Store the parameter value in the local that the dex code will use
178 // to reference that parameter.
179 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
180 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
181 || (parameter->GetType() == Primitive::kPrimDouble);
182 if (is_wide) {
183 i++;
184 locals_index++;
185 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100186 }
187 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100188}
189
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100190template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000191void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000192 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000193 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
194 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
195 DCHECK(branch_target != nullptr);
196 DCHECK(fallthrough_target != nullptr);
197 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100198 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
199 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700200 T* comparison = new (arena_) T(first, second);
201 current_block_->AddInstruction(comparison);
202 HInstruction* ifinst = new (arena_) HIf(comparison);
203 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000204 current_block_->AddSuccessor(branch_target);
205 current_block_->AddSuccessor(fallthrough_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700206 current_block_ = nullptr;
207}
208
209template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000210void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000211 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000212 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
213 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
214 DCHECK(branch_target != nullptr);
215 DCHECK(fallthrough_target != nullptr);
216 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700217 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000218 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0));
Dave Allison20dfc792014-06-16 20:44:29 -0700219 current_block_->AddInstruction(comparison);
220 HInstruction* ifinst = new (arena_) HIf(comparison);
221 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000222 current_block_->AddSuccessor(branch_target);
223 current_block_->AddSuccessor(fallthrough_target);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100224 current_block_ = nullptr;
225}
226
Calin Juravle48c2b032014-12-09 18:11:36 +0000227void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
228 if (compilation_stats_ != nullptr) {
229 compilation_stats_->RecordStat(compilation_stat);
230 }
231}
232
David Brazdil1b498722015-03-31 11:37:18 +0100233bool HGraphBuilder::SkipCompilation(const DexFile::CodeItem& code_item,
Calin Juravle48c2b032014-12-09 18:11:36 +0000234 size_t number_of_branches) {
235 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000236 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
237 if (compiler_filter == CompilerOptions::kEverything) {
238 return false;
239 }
240
David Brazdil1b498722015-03-31 11:37:18 +0100241 if (compiler_options.IsHugeMethod(code_item.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000242 VLOG(compiler) << "Skip compilation of huge method "
243 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100244 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000245 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000246 return true;
247 }
248
249 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil1b498722015-03-31 11:37:18 +0100250 if (compiler_options.IsLargeMethod(code_item.insns_size_in_code_units_)
251 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000252 VLOG(compiler) << "Skip compilation of large method with no branch "
253 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100254 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000255 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000256 return true;
257 }
258
259 return false;
260}
261
David Brazdil5e8b1372015-01-23 14:39:08 +0000262bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
263 DCHECK(graph_->GetBlocks().IsEmpty());
264
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000265 const uint16_t* code_ptr = code_item.insns_;
266 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100267 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000268
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000269 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100270 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000271 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100272 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000273 graph_->SetEntryBlock(entry_block_);
274 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000275
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000276 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000277 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000278
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000279 // Compute the number of dex instructions, blocks, and branches. We will
280 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000281 size_t number_of_branches = 0;
282
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000283 // To avoid splitting blocks, we compute ahead of time the instructions that
284 // start a new block, and create these blocks.
David Brazdil1b498722015-03-31 11:37:18 +0100285 ComputeBranchTargets(code_ptr, code_end, &number_of_branches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000286
287 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100288 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000289 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000290 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000291
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000292 // Also create blocks for catch handlers.
293 if (code_item.tries_size_ != 0) {
294 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
295 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
296 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
297 CatchHandlerIterator iterator(handlers_ptr);
298 for (; iterator.HasNext(); iterator.Next()) {
299 uint32_t address = iterator.GetHandlerAddress();
300 HBasicBlock* block = FindBlockStartingAt(address);
301 if (block == nullptr) {
302 block = new (arena_) HBasicBlock(graph_, address);
303 branch_targets_.Put(address, block);
304 }
305 block->SetIsCatchBlock();
306 }
307 handlers_ptr = iterator.EndDataPointer();
308 }
309 }
310
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000311 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100312
Calin Juravle225ff812014-11-13 16:46:39 +0000313 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000314 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000315 // Update the current block if dex_pc starts a new block.
316 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000317 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle48c2b032014-12-09 18:11:36 +0000318 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000319 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000320 }
Calin Juravle225ff812014-11-13 16:46:39 +0000321 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000322 code_ptr += instruction.SizeInCodeUnits();
323 }
324
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000325 // Add the exit block at the end to give it the highest id.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000326 graph_->AddBlock(exit_block_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000327 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000328 // Add the suspend check to the entry block.
329 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000330 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdil5e8b1372015-01-23 14:39:08 +0000331
332 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000333}
334
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000335void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) {
336 HBasicBlock* block = FindBlockStartingAt(index);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000337 if (block == nullptr) {
338 return;
339 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000340
341 if (current_block_ != nullptr) {
342 // Branching instructions clear current_block, so we know
343 // the last instruction of the current block is not a branching
344 // instruction. We add an unconditional goto to the found block.
345 current_block_->AddInstruction(new (arena_) HGoto());
346 current_block_->AddSuccessor(block);
347 }
348 graph_->AddBlock(block);
349 current_block_ = block;
350}
351
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000352void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
353 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000354 size_t* number_of_branches) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000355 branch_targets_.SetSize(code_end - code_ptr);
356
357 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100358 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000359 branch_targets_.Put(0, block);
360 entry_block_->AddSuccessor(block);
361
362 // Iterate over all instructions and find branching instructions. Create blocks for
363 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800364 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000365 while (code_ptr < code_end) {
366 const Instruction& instruction = *Instruction::At(code_ptr);
367 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000368 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000369 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000370 // Create a block for the target instruction.
371 if (FindBlockStartingAt(target) == nullptr) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100372 block = new (arena_) HBasicBlock(graph_, target);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000373 branch_targets_.Put(target, block);
374 }
Calin Juravle225ff812014-11-13 16:46:39 +0000375 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000376 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000377 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_pc) == nullptr)) {
378 block = new (arena_) HBasicBlock(graph_, dex_pc);
379 branch_targets_.Put(dex_pc, block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000380 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800381 } else if (instruction.IsSwitch()) {
382 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800383
384 uint16_t num_entries = table.GetNumEntries();
385
Andreas Gampee4d4d322014-12-04 09:09:57 -0800386 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
387 // entry at index 0 is the first key, and values are after *all* keys.
388 size_t offset = table.GetFirstValueIndex();
389
390 // Use a larger loop counter type to avoid overflow issues.
391 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800392 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800393 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
Andreas Gamped881df52014-11-24 23:28:39 -0800394 if (FindBlockStartingAt(target) == nullptr) {
395 block = new (arena_) HBasicBlock(graph_, target);
396 branch_targets_.Put(target, block);
Andreas Gamped881df52014-11-24 23:28:39 -0800397 }
398
399 // The next case gets its own block.
400 if (i < num_entries) {
401 block = new (arena_) HBasicBlock(graph_, target);
402 branch_targets_.Put(table.GetDexPcForIndex(i), block);
Andreas Gamped881df52014-11-24 23:28:39 -0800403 }
404 }
405
406 // Fall-through. Add a block if there is more code afterwards.
407 dex_pc += instruction.SizeInCodeUnits();
408 code_ptr += instruction.SizeInCodeUnits();
409 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_pc) == nullptr)) {
410 block = new (arena_) HBasicBlock(graph_, dex_pc);
411 branch_targets_.Put(dex_pc, block);
Andreas Gamped881df52014-11-24 23:28:39 -0800412 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000413 } else {
414 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000415 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000416 }
417 }
418}
419
420HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const {
421 DCHECK_GE(index, 0);
422 return branch_targets_.Get(index);
423}
424
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100425template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100426void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
427 HInstruction* first = LoadLocal(instruction.VRegB(), type);
428 current_block_->AddInstruction(new (arena_) T(type, first));
429 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
430}
431
Roland Levillaindff1f282014-11-05 14:15:05 +0000432void HGraphBuilder::Conversion_12x(const Instruction& instruction,
433 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000434 Primitive::Type result_type,
435 uint32_t dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +0000436 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Roland Levillain624279f2014-12-04 11:54:28 +0000437 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Roland Levillaindff1f282014-11-05 14:15:05 +0000438 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
439}
440
Roland Levillain88cb1752014-10-20 16:36:47 +0100441template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100442void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100443 HInstruction* first = LoadLocal(instruction.VRegB(), type);
444 HInstruction* second = LoadLocal(instruction.VRegC(), type);
445 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100446 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
447}
448
449template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000450void HGraphBuilder::Binop_23x(const Instruction& instruction,
451 Primitive::Type type,
452 uint32_t dex_pc) {
453 HInstruction* first = LoadLocal(instruction.VRegB(), type);
454 HInstruction* second = LoadLocal(instruction.VRegC(), type);
455 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
456 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
457}
458
459template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000460void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
461 Primitive::Type type) {
462 HInstruction* first = LoadLocal(instruction.VRegB(), type);
463 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
464 current_block_->AddInstruction(new (arena_) T(type, first, second));
465 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
466}
467
Calin Juravleddb7df22014-11-25 20:56:51 +0000468void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
469 Primitive::Type type,
470 HCompare::Bias bias) {
471 HInstruction* first = LoadLocal(instruction.VRegB(), type);
472 HInstruction* second = LoadLocal(instruction.VRegC(), type);
473 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias));
474 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
475}
476
Calin Juravle9aec02f2014-11-18 23:06:35 +0000477template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100478void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
479 HInstruction* first = LoadLocal(instruction.VRegA(), type);
480 HInstruction* second = LoadLocal(instruction.VRegB(), type);
481 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100482 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
483}
484
485template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000486void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) {
487 HInstruction* first = LoadLocal(instruction.VRegA(), type);
488 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
489 current_block_->AddInstruction(new (arena_) T(type, first, second));
490 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
491}
492
493template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000494void HGraphBuilder::Binop_12x(const Instruction& instruction,
495 Primitive::Type type,
496 uint32_t dex_pc) {
497 HInstruction* first = LoadLocal(instruction.VRegA(), type);
498 HInstruction* second = LoadLocal(instruction.VRegB(), type);
499 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
500 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
501}
502
503template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100504void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100505 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000506 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100507 if (reverse) {
508 std::swap(first, second);
509 }
510 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
511 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
512}
513
514template<typename T>
515void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100516 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000517 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100518 if (reverse) {
519 std::swap(first, second);
520 }
521 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
522 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
523}
524
Calin Juravle0c25d102015-04-20 14:49:09 +0100525static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
526 // dex compilation unit is null only when unit testing.
527 if (cu == nullptr) {
528 return false;
529 }
530
Calin Juravle27df7582015-04-17 19:12:31 +0100531 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100532 return cu->IsConstructor()
533 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100534}
535
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100536void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
537 if (type == Primitive::kPrimVoid) {
Calin Juravle27df7582015-04-17 19:12:31 +0100538 // Note that we might insert redundant barriers when inlining `super` calls.
539 // TODO: add a data flow analysis to get rid of duplicate barriers.
Calin Juravle0c25d102015-04-20 14:49:09 +0100540 if (RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_)) {
Calin Juravle27df7582015-04-17 19:12:31 +0100541 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore));
542 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100543 current_block_->AddInstruction(new (arena_) HReturnVoid());
544 } else {
545 HInstruction* value = LoadLocal(instruction.VRegA(), type);
546 current_block_->AddInstruction(new (arena_) HReturn(value));
547 }
548 current_block_->AddSuccessor(exit_block_);
549 current_block_ = nullptr;
550}
551
552bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000553 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100554 uint32_t method_idx,
555 uint32_t number_of_vreg_arguments,
556 bool is_range,
557 uint32_t* args,
558 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100559 Instruction::Code opcode = instruction.Opcode();
560 InvokeType invoke_type;
561 switch (opcode) {
562 case Instruction::INVOKE_STATIC:
563 case Instruction::INVOKE_STATIC_RANGE:
564 invoke_type = kStatic;
565 break;
566 case Instruction::INVOKE_DIRECT:
567 case Instruction::INVOKE_DIRECT_RANGE:
568 invoke_type = kDirect;
569 break;
570 case Instruction::INVOKE_VIRTUAL:
571 case Instruction::INVOKE_VIRTUAL_RANGE:
572 invoke_type = kVirtual;
573 break;
574 case Instruction::INVOKE_INTERFACE:
575 case Instruction::INVOKE_INTERFACE_RANGE:
576 invoke_type = kInterface;
577 break;
578 case Instruction::INVOKE_SUPER_RANGE:
579 case Instruction::INVOKE_SUPER:
580 invoke_type = kSuper;
581 break;
582 default:
583 LOG(FATAL) << "Unexpected invoke op: " << opcode;
584 return false;
585 }
586
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100587 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
588 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
589 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
590 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100591 bool is_instance_call = invoke_type != kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +0100592 size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100593
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000594 MethodReference target_method(dex_file_, method_idx);
595 uintptr_t direct_code;
596 uintptr_t direct_method;
597 int table_index;
598 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000599
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000600 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
601 &optimized_invoke_type, &target_method, &table_index,
602 &direct_code, &direct_method)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000603 VLOG(compiler) << "Did not compile " << PrettyMethod(method_idx, *dex_file_)
604 << " because a method call could not be resolved";
605 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000606 return false;
607 }
608 DCHECK(optimized_invoke_type != kSuper);
609
Roland Levillain4c0eb422015-04-24 16:43:49 +0100610 // By default, consider that the called method implicitly requires
611 // an initialization check of its declaring method.
612 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement =
613 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
614 // Potential class initialization check, in the case of a static method call.
615 HClinitCheck* clinit_check = nullptr;
Jeff Hao848f70a2014-01-15 13:49:50 -0800616 // Replace calls to String.<init> with StringFactory.
617 int32_t string_init_offset = 0;
618 bool is_string_init = compiler_driver_->IsStringInit(method_idx, dex_file_, &string_init_offset);
619 if (is_string_init) {
620 return_type = Primitive::kPrimNot;
621 is_instance_call = false;
622 number_of_arguments--;
623 invoke_type = kStatic;
624 optimized_invoke_type = kStatic;
625 }
Roland Levillain4c0eb422015-04-24 16:43:49 +0100626
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000627 HInvoke* invoke = nullptr;
Roland Levillain4c0eb422015-04-24 16:43:49 +0100628
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000629 if (optimized_invoke_type == kVirtual) {
630 invoke = new (arena_) HInvokeVirtual(
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800631 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000632 } else if (optimized_invoke_type == kInterface) {
633 invoke = new (arena_) HInvokeInterface(
634 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100635 } else {
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000636 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic);
637 // Sharpening to kDirect only works if we compile PIC.
638 DCHECK((optimized_invoke_type == invoke_type) || (optimized_invoke_type != kDirect)
639 || compiler_driver_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000640 bool is_recursive =
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000641 (target_method.dex_method_index == dex_compilation_unit_->GetDexMethodIndex());
642 DCHECK(!is_recursive || (target_method.dex_file == dex_compilation_unit_->GetDexFile()));
Roland Levillain4c0eb422015-04-24 16:43:49 +0100643
644 if (optimized_invoke_type == kStatic) {
645 ScopedObjectAccess soa(Thread::Current());
646 StackHandleScope<4> hs(soa.Self());
647 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
648 dex_compilation_unit_->GetClassLinker()->FindDexCache(
649 *dex_compilation_unit_->GetDexFile())));
650 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
651 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
652 mirror::ArtMethod* resolved_method = compiler_driver_->ResolveMethod(
653 soa, dex_cache, class_loader, dex_compilation_unit_, method_idx,
654 optimized_invoke_type);
655
656 if (resolved_method == nullptr) {
657 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
658 return false;
659 }
660
661 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
662 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
663 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
664 Handle<mirror::Class> referrer_class(hs.NewHandle(GetOutermostCompilingClass()));
665
666 // The index at which the method's class is stored in the DexCache's type array.
667 uint32_t storage_index = DexFile::kDexNoIndex;
668 bool is_referrer_class = (resolved_method->GetDeclaringClass() == referrer_class.Get());
669 if (is_referrer_class) {
670 storage_index = referrer_class->GetDexTypeIndex();
671 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
672 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
673 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
674 referrer_class.Get(),
675 resolved_method,
676 method_idx,
677 &storage_index);
678 }
679
Roland Levillain5f02c6c2015-04-24 19:14:22 +0100680 if (referrer_class.Get()->IsSubClass(resolved_method->GetDeclaringClass())) {
681 // If the referrer class is the declaring class or a subclass
682 // of the declaring class, no class initialization is needed
683 // before the static method call.
Roland Levillain4c0eb422015-04-24 16:43:49 +0100684 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
685 } else if (storage_index != DexFile::kDexNoIndex) {
686 // If the method's class type index is available, check
687 // whether we should add an explicit class initialization
688 // check for its declaring class before the static method call.
689
690 // TODO: find out why this check is needed.
691 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
692 *outer_compilation_unit_->GetDexFile(), storage_index);
693 bool is_initialized =
694 resolved_method->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
695
696 if (is_initialized) {
697 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
698 } else {
699 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
700 HLoadClass* load_class =
701 new (arena_) HLoadClass(storage_index, is_referrer_class, dex_pc);
702 current_block_->AddInstruction(load_class);
703 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
704 current_block_->AddInstruction(clinit_check);
705 ++number_of_arguments;
706 }
707 }
708 }
709
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000710 invoke = new (arena_) HInvokeStaticOrDirect(
711 arena_, number_of_arguments, return_type, dex_pc, target_method.dex_method_index,
Jeff Hao848f70a2014-01-15 13:49:50 -0800712 is_recursive, string_init_offset, invoke_type, optimized_invoke_type,
713 clinit_check_requirement);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100714 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100715
716 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000717 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100718 if (is_instance_call) {
719 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000720 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100721 current_block_->AddInstruction(null_check);
722 temps.Add(null_check);
723 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100724 start_index = 1;
725 }
726
727 uint32_t descriptor_index = 1;
728 uint32_t argument_index = start_index;
Jeff Hao848f70a2014-01-15 13:49:50 -0800729 if (is_string_init) {
730 start_index = 1;
731 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100732 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
733 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100734 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
735 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100736 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
Calin Juravle225ff812014-11-13 16:46:39 +0000737 << " at " << dex_pc;
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100738 // We do not implement non sequential register pair.
Calin Juravle48c2b032014-12-09 18:11:36 +0000739 MaybeRecordStat(MethodCompilationStat::kNotCompiledNonSequentialRegPair);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100740 return false;
741 }
742 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
743 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100744 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100745 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100746 }
747 }
748
Roland Levillain4c0eb422015-04-24 16:43:49 +0100749 if (clinit_check_requirement == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit) {
750 // Add the class initialization check as last input of `invoke`.
751 DCHECK(clinit_check != nullptr);
752 invoke->SetArgumentAt(argument_index++, clinit_check);
753 }
754
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100755 DCHECK_EQ(argument_index, number_of_arguments);
756 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100757 latest_result_ = invoke;
Jeff Hao848f70a2014-01-15 13:49:50 -0800758
759 // Add move-result for StringFactory method.
760 if (is_string_init) {
761 uint32_t orig_this_reg = is_range ? register_index : args[0];
762 const VerifiedMethod* verified_method =
763 compiler_driver_->GetVerifiedMethod(dex_file_, dex_compilation_unit_->GetDexMethodIndex());
764 if (verified_method == nullptr) {
765 LOG(WARNING) << "No verified method for method calling String.<init>: "
766 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_);
767 return false;
768 }
769 const SafeMap<uint32_t, std::set<uint32_t>>& string_init_map =
770 verified_method->GetStringInitPcRegMap();
771 auto map_it = string_init_map.find(dex_pc);
772 if (map_it != string_init_map.end()) {
773 std::set<uint32_t> reg_set = map_it->second;
774 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
775 UpdateLocal(*set_it, invoke);
776 }
777 }
778 UpdateLocal(orig_this_reg, invoke);
779 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100780 return true;
781}
782
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100783bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000784 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100785 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100786 uint32_t source_or_dest_reg = instruction.VRegA_22c();
787 uint32_t obj_reg = instruction.VRegB_22c();
788 uint16_t field_index = instruction.VRegC_22c();
789
790 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700791 ArtField* resolved_field =
792 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100793
Mathieu Chartierc7853442015-03-27 14:35:38 -0700794 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000795 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100796 return false;
797 }
Calin Juravle52c48962014-12-16 17:02:57 +0000798
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100799 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100800
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100801 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000802 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100803 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000804 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100805 HInstruction* null_check = current_block_->GetLastInstruction();
806 // We need one temporary for the null check.
807 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100808 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100809 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
810 null_check,
811 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100812 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +0000813 resolved_field->GetOffset(),
814 resolved_field->IsVolatile()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100815 } else {
816 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
817 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100818 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +0000819 resolved_field->GetOffset(),
820 resolved_field->IsVolatile()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100821
822 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
823 }
824 return true;
825}
826
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000827mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
828 ScopedObjectAccess soa(Thread::Current());
829 StackHandleScope<2> hs(soa.Self());
830 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
831 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
832 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
833 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
834 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
835
836 return compiler_driver_->ResolveCompilingMethodsClass(
837 soa, outer_dex_cache, class_loader, outer_compilation_unit_);
838}
839
840bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
841 ScopedObjectAccess soa(Thread::Current());
842 StackHandleScope<4> hs(soa.Self());
843 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
844 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
845 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
846 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
847 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
848 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
849 Handle<mirror::Class> compiling_class(hs.NewHandle(GetOutermostCompilingClass()));
850
851 return compiling_class.Get() == cls.Get();
852}
853
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100854bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000855 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100856 bool is_put) {
857 uint32_t source_or_dest_reg = instruction.VRegA_21c();
858 uint16_t field_index = instruction.VRegB_21c();
859
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000860 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700861 StackHandleScope<4> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000862 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
863 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
864 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
865 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700866 ArtField* resolved_field = compiler_driver_->ResolveField(
867 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100868
Mathieu Chartierc7853442015-03-27 14:35:38 -0700869 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000870 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100871 return false;
872 }
873
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000874 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
875 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
876 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
877 Handle<mirror::Class> referrer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000878
879 // The index at which the field's class is stored in the DexCache's type array.
880 uint32_t storage_index;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000881 bool is_referrer_class = (referrer_class.Get() == resolved_field->GetDeclaringClass());
882 if (is_referrer_class) {
883 storage_index = referrer_class->GetDexTypeIndex();
884 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100885 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000886 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000887 } else {
888 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
889 outer_dex_cache.Get(),
890 referrer_class.Get(),
Mathieu Chartierc7853442015-03-27 14:35:38 -0700891 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000892 field_index,
893 &storage_index);
894 bool can_easily_access = is_put ? pair.second : pair.first;
895 if (!can_easily_access) {
896 return false;
897 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000898 }
899
900 // TODO: find out why this check is needed.
901 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +0000902 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000903 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000904
905 HLoadClass* constant = new (arena_) HLoadClass(storage_index, is_referrer_class, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100906 current_block_->AddInstruction(constant);
907
908 HInstruction* cls = constant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000909 if (!is_initialized && !is_referrer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +0000910 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100911 current_block_->AddInstruction(cls);
912 }
913
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000914 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100915 if (is_put) {
916 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000917 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100918 temps.Add(cls);
919 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
920 DCHECK_EQ(value->GetType(), field_type);
921 current_block_->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000922 new (arena_) HStaticFieldSet(cls, value, field_type, resolved_field->GetOffset(),
923 resolved_field->IsVolatile()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100924 } else {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000925 current_block_->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000926 new (arena_) HStaticFieldGet(cls, field_type, resolved_field->GetOffset(),
927 resolved_field->IsVolatile()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100928 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
929 }
930 return true;
931}
932
Calin Juravlebacfec32014-11-14 15:54:36 +0000933void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
934 uint16_t first_vreg,
935 int64_t second_vreg_or_constant,
936 uint32_t dex_pc,
937 Primitive::Type type,
938 bool second_is_constant,
939 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000940 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +0000941
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000942 HInstruction* first = LoadLocal(first_vreg, type);
943 HInstruction* second = nullptr;
944 if (second_is_constant) {
945 if (type == Primitive::kPrimInt) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000946 second = graph_->GetIntConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000947 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000948 second = graph_->GetLongConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000949 }
950 } else {
951 second = LoadLocal(second_vreg_or_constant, type);
952 }
953
954 if (!second_is_constant
955 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
956 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
957 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000958 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000959 current_block_->AddInstruction(second);
960 temps.Add(current_block_->GetLastInstruction());
961 }
962
Calin Juravlebacfec32014-11-14 15:54:36 +0000963 if (isDiv) {
964 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
965 } else {
966 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
967 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000968 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +0000969}
970
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100971void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000972 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100973 bool is_put,
974 Primitive::Type anticipated_type) {
975 uint8_t source_or_dest_reg = instruction.VRegA_23x();
976 uint8_t array_reg = instruction.VRegB_23x();
977 uint8_t index_reg = instruction.VRegC_23x();
978
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100979 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000980 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100981
982 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000983 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100984 current_block_->AddInstruction(object);
985 temps.Add(object);
986
987 HInstruction* length = new (arena_) HArrayLength(object);
988 current_block_->AddInstruction(length);
989 temps.Add(length);
990 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +0000991 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100992 current_block_->AddInstruction(index);
993 temps.Add(index);
994 if (is_put) {
995 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
996 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100997 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000998 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100999 } else {
1000 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
1001 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1002 }
Mark Mendell1152c922015-04-24 17:06:35 -04001003 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001004}
1005
Calin Juravle225ff812014-11-13 16:46:39 +00001006void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001007 uint32_t type_index,
1008 uint32_t number_of_vreg_arguments,
1009 bool is_range,
1010 uint32_t* args,
1011 uint32_t register_index) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001012 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001013 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1014 ? kQuickAllocArrayWithAccessCheck
1015 : kQuickAllocArray;
1016 HInstruction* object = new (arena_) HNewArray(length, dex_pc, type_index, entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001017 current_block_->AddInstruction(object);
1018
1019 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1020 DCHECK_EQ(descriptor[0], '[') << descriptor;
1021 char primitive = descriptor[1];
1022 DCHECK(primitive == 'I'
1023 || primitive == 'L'
1024 || primitive == '[') << descriptor;
1025 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1026 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1027
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001028 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001029 temps.Add(object);
1030 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1031 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
David Brazdil8d5b8b22015-03-24 10:51:52 +00001032 HInstruction* index = graph_->GetIntConstant(i);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001033 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001034 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001035 }
1036 latest_result_ = object;
1037}
1038
1039template <typename T>
1040void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1041 const T* data,
1042 uint32_t element_count,
1043 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001044 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001045 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001046 HInstruction* index = graph_->GetIntConstant(i);
1047 HInstruction* value = graph_->GetIntConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001048 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001049 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001050 }
1051}
1052
Calin Juravle225ff812014-11-13 16:46:39 +00001053void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001054 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001055 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001056 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001057 current_block_->AddInstruction(null_check);
1058 temps.Add(null_check);
1059
1060 HInstruction* length = new (arena_) HArrayLength(null_check);
1061 current_block_->AddInstruction(length);
1062
Calin Juravle225ff812014-11-13 16:46:39 +00001063 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001064 const Instruction::ArrayDataPayload* payload =
1065 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1066 const uint8_t* data = payload->data;
1067 uint32_t element_count = payload->element_count;
1068
1069 // Implementation of this DEX instruction seems to be that the bounds check is
1070 // done before doing any stores.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001071 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +00001072 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001073
1074 switch (payload->element_width) {
1075 case 1:
1076 BuildFillArrayData(null_check,
1077 reinterpret_cast<const int8_t*>(data),
1078 element_count,
1079 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001080 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001081 break;
1082 case 2:
1083 BuildFillArrayData(null_check,
1084 reinterpret_cast<const int16_t*>(data),
1085 element_count,
1086 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001087 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001088 break;
1089 case 4:
1090 BuildFillArrayData(null_check,
1091 reinterpret_cast<const int32_t*>(data),
1092 element_count,
1093 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001094 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001095 break;
1096 case 8:
1097 BuildFillWideArrayData(null_check,
1098 reinterpret_cast<const int64_t*>(data),
1099 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001100 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001101 break;
1102 default:
1103 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1104 }
Mark Mendell1152c922015-04-24 17:06:35 -04001105 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001106}
1107
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001108void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001109 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001110 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001111 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001112 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001113 HInstruction* index = graph_->GetIntConstant(i);
1114 HInstruction* value = graph_->GetLongConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001115 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001116 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001117 }
1118}
1119
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001120bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
1121 uint8_t destination,
1122 uint8_t reference,
1123 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001124 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001125 bool type_known_final;
1126 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001127 // `CanAccessTypeWithoutChecks` will tell whether the method being
1128 // built is trying to access its own class, so that the generated
1129 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001130 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001131 bool dont_use_is_referrers_class;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001132 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1133 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001134 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001135 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001136 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001137 return false;
1138 }
1139 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001140 HLoadClass* cls = new (arena_) HLoadClass(
1141 type_index, IsOutermostCompilingClass(type_index), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001142 current_block_->AddInstruction(cls);
1143 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001144 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001145 temps.Add(cls);
1146 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1147 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001148 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001149 UpdateLocal(destination, current_block_->GetLastInstruction());
1150 } else {
1151 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1152 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001153 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001154 }
1155 return true;
1156}
1157
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001158bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1159 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1160 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1161}
1162
Calin Juravle48c2b032014-12-09 18:11:36 +00001163void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
Andreas Gamped881df52014-11-24 23:28:39 -08001164 SwitchTable table(instruction, dex_pc, false);
1165
1166 // Value to test against.
1167 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1168
Andreas Gampee4d4d322014-12-04 09:09:57 -08001169 uint16_t num_entries = table.GetNumEntries();
1170 // There should be at least one entry here.
1171 DCHECK_GT(num_entries, 0U);
1172
Andreas Gamped881df52014-11-24 23:28:39 -08001173 // Chained cmp-and-branch, starting from starting_key.
1174 int32_t starting_key = table.GetEntryAt(0);
1175
Andreas Gamped881df52014-11-24 23:28:39 -08001176 for (size_t i = 1; i <= num_entries; i++) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001177 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value, starting_key + i - 1,
1178 table.GetEntryAt(i), dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001179 }
Andreas Gamped881df52014-11-24 23:28:39 -08001180}
1181
Calin Juravle48c2b032014-12-09 18:11:36 +00001182void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001183 SwitchTable table(instruction, dex_pc, true);
1184
1185 // Value to test against.
1186 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1187
1188 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001189
1190 for (size_t i = 0; i < num_entries; i++) {
1191 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1192 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1193 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001194}
1195
1196void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1197 bool is_last_case, const SwitchTable& table,
1198 HInstruction* value, int32_t case_value_int,
1199 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001200 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1201 DCHECK(case_target != nullptr);
1202 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001203
1204 // The current case's value.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001205 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001206
1207 // Compare value and this_case_value.
1208 HEqual* comparison = new (arena_) HEqual(value, this_case_value);
1209 current_block_->AddInstruction(comparison);
1210 HInstruction* ifinst = new (arena_) HIf(comparison);
1211 current_block_->AddInstruction(ifinst);
1212
1213 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001214 current_block_->AddSuccessor(case_target);
1215
1216 // Case miss: go to the next case (or default fall-through).
1217 // When there is a next case, we use the block stored with the table offset representing this
1218 // case (that is where we registered them in ComputeBranchTargets).
1219 // When there is no next case, we use the following instruction.
1220 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1221 if (!is_last_case) {
1222 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1223 DCHECK(next_case_target != nullptr);
1224 current_block_->AddSuccessor(next_case_target);
1225
1226 // Need to manually add the block, as there is no dex-pc transition for the cases.
1227 graph_->AddBlock(next_case_target);
1228
1229 current_block_ = next_case_target;
1230 } else {
1231 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1232 DCHECK(default_target != nullptr);
1233 current_block_->AddSuccessor(default_target);
1234 current_block_ = nullptr;
1235 }
1236}
1237
David Brazdil852eaff2015-02-02 15:23:05 +00001238void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1239 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001240 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001241 // DX generates back edges to the first encountered return. We can save
1242 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001243 HInstruction* last_in_target = target->GetLastInstruction();
1244 if (last_in_target != nullptr &&
1245 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1246 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001247 }
1248
1249 // Add a suspend check to backward branches which may potentially loop. We
1250 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001251 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001252 }
1253}
1254
Calin Juravle225ff812014-11-13 16:46:39 +00001255bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001256 if (current_block_ == nullptr) {
1257 return true; // Dead code
1258 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001259
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001260 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001261 case Instruction::CONST_4: {
1262 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001263 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001264 UpdateLocal(register_index, constant);
1265 break;
1266 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001267
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001268 case Instruction::CONST_16: {
1269 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001270 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001271 UpdateLocal(register_index, constant);
1272 break;
1273 }
1274
Dave Allison20dfc792014-06-16 20:44:29 -07001275 case Instruction::CONST: {
1276 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001277 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i());
Dave Allison20dfc792014-06-16 20:44:29 -07001278 UpdateLocal(register_index, constant);
1279 break;
1280 }
1281
1282 case Instruction::CONST_HIGH16: {
1283 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001284 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16);
Dave Allison20dfc792014-06-16 20:44:29 -07001285 UpdateLocal(register_index, constant);
1286 break;
1287 }
1288
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001289 case Instruction::CONST_WIDE_16: {
1290 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001291 // Get 16 bits of constant value, sign extended to 64 bits.
1292 int64_t value = instruction.VRegB_21s();
1293 value <<= 48;
1294 value >>= 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001295 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001296 UpdateLocal(register_index, constant);
1297 break;
1298 }
1299
1300 case Instruction::CONST_WIDE_32: {
1301 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001302 // Get 32 bits of constant value, sign extended to 64 bits.
1303 int64_t value = instruction.VRegB_31i();
1304 value <<= 32;
1305 value >>= 32;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001306 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001307 UpdateLocal(register_index, constant);
1308 break;
1309 }
1310
1311 case Instruction::CONST_WIDE: {
1312 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001313 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001314 UpdateLocal(register_index, constant);
1315 break;
1316 }
1317
Dave Allison20dfc792014-06-16 20:44:29 -07001318 case Instruction::CONST_WIDE_HIGH16: {
1319 int32_t register_index = instruction.VRegA();
1320 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001321 HLongConstant* constant = graph_->GetLongConstant(value);
Dave Allison20dfc792014-06-16 20:44:29 -07001322 UpdateLocal(register_index, constant);
1323 break;
1324 }
1325
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001326 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001327 case Instruction::MOVE:
1328 case Instruction::MOVE_FROM16:
1329 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001330 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001331 UpdateLocal(instruction.VRegA(), value);
1332 break;
1333 }
1334
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001335 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001336 case Instruction::MOVE_WIDE:
1337 case Instruction::MOVE_WIDE_FROM16:
1338 case Instruction::MOVE_WIDE_16: {
1339 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1340 UpdateLocal(instruction.VRegA(), value);
1341 break;
1342 }
1343
1344 case Instruction::MOVE_OBJECT:
1345 case Instruction::MOVE_OBJECT_16:
1346 case Instruction::MOVE_OBJECT_FROM16: {
1347 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
1348 UpdateLocal(instruction.VRegA(), value);
1349 break;
1350 }
1351
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001352 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001353 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001354 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001355 }
1356
Dave Allison20dfc792014-06-16 20:44:29 -07001357#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001358 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1359 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001360
Dave Allison20dfc792014-06-16 20:44:29 -07001361 IF_XX(HEqual, EQ);
1362 IF_XX(HNotEqual, NE);
1363 IF_XX(HLessThan, LT);
1364 IF_XX(HLessThanOrEqual, LE);
1365 IF_XX(HGreaterThan, GT);
1366 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001367
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001368 case Instruction::GOTO:
1369 case Instruction::GOTO_16:
1370 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001371 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001372 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001373 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001374 PotentiallyAddSuspendCheck(target, dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001375 current_block_->AddInstruction(new (arena_) HGoto());
1376 current_block_->AddSuccessor(target);
1377 current_block_ = nullptr;
1378 break;
1379 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001380
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001381 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001382 DCHECK_NE(return_type_, Primitive::kPrimNot);
1383 DCHECK_NE(return_type_, Primitive::kPrimLong);
1384 DCHECK_NE(return_type_, Primitive::kPrimDouble);
1385 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001386 break;
1387 }
1388
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001389 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001390 DCHECK(return_type_ == Primitive::kPrimNot);
1391 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001392 break;
1393 }
1394
1395 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001396 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
1397 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001398 break;
1399 }
1400
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001401 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001402 case Instruction::INVOKE_INTERFACE:
1403 case Instruction::INVOKE_STATIC:
1404 case Instruction::INVOKE_SUPER:
1405 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001406 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001407 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001408 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001409 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001410 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001411 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001412 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001413 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001414 break;
1415 }
1416
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001417 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001418 case Instruction::INVOKE_INTERFACE_RANGE:
1419 case Instruction::INVOKE_STATIC_RANGE:
1420 case Instruction::INVOKE_SUPER_RANGE:
1421 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001422 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001423 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1424 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001425 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001426 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001427 return false;
1428 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001429 break;
1430 }
1431
Roland Levillain88cb1752014-10-20 16:36:47 +01001432 case Instruction::NEG_INT: {
1433 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1434 break;
1435 }
1436
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001437 case Instruction::NEG_LONG: {
1438 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1439 break;
1440 }
1441
Roland Levillain3dbcb382014-10-28 17:30:07 +00001442 case Instruction::NEG_FLOAT: {
1443 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1444 break;
1445 }
1446
1447 case Instruction::NEG_DOUBLE: {
1448 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1449 break;
1450 }
1451
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001452 case Instruction::NOT_INT: {
1453 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1454 break;
1455 }
1456
Roland Levillain70566432014-10-24 16:20:17 +01001457 case Instruction::NOT_LONG: {
1458 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1459 break;
1460 }
1461
Roland Levillaindff1f282014-11-05 14:15:05 +00001462 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001463 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001464 break;
1465 }
1466
Roland Levillaincff13742014-11-17 14:32:17 +00001467 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001468 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001469 break;
1470 }
1471
1472 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001473 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001474 break;
1475 }
1476
Roland Levillain946e1432014-11-11 17:35:19 +00001477 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001478 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001479 break;
1480 }
1481
Roland Levillain6d0e4832014-11-27 18:31:21 +00001482 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001483 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001484 break;
1485 }
1486
Roland Levillain647b9ed2014-11-27 12:06:00 +00001487 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001488 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001489 break;
1490 }
1491
Roland Levillain3f8f9362014-12-02 17:45:01 +00001492 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001493 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1494 break;
1495 }
1496
1497 case Instruction::FLOAT_TO_LONG: {
1498 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001499 break;
1500 }
1501
Roland Levillain8964e2b2014-12-04 12:10:50 +00001502 case Instruction::FLOAT_TO_DOUBLE: {
1503 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1504 break;
1505 }
1506
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001507 case Instruction::DOUBLE_TO_INT: {
1508 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1509 break;
1510 }
1511
1512 case Instruction::DOUBLE_TO_LONG: {
1513 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1514 break;
1515 }
1516
Roland Levillain8964e2b2014-12-04 12:10:50 +00001517 case Instruction::DOUBLE_TO_FLOAT: {
1518 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1519 break;
1520 }
1521
Roland Levillain51d3fc42014-11-13 14:11:42 +00001522 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001523 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001524 break;
1525 }
1526
Roland Levillain01a8d712014-11-14 16:27:39 +00001527 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001528 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001529 break;
1530 }
1531
Roland Levillain981e4542014-11-14 11:47:14 +00001532 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001533 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001534 break;
1535 }
1536
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001537 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001538 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001539 break;
1540 }
1541
1542 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001543 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001544 break;
1545 }
1546
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001547 case Instruction::ADD_DOUBLE: {
1548 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1549 break;
1550 }
1551
1552 case Instruction::ADD_FLOAT: {
1553 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1554 break;
1555 }
1556
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001557 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001558 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001559 break;
1560 }
1561
1562 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001563 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001564 break;
1565 }
1566
Calin Juravle096cc022014-10-23 17:01:13 +01001567 case Instruction::SUB_FLOAT: {
1568 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1569 break;
1570 }
1571
1572 case Instruction::SUB_DOUBLE: {
1573 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1574 break;
1575 }
1576
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001577 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001578 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1579 break;
1580 }
1581
Calin Juravle34bacdf2014-10-07 20:23:36 +01001582 case Instruction::MUL_INT: {
1583 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1584 break;
1585 }
1586
1587 case Instruction::MUL_LONG: {
1588 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1589 break;
1590 }
1591
Calin Juravleb5bfa962014-10-21 18:02:24 +01001592 case Instruction::MUL_FLOAT: {
1593 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1594 break;
1595 }
1596
1597 case Instruction::MUL_DOUBLE: {
1598 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1599 break;
1600 }
1601
Calin Juravled0d48522014-11-04 16:40:20 +00001602 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001603 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1604 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001605 break;
1606 }
1607
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001608 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001609 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1610 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001611 break;
1612 }
1613
Calin Juravle7c4954d2014-10-28 16:57:40 +00001614 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001615 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001616 break;
1617 }
1618
1619 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001620 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001621 break;
1622 }
1623
Calin Juravlebacfec32014-11-14 15:54:36 +00001624 case Instruction::REM_INT: {
1625 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1626 dex_pc, Primitive::kPrimInt, false, false);
1627 break;
1628 }
1629
1630 case Instruction::REM_LONG: {
1631 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1632 dex_pc, Primitive::kPrimLong, false, false);
1633 break;
1634 }
1635
Calin Juravled2ec87d2014-12-08 14:24:46 +00001636 case Instruction::REM_FLOAT: {
1637 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
1638 break;
1639 }
1640
1641 case Instruction::REM_DOUBLE: {
1642 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
1643 break;
1644 }
1645
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001646 case Instruction::AND_INT: {
1647 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1648 break;
1649 }
1650
1651 case Instruction::AND_LONG: {
1652 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1653 break;
1654 }
1655
Calin Juravle9aec02f2014-11-18 23:06:35 +00001656 case Instruction::SHL_INT: {
1657 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
1658 break;
1659 }
1660
1661 case Instruction::SHL_LONG: {
1662 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
1663 break;
1664 }
1665
1666 case Instruction::SHR_INT: {
1667 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
1668 break;
1669 }
1670
1671 case Instruction::SHR_LONG: {
1672 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
1673 break;
1674 }
1675
1676 case Instruction::USHR_INT: {
1677 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
1678 break;
1679 }
1680
1681 case Instruction::USHR_LONG: {
1682 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
1683 break;
1684 }
1685
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001686 case Instruction::OR_INT: {
1687 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1688 break;
1689 }
1690
1691 case Instruction::OR_LONG: {
1692 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1693 break;
1694 }
1695
1696 case Instruction::XOR_INT: {
1697 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1698 break;
1699 }
1700
1701 case Instruction::XOR_LONG: {
1702 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1703 break;
1704 }
1705
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001706 case Instruction::ADD_LONG_2ADDR: {
1707 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001708 break;
1709 }
1710
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001711 case Instruction::ADD_DOUBLE_2ADDR: {
1712 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1713 break;
1714 }
1715
1716 case Instruction::ADD_FLOAT_2ADDR: {
1717 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
1718 break;
1719 }
1720
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001721 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001722 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
1723 break;
1724 }
1725
1726 case Instruction::SUB_LONG_2ADDR: {
1727 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001728 break;
1729 }
1730
Calin Juravle096cc022014-10-23 17:01:13 +01001731 case Instruction::SUB_FLOAT_2ADDR: {
1732 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
1733 break;
1734 }
1735
1736 case Instruction::SUB_DOUBLE_2ADDR: {
1737 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
1738 break;
1739 }
1740
Calin Juravle34bacdf2014-10-07 20:23:36 +01001741 case Instruction::MUL_INT_2ADDR: {
1742 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
1743 break;
1744 }
1745
1746 case Instruction::MUL_LONG_2ADDR: {
1747 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
1748 break;
1749 }
1750
Calin Juravleb5bfa962014-10-21 18:02:24 +01001751 case Instruction::MUL_FLOAT_2ADDR: {
1752 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
1753 break;
1754 }
1755
1756 case Instruction::MUL_DOUBLE_2ADDR: {
1757 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
1758 break;
1759 }
1760
Calin Juravle865fc882014-11-06 17:09:03 +00001761 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001762 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1763 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00001764 break;
1765 }
1766
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001767 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001768 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1769 dex_pc, Primitive::kPrimLong, false, true);
1770 break;
1771 }
1772
1773 case Instruction::REM_INT_2ADDR: {
1774 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1775 dex_pc, Primitive::kPrimInt, false, false);
1776 break;
1777 }
1778
1779 case Instruction::REM_LONG_2ADDR: {
1780 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1781 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001782 break;
1783 }
1784
Calin Juravled2ec87d2014-12-08 14:24:46 +00001785 case Instruction::REM_FLOAT_2ADDR: {
1786 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
1787 break;
1788 }
1789
1790 case Instruction::REM_DOUBLE_2ADDR: {
1791 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
1792 break;
1793 }
1794
Calin Juravle9aec02f2014-11-18 23:06:35 +00001795 case Instruction::SHL_INT_2ADDR: {
1796 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
1797 break;
1798 }
1799
1800 case Instruction::SHL_LONG_2ADDR: {
1801 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
1802 break;
1803 }
1804
1805 case Instruction::SHR_INT_2ADDR: {
1806 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
1807 break;
1808 }
1809
1810 case Instruction::SHR_LONG_2ADDR: {
1811 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
1812 break;
1813 }
1814
1815 case Instruction::USHR_INT_2ADDR: {
1816 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
1817 break;
1818 }
1819
1820 case Instruction::USHR_LONG_2ADDR: {
1821 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
1822 break;
1823 }
1824
Calin Juravle7c4954d2014-10-28 16:57:40 +00001825 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001826 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001827 break;
1828 }
1829
1830 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001831 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001832 break;
1833 }
1834
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001835 case Instruction::AND_INT_2ADDR: {
1836 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
1837 break;
1838 }
1839
1840 case Instruction::AND_LONG_2ADDR: {
1841 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
1842 break;
1843 }
1844
1845 case Instruction::OR_INT_2ADDR: {
1846 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
1847 break;
1848 }
1849
1850 case Instruction::OR_LONG_2ADDR: {
1851 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
1852 break;
1853 }
1854
1855 case Instruction::XOR_INT_2ADDR: {
1856 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
1857 break;
1858 }
1859
1860 case Instruction::XOR_LONG_2ADDR: {
1861 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
1862 break;
1863 }
1864
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001865 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001866 Binop_22s<HAdd>(instruction, false);
1867 break;
1868 }
1869
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001870 case Instruction::AND_INT_LIT16: {
1871 Binop_22s<HAnd>(instruction, false);
1872 break;
1873 }
1874
1875 case Instruction::OR_INT_LIT16: {
1876 Binop_22s<HOr>(instruction, false);
1877 break;
1878 }
1879
1880 case Instruction::XOR_INT_LIT16: {
1881 Binop_22s<HXor>(instruction, false);
1882 break;
1883 }
1884
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001885 case Instruction::RSUB_INT: {
1886 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001887 break;
1888 }
1889
Calin Juravle34bacdf2014-10-07 20:23:36 +01001890 case Instruction::MUL_INT_LIT16: {
1891 Binop_22s<HMul>(instruction, false);
1892 break;
1893 }
1894
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001895 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001896 Binop_22b<HAdd>(instruction, false);
1897 break;
1898 }
1899
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001900 case Instruction::AND_INT_LIT8: {
1901 Binop_22b<HAnd>(instruction, false);
1902 break;
1903 }
1904
1905 case Instruction::OR_INT_LIT8: {
1906 Binop_22b<HOr>(instruction, false);
1907 break;
1908 }
1909
1910 case Instruction::XOR_INT_LIT8: {
1911 Binop_22b<HXor>(instruction, false);
1912 break;
1913 }
1914
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001915 case Instruction::RSUB_INT_LIT8: {
1916 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001917 break;
1918 }
1919
Calin Juravle34bacdf2014-10-07 20:23:36 +01001920 case Instruction::MUL_INT_LIT8: {
1921 Binop_22b<HMul>(instruction, false);
1922 break;
1923 }
1924
Calin Juravled0d48522014-11-04 16:40:20 +00001925 case Instruction::DIV_INT_LIT16:
1926 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001927 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1928 dex_pc, Primitive::kPrimInt, true, true);
1929 break;
1930 }
1931
1932 case Instruction::REM_INT_LIT16:
1933 case Instruction::REM_INT_LIT8: {
1934 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1935 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00001936 break;
1937 }
1938
Calin Juravle9aec02f2014-11-18 23:06:35 +00001939 case Instruction::SHL_INT_LIT8: {
1940 Binop_22b<HShl>(instruction, false);
1941 break;
1942 }
1943
1944 case Instruction::SHR_INT_LIT8: {
1945 Binop_22b<HShr>(instruction, false);
1946 break;
1947 }
1948
1949 case Instruction::USHR_INT_LIT8: {
1950 Binop_22b<HUShr>(instruction, false);
1951 break;
1952 }
1953
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001954 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001955 uint16_t type_index = instruction.VRegB_21c();
Jeff Hao848f70a2014-01-15 13:49:50 -08001956 if (compiler_driver_->IsStringTypeIndex(type_index, dex_file_)) {
1957 // Turn new-instance of string into a const 0.
1958 int32_t register_index = instruction.VRegA();
1959 HNullConstant* constant = graph_->GetNullConstant();
1960 UpdateLocal(register_index, constant);
1961 } else {
1962 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1963 ? kQuickAllocObjectWithAccessCheck
1964 : kQuickAllocObject;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001965
Jeff Hao848f70a2014-01-15 13:49:50 -08001966 current_block_->AddInstruction(new (arena_) HNewInstance(dex_pc, type_index, entrypoint));
1967 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1968 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001969 break;
1970 }
1971
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001972 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001973 uint16_t type_index = instruction.VRegC_22c();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001974 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001975 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1976 ? kQuickAllocArrayWithAccessCheck
1977 : kQuickAllocArray;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001978 current_block_->AddInstruction(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001979 new (arena_) HNewArray(length, dex_pc, type_index, entrypoint));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001980 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1981 break;
1982 }
1983
1984 case Instruction::FILLED_NEW_ARRAY: {
1985 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1986 uint32_t type_index = instruction.VRegB_35c();
1987 uint32_t args[5];
1988 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001989 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001990 break;
1991 }
1992
1993 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1994 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1995 uint32_t type_index = instruction.VRegB_3rc();
1996 uint32_t register_index = instruction.VRegC_3rc();
1997 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00001998 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001999 break;
2000 }
2001
2002 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002003 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002004 break;
2005 }
2006
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002007 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002008 case Instruction::MOVE_RESULT_WIDE:
2009 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002010 UpdateLocal(instruction.VRegA(), latest_result_);
2011 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002012 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002013
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002014 case Instruction::CMP_LONG: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002015 Binop_23x_cmp(instruction, Primitive::kPrimLong, HCompare::kNoBias);
2016 break;
2017 }
2018
2019 case Instruction::CMPG_FLOAT: {
2020 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kGtBias);
2021 break;
2022 }
2023
2024 case Instruction::CMPG_DOUBLE: {
2025 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kGtBias);
2026 break;
2027 }
2028
2029 case Instruction::CMPL_FLOAT: {
2030 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kLtBias);
2031 break;
2032 }
2033
2034 case Instruction::CMPL_DOUBLE: {
2035 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kLtBias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002036 break;
2037 }
2038
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002039 case Instruction::NOP:
2040 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002041
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002042 case Instruction::IGET:
2043 case Instruction::IGET_WIDE:
2044 case Instruction::IGET_OBJECT:
2045 case Instruction::IGET_BOOLEAN:
2046 case Instruction::IGET_BYTE:
2047 case Instruction::IGET_CHAR:
2048 case Instruction::IGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002049 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002050 return false;
2051 }
2052 break;
2053 }
2054
2055 case Instruction::IPUT:
2056 case Instruction::IPUT_WIDE:
2057 case Instruction::IPUT_OBJECT:
2058 case Instruction::IPUT_BOOLEAN:
2059 case Instruction::IPUT_BYTE:
2060 case Instruction::IPUT_CHAR:
2061 case Instruction::IPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002062 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002063 return false;
2064 }
2065 break;
2066 }
2067
2068 case Instruction::SGET:
2069 case Instruction::SGET_WIDE:
2070 case Instruction::SGET_OBJECT:
2071 case Instruction::SGET_BOOLEAN:
2072 case Instruction::SGET_BYTE:
2073 case Instruction::SGET_CHAR:
2074 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002075 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002076 return false;
2077 }
2078 break;
2079 }
2080
2081 case Instruction::SPUT:
2082 case Instruction::SPUT_WIDE:
2083 case Instruction::SPUT_OBJECT:
2084 case Instruction::SPUT_BOOLEAN:
2085 case Instruction::SPUT_BYTE:
2086 case Instruction::SPUT_CHAR:
2087 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002088 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002089 return false;
2090 }
2091 break;
2092 }
2093
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002094#define ARRAY_XX(kind, anticipated_type) \
2095 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002096 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002097 break; \
2098 } \
2099 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002100 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002101 break; \
2102 }
2103
2104 ARRAY_XX(, Primitive::kPrimInt);
2105 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2106 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2107 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2108 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2109 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2110 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2111
Nicolas Geoffray39468442014-09-02 15:17:15 +01002112 case Instruction::ARRAY_LENGTH: {
2113 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002114 // No need for a temporary for the null check, it is the only input of the following
2115 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002116 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002117 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002118 current_block_->AddInstruction(new (arena_) HArrayLength(object));
2119 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2120 break;
2121 }
2122
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002123 case Instruction::CONST_STRING: {
Calin Juravle225ff812014-11-13 16:46:39 +00002124 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002125 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2126 break;
2127 }
2128
2129 case Instruction::CONST_STRING_JUMBO: {
Calin Juravle225ff812014-11-13 16:46:39 +00002130 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002131 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2132 break;
2133 }
2134
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002135 case Instruction::CONST_CLASS: {
2136 uint16_t type_index = instruction.VRegB_21c();
2137 bool type_known_final;
2138 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002139 bool dont_use_is_referrers_class;
2140 // `CanAccessTypeWithoutChecks` will tell whether the method being
2141 // built is trying to access its own class, so that the generated
2142 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002143 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002144 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2145 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002146 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002147 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00002148 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002149 return false;
2150 }
2151 current_block_->AddInstruction(
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002152 new (arena_) HLoadClass(type_index, IsOutermostCompilingClass(type_index), dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002153 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2154 break;
2155 }
2156
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002157 case Instruction::MOVE_EXCEPTION: {
2158 current_block_->AddInstruction(new (arena_) HLoadException());
2159 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2160 break;
2161 }
2162
2163 case Instruction::THROW: {
2164 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00002165 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002166 // A throw instruction must branch to the exit block.
2167 current_block_->AddSuccessor(exit_block_);
2168 // We finished building this block. Set the current block to null to avoid
2169 // adding dead instructions to it.
2170 current_block_ = nullptr;
2171 break;
2172 }
2173
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002174 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002175 uint8_t destination = instruction.VRegA_22c();
2176 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002177 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00002178 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002179 return false;
2180 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002181 break;
2182 }
2183
2184 case Instruction::CHECK_CAST: {
2185 uint8_t reference = instruction.VRegA_21c();
2186 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00002187 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002188 return false;
2189 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002190 break;
2191 }
2192
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002193 case Instruction::MONITOR_ENTER: {
2194 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2195 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2196 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002197 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002198 break;
2199 }
2200
2201 case Instruction::MONITOR_EXIT: {
2202 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2203 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2204 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002205 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002206 break;
2207 }
2208
Andreas Gamped881df52014-11-24 23:28:39 -08002209 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002210 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002211 break;
2212 }
2213
Andreas Gampee4d4d322014-12-04 09:09:57 -08002214 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002215 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002216 break;
2217 }
2218
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002219 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002220 VLOG(compiler) << "Did not compile "
2221 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2222 << " because of unhandled instruction "
2223 << instruction.Name();
2224 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002225 return false;
2226 }
2227 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002228} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002229
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002230HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
2231 return locals_.Get(register_index);
2232}
2233
2234void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
2235 HLocal* local = GetLocalAt(register_index);
2236 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
2237}
2238
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002239HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002240 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002241 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002242 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002243}
2244
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002245} // namespace art