blob: eb6181c7110adea06671ef2e489066bbc94e70e5 [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
19#include "class_linker.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000020#include "dex_file.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000021#include "dex_file-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000022#include "dex_instruction.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000023#include "dex_instruction-inl.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010024#include "driver/compiler_driver-inl.h"
25#include "mirror/art_field.h"
26#include "mirror/art_field-inl.h"
27#include "mirror/class_loader.h"
28#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000029#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000030#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010031#include "scoped_thread_state_change.h"
32#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033
34namespace art {
35
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036/**
37 * Helper class to add HTemporary instructions. This class is used when
38 * converting a DEX instruction to multiple HInstruction, and where those
39 * instructions do not die at the following instruction, but instead spans
40 * multiple instructions.
41 */
42class Temporaries : public ValueObject {
43 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000044 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010045
46 void Add(HInstruction* instruction) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +000047 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000049
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000051
52 size_t offset;
53 if (instruction->GetType() == Primitive::kPrimLong
54 || instruction->GetType() == Primitive::kPrimDouble) {
55 offset = 2;
56 } else {
57 offset = 1;
58 }
59 index_ += offset;
60
61 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010062 }
63
64 private:
65 HGraph* const graph_;
66
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010067 // Current index in the temporary stack, updated by `Add`.
68 size_t index_;
69};
70
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010071void HGraphBuilder::InitializeLocals(uint16_t count) {
72 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000073 locals_.SetSize(count);
74 for (int i = 0; i < count; i++) {
75 HLocal* local = new (arena_) HLocal(i);
76 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000077 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000078 }
79}
80
Nicolas Geoffray52e832b2014-11-06 15:15:31 +000081void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010082 // dex_compilation_unit_ is null only when unit testing.
83 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +000084 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010085 }
86
87 graph_->SetNumberOfInVRegs(number_of_parameters);
88 const char* shorty = dex_compilation_unit_->GetShorty();
89 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010090 int parameter_index = 0;
91
92 if (!dex_compilation_unit_->IsStatic()) {
93 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010094 HParameterValue* parameter =
95 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010096 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010097 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010098 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010099 number_of_parameters--;
100 }
101
102 uint32_t pos = 1;
103 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100104 HParameterValue* parameter =
105 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
106 entry_block_->AddInstruction(parameter);
107 HLocal* local = GetLocalAt(locals_index++);
108 // Store the parameter value in the local that the dex code will use
109 // to reference that parameter.
110 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
111 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
112 || (parameter->GetType() == Primitive::kPrimDouble);
113 if (is_wide) {
114 i++;
115 locals_index++;
116 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100117 }
118 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100119}
120
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100121template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000122void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000123 int32_t target_offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +0000124 PotentiallyAddSuspendCheck(target_offset, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100125 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
126 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700127 T* comparison = new (arena_) T(first, second);
128 current_block_->AddInstruction(comparison);
129 HInstruction* ifinst = new (arena_) HIf(comparison);
130 current_block_->AddInstruction(ifinst);
Calin Juravle225ff812014-11-13 16:46:39 +0000131 HBasicBlock* target = FindBlockStartingAt(dex_pc + target_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700132 DCHECK(target != nullptr);
133 current_block_->AddSuccessor(target);
Calin Juravle225ff812014-11-13 16:46:39 +0000134 target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
Dave Allison20dfc792014-06-16 20:44:29 -0700135 DCHECK(target != nullptr);
136 current_block_->AddSuccessor(target);
137 current_block_ = nullptr;
138}
139
140template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000141void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 int32_t target_offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +0000143 PotentiallyAddSuspendCheck(target_offset, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700144 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
145 T* comparison = new (arena_) T(value, GetIntConstant(0));
146 current_block_->AddInstruction(comparison);
147 HInstruction* ifinst = new (arena_) HIf(comparison);
148 current_block_->AddInstruction(ifinst);
Calin Juravle225ff812014-11-13 16:46:39 +0000149 HBasicBlock* target = FindBlockStartingAt(dex_pc + target_offset);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100150 DCHECK(target != nullptr);
151 current_block_->AddSuccessor(target);
Calin Juravle225ff812014-11-13 16:46:39 +0000152 target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100153 DCHECK(target != nullptr);
154 current_block_->AddSuccessor(target);
155 current_block_ = nullptr;
156}
157
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000158static bool ShouldSkipCompilation(const CompilerDriver& compiler_driver,
159 const DexCompilationUnit& dex_compilation_unit,
160 size_t number_of_dex_instructions,
161 size_t number_of_blocks ATTRIBUTE_UNUSED,
162 size_t number_of_branches) {
163 const CompilerOptions& compiler_options = compiler_driver.GetCompilerOptions();
164 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
165 if (compiler_filter == CompilerOptions::kEverything) {
166 return false;
167 }
168
169 if (compiler_options.IsHugeMethod(number_of_dex_instructions)) {
170 LOG(INFO) << "Skip compilation of huge method "
171 << PrettyMethod(dex_compilation_unit.GetDexMethodIndex(),
172 *dex_compilation_unit.GetDexFile())
173 << ": " << number_of_dex_instructions << " dex instructions";
174 return true;
175 }
176
177 // If it's large and contains no branches, it's likely to be machine generated initialization.
178 if (compiler_options.IsLargeMethod(number_of_dex_instructions) && (number_of_branches == 0)) {
179 LOG(INFO) << "Skip compilation of large method with no branch "
180 << PrettyMethod(dex_compilation_unit.GetDexMethodIndex(),
181 *dex_compilation_unit.GetDexFile())
182 << ": " << number_of_dex_instructions << " dex instructions";
183 return true;
184 }
185
186 return false;
187}
188
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000189HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000190 const uint16_t* code_ptr = code_item.insns_;
191 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100192 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000193
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000194 // Setup the graph with the entry block and exit block.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000195 graph_ = new (arena_) HGraph(arena_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100196 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000197 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100198 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000199 graph_->SetEntryBlock(entry_block_);
200 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000201
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000202 InitializeLocals(code_item.registers_size_);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100203 graph_->UpdateMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000204
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000205 // Compute the number of dex instructions, blocks, and branches. We will
206 // check these values against limits given to the compiler.
207 size_t number_of_dex_instructions = 0;
208 size_t number_of_blocks = 0;
209 size_t number_of_branches = 0;
210
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000211 // To avoid splitting blocks, we compute ahead of time the instructions that
212 // start a new block, and create these blocks.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000213 ComputeBranchTargets(
214 code_ptr, code_end, &number_of_dex_instructions, &number_of_blocks, &number_of_branches);
215
216 // Note that the compiler driver is null when unit testing.
217 if (compiler_driver_ != nullptr) {
218 if (ShouldSkipCompilation(*compiler_driver_,
219 *dex_compilation_unit_,
220 number_of_dex_instructions,
221 number_of_blocks,
222 number_of_branches)) {
223 return nullptr;
224 }
225 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000226
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000227 // Also create blocks for catch handlers.
228 if (code_item.tries_size_ != 0) {
229 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
230 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
231 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
232 CatchHandlerIterator iterator(handlers_ptr);
233 for (; iterator.HasNext(); iterator.Next()) {
234 uint32_t address = iterator.GetHandlerAddress();
235 HBasicBlock* block = FindBlockStartingAt(address);
236 if (block == nullptr) {
237 block = new (arena_) HBasicBlock(graph_, address);
238 branch_targets_.Put(address, block);
239 }
240 block->SetIsCatchBlock();
241 }
242 handlers_ptr = iterator.EndDataPointer();
243 }
244 }
245
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000246 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100247
Calin Juravle225ff812014-11-13 16:46:39 +0000248 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000249 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000250 // Update the current block if dex_pc starts a new block.
251 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000252 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle225ff812014-11-13 16:46:39 +0000253 if (!AnalyzeDexInstruction(instruction, dex_pc)) return nullptr;
254 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000255 code_ptr += instruction.SizeInCodeUnits();
256 }
257
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000258 // Add the exit block at the end to give it the highest id.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000259 graph_->AddBlock(exit_block_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000260 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000261 // Add the suspend check to the entry block.
262 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000263 entry_block_->AddInstruction(new (arena_) HGoto());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000264 return graph_;
265}
266
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000267void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) {
268 HBasicBlock* block = FindBlockStartingAt(index);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000269 if (block == nullptr) {
270 return;
271 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000272
273 if (current_block_ != nullptr) {
274 // Branching instructions clear current_block, so we know
275 // the last instruction of the current block is not a branching
276 // instruction. We add an unconditional goto to the found block.
277 current_block_->AddInstruction(new (arena_) HGoto());
278 current_block_->AddSuccessor(block);
279 }
280 graph_->AddBlock(block);
281 current_block_ = block;
282}
283
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000284void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
285 const uint16_t* code_end,
286 size_t* number_of_dex_instructions,
287 size_t* number_of_blocks,
288 size_t* number_of_branches) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000289 // TODO: Support switch instructions.
290 branch_targets_.SetSize(code_end - code_ptr);
291
292 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100293 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000294 branch_targets_.Put(0, block);
295 entry_block_->AddSuccessor(block);
296
297 // Iterate over all instructions and find branching instructions. Create blocks for
298 // the locations these instructions branch to.
Calin Juravle225ff812014-11-13 16:46:39 +0000299 size_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000300 while (code_ptr < code_end) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000301 (*number_of_dex_instructions)++;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000302 const Instruction& instruction = *Instruction::At(code_ptr);
303 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000304 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000305 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000306 // Create a block for the target instruction.
307 if (FindBlockStartingAt(target) == nullptr) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100308 block = new (arena_) HBasicBlock(graph_, target);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000309 branch_targets_.Put(target, block);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000310 (*number_of_blocks)++;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000311 }
Calin Juravle225ff812014-11-13 16:46:39 +0000312 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000313 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000314 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_pc) == nullptr)) {
315 block = new (arena_) HBasicBlock(graph_, dex_pc);
316 branch_targets_.Put(dex_pc, block);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000317 (*number_of_blocks)++;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000318 }
319 } else {
320 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000321 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000322 }
323 }
324}
325
326HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const {
327 DCHECK_GE(index, 0);
328 return branch_targets_.Get(index);
329}
330
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100331template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100332void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
333 HInstruction* first = LoadLocal(instruction.VRegB(), type);
334 current_block_->AddInstruction(new (arena_) T(type, first));
335 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
336}
337
Roland Levillaindff1f282014-11-05 14:15:05 +0000338void HGraphBuilder::Conversion_12x(const Instruction& instruction,
339 Primitive::Type input_type,
340 Primitive::Type result_type) {
341 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
342 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first));
343 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
344}
345
Roland Levillain88cb1752014-10-20 16:36:47 +0100346template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100347void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100348 HInstruction* first = LoadLocal(instruction.VRegB(), type);
349 HInstruction* second = LoadLocal(instruction.VRegC(), type);
350 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100351 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
352}
353
354template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000355void HGraphBuilder::Binop_23x(const Instruction& instruction,
356 Primitive::Type type,
357 uint32_t dex_pc) {
358 HInstruction* first = LoadLocal(instruction.VRegB(), type);
359 HInstruction* second = LoadLocal(instruction.VRegC(), type);
360 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
361 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
362}
363
364template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000365void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
366 Primitive::Type type) {
367 HInstruction* first = LoadLocal(instruction.VRegB(), type);
368 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
369 current_block_->AddInstruction(new (arena_) T(type, first, second));
370 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
371}
372
Calin Juravleddb7df22014-11-25 20:56:51 +0000373void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
374 Primitive::Type type,
375 HCompare::Bias bias) {
376 HInstruction* first = LoadLocal(instruction.VRegB(), type);
377 HInstruction* second = LoadLocal(instruction.VRegC(), type);
378 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias));
379 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
380}
381
Calin Juravle9aec02f2014-11-18 23:06:35 +0000382template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100383void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
384 HInstruction* first = LoadLocal(instruction.VRegA(), type);
385 HInstruction* second = LoadLocal(instruction.VRegB(), type);
386 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100387 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
388}
389
390template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000391void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) {
392 HInstruction* first = LoadLocal(instruction.VRegA(), type);
393 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
394 current_block_->AddInstruction(new (arena_) T(type, first, second));
395 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
396}
397
398template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000399void HGraphBuilder::Binop_12x(const Instruction& instruction,
400 Primitive::Type type,
401 uint32_t dex_pc) {
402 HInstruction* first = LoadLocal(instruction.VRegA(), type);
403 HInstruction* second = LoadLocal(instruction.VRegB(), type);
404 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
405 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
406}
407
408template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100409void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100410 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
411 HInstruction* second = GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100412 if (reverse) {
413 std::swap(first, second);
414 }
415 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
416 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
417}
418
419template<typename T>
420void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100421 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
422 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100423 if (reverse) {
424 std::swap(first, second);
425 }
426 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
427 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
428}
429
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100430void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
431 if (type == Primitive::kPrimVoid) {
432 current_block_->AddInstruction(new (arena_) HReturnVoid());
433 } else {
434 HInstruction* value = LoadLocal(instruction.VRegA(), type);
435 current_block_->AddInstruction(new (arena_) HReturn(value));
436 }
437 current_block_->AddSuccessor(exit_block_);
438 current_block_ = nullptr;
439}
440
441bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000442 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100443 uint32_t method_idx,
444 uint32_t number_of_vreg_arguments,
445 bool is_range,
446 uint32_t* args,
447 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100448 Instruction::Code opcode = instruction.Opcode();
449 InvokeType invoke_type;
450 switch (opcode) {
451 case Instruction::INVOKE_STATIC:
452 case Instruction::INVOKE_STATIC_RANGE:
453 invoke_type = kStatic;
454 break;
455 case Instruction::INVOKE_DIRECT:
456 case Instruction::INVOKE_DIRECT_RANGE:
457 invoke_type = kDirect;
458 break;
459 case Instruction::INVOKE_VIRTUAL:
460 case Instruction::INVOKE_VIRTUAL_RANGE:
461 invoke_type = kVirtual;
462 break;
463 case Instruction::INVOKE_INTERFACE:
464 case Instruction::INVOKE_INTERFACE_RANGE:
465 invoke_type = kInterface;
466 break;
467 case Instruction::INVOKE_SUPER_RANGE:
468 case Instruction::INVOKE_SUPER:
469 invoke_type = kSuper;
470 break;
471 default:
472 LOG(FATAL) << "Unexpected invoke op: " << opcode;
473 return false;
474 }
475
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100476 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
477 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
478 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
479 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100480 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100481 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
482
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000483 MethodReference target_method(dex_file_, method_idx);
484 uintptr_t direct_code;
485 uintptr_t direct_method;
486 int table_index;
487 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000488
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000489 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
490 &optimized_invoke_type, &target_method, &table_index,
491 &direct_code, &direct_method)) {
492 LOG(INFO) << "Did not compile " << PrettyMethod(method_idx, *dex_file_)
493 << " because a method call could not be resolved";
494 return false;
495 }
496 DCHECK(optimized_invoke_type != kSuper);
497
498 HInvoke* invoke = nullptr;
499 if (optimized_invoke_type == kVirtual) {
500 invoke = new (arena_) HInvokeVirtual(
501 arena_, number_of_arguments, return_type, dex_pc, table_index);
502 } else if (optimized_invoke_type == kInterface) {
503 invoke = new (arena_) HInvokeInterface(
504 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100505 } else {
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000506 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic);
507 // Sharpening to kDirect only works if we compile PIC.
508 DCHECK((optimized_invoke_type == invoke_type) || (optimized_invoke_type != kDirect)
509 || compiler_driver_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100510 // Treat invoke-direct like static calls for now.
511 invoke = new (arena_) HInvokeStatic(
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000512 arena_, number_of_arguments, return_type, dex_pc, target_method.dex_method_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100513 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100514
515 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000516 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100517 if (is_instance_call) {
518 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000519 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100520 current_block_->AddInstruction(null_check);
521 temps.Add(null_check);
522 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100523 start_index = 1;
524 }
525
526 uint32_t descriptor_index = 1;
527 uint32_t argument_index = start_index;
528 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
529 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100530 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
531 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100532 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
Calin Juravle225ff812014-11-13 16:46:39 +0000533 << " at " << dex_pc;
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100534 // We do not implement non sequential register pair.
535 return false;
536 }
537 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
538 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100539 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100540 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100541 }
542 }
543
544 DCHECK_EQ(argument_index, number_of_arguments);
545 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100546 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100547 return true;
548}
549
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100550bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000551 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100552 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100553 uint32_t source_or_dest_reg = instruction.VRegA_22c();
554 uint32_t obj_reg = instruction.VRegB_22c();
555 uint16_t field_index = instruction.VRegC_22c();
556
557 ScopedObjectAccess soa(Thread::Current());
558 StackHandleScope<1> hs(soa.Self());
559 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
560 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
561
562 if (resolved_field.Get() == nullptr) {
563 return false;
564 }
565 if (resolved_field->IsVolatile()) {
566 return false;
567 }
568
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100569 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100570
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100571 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000572 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100573 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000574 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100575 HInstruction* null_check = current_block_->GetLastInstruction();
576 // We need one temporary for the null check.
577 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100578 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100579 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
580 null_check,
581 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100582 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100583 resolved_field->GetOffset()));
584 } else {
585 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
586 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100587 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100588 resolved_field->GetOffset()));
589
590 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
591 }
592 return true;
593}
594
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100595
596
597bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000598 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100599 bool is_put) {
600 uint32_t source_or_dest_reg = instruction.VRegA_21c();
601 uint16_t field_index = instruction.VRegB_21c();
602
603 uint32_t storage_index;
604 bool is_referrers_class;
605 bool is_initialized;
606 bool is_volatile;
607 MemberOffset field_offset(0u);
608 Primitive::Type field_type;
609
610 bool fast_path = compiler_driver_->ComputeStaticFieldInfo(field_index,
611 dex_compilation_unit_,
612 is_put,
613 &field_offset,
614 &storage_index,
615 &is_referrers_class,
616 &is_volatile,
617 &is_initialized,
618 &field_type);
619 if (!fast_path) {
620 return false;
621 }
622
623 if (is_volatile) {
624 return false;
625 }
626
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100627 HLoadClass* constant = new (arena_) HLoadClass(
Calin Juravle225ff812014-11-13 16:46:39 +0000628 storage_index, is_referrers_class, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100629 current_block_->AddInstruction(constant);
630
631 HInstruction* cls = constant;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000632 if (!is_initialized) {
Calin Juravle225ff812014-11-13 16:46:39 +0000633 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100634 current_block_->AddInstruction(cls);
635 }
636
637 if (is_put) {
638 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000639 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100640 temps.Add(cls);
641 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
642 DCHECK_EQ(value->GetType(), field_type);
643 current_block_->AddInstruction(
644 new (arena_) HStaticFieldSet(cls, value, field_type, field_offset));
645 } else {
646 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls, field_type, field_offset));
647 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
648 }
649 return true;
650}
651
Calin Juravlebacfec32014-11-14 15:54:36 +0000652void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
653 uint16_t first_vreg,
654 int64_t second_vreg_or_constant,
655 uint32_t dex_pc,
656 Primitive::Type type,
657 bool second_is_constant,
658 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000659 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +0000660
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000661 HInstruction* first = LoadLocal(first_vreg, type);
662 HInstruction* second = nullptr;
663 if (second_is_constant) {
664 if (type == Primitive::kPrimInt) {
665 second = GetIntConstant(second_vreg_or_constant);
666 } else {
667 second = GetLongConstant(second_vreg_or_constant);
668 }
669 } else {
670 second = LoadLocal(second_vreg_or_constant, type);
671 }
672
673 if (!second_is_constant
674 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
675 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
676 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000677 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000678 current_block_->AddInstruction(second);
679 temps.Add(current_block_->GetLastInstruction());
680 }
681
Calin Juravlebacfec32014-11-14 15:54:36 +0000682 if (isDiv) {
683 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
684 } else {
685 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
686 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000687 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +0000688}
689
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100690void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000691 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100692 bool is_put,
693 Primitive::Type anticipated_type) {
694 uint8_t source_or_dest_reg = instruction.VRegA_23x();
695 uint8_t array_reg = instruction.VRegB_23x();
696 uint8_t index_reg = instruction.VRegC_23x();
697
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100698 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000699 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100700
701 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000702 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100703 current_block_->AddInstruction(object);
704 temps.Add(object);
705
706 HInstruction* length = new (arena_) HArrayLength(object);
707 current_block_->AddInstruction(length);
708 temps.Add(length);
709 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +0000710 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100711 current_block_->AddInstruction(index);
712 temps.Add(index);
713 if (is_put) {
714 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
715 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100716 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000717 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100718 } else {
719 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
720 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
721 }
722}
723
Calin Juravle225ff812014-11-13 16:46:39 +0000724void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100725 uint32_t type_index,
726 uint32_t number_of_vreg_arguments,
727 bool is_range,
728 uint32_t* args,
729 uint32_t register_index) {
730 HInstruction* length = GetIntConstant(number_of_vreg_arguments);
Calin Juravle225ff812014-11-13 16:46:39 +0000731 HInstruction* object = new (arena_) HNewArray(length, dex_pc, type_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100732 current_block_->AddInstruction(object);
733
734 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
735 DCHECK_EQ(descriptor[0], '[') << descriptor;
736 char primitive = descriptor[1];
737 DCHECK(primitive == 'I'
738 || primitive == 'L'
739 || primitive == '[') << descriptor;
740 bool is_reference_array = (primitive == 'L') || (primitive == '[');
741 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
742
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000743 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100744 temps.Add(object);
745 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
746 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
747 HInstruction* index = GetIntConstant(i);
748 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000749 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100750 }
751 latest_result_ = object;
752}
753
754template <typename T>
755void HGraphBuilder::BuildFillArrayData(HInstruction* object,
756 const T* data,
757 uint32_t element_count,
758 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +0000759 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100760 for (uint32_t i = 0; i < element_count; ++i) {
761 HInstruction* index = GetIntConstant(i);
762 HInstruction* value = GetIntConstant(data[i]);
763 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000764 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100765 }
766}
767
Calin Juravle225ff812014-11-13 16:46:39 +0000768void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000769 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000770 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000771 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000772 current_block_->AddInstruction(null_check);
773 temps.Add(null_check);
774
775 HInstruction* length = new (arena_) HArrayLength(null_check);
776 current_block_->AddInstruction(length);
777
Calin Juravle225ff812014-11-13 16:46:39 +0000778 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +0000779 const Instruction::ArrayDataPayload* payload =
780 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
781 const uint8_t* data = payload->data;
782 uint32_t element_count = payload->element_count;
783
784 // Implementation of this DEX instruction seems to be that the bounds check is
785 // done before doing any stores.
786 HInstruction* last_index = GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +0000787 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +0000788
789 switch (payload->element_width) {
790 case 1:
791 BuildFillArrayData(null_check,
792 reinterpret_cast<const int8_t*>(data),
793 element_count,
794 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +0000795 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000796 break;
797 case 2:
798 BuildFillArrayData(null_check,
799 reinterpret_cast<const int16_t*>(data),
800 element_count,
801 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +0000802 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000803 break;
804 case 4:
805 BuildFillArrayData(null_check,
806 reinterpret_cast<const int32_t*>(data),
807 element_count,
808 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +0000809 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000810 break;
811 case 8:
812 BuildFillWideArrayData(null_check,
813 reinterpret_cast<const int64_t*>(data),
814 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000815 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000816 break;
817 default:
818 LOG(FATAL) << "Unknown element width for " << payload->element_width;
819 }
820}
821
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100822void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100823 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100824 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000825 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100826 for (uint32_t i = 0; i < element_count; ++i) {
827 HInstruction* index = GetIntConstant(i);
828 HInstruction* value = GetLongConstant(data[i]);
829 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000830 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100831 }
832}
833
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000834bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
835 uint8_t destination,
836 uint8_t reference,
837 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +0000838 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000839 bool type_known_final;
840 bool type_known_abstract;
841 bool is_referrers_class;
842 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
843 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
844 &type_known_final, &type_known_abstract, &is_referrers_class);
845 if (!can_access) {
846 return false;
847 }
848 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000849 HLoadClass* cls = new (arena_) HLoadClass(type_index, is_referrers_class, dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000850 current_block_->AddInstruction(cls);
851 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000852 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000853 temps.Add(cls);
854 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
855 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000856 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000857 UpdateLocal(destination, current_block_->GetLastInstruction());
858 } else {
859 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
860 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000861 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000862 }
863 return true;
864}
865
Calin Juravle225ff812014-11-13 16:46:39 +0000866void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000867 if (target_offset <= 0) {
868 // Unconditionnally add a suspend check to backward branches. We can remove
869 // them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +0000870 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000871 }
872}
873
Calin Juravle225ff812014-11-13 16:46:39 +0000874bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000875 if (current_block_ == nullptr) {
876 return true; // Dead code
877 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000878
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000879 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000880 case Instruction::CONST_4: {
881 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100882 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000883 UpdateLocal(register_index, constant);
884 break;
885 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000886
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100887 case Instruction::CONST_16: {
888 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100889 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
890 UpdateLocal(register_index, constant);
891 break;
892 }
893
Dave Allison20dfc792014-06-16 20:44:29 -0700894 case Instruction::CONST: {
895 int32_t register_index = instruction.VRegA();
896 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
897 UpdateLocal(register_index, constant);
898 break;
899 }
900
901 case Instruction::CONST_HIGH16: {
902 int32_t register_index = instruction.VRegA();
903 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
904 UpdateLocal(register_index, constant);
905 break;
906 }
907
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100908 case Instruction::CONST_WIDE_16: {
909 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700910 // Get 16 bits of constant value, sign extended to 64 bits.
911 int64_t value = instruction.VRegB_21s();
912 value <<= 48;
913 value >>= 48;
914 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100915 UpdateLocal(register_index, constant);
916 break;
917 }
918
919 case Instruction::CONST_WIDE_32: {
920 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700921 // Get 32 bits of constant value, sign extended to 64 bits.
922 int64_t value = instruction.VRegB_31i();
923 value <<= 32;
924 value >>= 32;
925 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100926 UpdateLocal(register_index, constant);
927 break;
928 }
929
930 case Instruction::CONST_WIDE: {
931 int32_t register_index = instruction.VRegA();
932 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100933 UpdateLocal(register_index, constant);
934 break;
935 }
936
Dave Allison20dfc792014-06-16 20:44:29 -0700937 case Instruction::CONST_WIDE_HIGH16: {
938 int32_t register_index = instruction.VRegA();
939 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
940 HLongConstant* constant = GetLongConstant(value);
941 UpdateLocal(register_index, constant);
942 break;
943 }
944
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000945 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -0700946 case Instruction::MOVE:
947 case Instruction::MOVE_FROM16:
948 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100949 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100950 UpdateLocal(instruction.VRegA(), value);
951 break;
952 }
953
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000954 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -0700955 case Instruction::MOVE_WIDE:
956 case Instruction::MOVE_WIDE_FROM16:
957 case Instruction::MOVE_WIDE_16: {
958 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
959 UpdateLocal(instruction.VRegA(), value);
960 break;
961 }
962
963 case Instruction::MOVE_OBJECT:
964 case Instruction::MOVE_OBJECT_16:
965 case Instruction::MOVE_OBJECT_FROM16: {
966 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
967 UpdateLocal(instruction.VRegA(), value);
968 break;
969 }
970
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000971 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100972 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000973 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000974 }
975
Dave Allison20dfc792014-06-16 20:44:29 -0700976#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +0000977 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
978 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100979
Dave Allison20dfc792014-06-16 20:44:29 -0700980 IF_XX(HEqual, EQ);
981 IF_XX(HNotEqual, NE);
982 IF_XX(HLessThan, LT);
983 IF_XX(HLessThanOrEqual, LE);
984 IF_XX(HGreaterThan, GT);
985 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000986
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000987 case Instruction::GOTO:
988 case Instruction::GOTO_16:
989 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000990 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +0000991 PotentiallyAddSuspendCheck(offset, dex_pc);
992 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000993 DCHECK(target != nullptr);
994 current_block_->AddInstruction(new (arena_) HGoto());
995 current_block_->AddSuccessor(target);
996 current_block_ = nullptr;
997 break;
998 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000999
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001000 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001001 DCHECK_NE(return_type_, Primitive::kPrimNot);
1002 DCHECK_NE(return_type_, Primitive::kPrimLong);
1003 DCHECK_NE(return_type_, Primitive::kPrimDouble);
1004 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001005 break;
1006 }
1007
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001008 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001009 DCHECK(return_type_ == Primitive::kPrimNot);
1010 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001011 break;
1012 }
1013
1014 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001015 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
1016 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001017 break;
1018 }
1019
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001020 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001021 case Instruction::INVOKE_INTERFACE:
1022 case Instruction::INVOKE_STATIC:
1023 case Instruction::INVOKE_SUPER:
1024 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001025 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001026 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001027 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001028 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001029 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001030 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001031 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001032 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001033 break;
1034 }
1035
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001036 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001037 case Instruction::INVOKE_INTERFACE_RANGE:
1038 case Instruction::INVOKE_STATIC_RANGE:
1039 case Instruction::INVOKE_SUPER_RANGE:
1040 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001041 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001042 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1043 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001044 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001045 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001046 return false;
1047 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001048 break;
1049 }
1050
Roland Levillain88cb1752014-10-20 16:36:47 +01001051 case Instruction::NEG_INT: {
1052 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1053 break;
1054 }
1055
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001056 case Instruction::NEG_LONG: {
1057 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1058 break;
1059 }
1060
Roland Levillain3dbcb382014-10-28 17:30:07 +00001061 case Instruction::NEG_FLOAT: {
1062 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1063 break;
1064 }
1065
1066 case Instruction::NEG_DOUBLE: {
1067 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1068 break;
1069 }
1070
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001071 case Instruction::NOT_INT: {
1072 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1073 break;
1074 }
1075
Roland Levillain70566432014-10-24 16:20:17 +01001076 case Instruction::NOT_LONG: {
1077 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1078 break;
1079 }
1080
Roland Levillaindff1f282014-11-05 14:15:05 +00001081 case Instruction::INT_TO_LONG: {
1082 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong);
1083 break;
1084 }
1085
Roland Levillaincff13742014-11-17 14:32:17 +00001086 case Instruction::INT_TO_FLOAT: {
1087 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat);
1088 break;
1089 }
1090
1091 case Instruction::INT_TO_DOUBLE: {
1092 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble);
1093 break;
1094 }
1095
Roland Levillain946e1432014-11-11 17:35:19 +00001096 case Instruction::LONG_TO_INT: {
1097 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt);
1098 break;
1099 }
1100
Roland Levillain6d0e4832014-11-27 18:31:21 +00001101 case Instruction::LONG_TO_FLOAT: {
1102 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat);
1103 break;
1104 }
1105
Roland Levillain647b9ed2014-11-27 12:06:00 +00001106 case Instruction::LONG_TO_DOUBLE: {
1107 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble);
1108 break;
1109 }
1110
Roland Levillain3f8f9362014-12-02 17:45:01 +00001111 case Instruction::FLOAT_TO_INT: {
1112 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt);
1113 break;
1114 }
1115
Roland Levillain51d3fc42014-11-13 14:11:42 +00001116 case Instruction::INT_TO_BYTE: {
1117 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte);
1118 break;
1119 }
1120
Roland Levillain01a8d712014-11-14 16:27:39 +00001121 case Instruction::INT_TO_SHORT: {
1122 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort);
1123 break;
1124 }
1125
Roland Levillain981e4542014-11-14 11:47:14 +00001126 case Instruction::INT_TO_CHAR: {
1127 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar);
1128 break;
1129 }
1130
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001131 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001132 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001133 break;
1134 }
1135
1136 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001137 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001138 break;
1139 }
1140
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001141 case Instruction::ADD_DOUBLE: {
1142 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1143 break;
1144 }
1145
1146 case Instruction::ADD_FLOAT: {
1147 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1148 break;
1149 }
1150
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001151 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001152 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001153 break;
1154 }
1155
1156 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001157 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001158 break;
1159 }
1160
Calin Juravle096cc022014-10-23 17:01:13 +01001161 case Instruction::SUB_FLOAT: {
1162 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1163 break;
1164 }
1165
1166 case Instruction::SUB_DOUBLE: {
1167 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1168 break;
1169 }
1170
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001171 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1173 break;
1174 }
1175
Calin Juravle34bacdf2014-10-07 20:23:36 +01001176 case Instruction::MUL_INT: {
1177 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1178 break;
1179 }
1180
1181 case Instruction::MUL_LONG: {
1182 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1183 break;
1184 }
1185
Calin Juravleb5bfa962014-10-21 18:02:24 +01001186 case Instruction::MUL_FLOAT: {
1187 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1188 break;
1189 }
1190
1191 case Instruction::MUL_DOUBLE: {
1192 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1193 break;
1194 }
1195
Calin Juravled0d48522014-11-04 16:40:20 +00001196 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001197 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1198 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001199 break;
1200 }
1201
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001202 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001203 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1204 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001205 break;
1206 }
1207
Calin Juravle7c4954d2014-10-28 16:57:40 +00001208 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001209 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001210 break;
1211 }
1212
1213 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001214 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001215 break;
1216 }
1217
Calin Juravlebacfec32014-11-14 15:54:36 +00001218 case Instruction::REM_INT: {
1219 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1220 dex_pc, Primitive::kPrimInt, false, false);
1221 break;
1222 }
1223
1224 case Instruction::REM_LONG: {
1225 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1226 dex_pc, Primitive::kPrimLong, false, false);
1227 break;
1228 }
1229
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001230 case Instruction::AND_INT: {
1231 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1232 break;
1233 }
1234
1235 case Instruction::AND_LONG: {
1236 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1237 break;
1238 }
1239
Calin Juravle9aec02f2014-11-18 23:06:35 +00001240 case Instruction::SHL_INT: {
1241 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
1242 break;
1243 }
1244
1245 case Instruction::SHL_LONG: {
1246 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
1247 break;
1248 }
1249
1250 case Instruction::SHR_INT: {
1251 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
1252 break;
1253 }
1254
1255 case Instruction::SHR_LONG: {
1256 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
1257 break;
1258 }
1259
1260 case Instruction::USHR_INT: {
1261 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
1262 break;
1263 }
1264
1265 case Instruction::USHR_LONG: {
1266 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
1267 break;
1268 }
1269
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001270 case Instruction::OR_INT: {
1271 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1272 break;
1273 }
1274
1275 case Instruction::OR_LONG: {
1276 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1277 break;
1278 }
1279
1280 case Instruction::XOR_INT: {
1281 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1282 break;
1283 }
1284
1285 case Instruction::XOR_LONG: {
1286 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1287 break;
1288 }
1289
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001290 case Instruction::ADD_LONG_2ADDR: {
1291 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001292 break;
1293 }
1294
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001295 case Instruction::ADD_DOUBLE_2ADDR: {
1296 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1297 break;
1298 }
1299
1300 case Instruction::ADD_FLOAT_2ADDR: {
1301 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
1302 break;
1303 }
1304
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001305 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001306 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
1307 break;
1308 }
1309
1310 case Instruction::SUB_LONG_2ADDR: {
1311 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001312 break;
1313 }
1314
Calin Juravle096cc022014-10-23 17:01:13 +01001315 case Instruction::SUB_FLOAT_2ADDR: {
1316 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
1317 break;
1318 }
1319
1320 case Instruction::SUB_DOUBLE_2ADDR: {
1321 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
1322 break;
1323 }
1324
Calin Juravle34bacdf2014-10-07 20:23:36 +01001325 case Instruction::MUL_INT_2ADDR: {
1326 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
1327 break;
1328 }
1329
1330 case Instruction::MUL_LONG_2ADDR: {
1331 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
1332 break;
1333 }
1334
Calin Juravleb5bfa962014-10-21 18:02:24 +01001335 case Instruction::MUL_FLOAT_2ADDR: {
1336 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
1337 break;
1338 }
1339
1340 case Instruction::MUL_DOUBLE_2ADDR: {
1341 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
1342 break;
1343 }
1344
Calin Juravle865fc882014-11-06 17:09:03 +00001345 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001346 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1347 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00001348 break;
1349 }
1350
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001351 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001352 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1353 dex_pc, Primitive::kPrimLong, false, true);
1354 break;
1355 }
1356
1357 case Instruction::REM_INT_2ADDR: {
1358 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1359 dex_pc, Primitive::kPrimInt, false, false);
1360 break;
1361 }
1362
1363 case Instruction::REM_LONG_2ADDR: {
1364 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1365 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001366 break;
1367 }
1368
Calin Juravle9aec02f2014-11-18 23:06:35 +00001369 case Instruction::SHL_INT_2ADDR: {
1370 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
1371 break;
1372 }
1373
1374 case Instruction::SHL_LONG_2ADDR: {
1375 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
1376 break;
1377 }
1378
1379 case Instruction::SHR_INT_2ADDR: {
1380 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
1381 break;
1382 }
1383
1384 case Instruction::SHR_LONG_2ADDR: {
1385 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
1386 break;
1387 }
1388
1389 case Instruction::USHR_INT_2ADDR: {
1390 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
1391 break;
1392 }
1393
1394 case Instruction::USHR_LONG_2ADDR: {
1395 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
1396 break;
1397 }
1398
Calin Juravle7c4954d2014-10-28 16:57:40 +00001399 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001400 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001401 break;
1402 }
1403
1404 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001405 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001406 break;
1407 }
1408
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001409 case Instruction::AND_INT_2ADDR: {
1410 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
1411 break;
1412 }
1413
1414 case Instruction::AND_LONG_2ADDR: {
1415 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
1416 break;
1417 }
1418
1419 case Instruction::OR_INT_2ADDR: {
1420 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
1421 break;
1422 }
1423
1424 case Instruction::OR_LONG_2ADDR: {
1425 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
1426 break;
1427 }
1428
1429 case Instruction::XOR_INT_2ADDR: {
1430 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
1431 break;
1432 }
1433
1434 case Instruction::XOR_LONG_2ADDR: {
1435 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
1436 break;
1437 }
1438
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001439 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001440 Binop_22s<HAdd>(instruction, false);
1441 break;
1442 }
1443
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001444 case Instruction::AND_INT_LIT16: {
1445 Binop_22s<HAnd>(instruction, false);
1446 break;
1447 }
1448
1449 case Instruction::OR_INT_LIT16: {
1450 Binop_22s<HOr>(instruction, false);
1451 break;
1452 }
1453
1454 case Instruction::XOR_INT_LIT16: {
1455 Binop_22s<HXor>(instruction, false);
1456 break;
1457 }
1458
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001459 case Instruction::RSUB_INT: {
1460 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001461 break;
1462 }
1463
Calin Juravle34bacdf2014-10-07 20:23:36 +01001464 case Instruction::MUL_INT_LIT16: {
1465 Binop_22s<HMul>(instruction, false);
1466 break;
1467 }
1468
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001469 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001470 Binop_22b<HAdd>(instruction, false);
1471 break;
1472 }
1473
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001474 case Instruction::AND_INT_LIT8: {
1475 Binop_22b<HAnd>(instruction, false);
1476 break;
1477 }
1478
1479 case Instruction::OR_INT_LIT8: {
1480 Binop_22b<HOr>(instruction, false);
1481 break;
1482 }
1483
1484 case Instruction::XOR_INT_LIT8: {
1485 Binop_22b<HXor>(instruction, false);
1486 break;
1487 }
1488
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001489 case Instruction::RSUB_INT_LIT8: {
1490 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001491 break;
1492 }
1493
Calin Juravle34bacdf2014-10-07 20:23:36 +01001494 case Instruction::MUL_INT_LIT8: {
1495 Binop_22b<HMul>(instruction, false);
1496 break;
1497 }
1498
Calin Juravled0d48522014-11-04 16:40:20 +00001499 case Instruction::DIV_INT_LIT16:
1500 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001501 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1502 dex_pc, Primitive::kPrimInt, true, true);
1503 break;
1504 }
1505
1506 case Instruction::REM_INT_LIT16:
1507 case Instruction::REM_INT_LIT8: {
1508 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1509 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00001510 break;
1511 }
1512
Calin Juravle9aec02f2014-11-18 23:06:35 +00001513 case Instruction::SHL_INT_LIT8: {
1514 Binop_22b<HShl>(instruction, false);
1515 break;
1516 }
1517
1518 case Instruction::SHR_INT_LIT8: {
1519 Binop_22b<HShr>(instruction, false);
1520 break;
1521 }
1522
1523 case Instruction::USHR_INT_LIT8: {
1524 Binop_22b<HUShr>(instruction, false);
1525 break;
1526 }
1527
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001528 case Instruction::NEW_INSTANCE: {
1529 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001530 new (arena_) HNewInstance(dex_pc, instruction.VRegB_21c()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001531 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1532 break;
1533 }
1534
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001535 case Instruction::NEW_ARRAY: {
1536 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
1537 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001538 new (arena_) HNewArray(length, dex_pc, instruction.VRegC_22c()));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001539 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1540 break;
1541 }
1542
1543 case Instruction::FILLED_NEW_ARRAY: {
1544 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1545 uint32_t type_index = instruction.VRegB_35c();
1546 uint32_t args[5];
1547 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001548 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001549 break;
1550 }
1551
1552 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1553 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1554 uint32_t type_index = instruction.VRegB_3rc();
1555 uint32_t register_index = instruction.VRegC_3rc();
1556 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00001557 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001558 break;
1559 }
1560
1561 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00001562 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001563 break;
1564 }
1565
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001566 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001567 case Instruction::MOVE_RESULT_WIDE:
1568 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001569 UpdateLocal(instruction.VRegA(), latest_result_);
1570 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001571 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001572
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001573 case Instruction::CMP_LONG: {
Calin Juravleddb7df22014-11-25 20:56:51 +00001574 Binop_23x_cmp(instruction, Primitive::kPrimLong, HCompare::kNoBias);
1575 break;
1576 }
1577
1578 case Instruction::CMPG_FLOAT: {
1579 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kGtBias);
1580 break;
1581 }
1582
1583 case Instruction::CMPG_DOUBLE: {
1584 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kGtBias);
1585 break;
1586 }
1587
1588 case Instruction::CMPL_FLOAT: {
1589 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kLtBias);
1590 break;
1591 }
1592
1593 case Instruction::CMPL_DOUBLE: {
1594 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kLtBias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001595 break;
1596 }
1597
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001598 case Instruction::NOP:
1599 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001600
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001601 case Instruction::IGET:
1602 case Instruction::IGET_WIDE:
1603 case Instruction::IGET_OBJECT:
1604 case Instruction::IGET_BOOLEAN:
1605 case Instruction::IGET_BYTE:
1606 case Instruction::IGET_CHAR:
1607 case Instruction::IGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001608 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001609 return false;
1610 }
1611 break;
1612 }
1613
1614 case Instruction::IPUT:
1615 case Instruction::IPUT_WIDE:
1616 case Instruction::IPUT_OBJECT:
1617 case Instruction::IPUT_BOOLEAN:
1618 case Instruction::IPUT_BYTE:
1619 case Instruction::IPUT_CHAR:
1620 case Instruction::IPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001621 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001622 return false;
1623 }
1624 break;
1625 }
1626
1627 case Instruction::SGET:
1628 case Instruction::SGET_WIDE:
1629 case Instruction::SGET_OBJECT:
1630 case Instruction::SGET_BOOLEAN:
1631 case Instruction::SGET_BYTE:
1632 case Instruction::SGET_CHAR:
1633 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001634 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001635 return false;
1636 }
1637 break;
1638 }
1639
1640 case Instruction::SPUT:
1641 case Instruction::SPUT_WIDE:
1642 case Instruction::SPUT_OBJECT:
1643 case Instruction::SPUT_BOOLEAN:
1644 case Instruction::SPUT_BYTE:
1645 case Instruction::SPUT_CHAR:
1646 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001647 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001648 return false;
1649 }
1650 break;
1651 }
1652
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001653#define ARRAY_XX(kind, anticipated_type) \
1654 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001655 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001656 break; \
1657 } \
1658 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001659 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001660 break; \
1661 }
1662
1663 ARRAY_XX(, Primitive::kPrimInt);
1664 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1665 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1666 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1667 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1668 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1669 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1670
Nicolas Geoffray39468442014-09-02 15:17:15 +01001671 case Instruction::ARRAY_LENGTH: {
1672 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001673 // No need for a temporary for the null check, it is the only input of the following
1674 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00001675 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001676 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001677 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1678 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1679 break;
1680 }
1681
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001682 case Instruction::CONST_STRING: {
Calin Juravle225ff812014-11-13 16:46:39 +00001683 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001684 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1685 break;
1686 }
1687
1688 case Instruction::CONST_STRING_JUMBO: {
Calin Juravle225ff812014-11-13 16:46:39 +00001689 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001690 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
1691 break;
1692 }
1693
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001694 case Instruction::CONST_CLASS: {
1695 uint16_t type_index = instruction.VRegB_21c();
1696 bool type_known_final;
1697 bool type_known_abstract;
1698 bool is_referrers_class;
1699 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1700 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
1701 &type_known_final, &type_known_abstract, &is_referrers_class);
1702 if (!can_access) {
1703 return false;
1704 }
1705 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001706 new (arena_) HLoadClass(type_index, is_referrers_class, dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001707 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1708 break;
1709 }
1710
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001711 case Instruction::MOVE_EXCEPTION: {
1712 current_block_->AddInstruction(new (arena_) HLoadException());
1713 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
1714 break;
1715 }
1716
1717 case Instruction::THROW: {
1718 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001719 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001720 // A throw instruction must branch to the exit block.
1721 current_block_->AddSuccessor(exit_block_);
1722 // We finished building this block. Set the current block to null to avoid
1723 // adding dead instructions to it.
1724 current_block_ = nullptr;
1725 break;
1726 }
1727
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001728 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001729 uint8_t destination = instruction.VRegA_22c();
1730 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001731 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00001732 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001733 return false;
1734 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001735 break;
1736 }
1737
1738 case Instruction::CHECK_CAST: {
1739 uint8_t reference = instruction.VRegA_21c();
1740 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00001741 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001742 return false;
1743 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001744 break;
1745 }
1746
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001747 case Instruction::MONITOR_ENTER: {
1748 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1749 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1750 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00001751 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001752 break;
1753 }
1754
1755 case Instruction::MONITOR_EXIT: {
1756 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1757 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1758 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00001759 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001760 break;
1761 }
1762
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001763 default:
1764 return false;
1765 }
1766 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001767} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001768
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001769HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001770 if (constant0_ != nullptr) {
1771 return constant0_;
1772 }
1773 constant0_ = new(arena_) HIntConstant(0);
1774 entry_block_->AddInstruction(constant0_);
1775 return constant0_;
1776}
1777
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001778HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001779 if (constant1_ != nullptr) {
1780 return constant1_;
1781 }
1782 constant1_ = new(arena_) HIntConstant(1);
1783 entry_block_->AddInstruction(constant1_);
1784 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001785}
1786
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001787HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001788 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001789 case 0: return GetIntConstant0();
1790 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001791 default: {
1792 HIntConstant* instruction = new (arena_) HIntConstant(constant);
1793 entry_block_->AddInstruction(instruction);
1794 return instruction;
1795 }
1796 }
1797}
1798
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001799HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
1800 HLongConstant* instruction = new (arena_) HLongConstant(constant);
1801 entry_block_->AddInstruction(instruction);
1802 return instruction;
1803}
1804
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001805HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
1806 return locals_.Get(register_index);
1807}
1808
1809void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
1810 HLocal* local = GetLocalAt(register_index);
1811 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
1812}
1813
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001814HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001815 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001816 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001817 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001818}
1819
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001820} // namespace art