blob: 76a2be927e0bf761c69b42da06c050bba3275e09 [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:
44 Temporaries(HGraph* graph, size_t count) : graph_(graph), count_(count), index_(0) {
45 graph_->UpdateNumberOfTemporaries(count_);
46 }
47
48 void Add(HInstruction* instruction) {
49 // We currently only support vreg size temps.
50 DCHECK(instruction->GetType() != Primitive::kPrimLong
51 && instruction->GetType() != Primitive::kPrimDouble);
52 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_++);
53 instruction->GetBlock()->AddInstruction(temp);
54 DCHECK(temp->GetPrevious() == instruction);
55 }
56
57 private:
58 HGraph* const graph_;
59
60 // The total number of temporaries that will be used.
61 const size_t count_;
62
63 // Current index in the temporary stack, updated by `Add`.
64 size_t index_;
65};
66
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010067void HGraphBuilder::InitializeLocals(uint16_t count) {
68 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000069 locals_.SetSize(count);
70 for (int i = 0; i < count; i++) {
71 HLocal* local = new (arena_) HLocal(i);
72 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000073 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000074 }
75}
76
Nicolas Geoffray52e832b2014-11-06 15:15:31 +000077void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010078 // dex_compilation_unit_ is null only when unit testing.
79 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +000080 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010081 }
82
83 graph_->SetNumberOfInVRegs(number_of_parameters);
84 const char* shorty = dex_compilation_unit_->GetShorty();
85 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010086 int parameter_index = 0;
87
88 if (!dex_compilation_unit_->IsStatic()) {
89 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010090 HParameterValue* parameter =
91 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010092 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010093 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010094 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010095 number_of_parameters--;
96 }
97
98 uint32_t pos = 1;
99 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100100 HParameterValue* parameter =
101 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
102 entry_block_->AddInstruction(parameter);
103 HLocal* local = GetLocalAt(locals_index++);
104 // Store the parameter value in the local that the dex code will use
105 // to reference that parameter.
106 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
107 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
108 || (parameter->GetType() == Primitive::kPrimDouble);
109 if (is_wide) {
110 i++;
111 locals_index++;
112 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100113 }
114 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100115}
116
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100117template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100118void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000119 int32_t target_offset = instruction.GetTargetOffset();
120 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100121 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
122 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700123 T* comparison = new (arena_) T(first, second);
124 current_block_->AddInstruction(comparison);
125 HInstruction* ifinst = new (arena_) HIf(comparison);
126 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000127 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700128 DCHECK(target != nullptr);
129 current_block_->AddSuccessor(target);
130 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
131 DCHECK(target != nullptr);
132 current_block_->AddSuccessor(target);
133 current_block_ = nullptr;
134}
135
136template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100137void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138 int32_t target_offset = instruction.GetTargetOffset();
139 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700140 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
141 T* comparison = new (arena_) T(value, GetIntConstant(0));
142 current_block_->AddInstruction(comparison);
143 HInstruction* ifinst = new (arena_) HIf(comparison);
144 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100146 DCHECK(target != nullptr);
147 current_block_->AddSuccessor(target);
148 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
149 DCHECK(target != nullptr);
150 current_block_->AddSuccessor(target);
151 current_block_ = nullptr;
152}
153
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000154HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000155 const uint16_t* code_ptr = code_item.insns_;
156 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100157 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000158
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000159 // Setup the graph with the entry block and exit block.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000160 graph_ = new (arena_) HGraph(arena_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100161 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000162 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100163 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000164 graph_->SetEntryBlock(entry_block_);
165 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000166
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000167 InitializeLocals(code_item.registers_size_);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100168 graph_->UpdateMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000169
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000170 // To avoid splitting blocks, we compute ahead of time the instructions that
171 // start a new block, and create these blocks.
172 ComputeBranchTargets(code_ptr, code_end);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000173
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000174 // Also create blocks for catch handlers.
175 if (code_item.tries_size_ != 0) {
176 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
177 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
178 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
179 CatchHandlerIterator iterator(handlers_ptr);
180 for (; iterator.HasNext(); iterator.Next()) {
181 uint32_t address = iterator.GetHandlerAddress();
182 HBasicBlock* block = FindBlockStartingAt(address);
183 if (block == nullptr) {
184 block = new (arena_) HBasicBlock(graph_, address);
185 branch_targets_.Put(address, block);
186 }
187 block->SetIsCatchBlock();
188 }
189 handlers_ptr = iterator.EndDataPointer();
190 }
191 }
192
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000193 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100194
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000195 size_t dex_offset = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000196 while (code_ptr < code_end) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000197 // Update the current block if dex_offset starts a new block.
198 MaybeUpdateCurrentBlock(dex_offset);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000199 const Instruction& instruction = *Instruction::At(code_ptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000200 if (!AnalyzeDexInstruction(instruction, dex_offset)) return nullptr;
201 dex_offset += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000202 code_ptr += instruction.SizeInCodeUnits();
203 }
204
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000205 // Add the exit block at the end to give it the highest id.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000206 graph_->AddBlock(exit_block_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000207 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000208 // Add the suspend check to the entry block.
209 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000210 entry_block_->AddInstruction(new (arena_) HGoto());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000211 return graph_;
212}
213
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000214void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) {
215 HBasicBlock* block = FindBlockStartingAt(index);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000216 if (block == nullptr) {
217 return;
218 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000219
220 if (current_block_ != nullptr) {
221 // Branching instructions clear current_block, so we know
222 // the last instruction of the current block is not a branching
223 // instruction. We add an unconditional goto to the found block.
224 current_block_->AddInstruction(new (arena_) HGoto());
225 current_block_->AddSuccessor(block);
226 }
227 graph_->AddBlock(block);
228 current_block_ = block;
229}
230
231void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr, const uint16_t* code_end) {
232 // TODO: Support switch instructions.
233 branch_targets_.SetSize(code_end - code_ptr);
234
235 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100236 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000237 branch_targets_.Put(0, block);
238 entry_block_->AddSuccessor(block);
239
240 // Iterate over all instructions and find branching instructions. Create blocks for
241 // the locations these instructions branch to.
242 size_t dex_offset = 0;
243 while (code_ptr < code_end) {
244 const Instruction& instruction = *Instruction::At(code_ptr);
245 if (instruction.IsBranch()) {
246 int32_t target = instruction.GetTargetOffset() + dex_offset;
247 // Create a block for the target instruction.
248 if (FindBlockStartingAt(target) == nullptr) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100249 block = new (arena_) HBasicBlock(graph_, target);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000250 branch_targets_.Put(target, block);
251 }
252 dex_offset += instruction.SizeInCodeUnits();
253 code_ptr += instruction.SizeInCodeUnits();
254 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_offset) == nullptr)) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100255 block = new (arena_) HBasicBlock(graph_, dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000256 branch_targets_.Put(dex_offset, block);
257 }
258 } else {
259 code_ptr += instruction.SizeInCodeUnits();
260 dex_offset += instruction.SizeInCodeUnits();
261 }
262 }
263}
264
265HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const {
266 DCHECK_GE(index, 0);
267 return branch_targets_.Get(index);
268}
269
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100270template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100271void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
272 HInstruction* first = LoadLocal(instruction.VRegB(), type);
273 current_block_->AddInstruction(new (arena_) T(type, first));
274 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
275}
276
Roland Levillaindff1f282014-11-05 14:15:05 +0000277void HGraphBuilder::Conversion_12x(const Instruction& instruction,
278 Primitive::Type input_type,
279 Primitive::Type result_type) {
280 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
281 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first));
282 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
283}
284
Roland Levillain88cb1752014-10-20 16:36:47 +0100285template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100286void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100287 HInstruction* first = LoadLocal(instruction.VRegB(), type);
288 HInstruction* second = LoadLocal(instruction.VRegC(), type);
289 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100290 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
291}
292
293template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100294void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
295 HInstruction* first = LoadLocal(instruction.VRegA(), type);
296 HInstruction* second = LoadLocal(instruction.VRegB(), type);
297 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100298 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
299}
300
301template<typename T>
302void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100303 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
304 HInstruction* second = GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100305 if (reverse) {
306 std::swap(first, second);
307 }
308 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
309 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
310}
311
312template<typename T>
313void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100314 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
315 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100316 if (reverse) {
317 std::swap(first, second);
318 }
319 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
320 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
321}
322
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100323void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
324 if (type == Primitive::kPrimVoid) {
325 current_block_->AddInstruction(new (arena_) HReturnVoid());
326 } else {
327 HInstruction* value = LoadLocal(instruction.VRegA(), type);
328 current_block_->AddInstruction(new (arena_) HReturn(value));
329 }
330 current_block_->AddSuccessor(exit_block_);
331 current_block_ = nullptr;
332}
333
334bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
335 uint32_t dex_offset,
336 uint32_t method_idx,
337 uint32_t number_of_vreg_arguments,
338 bool is_range,
339 uint32_t* args,
340 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100341 Instruction::Code opcode = instruction.Opcode();
342 InvokeType invoke_type;
343 switch (opcode) {
344 case Instruction::INVOKE_STATIC:
345 case Instruction::INVOKE_STATIC_RANGE:
346 invoke_type = kStatic;
347 break;
348 case Instruction::INVOKE_DIRECT:
349 case Instruction::INVOKE_DIRECT_RANGE:
350 invoke_type = kDirect;
351 break;
352 case Instruction::INVOKE_VIRTUAL:
353 case Instruction::INVOKE_VIRTUAL_RANGE:
354 invoke_type = kVirtual;
355 break;
356 case Instruction::INVOKE_INTERFACE:
357 case Instruction::INVOKE_INTERFACE_RANGE:
358 invoke_type = kInterface;
359 break;
360 case Instruction::INVOKE_SUPER_RANGE:
361 case Instruction::INVOKE_SUPER:
362 invoke_type = kSuper;
363 break;
364 default:
365 LOG(FATAL) << "Unexpected invoke op: " << opcode;
366 return false;
367 }
368
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100369 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
370 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
371 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
372 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100373 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100374 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
375
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100376 HInvoke* invoke = nullptr;
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000377 if (invoke_type == kVirtual || invoke_type == kInterface || invoke_type == kSuper) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100378 MethodReference target_method(dex_file_, method_idx);
379 uintptr_t direct_code;
380 uintptr_t direct_method;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000381 int table_index;
382 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100383 compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_offset, true, true,
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000384 &optimized_invoke_type, &target_method, &table_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100385 &direct_code, &direct_method);
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000386 if (table_index == -1) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100387 return false;
388 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000389
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000390 if (optimized_invoke_type == kVirtual) {
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000391 invoke = new (arena_) HInvokeVirtual(
392 arena_, number_of_arguments, return_type, dex_offset, table_index);
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000393 } else if (optimized_invoke_type == kInterface) {
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000394 invoke = new (arena_) HInvokeInterface(
395 arena_, number_of_arguments, return_type, dex_offset, method_idx, table_index);
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000396 } else if (optimized_invoke_type == kDirect) {
397 // For this compiler, sharpening only works if we compile PIC.
398 DCHECK(compiler_driver_->GetCompilerOptions().GetCompilePic());
399 // Treat invoke-direct like static calls for now.
400 invoke = new (arena_) HInvokeStatic(
401 arena_, number_of_arguments, return_type, dex_offset, target_method.dex_method_index);
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000402 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100403 } else {
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000404 DCHECK(invoke_type == kDirect || invoke_type == kStatic);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100405 // Treat invoke-direct like static calls for now.
406 invoke = new (arena_) HInvokeStatic(
407 arena_, number_of_arguments, return_type, dex_offset, method_idx);
408 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100409
410 size_t start_index = 0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100411 Temporaries temps(graph_, is_instance_call ? 1 : 0);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100412 if (is_instance_call) {
413 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100414 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_offset);
415 current_block_->AddInstruction(null_check);
416 temps.Add(null_check);
417 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100418 start_index = 1;
419 }
420
421 uint32_t descriptor_index = 1;
422 uint32_t argument_index = start_index;
423 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
424 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100425 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
426 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100427 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
428 << " at " << dex_offset;
429 // We do not implement non sequential register pair.
430 return false;
431 }
432 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
433 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100434 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100435 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100436 }
437 }
438
439 DCHECK_EQ(argument_index, number_of_arguments);
440 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100441 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100442 return true;
443}
444
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100445bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
446 uint32_t dex_offset,
447 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100448 uint32_t source_or_dest_reg = instruction.VRegA_22c();
449 uint32_t obj_reg = instruction.VRegB_22c();
450 uint16_t field_index = instruction.VRegC_22c();
451
452 ScopedObjectAccess soa(Thread::Current());
453 StackHandleScope<1> hs(soa.Self());
454 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
455 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
456
457 if (resolved_field.Get() == nullptr) {
458 return false;
459 }
460 if (resolved_field->IsVolatile()) {
461 return false;
462 }
463
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100464 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100465
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100466 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
467 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_offset));
468 if (is_put) {
469 Temporaries temps(graph_, 1);
470 HInstruction* null_check = current_block_->GetLastInstruction();
471 // We need one temporary for the null check.
472 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100473 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100474 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
475 null_check,
476 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100477 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100478 resolved_field->GetOffset()));
479 } else {
480 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
481 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100482 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100483 resolved_field->GetOffset()));
484
485 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
486 }
487 return true;
488}
489
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100490
491
492bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
493 uint32_t dex_offset,
494 bool is_put) {
495 uint32_t source_or_dest_reg = instruction.VRegA_21c();
496 uint16_t field_index = instruction.VRegB_21c();
497
498 uint32_t storage_index;
499 bool is_referrers_class;
500 bool is_initialized;
501 bool is_volatile;
502 MemberOffset field_offset(0u);
503 Primitive::Type field_type;
504
505 bool fast_path = compiler_driver_->ComputeStaticFieldInfo(field_index,
506 dex_compilation_unit_,
507 is_put,
508 &field_offset,
509 &storage_index,
510 &is_referrers_class,
511 &is_volatile,
512 &is_initialized,
513 &field_type);
514 if (!fast_path) {
515 return false;
516 }
517
518 if (is_volatile) {
519 return false;
520 }
521
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100522 HLoadClass* constant = new (arena_) HLoadClass(
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000523 storage_index, is_referrers_class, dex_offset);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100524 current_block_->AddInstruction(constant);
525
526 HInstruction* cls = constant;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000527 if (!is_initialized) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100528 cls = new (arena_) HClinitCheck(constant, dex_offset);
529 current_block_->AddInstruction(cls);
530 }
531
532 if (is_put) {
533 // We need to keep the class alive before loading the value.
534 Temporaries temps(graph_, 1);
535 temps.Add(cls);
536 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
537 DCHECK_EQ(value->GetType(), field_type);
538 current_block_->AddInstruction(
539 new (arena_) HStaticFieldSet(cls, value, field_type, field_offset));
540 } else {
541 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls, field_type, field_offset));
542 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
543 }
544 return true;
545}
546
Calin Juravle865fc882014-11-06 17:09:03 +0000547void HGraphBuilder::BuildCheckedDiv(uint16_t out_reg,
548 uint16_t first_reg,
Calin Juravle0f00db72014-11-06 18:19:23 +0000549 int32_t second_reg,
Calin Juravled0d48522014-11-04 16:40:20 +0000550 uint32_t dex_offset,
551 Primitive::Type type,
552 bool second_is_lit) {
553 DCHECK(type == Primitive::kPrimInt);
554
Calin Juravle865fc882014-11-06 17:09:03 +0000555 HInstruction* first = LoadLocal(first_reg, type);
556 HInstruction* second = second_is_lit ? GetIntConstant(second_reg) : LoadLocal(second_reg, type);
Calin Juravled0d48522014-11-04 16:40:20 +0000557 if (!second->IsIntConstant() || (second->AsIntConstant()->GetValue() == 0)) {
558 second = new (arena_) HDivZeroCheck(second, dex_offset);
559 Temporaries temps(graph_, 1);
560 current_block_->AddInstruction(second);
561 temps.Add(current_block_->GetLastInstruction());
562 }
563
564 current_block_->AddInstruction(new (arena_) HDiv(type, first, second));
Calin Juravle865fc882014-11-06 17:09:03 +0000565 UpdateLocal(out_reg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +0000566}
567
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100568void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
569 uint32_t dex_offset,
570 bool is_put,
571 Primitive::Type anticipated_type) {
572 uint8_t source_or_dest_reg = instruction.VRegA_23x();
573 uint8_t array_reg = instruction.VRegB_23x();
574 uint8_t index_reg = instruction.VRegC_23x();
575
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100576 // We need one temporary for the null check, one for the index, and one for the length.
577 Temporaries temps(graph_, 3);
578
579 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
580 object = new (arena_) HNullCheck(object, dex_offset);
581 current_block_->AddInstruction(object);
582 temps.Add(object);
583
584 HInstruction* length = new (arena_) HArrayLength(object);
585 current_block_->AddInstruction(length);
586 temps.Add(length);
587 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
588 index = new (arena_) HBoundsCheck(index, length, dex_offset);
589 current_block_->AddInstruction(index);
590 temps.Add(index);
591 if (is_put) {
592 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
593 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100594 current_block_->AddInstruction(new (arena_) HArraySet(
595 object, index, value, anticipated_type, dex_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100596 } else {
597 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
598 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
599 }
600}
601
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100602void HGraphBuilder::BuildFilledNewArray(uint32_t dex_offset,
603 uint32_t type_index,
604 uint32_t number_of_vreg_arguments,
605 bool is_range,
606 uint32_t* args,
607 uint32_t register_index) {
608 HInstruction* length = GetIntConstant(number_of_vreg_arguments);
609 HInstruction* object = new (arena_) HNewArray(length, dex_offset, type_index);
610 current_block_->AddInstruction(object);
611
612 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
613 DCHECK_EQ(descriptor[0], '[') << descriptor;
614 char primitive = descriptor[1];
615 DCHECK(primitive == 'I'
616 || primitive == 'L'
617 || primitive == '[') << descriptor;
618 bool is_reference_array = (primitive == 'L') || (primitive == '[');
619 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
620
621 Temporaries temps(graph_, 1);
622 temps.Add(object);
623 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
624 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
625 HInstruction* index = GetIntConstant(i);
626 current_block_->AddInstruction(
627 new (arena_) HArraySet(object, index, value, type, dex_offset));
628 }
629 latest_result_ = object;
630}
631
632template <typename T>
633void HGraphBuilder::BuildFillArrayData(HInstruction* object,
634 const T* data,
635 uint32_t element_count,
636 Primitive::Type anticipated_type,
637 uint32_t dex_offset) {
638 for (uint32_t i = 0; i < element_count; ++i) {
639 HInstruction* index = GetIntConstant(i);
640 HInstruction* value = GetIntConstant(data[i]);
641 current_block_->AddInstruction(new (arena_) HArraySet(
642 object, index, value, anticipated_type, dex_offset));
643 }
644}
645
Calin Juravled0d48522014-11-04 16:40:20 +0000646void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_offset) {
647 Temporaries temps(graph_, 1);
648 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
649 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_offset);
650 current_block_->AddInstruction(null_check);
651 temps.Add(null_check);
652
653 HInstruction* length = new (arena_) HArrayLength(null_check);
654 current_block_->AddInstruction(length);
655
656 int32_t payload_offset = instruction.VRegB_31t() + dex_offset;
657 const Instruction::ArrayDataPayload* payload =
658 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
659 const uint8_t* data = payload->data;
660 uint32_t element_count = payload->element_count;
661
662 // Implementation of this DEX instruction seems to be that the bounds check is
663 // done before doing any stores.
664 HInstruction* last_index = GetIntConstant(payload->element_count - 1);
665 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_offset));
666
667 switch (payload->element_width) {
668 case 1:
669 BuildFillArrayData(null_check,
670 reinterpret_cast<const int8_t*>(data),
671 element_count,
672 Primitive::kPrimByte,
673 dex_offset);
674 break;
675 case 2:
676 BuildFillArrayData(null_check,
677 reinterpret_cast<const int16_t*>(data),
678 element_count,
679 Primitive::kPrimShort,
680 dex_offset);
681 break;
682 case 4:
683 BuildFillArrayData(null_check,
684 reinterpret_cast<const int32_t*>(data),
685 element_count,
686 Primitive::kPrimInt,
687 dex_offset);
688 break;
689 case 8:
690 BuildFillWideArrayData(null_check,
691 reinterpret_cast<const int64_t*>(data),
692 element_count,
693 dex_offset);
694 break;
695 default:
696 LOG(FATAL) << "Unknown element width for " << payload->element_width;
697 }
698}
699
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100700void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100701 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100702 uint32_t element_count,
703 uint32_t dex_offset) {
704 for (uint32_t i = 0; i < element_count; ++i) {
705 HInstruction* index = GetIntConstant(i);
706 HInstruction* value = GetLongConstant(data[i]);
707 current_block_->AddInstruction(new (arena_) HArraySet(
708 object, index, value, Primitive::kPrimLong, dex_offset));
709 }
710}
711
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000712bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
713 uint8_t destination,
714 uint8_t reference,
715 uint16_t type_index,
716 uint32_t dex_offset) {
717 bool type_known_final;
718 bool type_known_abstract;
719 bool is_referrers_class;
720 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
721 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
722 &type_known_final, &type_known_abstract, &is_referrers_class);
723 if (!can_access) {
724 return false;
725 }
726 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
727 HLoadClass* cls = new (arena_) HLoadClass(type_index, is_referrers_class, dex_offset);
728 current_block_->AddInstruction(cls);
729 // The class needs a temporary before being used by the type check.
730 Temporaries temps(graph_, 1);
731 temps.Add(cls);
732 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
733 current_block_->AddInstruction(
734 new (arena_) HInstanceOf(object, cls, type_known_final, dex_offset));
735 UpdateLocal(destination, current_block_->GetLastInstruction());
736 } else {
737 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
738 current_block_->AddInstruction(
739 new (arena_) HCheckCast(object, cls, type_known_final, dex_offset));
740 }
741 return true;
742}
743
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000744void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset) {
745 if (target_offset <= 0) {
746 // Unconditionnally add a suspend check to backward branches. We can remove
747 // them after we recognize loops in the graph.
748 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_offset));
749 }
750}
751
752bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000753 if (current_block_ == nullptr) {
754 return true; // Dead code
755 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000756
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000757 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000758 case Instruction::CONST_4: {
759 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100760 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000761 UpdateLocal(register_index, constant);
762 break;
763 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000764
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100765 case Instruction::CONST_16: {
766 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100767 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
768 UpdateLocal(register_index, constant);
769 break;
770 }
771
Dave Allison20dfc792014-06-16 20:44:29 -0700772 case Instruction::CONST: {
773 int32_t register_index = instruction.VRegA();
774 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
775 UpdateLocal(register_index, constant);
776 break;
777 }
778
779 case Instruction::CONST_HIGH16: {
780 int32_t register_index = instruction.VRegA();
781 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
782 UpdateLocal(register_index, constant);
783 break;
784 }
785
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100786 case Instruction::CONST_WIDE_16: {
787 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700788 // Get 16 bits of constant value, sign extended to 64 bits.
789 int64_t value = instruction.VRegB_21s();
790 value <<= 48;
791 value >>= 48;
792 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100793 UpdateLocal(register_index, constant);
794 break;
795 }
796
797 case Instruction::CONST_WIDE_32: {
798 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700799 // Get 32 bits of constant value, sign extended to 64 bits.
800 int64_t value = instruction.VRegB_31i();
801 value <<= 32;
802 value >>= 32;
803 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100804 UpdateLocal(register_index, constant);
805 break;
806 }
807
808 case Instruction::CONST_WIDE: {
809 int32_t register_index = instruction.VRegA();
810 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100811 UpdateLocal(register_index, constant);
812 break;
813 }
814
Dave Allison20dfc792014-06-16 20:44:29 -0700815 case Instruction::CONST_WIDE_HIGH16: {
816 int32_t register_index = instruction.VRegA();
817 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
818 HLongConstant* constant = GetLongConstant(value);
819 UpdateLocal(register_index, constant);
820 break;
821 }
822
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000823 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -0700824 case Instruction::MOVE:
825 case Instruction::MOVE_FROM16:
826 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100827 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100828 UpdateLocal(instruction.VRegA(), value);
829 break;
830 }
831
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000832 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -0700833 case Instruction::MOVE_WIDE:
834 case Instruction::MOVE_WIDE_FROM16:
835 case Instruction::MOVE_WIDE_16: {
836 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
837 UpdateLocal(instruction.VRegA(), value);
838 break;
839 }
840
841 case Instruction::MOVE_OBJECT:
842 case Instruction::MOVE_OBJECT_16:
843 case Instruction::MOVE_OBJECT_FROM16: {
844 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
845 UpdateLocal(instruction.VRegA(), value);
846 break;
847 }
848
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000849 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100850 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000851 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000852 }
853
Dave Allison20dfc792014-06-16 20:44:29 -0700854#define IF_XX(comparison, cond) \
855 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_offset); break; \
856 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_offset); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100857
Dave Allison20dfc792014-06-16 20:44:29 -0700858 IF_XX(HEqual, EQ);
859 IF_XX(HNotEqual, NE);
860 IF_XX(HLessThan, LT);
861 IF_XX(HLessThanOrEqual, LE);
862 IF_XX(HGreaterThan, GT);
863 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000864
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000865 case Instruction::GOTO:
866 case Instruction::GOTO_16:
867 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000868 int32_t offset = instruction.GetTargetOffset();
869 PotentiallyAddSuspendCheck(offset, dex_offset);
870 HBasicBlock* target = FindBlockStartingAt(offset + dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000871 DCHECK(target != nullptr);
872 current_block_->AddInstruction(new (arena_) HGoto());
873 current_block_->AddSuccessor(target);
874 current_block_ = nullptr;
875 break;
876 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000877
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100878 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100879 DCHECK_NE(return_type_, Primitive::kPrimNot);
880 DCHECK_NE(return_type_, Primitive::kPrimLong);
881 DCHECK_NE(return_type_, Primitive::kPrimDouble);
882 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100883 break;
884 }
885
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100886 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100887 DCHECK(return_type_ == Primitive::kPrimNot);
888 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100889 break;
890 }
891
892 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100893 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
894 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000895 break;
896 }
897
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100898 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000899 case Instruction::INVOKE_INTERFACE:
900 case Instruction::INVOKE_STATIC:
901 case Instruction::INVOKE_SUPER:
902 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000903 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100904 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100905 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -0700906 instruction.GetVarArgs(args);
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000907 if (!BuildInvoke(instruction, dex_offset, method_idx,
908 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100909 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100910 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100911 break;
912 }
913
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100914 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000915 case Instruction::INVOKE_INTERFACE_RANGE:
916 case Instruction::INVOKE_STATIC_RANGE:
917 case Instruction::INVOKE_SUPER_RANGE:
918 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100919 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100920 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
921 uint32_t register_index = instruction.VRegC();
922 if (!BuildInvoke(instruction, dex_offset, method_idx,
923 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100924 return false;
925 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000926 break;
927 }
928
Roland Levillain88cb1752014-10-20 16:36:47 +0100929 case Instruction::NEG_INT: {
930 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
931 break;
932 }
933
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100934 case Instruction::NEG_LONG: {
935 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
936 break;
937 }
938
Roland Levillain3dbcb382014-10-28 17:30:07 +0000939 case Instruction::NEG_FLOAT: {
940 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
941 break;
942 }
943
944 case Instruction::NEG_DOUBLE: {
945 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
946 break;
947 }
948
Roland Levillain1cc5f252014-10-22 18:06:21 +0100949 case Instruction::NOT_INT: {
950 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
951 break;
952 }
953
Roland Levillain70566432014-10-24 16:20:17 +0100954 case Instruction::NOT_LONG: {
955 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
956 break;
957 }
958
Roland Levillaindff1f282014-11-05 14:15:05 +0000959 case Instruction::INT_TO_LONG: {
960 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong);
961 break;
962 }
963
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000964 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100965 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100966 break;
967 }
968
969 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100970 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100971 break;
972 }
973
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100974 case Instruction::ADD_DOUBLE: {
975 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
976 break;
977 }
978
979 case Instruction::ADD_FLOAT: {
980 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
981 break;
982 }
983
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100984 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100985 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100986 break;
987 }
988
989 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100990 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000991 break;
992 }
993
Calin Juravle096cc022014-10-23 17:01:13 +0100994 case Instruction::SUB_FLOAT: {
995 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
996 break;
997 }
998
999 case Instruction::SUB_DOUBLE: {
1000 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1001 break;
1002 }
1003
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001004 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001005 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1006 break;
1007 }
1008
Calin Juravle34bacdf2014-10-07 20:23:36 +01001009 case Instruction::MUL_INT: {
1010 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1011 break;
1012 }
1013
1014 case Instruction::MUL_LONG: {
1015 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1016 break;
1017 }
1018
Calin Juravleb5bfa962014-10-21 18:02:24 +01001019 case Instruction::MUL_FLOAT: {
1020 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1021 break;
1022 }
1023
1024 case Instruction::MUL_DOUBLE: {
1025 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1026 break;
1027 }
1028
Calin Juravled0d48522014-11-04 16:40:20 +00001029 case Instruction::DIV_INT: {
Calin Juravle865fc882014-11-06 17:09:03 +00001030 BuildCheckedDiv(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1031 dex_offset, Primitive::kPrimInt, false);
Calin Juravled0d48522014-11-04 16:40:20 +00001032 break;
1033 }
1034
Calin Juravle7c4954d2014-10-28 16:57:40 +00001035 case Instruction::DIV_FLOAT: {
1036 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat);
1037 break;
1038 }
1039
1040 case Instruction::DIV_DOUBLE: {
1041 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble);
1042 break;
1043 }
1044
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001045 case Instruction::ADD_LONG_2ADDR: {
1046 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001047 break;
1048 }
1049
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001050 case Instruction::ADD_DOUBLE_2ADDR: {
1051 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1052 break;
1053 }
1054
1055 case Instruction::ADD_FLOAT_2ADDR: {
1056 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
1057 break;
1058 }
1059
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001060 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001061 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
1062 break;
1063 }
1064
1065 case Instruction::SUB_LONG_2ADDR: {
1066 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001067 break;
1068 }
1069
Calin Juravle096cc022014-10-23 17:01:13 +01001070 case Instruction::SUB_FLOAT_2ADDR: {
1071 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
1072 break;
1073 }
1074
1075 case Instruction::SUB_DOUBLE_2ADDR: {
1076 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
1077 break;
1078 }
1079
Calin Juravle34bacdf2014-10-07 20:23:36 +01001080 case Instruction::MUL_INT_2ADDR: {
1081 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
1082 break;
1083 }
1084
1085 case Instruction::MUL_LONG_2ADDR: {
1086 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
1087 break;
1088 }
1089
Calin Juravleb5bfa962014-10-21 18:02:24 +01001090 case Instruction::MUL_FLOAT_2ADDR: {
1091 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
1092 break;
1093 }
1094
1095 case Instruction::MUL_DOUBLE_2ADDR: {
1096 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
1097 break;
1098 }
1099
Calin Juravle865fc882014-11-06 17:09:03 +00001100 case Instruction::DIV_INT_2ADDR: {
1101 BuildCheckedDiv(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1102 dex_offset, Primitive::kPrimInt, false);
1103 break;
1104 }
1105
Calin Juravle7c4954d2014-10-28 16:57:40 +00001106 case Instruction::DIV_FLOAT_2ADDR: {
1107 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat);
1108 break;
1109 }
1110
1111 case Instruction::DIV_DOUBLE_2ADDR: {
1112 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble);
1113 break;
1114 }
1115
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001116 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001117 Binop_22s<HAdd>(instruction, false);
1118 break;
1119 }
1120
1121 case Instruction::RSUB_INT: {
1122 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001123 break;
1124 }
1125
Calin Juravle34bacdf2014-10-07 20:23:36 +01001126 case Instruction::MUL_INT_LIT16: {
1127 Binop_22s<HMul>(instruction, false);
1128 break;
1129 }
1130
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001131 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001132 Binop_22b<HAdd>(instruction, false);
1133 break;
1134 }
1135
1136 case Instruction::RSUB_INT_LIT8: {
1137 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001138 break;
1139 }
1140
Calin Juravle34bacdf2014-10-07 20:23:36 +01001141 case Instruction::MUL_INT_LIT8: {
1142 Binop_22b<HMul>(instruction, false);
1143 break;
1144 }
1145
Calin Juravled0d48522014-11-04 16:40:20 +00001146 case Instruction::DIV_INT_LIT16:
1147 case Instruction::DIV_INT_LIT8: {
Calin Juravle865fc882014-11-06 17:09:03 +00001148 BuildCheckedDiv(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1149 dex_offset, Primitive::kPrimInt, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001150 break;
1151 }
1152
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001153 case Instruction::NEW_INSTANCE: {
1154 current_block_->AddInstruction(
1155 new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c()));
1156 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1157 break;
1158 }
1159
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001160 case Instruction::NEW_ARRAY: {
1161 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
1162 current_block_->AddInstruction(
1163 new (arena_) HNewArray(length, dex_offset, instruction.VRegC_22c()));
1164 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1165 break;
1166 }
1167
1168 case Instruction::FILLED_NEW_ARRAY: {
1169 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1170 uint32_t type_index = instruction.VRegB_35c();
1171 uint32_t args[5];
1172 instruction.GetVarArgs(args);
1173 BuildFilledNewArray(dex_offset, type_index, number_of_vreg_arguments, false, args, 0);
1174 break;
1175 }
1176
1177 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1178 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1179 uint32_t type_index = instruction.VRegB_3rc();
1180 uint32_t register_index = instruction.VRegC_3rc();
1181 BuildFilledNewArray(
1182 dex_offset, type_index, number_of_vreg_arguments, true, nullptr, register_index);
1183 break;
1184 }
1185
1186 case Instruction::FILL_ARRAY_DATA: {
Calin Juravled0d48522014-11-04 16:40:20 +00001187 BuildFillArrayData(instruction, dex_offset);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001188 break;
1189 }
1190
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001191 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001192 case Instruction::MOVE_RESULT_WIDE:
1193 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001194 UpdateLocal(instruction.VRegA(), latest_result_);
1195 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001196 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001197
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001198 case Instruction::CMP_LONG: {
1199 Binop_23x<HCompare>(instruction, Primitive::kPrimLong);
1200 break;
1201 }
1202
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001203 case Instruction::NOP:
1204 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001205
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001206 case Instruction::IGET:
1207 case Instruction::IGET_WIDE:
1208 case Instruction::IGET_OBJECT:
1209 case Instruction::IGET_BOOLEAN:
1210 case Instruction::IGET_BYTE:
1211 case Instruction::IGET_CHAR:
1212 case Instruction::IGET_SHORT: {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001213 if (!BuildInstanceFieldAccess(instruction, dex_offset, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001214 return false;
1215 }
1216 break;
1217 }
1218
1219 case Instruction::IPUT:
1220 case Instruction::IPUT_WIDE:
1221 case Instruction::IPUT_OBJECT:
1222 case Instruction::IPUT_BOOLEAN:
1223 case Instruction::IPUT_BYTE:
1224 case Instruction::IPUT_CHAR:
1225 case Instruction::IPUT_SHORT: {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001226 if (!BuildInstanceFieldAccess(instruction, dex_offset, true)) {
1227 return false;
1228 }
1229 break;
1230 }
1231
1232 case Instruction::SGET:
1233 case Instruction::SGET_WIDE:
1234 case Instruction::SGET_OBJECT:
1235 case Instruction::SGET_BOOLEAN:
1236 case Instruction::SGET_BYTE:
1237 case Instruction::SGET_CHAR:
1238 case Instruction::SGET_SHORT: {
1239 if (!BuildStaticFieldAccess(instruction, dex_offset, false)) {
1240 return false;
1241 }
1242 break;
1243 }
1244
1245 case Instruction::SPUT:
1246 case Instruction::SPUT_WIDE:
1247 case Instruction::SPUT_OBJECT:
1248 case Instruction::SPUT_BOOLEAN:
1249 case Instruction::SPUT_BYTE:
1250 case Instruction::SPUT_CHAR:
1251 case Instruction::SPUT_SHORT: {
1252 if (!BuildStaticFieldAccess(instruction, dex_offset, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001253 return false;
1254 }
1255 break;
1256 }
1257
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001258#define ARRAY_XX(kind, anticipated_type) \
1259 case Instruction::AGET##kind: { \
1260 BuildArrayAccess(instruction, dex_offset, false, anticipated_type); \
1261 break; \
1262 } \
1263 case Instruction::APUT##kind: { \
1264 BuildArrayAccess(instruction, dex_offset, true, anticipated_type); \
1265 break; \
1266 }
1267
1268 ARRAY_XX(, Primitive::kPrimInt);
1269 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1270 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1271 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1272 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1273 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1274 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1275
Nicolas Geoffray39468442014-09-02 15:17:15 +01001276 case Instruction::ARRAY_LENGTH: {
1277 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001278 // No need for a temporary for the null check, it is the only input of the following
1279 // instruction.
1280 object = new (arena_) HNullCheck(object, dex_offset);
1281 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001282 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1283 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1284 break;
1285 }
1286
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001287 case Instruction::CONST_STRING: {
1288 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_offset));
1289 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1290 break;
1291 }
1292
1293 case Instruction::CONST_STRING_JUMBO: {
1294 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_offset));
1295 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
1296 break;
1297 }
1298
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001299 case Instruction::CONST_CLASS: {
1300 uint16_t type_index = instruction.VRegB_21c();
1301 bool type_known_final;
1302 bool type_known_abstract;
1303 bool is_referrers_class;
1304 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1305 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
1306 &type_known_final, &type_known_abstract, &is_referrers_class);
1307 if (!can_access) {
1308 return false;
1309 }
1310 current_block_->AddInstruction(
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001311 new (arena_) HLoadClass(type_index, is_referrers_class, dex_offset));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001312 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1313 break;
1314 }
1315
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001316 case Instruction::MOVE_EXCEPTION: {
1317 current_block_->AddInstruction(new (arena_) HLoadException());
1318 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
1319 break;
1320 }
1321
1322 case Instruction::THROW: {
1323 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
1324 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_offset));
1325 // A throw instruction must branch to the exit block.
1326 current_block_->AddSuccessor(exit_block_);
1327 // We finished building this block. Set the current block to null to avoid
1328 // adding dead instructions to it.
1329 current_block_ = nullptr;
1330 break;
1331 }
1332
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001333 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001334 uint8_t destination = instruction.VRegA_22c();
1335 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001336 uint16_t type_index = instruction.VRegC_22c();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001337 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_offset)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001338 return false;
1339 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001340 break;
1341 }
1342
1343 case Instruction::CHECK_CAST: {
1344 uint8_t reference = instruction.VRegA_21c();
1345 uint16_t type_index = instruction.VRegB_21c();
1346 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_offset)) {
1347 return false;
1348 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001349 break;
1350 }
1351
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001352 default:
1353 return false;
1354 }
1355 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001356} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001357
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001358HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001359 if (constant0_ != nullptr) {
1360 return constant0_;
1361 }
1362 constant0_ = new(arena_) HIntConstant(0);
1363 entry_block_->AddInstruction(constant0_);
1364 return constant0_;
1365}
1366
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001367HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001368 if (constant1_ != nullptr) {
1369 return constant1_;
1370 }
1371 constant1_ = new(arena_) HIntConstant(1);
1372 entry_block_->AddInstruction(constant1_);
1373 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001374}
1375
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001376HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001377 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001378 case 0: return GetIntConstant0();
1379 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001380 default: {
1381 HIntConstant* instruction = new (arena_) HIntConstant(constant);
1382 entry_block_->AddInstruction(instruction);
1383 return instruction;
1384 }
1385 }
1386}
1387
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001388HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
1389 HLongConstant* instruction = new (arena_) HLongConstant(constant);
1390 entry_block_->AddInstruction(instruction);
1391 return instruction;
1392}
1393
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001394HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
1395 return locals_.Get(register_index);
1396}
1397
1398void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
1399 HLocal* local = GetLocalAt(register_index);
1400 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
1401}
1402
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001403HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001404 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001405 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001406 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001407}
1408
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001409} // namespace art