blob: 0ac93a24fc901257413be88e6398af9a0b43d439 [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,
Roland Levillain624279f2014-12-04 11:54:28 +0000340 Primitive::Type result_type,
341 uint32_t dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +0000342 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Roland Levillain624279f2014-12-04 11:54:28 +0000343 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Roland Levillaindff1f282014-11-05 14:15:05 +0000344 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
345}
346
Roland Levillain88cb1752014-10-20 16:36:47 +0100347template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100348void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100349 HInstruction* first = LoadLocal(instruction.VRegB(), type);
350 HInstruction* second = LoadLocal(instruction.VRegC(), type);
351 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100352 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
353}
354
355template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000356void HGraphBuilder::Binop_23x(const Instruction& instruction,
357 Primitive::Type type,
358 uint32_t dex_pc) {
359 HInstruction* first = LoadLocal(instruction.VRegB(), type);
360 HInstruction* second = LoadLocal(instruction.VRegC(), type);
361 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
362 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
363}
364
365template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000366void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
367 Primitive::Type type) {
368 HInstruction* first = LoadLocal(instruction.VRegB(), type);
369 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
370 current_block_->AddInstruction(new (arena_) T(type, first, second));
371 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
372}
373
Calin Juravleddb7df22014-11-25 20:56:51 +0000374void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
375 Primitive::Type type,
376 HCompare::Bias bias) {
377 HInstruction* first = LoadLocal(instruction.VRegB(), type);
378 HInstruction* second = LoadLocal(instruction.VRegC(), type);
379 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias));
380 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
381}
382
Calin Juravle9aec02f2014-11-18 23:06:35 +0000383template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100384void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
385 HInstruction* first = LoadLocal(instruction.VRegA(), type);
386 HInstruction* second = LoadLocal(instruction.VRegB(), type);
387 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100388 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
389}
390
391template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000392void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) {
393 HInstruction* first = LoadLocal(instruction.VRegA(), type);
394 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
395 current_block_->AddInstruction(new (arena_) T(type, first, second));
396 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
397}
398
399template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000400void HGraphBuilder::Binop_12x(const Instruction& instruction,
401 Primitive::Type type,
402 uint32_t dex_pc) {
403 HInstruction* first = LoadLocal(instruction.VRegA(), type);
404 HInstruction* second = LoadLocal(instruction.VRegB(), type);
405 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
406 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
407}
408
409template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100410void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100411 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
412 HInstruction* second = GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100413 if (reverse) {
414 std::swap(first, second);
415 }
416 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
417 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
418}
419
420template<typename T>
421void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100422 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
423 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100424 if (reverse) {
425 std::swap(first, second);
426 }
427 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
428 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
429}
430
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100431void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
432 if (type == Primitive::kPrimVoid) {
433 current_block_->AddInstruction(new (arena_) HReturnVoid());
434 } else {
435 HInstruction* value = LoadLocal(instruction.VRegA(), type);
436 current_block_->AddInstruction(new (arena_) HReturn(value));
437 }
438 current_block_->AddSuccessor(exit_block_);
439 current_block_ = nullptr;
440}
441
442bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000443 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100444 uint32_t method_idx,
445 uint32_t number_of_vreg_arguments,
446 bool is_range,
447 uint32_t* args,
448 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100449 Instruction::Code opcode = instruction.Opcode();
450 InvokeType invoke_type;
451 switch (opcode) {
452 case Instruction::INVOKE_STATIC:
453 case Instruction::INVOKE_STATIC_RANGE:
454 invoke_type = kStatic;
455 break;
456 case Instruction::INVOKE_DIRECT:
457 case Instruction::INVOKE_DIRECT_RANGE:
458 invoke_type = kDirect;
459 break;
460 case Instruction::INVOKE_VIRTUAL:
461 case Instruction::INVOKE_VIRTUAL_RANGE:
462 invoke_type = kVirtual;
463 break;
464 case Instruction::INVOKE_INTERFACE:
465 case Instruction::INVOKE_INTERFACE_RANGE:
466 invoke_type = kInterface;
467 break;
468 case Instruction::INVOKE_SUPER_RANGE:
469 case Instruction::INVOKE_SUPER:
470 invoke_type = kSuper;
471 break;
472 default:
473 LOG(FATAL) << "Unexpected invoke op: " << opcode;
474 return false;
475 }
476
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100477 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
478 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
479 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
480 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100481 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100482 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
483
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000484 MethodReference target_method(dex_file_, method_idx);
485 uintptr_t direct_code;
486 uintptr_t direct_method;
487 int table_index;
488 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000489
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000490 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
491 &optimized_invoke_type, &target_method, &table_index,
492 &direct_code, &direct_method)) {
493 LOG(INFO) << "Did not compile " << PrettyMethod(method_idx, *dex_file_)
494 << " because a method call could not be resolved";
495 return false;
496 }
497 DCHECK(optimized_invoke_type != kSuper);
498
499 HInvoke* invoke = nullptr;
500 if (optimized_invoke_type == kVirtual) {
501 invoke = new (arena_) HInvokeVirtual(
502 arena_, number_of_arguments, return_type, dex_pc, table_index);
503 } else if (optimized_invoke_type == kInterface) {
504 invoke = new (arena_) HInvokeInterface(
505 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100506 } else {
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000507 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic);
508 // Sharpening to kDirect only works if we compile PIC.
509 DCHECK((optimized_invoke_type == invoke_type) || (optimized_invoke_type != kDirect)
510 || compiler_driver_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100511 // Treat invoke-direct like static calls for now.
512 invoke = new (arena_) HInvokeStatic(
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000513 arena_, number_of_arguments, return_type, dex_pc, target_method.dex_method_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100514 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100515
516 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000517 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100518 if (is_instance_call) {
519 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000520 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100521 current_block_->AddInstruction(null_check);
522 temps.Add(null_check);
523 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100524 start_index = 1;
525 }
526
527 uint32_t descriptor_index = 1;
528 uint32_t argument_index = start_index;
529 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
530 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100531 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
532 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100533 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
Calin Juravle225ff812014-11-13 16:46:39 +0000534 << " at " << dex_pc;
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100535 // We do not implement non sequential register pair.
536 return false;
537 }
538 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
539 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100540 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100541 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100542 }
543 }
544
545 DCHECK_EQ(argument_index, number_of_arguments);
546 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100547 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100548 return true;
549}
550
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100551bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000552 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100553 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100554 uint32_t source_or_dest_reg = instruction.VRegA_22c();
555 uint32_t obj_reg = instruction.VRegB_22c();
556 uint16_t field_index = instruction.VRegC_22c();
557
558 ScopedObjectAccess soa(Thread::Current());
559 StackHandleScope<1> hs(soa.Self());
560 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
561 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
562
563 if (resolved_field.Get() == nullptr) {
564 return false;
565 }
566 if (resolved_field->IsVolatile()) {
567 return false;
568 }
569
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100570 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100571
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100572 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000573 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100574 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000575 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100576 HInstruction* null_check = current_block_->GetLastInstruction();
577 // We need one temporary for the null check.
578 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100579 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100580 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
581 null_check,
582 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100583 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100584 resolved_field->GetOffset()));
585 } else {
586 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
587 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100588 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100589 resolved_field->GetOffset()));
590
591 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
592 }
593 return true;
594}
595
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100596
597
598bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000599 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100600 bool is_put) {
601 uint32_t source_or_dest_reg = instruction.VRegA_21c();
602 uint16_t field_index = instruction.VRegB_21c();
603
604 uint32_t storage_index;
605 bool is_referrers_class;
606 bool is_initialized;
607 bool is_volatile;
608 MemberOffset field_offset(0u);
609 Primitive::Type field_type;
610
611 bool fast_path = compiler_driver_->ComputeStaticFieldInfo(field_index,
612 dex_compilation_unit_,
613 is_put,
614 &field_offset,
615 &storage_index,
616 &is_referrers_class,
617 &is_volatile,
618 &is_initialized,
619 &field_type);
620 if (!fast_path) {
621 return false;
622 }
623
624 if (is_volatile) {
625 return false;
626 }
627
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100628 HLoadClass* constant = new (arena_) HLoadClass(
Calin Juravle225ff812014-11-13 16:46:39 +0000629 storage_index, is_referrers_class, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100630 current_block_->AddInstruction(constant);
631
632 HInstruction* cls = constant;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000633 if (!is_initialized) {
Calin Juravle225ff812014-11-13 16:46:39 +0000634 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100635 current_block_->AddInstruction(cls);
636 }
637
638 if (is_put) {
639 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000640 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100641 temps.Add(cls);
642 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
643 DCHECK_EQ(value->GetType(), field_type);
644 current_block_->AddInstruction(
645 new (arena_) HStaticFieldSet(cls, value, field_type, field_offset));
646 } else {
647 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls, field_type, field_offset));
648 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
649 }
650 return true;
651}
652
Calin Juravlebacfec32014-11-14 15:54:36 +0000653void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
654 uint16_t first_vreg,
655 int64_t second_vreg_or_constant,
656 uint32_t dex_pc,
657 Primitive::Type type,
658 bool second_is_constant,
659 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000660 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +0000661
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000662 HInstruction* first = LoadLocal(first_vreg, type);
663 HInstruction* second = nullptr;
664 if (second_is_constant) {
665 if (type == Primitive::kPrimInt) {
666 second = GetIntConstant(second_vreg_or_constant);
667 } else {
668 second = GetLongConstant(second_vreg_or_constant);
669 }
670 } else {
671 second = LoadLocal(second_vreg_or_constant, type);
672 }
673
674 if (!second_is_constant
675 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
676 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
677 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000678 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000679 current_block_->AddInstruction(second);
680 temps.Add(current_block_->GetLastInstruction());
681 }
682
Calin Juravlebacfec32014-11-14 15:54:36 +0000683 if (isDiv) {
684 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
685 } else {
686 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
687 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000688 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +0000689}
690
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100691void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000692 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100693 bool is_put,
694 Primitive::Type anticipated_type) {
695 uint8_t source_or_dest_reg = instruction.VRegA_23x();
696 uint8_t array_reg = instruction.VRegB_23x();
697 uint8_t index_reg = instruction.VRegC_23x();
698
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100699 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000700 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100701
702 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000703 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100704 current_block_->AddInstruction(object);
705 temps.Add(object);
706
707 HInstruction* length = new (arena_) HArrayLength(object);
708 current_block_->AddInstruction(length);
709 temps.Add(length);
710 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +0000711 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100712 current_block_->AddInstruction(index);
713 temps.Add(index);
714 if (is_put) {
715 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
716 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100717 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000718 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100719 } else {
720 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
721 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
722 }
723}
724
Calin Juravle225ff812014-11-13 16:46:39 +0000725void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100726 uint32_t type_index,
727 uint32_t number_of_vreg_arguments,
728 bool is_range,
729 uint32_t* args,
730 uint32_t register_index) {
731 HInstruction* length = GetIntConstant(number_of_vreg_arguments);
Calin Juravle225ff812014-11-13 16:46:39 +0000732 HInstruction* object = new (arena_) HNewArray(length, dex_pc, type_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100733 current_block_->AddInstruction(object);
734
735 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
736 DCHECK_EQ(descriptor[0], '[') << descriptor;
737 char primitive = descriptor[1];
738 DCHECK(primitive == 'I'
739 || primitive == 'L'
740 || primitive == '[') << descriptor;
741 bool is_reference_array = (primitive == 'L') || (primitive == '[');
742 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
743
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000744 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100745 temps.Add(object);
746 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
747 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
748 HInstruction* index = GetIntConstant(i);
749 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000750 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100751 }
752 latest_result_ = object;
753}
754
755template <typename T>
756void HGraphBuilder::BuildFillArrayData(HInstruction* object,
757 const T* data,
758 uint32_t element_count,
759 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +0000760 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100761 for (uint32_t i = 0; i < element_count; ++i) {
762 HInstruction* index = GetIntConstant(i);
763 HInstruction* value = GetIntConstant(data[i]);
764 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000765 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100766 }
767}
768
Calin Juravle225ff812014-11-13 16:46:39 +0000769void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000770 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000771 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000772 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000773 current_block_->AddInstruction(null_check);
774 temps.Add(null_check);
775
776 HInstruction* length = new (arena_) HArrayLength(null_check);
777 current_block_->AddInstruction(length);
778
Calin Juravle225ff812014-11-13 16:46:39 +0000779 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +0000780 const Instruction::ArrayDataPayload* payload =
781 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
782 const uint8_t* data = payload->data;
783 uint32_t element_count = payload->element_count;
784
785 // Implementation of this DEX instruction seems to be that the bounds check is
786 // done before doing any stores.
787 HInstruction* last_index = GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +0000788 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +0000789
790 switch (payload->element_width) {
791 case 1:
792 BuildFillArrayData(null_check,
793 reinterpret_cast<const int8_t*>(data),
794 element_count,
795 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +0000796 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000797 break;
798 case 2:
799 BuildFillArrayData(null_check,
800 reinterpret_cast<const int16_t*>(data),
801 element_count,
802 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +0000803 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000804 break;
805 case 4:
806 BuildFillArrayData(null_check,
807 reinterpret_cast<const int32_t*>(data),
808 element_count,
809 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +0000810 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000811 break;
812 case 8:
813 BuildFillWideArrayData(null_check,
814 reinterpret_cast<const int64_t*>(data),
815 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000816 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000817 break;
818 default:
819 LOG(FATAL) << "Unknown element width for " << payload->element_width;
820 }
821}
822
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100823void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100824 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100825 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000826 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100827 for (uint32_t i = 0; i < element_count; ++i) {
828 HInstruction* index = GetIntConstant(i);
829 HInstruction* value = GetLongConstant(data[i]);
830 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000831 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100832 }
833}
834
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000835bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
836 uint8_t destination,
837 uint8_t reference,
838 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +0000839 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000840 bool type_known_final;
841 bool type_known_abstract;
842 bool is_referrers_class;
843 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
844 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
845 &type_known_final, &type_known_abstract, &is_referrers_class);
846 if (!can_access) {
847 return false;
848 }
849 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000850 HLoadClass* cls = new (arena_) HLoadClass(type_index, is_referrers_class, dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000851 current_block_->AddInstruction(cls);
852 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000853 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000854 temps.Add(cls);
855 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
856 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000857 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000858 UpdateLocal(destination, current_block_->GetLastInstruction());
859 } else {
860 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
861 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000862 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000863 }
864 return true;
865}
866
Calin Juravle225ff812014-11-13 16:46:39 +0000867void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000868 if (target_offset <= 0) {
869 // Unconditionnally add a suspend check to backward branches. We can remove
870 // them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +0000871 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000872 }
873}
874
Calin Juravle225ff812014-11-13 16:46:39 +0000875bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000876 if (current_block_ == nullptr) {
877 return true; // Dead code
878 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000879
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000880 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000881 case Instruction::CONST_4: {
882 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100883 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000884 UpdateLocal(register_index, constant);
885 break;
886 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000887
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100888 case Instruction::CONST_16: {
889 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100890 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
891 UpdateLocal(register_index, constant);
892 break;
893 }
894
Dave Allison20dfc792014-06-16 20:44:29 -0700895 case Instruction::CONST: {
896 int32_t register_index = instruction.VRegA();
897 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
898 UpdateLocal(register_index, constant);
899 break;
900 }
901
902 case Instruction::CONST_HIGH16: {
903 int32_t register_index = instruction.VRegA();
904 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
905 UpdateLocal(register_index, constant);
906 break;
907 }
908
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100909 case Instruction::CONST_WIDE_16: {
910 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700911 // Get 16 bits of constant value, sign extended to 64 bits.
912 int64_t value = instruction.VRegB_21s();
913 value <<= 48;
914 value >>= 48;
915 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100916 UpdateLocal(register_index, constant);
917 break;
918 }
919
920 case Instruction::CONST_WIDE_32: {
921 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700922 // Get 32 bits of constant value, sign extended to 64 bits.
923 int64_t value = instruction.VRegB_31i();
924 value <<= 32;
925 value >>= 32;
926 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100927 UpdateLocal(register_index, constant);
928 break;
929 }
930
931 case Instruction::CONST_WIDE: {
932 int32_t register_index = instruction.VRegA();
933 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100934 UpdateLocal(register_index, constant);
935 break;
936 }
937
Dave Allison20dfc792014-06-16 20:44:29 -0700938 case Instruction::CONST_WIDE_HIGH16: {
939 int32_t register_index = instruction.VRegA();
940 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
941 HLongConstant* constant = GetLongConstant(value);
942 UpdateLocal(register_index, constant);
943 break;
944 }
945
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000946 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -0700947 case Instruction::MOVE:
948 case Instruction::MOVE_FROM16:
949 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100950 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100951 UpdateLocal(instruction.VRegA(), value);
952 break;
953 }
954
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000955 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -0700956 case Instruction::MOVE_WIDE:
957 case Instruction::MOVE_WIDE_FROM16:
958 case Instruction::MOVE_WIDE_16: {
959 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
960 UpdateLocal(instruction.VRegA(), value);
961 break;
962 }
963
964 case Instruction::MOVE_OBJECT:
965 case Instruction::MOVE_OBJECT_16:
966 case Instruction::MOVE_OBJECT_FROM16: {
967 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
968 UpdateLocal(instruction.VRegA(), value);
969 break;
970 }
971
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000972 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100973 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000974 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000975 }
976
Dave Allison20dfc792014-06-16 20:44:29 -0700977#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +0000978 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
979 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100980
Dave Allison20dfc792014-06-16 20:44:29 -0700981 IF_XX(HEqual, EQ);
982 IF_XX(HNotEqual, NE);
983 IF_XX(HLessThan, LT);
984 IF_XX(HLessThanOrEqual, LE);
985 IF_XX(HGreaterThan, GT);
986 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000987
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000988 case Instruction::GOTO:
989 case Instruction::GOTO_16:
990 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000991 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +0000992 PotentiallyAddSuspendCheck(offset, dex_pc);
993 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000994 DCHECK(target != nullptr);
995 current_block_->AddInstruction(new (arena_) HGoto());
996 current_block_->AddSuccessor(target);
997 current_block_ = nullptr;
998 break;
999 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001000
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001001 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001002 DCHECK_NE(return_type_, Primitive::kPrimNot);
1003 DCHECK_NE(return_type_, Primitive::kPrimLong);
1004 DCHECK_NE(return_type_, Primitive::kPrimDouble);
1005 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001006 break;
1007 }
1008
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001009 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001010 DCHECK(return_type_ == Primitive::kPrimNot);
1011 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001012 break;
1013 }
1014
1015 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001016 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
1017 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001018 break;
1019 }
1020
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001021 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001022 case Instruction::INVOKE_INTERFACE:
1023 case Instruction::INVOKE_STATIC:
1024 case Instruction::INVOKE_SUPER:
1025 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001026 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001027 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001028 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001029 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001030 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001031 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001032 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001033 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001034 break;
1035 }
1036
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001037 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001038 case Instruction::INVOKE_INTERFACE_RANGE:
1039 case Instruction::INVOKE_STATIC_RANGE:
1040 case Instruction::INVOKE_SUPER_RANGE:
1041 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001042 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001043 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1044 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001045 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001046 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001047 return false;
1048 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001049 break;
1050 }
1051
Roland Levillain88cb1752014-10-20 16:36:47 +01001052 case Instruction::NEG_INT: {
1053 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1054 break;
1055 }
1056
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001057 case Instruction::NEG_LONG: {
1058 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1059 break;
1060 }
1061
Roland Levillain3dbcb382014-10-28 17:30:07 +00001062 case Instruction::NEG_FLOAT: {
1063 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1064 break;
1065 }
1066
1067 case Instruction::NEG_DOUBLE: {
1068 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1069 break;
1070 }
1071
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001072 case Instruction::NOT_INT: {
1073 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1074 break;
1075 }
1076
Roland Levillain70566432014-10-24 16:20:17 +01001077 case Instruction::NOT_LONG: {
1078 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1079 break;
1080 }
1081
Roland Levillaindff1f282014-11-05 14:15:05 +00001082 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001083 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001084 break;
1085 }
1086
Roland Levillaincff13742014-11-17 14:32:17 +00001087 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001088 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001089 break;
1090 }
1091
1092 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001093 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001094 break;
1095 }
1096
Roland Levillain946e1432014-11-11 17:35:19 +00001097 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001098 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001099 break;
1100 }
1101
Roland Levillain6d0e4832014-11-27 18:31:21 +00001102 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001103 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001104 break;
1105 }
1106
Roland Levillain647b9ed2014-11-27 12:06:00 +00001107 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001108 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001109 break;
1110 }
1111
Roland Levillain3f8f9362014-12-02 17:45:01 +00001112 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001113 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1114 break;
1115 }
1116
1117 case Instruction::FLOAT_TO_LONG: {
1118 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001119 break;
1120 }
1121
Roland Levillain51d3fc42014-11-13 14:11:42 +00001122 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001123 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001124 break;
1125 }
1126
Roland Levillain01a8d712014-11-14 16:27:39 +00001127 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001128 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001129 break;
1130 }
1131
Roland Levillain981e4542014-11-14 11:47:14 +00001132 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001133 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001134 break;
1135 }
1136
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001137 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001138 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001139 break;
1140 }
1141
1142 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001143 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001144 break;
1145 }
1146
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001147 case Instruction::ADD_DOUBLE: {
1148 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1149 break;
1150 }
1151
1152 case Instruction::ADD_FLOAT: {
1153 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1154 break;
1155 }
1156
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001157 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001158 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001159 break;
1160 }
1161
1162 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001163 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001164 break;
1165 }
1166
Calin Juravle096cc022014-10-23 17:01:13 +01001167 case Instruction::SUB_FLOAT: {
1168 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1169 break;
1170 }
1171
1172 case Instruction::SUB_DOUBLE: {
1173 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1174 break;
1175 }
1176
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001177 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001178 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1179 break;
1180 }
1181
Calin Juravle34bacdf2014-10-07 20:23:36 +01001182 case Instruction::MUL_INT: {
1183 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1184 break;
1185 }
1186
1187 case Instruction::MUL_LONG: {
1188 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1189 break;
1190 }
1191
Calin Juravleb5bfa962014-10-21 18:02:24 +01001192 case Instruction::MUL_FLOAT: {
1193 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1194 break;
1195 }
1196
1197 case Instruction::MUL_DOUBLE: {
1198 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1199 break;
1200 }
1201
Calin Juravled0d48522014-11-04 16:40:20 +00001202 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001203 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1204 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001205 break;
1206 }
1207
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001208 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001209 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1210 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001211 break;
1212 }
1213
Calin Juravle7c4954d2014-10-28 16:57:40 +00001214 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001215 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001216 break;
1217 }
1218
1219 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001220 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001221 break;
1222 }
1223
Calin Juravlebacfec32014-11-14 15:54:36 +00001224 case Instruction::REM_INT: {
1225 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1226 dex_pc, Primitive::kPrimInt, false, false);
1227 break;
1228 }
1229
1230 case Instruction::REM_LONG: {
1231 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1232 dex_pc, Primitive::kPrimLong, false, false);
1233 break;
1234 }
1235
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001236 case Instruction::AND_INT: {
1237 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1238 break;
1239 }
1240
1241 case Instruction::AND_LONG: {
1242 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1243 break;
1244 }
1245
Calin Juravle9aec02f2014-11-18 23:06:35 +00001246 case Instruction::SHL_INT: {
1247 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
1248 break;
1249 }
1250
1251 case Instruction::SHL_LONG: {
1252 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
1253 break;
1254 }
1255
1256 case Instruction::SHR_INT: {
1257 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
1258 break;
1259 }
1260
1261 case Instruction::SHR_LONG: {
1262 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
1263 break;
1264 }
1265
1266 case Instruction::USHR_INT: {
1267 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
1268 break;
1269 }
1270
1271 case Instruction::USHR_LONG: {
1272 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
1273 break;
1274 }
1275
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001276 case Instruction::OR_INT: {
1277 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1278 break;
1279 }
1280
1281 case Instruction::OR_LONG: {
1282 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1283 break;
1284 }
1285
1286 case Instruction::XOR_INT: {
1287 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1288 break;
1289 }
1290
1291 case Instruction::XOR_LONG: {
1292 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1293 break;
1294 }
1295
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001296 case Instruction::ADD_LONG_2ADDR: {
1297 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001298 break;
1299 }
1300
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001301 case Instruction::ADD_DOUBLE_2ADDR: {
1302 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1303 break;
1304 }
1305
1306 case Instruction::ADD_FLOAT_2ADDR: {
1307 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
1308 break;
1309 }
1310
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001311 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001312 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
1313 break;
1314 }
1315
1316 case Instruction::SUB_LONG_2ADDR: {
1317 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001318 break;
1319 }
1320
Calin Juravle096cc022014-10-23 17:01:13 +01001321 case Instruction::SUB_FLOAT_2ADDR: {
1322 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
1323 break;
1324 }
1325
1326 case Instruction::SUB_DOUBLE_2ADDR: {
1327 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
1328 break;
1329 }
1330
Calin Juravle34bacdf2014-10-07 20:23:36 +01001331 case Instruction::MUL_INT_2ADDR: {
1332 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
1333 break;
1334 }
1335
1336 case Instruction::MUL_LONG_2ADDR: {
1337 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
1338 break;
1339 }
1340
Calin Juravleb5bfa962014-10-21 18:02:24 +01001341 case Instruction::MUL_FLOAT_2ADDR: {
1342 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
1343 break;
1344 }
1345
1346 case Instruction::MUL_DOUBLE_2ADDR: {
1347 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
1348 break;
1349 }
1350
Calin Juravle865fc882014-11-06 17:09:03 +00001351 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001352 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1353 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00001354 break;
1355 }
1356
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001357 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001358 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1359 dex_pc, Primitive::kPrimLong, false, true);
1360 break;
1361 }
1362
1363 case Instruction::REM_INT_2ADDR: {
1364 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1365 dex_pc, Primitive::kPrimInt, false, false);
1366 break;
1367 }
1368
1369 case Instruction::REM_LONG_2ADDR: {
1370 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1371 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001372 break;
1373 }
1374
Calin Juravle9aec02f2014-11-18 23:06:35 +00001375 case Instruction::SHL_INT_2ADDR: {
1376 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
1377 break;
1378 }
1379
1380 case Instruction::SHL_LONG_2ADDR: {
1381 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
1382 break;
1383 }
1384
1385 case Instruction::SHR_INT_2ADDR: {
1386 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
1387 break;
1388 }
1389
1390 case Instruction::SHR_LONG_2ADDR: {
1391 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
1392 break;
1393 }
1394
1395 case Instruction::USHR_INT_2ADDR: {
1396 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
1397 break;
1398 }
1399
1400 case Instruction::USHR_LONG_2ADDR: {
1401 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
1402 break;
1403 }
1404
Calin Juravle7c4954d2014-10-28 16:57:40 +00001405 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001406 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001407 break;
1408 }
1409
1410 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001411 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001412 break;
1413 }
1414
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001415 case Instruction::AND_INT_2ADDR: {
1416 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
1417 break;
1418 }
1419
1420 case Instruction::AND_LONG_2ADDR: {
1421 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
1422 break;
1423 }
1424
1425 case Instruction::OR_INT_2ADDR: {
1426 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
1427 break;
1428 }
1429
1430 case Instruction::OR_LONG_2ADDR: {
1431 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
1432 break;
1433 }
1434
1435 case Instruction::XOR_INT_2ADDR: {
1436 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
1437 break;
1438 }
1439
1440 case Instruction::XOR_LONG_2ADDR: {
1441 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
1442 break;
1443 }
1444
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001445 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001446 Binop_22s<HAdd>(instruction, false);
1447 break;
1448 }
1449
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001450 case Instruction::AND_INT_LIT16: {
1451 Binop_22s<HAnd>(instruction, false);
1452 break;
1453 }
1454
1455 case Instruction::OR_INT_LIT16: {
1456 Binop_22s<HOr>(instruction, false);
1457 break;
1458 }
1459
1460 case Instruction::XOR_INT_LIT16: {
1461 Binop_22s<HXor>(instruction, false);
1462 break;
1463 }
1464
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001465 case Instruction::RSUB_INT: {
1466 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001467 break;
1468 }
1469
Calin Juravle34bacdf2014-10-07 20:23:36 +01001470 case Instruction::MUL_INT_LIT16: {
1471 Binop_22s<HMul>(instruction, false);
1472 break;
1473 }
1474
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001475 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001476 Binop_22b<HAdd>(instruction, false);
1477 break;
1478 }
1479
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001480 case Instruction::AND_INT_LIT8: {
1481 Binop_22b<HAnd>(instruction, false);
1482 break;
1483 }
1484
1485 case Instruction::OR_INT_LIT8: {
1486 Binop_22b<HOr>(instruction, false);
1487 break;
1488 }
1489
1490 case Instruction::XOR_INT_LIT8: {
1491 Binop_22b<HXor>(instruction, false);
1492 break;
1493 }
1494
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001495 case Instruction::RSUB_INT_LIT8: {
1496 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001497 break;
1498 }
1499
Calin Juravle34bacdf2014-10-07 20:23:36 +01001500 case Instruction::MUL_INT_LIT8: {
1501 Binop_22b<HMul>(instruction, false);
1502 break;
1503 }
1504
Calin Juravled0d48522014-11-04 16:40:20 +00001505 case Instruction::DIV_INT_LIT16:
1506 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001507 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1508 dex_pc, Primitive::kPrimInt, true, true);
1509 break;
1510 }
1511
1512 case Instruction::REM_INT_LIT16:
1513 case Instruction::REM_INT_LIT8: {
1514 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1515 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00001516 break;
1517 }
1518
Calin Juravle9aec02f2014-11-18 23:06:35 +00001519 case Instruction::SHL_INT_LIT8: {
1520 Binop_22b<HShl>(instruction, false);
1521 break;
1522 }
1523
1524 case Instruction::SHR_INT_LIT8: {
1525 Binop_22b<HShr>(instruction, false);
1526 break;
1527 }
1528
1529 case Instruction::USHR_INT_LIT8: {
1530 Binop_22b<HUShr>(instruction, false);
1531 break;
1532 }
1533
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001534 case Instruction::NEW_INSTANCE: {
1535 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001536 new (arena_) HNewInstance(dex_pc, instruction.VRegB_21c()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001537 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1538 break;
1539 }
1540
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001541 case Instruction::NEW_ARRAY: {
1542 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
1543 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001544 new (arena_) HNewArray(length, dex_pc, instruction.VRegC_22c()));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001545 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1546 break;
1547 }
1548
1549 case Instruction::FILLED_NEW_ARRAY: {
1550 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1551 uint32_t type_index = instruction.VRegB_35c();
1552 uint32_t args[5];
1553 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001554 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001555 break;
1556 }
1557
1558 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1559 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1560 uint32_t type_index = instruction.VRegB_3rc();
1561 uint32_t register_index = instruction.VRegC_3rc();
1562 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00001563 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001564 break;
1565 }
1566
1567 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00001568 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001569 break;
1570 }
1571
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001572 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001573 case Instruction::MOVE_RESULT_WIDE:
1574 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001575 UpdateLocal(instruction.VRegA(), latest_result_);
1576 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001577 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001578
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001579 case Instruction::CMP_LONG: {
Calin Juravleddb7df22014-11-25 20:56:51 +00001580 Binop_23x_cmp(instruction, Primitive::kPrimLong, HCompare::kNoBias);
1581 break;
1582 }
1583
1584 case Instruction::CMPG_FLOAT: {
1585 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kGtBias);
1586 break;
1587 }
1588
1589 case Instruction::CMPG_DOUBLE: {
1590 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kGtBias);
1591 break;
1592 }
1593
1594 case Instruction::CMPL_FLOAT: {
1595 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kLtBias);
1596 break;
1597 }
1598
1599 case Instruction::CMPL_DOUBLE: {
1600 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kLtBias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001601 break;
1602 }
1603
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001604 case Instruction::NOP:
1605 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001606
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001607 case Instruction::IGET:
1608 case Instruction::IGET_WIDE:
1609 case Instruction::IGET_OBJECT:
1610 case Instruction::IGET_BOOLEAN:
1611 case Instruction::IGET_BYTE:
1612 case Instruction::IGET_CHAR:
1613 case Instruction::IGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001614 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001615 return false;
1616 }
1617 break;
1618 }
1619
1620 case Instruction::IPUT:
1621 case Instruction::IPUT_WIDE:
1622 case Instruction::IPUT_OBJECT:
1623 case Instruction::IPUT_BOOLEAN:
1624 case Instruction::IPUT_BYTE:
1625 case Instruction::IPUT_CHAR:
1626 case Instruction::IPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001627 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001628 return false;
1629 }
1630 break;
1631 }
1632
1633 case Instruction::SGET:
1634 case Instruction::SGET_WIDE:
1635 case Instruction::SGET_OBJECT:
1636 case Instruction::SGET_BOOLEAN:
1637 case Instruction::SGET_BYTE:
1638 case Instruction::SGET_CHAR:
1639 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001640 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001641 return false;
1642 }
1643 break;
1644 }
1645
1646 case Instruction::SPUT:
1647 case Instruction::SPUT_WIDE:
1648 case Instruction::SPUT_OBJECT:
1649 case Instruction::SPUT_BOOLEAN:
1650 case Instruction::SPUT_BYTE:
1651 case Instruction::SPUT_CHAR:
1652 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001653 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001654 return false;
1655 }
1656 break;
1657 }
1658
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001659#define ARRAY_XX(kind, anticipated_type) \
1660 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001661 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001662 break; \
1663 } \
1664 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001665 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001666 break; \
1667 }
1668
1669 ARRAY_XX(, Primitive::kPrimInt);
1670 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1671 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1672 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1673 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1674 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1675 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1676
Nicolas Geoffray39468442014-09-02 15:17:15 +01001677 case Instruction::ARRAY_LENGTH: {
1678 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001679 // No need for a temporary for the null check, it is the only input of the following
1680 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00001681 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001682 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001683 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1684 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1685 break;
1686 }
1687
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001688 case Instruction::CONST_STRING: {
Calin Juravle225ff812014-11-13 16:46:39 +00001689 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001690 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1691 break;
1692 }
1693
1694 case Instruction::CONST_STRING_JUMBO: {
Calin Juravle225ff812014-11-13 16:46:39 +00001695 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001696 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
1697 break;
1698 }
1699
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001700 case Instruction::CONST_CLASS: {
1701 uint16_t type_index = instruction.VRegB_21c();
1702 bool type_known_final;
1703 bool type_known_abstract;
1704 bool is_referrers_class;
1705 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1706 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
1707 &type_known_final, &type_known_abstract, &is_referrers_class);
1708 if (!can_access) {
1709 return false;
1710 }
1711 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001712 new (arena_) HLoadClass(type_index, is_referrers_class, dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001713 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1714 break;
1715 }
1716
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001717 case Instruction::MOVE_EXCEPTION: {
1718 current_block_->AddInstruction(new (arena_) HLoadException());
1719 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
1720 break;
1721 }
1722
1723 case Instruction::THROW: {
1724 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001725 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001726 // A throw instruction must branch to the exit block.
1727 current_block_->AddSuccessor(exit_block_);
1728 // We finished building this block. Set the current block to null to avoid
1729 // adding dead instructions to it.
1730 current_block_ = nullptr;
1731 break;
1732 }
1733
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001734 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001735 uint8_t destination = instruction.VRegA_22c();
1736 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001737 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00001738 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001739 return false;
1740 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001741 break;
1742 }
1743
1744 case Instruction::CHECK_CAST: {
1745 uint8_t reference = instruction.VRegA_21c();
1746 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00001747 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001748 return false;
1749 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001750 break;
1751 }
1752
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001753 case Instruction::MONITOR_ENTER: {
1754 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1755 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1756 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00001757 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001758 break;
1759 }
1760
1761 case Instruction::MONITOR_EXIT: {
1762 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1763 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1764 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00001765 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001766 break;
1767 }
1768
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001769 default:
1770 return false;
1771 }
1772 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001773} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001774
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001775HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001776 if (constant0_ != nullptr) {
1777 return constant0_;
1778 }
1779 constant0_ = new(arena_) HIntConstant(0);
1780 entry_block_->AddInstruction(constant0_);
1781 return constant0_;
1782}
1783
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001784HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001785 if (constant1_ != nullptr) {
1786 return constant1_;
1787 }
1788 constant1_ = new(arena_) HIntConstant(1);
1789 entry_block_->AddInstruction(constant1_);
1790 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001791}
1792
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001793HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001794 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001795 case 0: return GetIntConstant0();
1796 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001797 default: {
1798 HIntConstant* instruction = new (arena_) HIntConstant(constant);
1799 entry_block_->AddInstruction(instruction);
1800 return instruction;
1801 }
1802 }
1803}
1804
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001805HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
1806 HLongConstant* instruction = new (arena_) HLongConstant(constant);
1807 entry_block_->AddInstruction(instruction);
1808 return instruction;
1809}
1810
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001811HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
1812 return locals_.Get(register_index);
1813}
1814
1815void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
1816 HLocal* local = GetLocalAt(register_index);
1817 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
1818}
1819
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001820HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001821 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001822 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001823 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001824}
1825
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001826} // namespace art