blob: a03588f4fd20cc76a802954caa58d37ebaec07e5 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 *
3 * Copyright (C) 2014 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Nicolas Geoffraye5038322014-07-04 09:41:32 +010018#include "builder.h"
19
20#include "class_linker.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000021#include "dex_file.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000022#include "dex_file-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "dex_instruction.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000024#include "dex_instruction-inl.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010025#include "driver/compiler_driver-inl.h"
26#include "mirror/art_field.h"
27#include "mirror/art_field-inl.h"
28#include "mirror/class_loader.h"
29#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000031#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010032#include "scoped_thread_state_change.h"
33#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000034
35namespace art {
36
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010037/**
38 * Helper class to add HTemporary instructions. This class is used when
39 * converting a DEX instruction to multiple HInstruction, and where those
40 * instructions do not die at the following instruction, but instead spans
41 * multiple instructions.
42 */
43class Temporaries : public ValueObject {
44 public:
45 Temporaries(HGraph* graph, size_t count) : graph_(graph), count_(count), index_(0) {
46 graph_->UpdateNumberOfTemporaries(count_);
47 }
48
49 void Add(HInstruction* instruction) {
50 // We currently only support vreg size temps.
51 DCHECK(instruction->GetType() != Primitive::kPrimLong
52 && instruction->GetType() != Primitive::kPrimDouble);
53 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_++);
54 instruction->GetBlock()->AddInstruction(temp);
55 DCHECK(temp->GetPrevious() == instruction);
56 }
57
58 private:
59 HGraph* const graph_;
60
61 // The total number of temporaries that will be used.
62 const size_t count_;
63
64 // Current index in the temporary stack, updated by `Add`.
65 size_t index_;
66};
67
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +010068static bool IsTypeSupported(Primitive::Type type) {
69 return type != Primitive::kPrimFloat && type != Primitive::kPrimDouble;
70}
71
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010072void HGraphBuilder::InitializeLocals(uint16_t count) {
73 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000074 locals_.SetSize(count);
75 for (int i = 0; i < count; i++) {
76 HLocal* local = new (arena_) HLocal(i);
77 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000078 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000079 }
80}
81
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010082bool HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
83 // dex_compilation_unit_ is null only when unit testing.
84 if (dex_compilation_unit_ == nullptr) {
85 return true;
86 }
87
88 graph_->SetNumberOfInVRegs(number_of_parameters);
89 const char* shorty = dex_compilation_unit_->GetShorty();
90 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010091 int parameter_index = 0;
92
93 if (!dex_compilation_unit_->IsStatic()) {
94 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010095 HParameterValue* parameter =
96 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010097 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010098 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010099 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100100 number_of_parameters--;
101 }
102
103 uint32_t pos = 1;
104 for (int i = 0; i < number_of_parameters; i++) {
105 switch (shorty[pos++]) {
106 case 'F':
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100107 case 'D': {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100108 return false;
109 }
110
111 default: {
112 // integer and reference parameters.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100113 HParameterValue* parameter =
114 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos - 1]));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100115 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100116 HLocal* local = GetLocalAt(locals_index++);
117 // Store the parameter value in the local that the dex code will use
118 // to reference that parameter.
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100119 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100120 if (parameter->GetType() == Primitive::kPrimLong) {
121 i++;
122 locals_index++;
123 parameter_index++;
124 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100125 break;
126 }
127 }
128 }
129 return true;
130}
131
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000132static bool CanHandleCodeItem(const DexFile::CodeItem& code_item) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000133 if (code_item.tries_size_ > 0) {
134 return false;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000135 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000136 return true;
137}
138
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100139template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100140void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000141 int32_t target_offset = instruction.GetTargetOffset();
142 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100143 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
144 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700145 T* comparison = new (arena_) T(first, second);
146 current_block_->AddInstruction(comparison);
147 HInstruction* ifinst = new (arena_) HIf(comparison);
148 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700150 DCHECK(target != nullptr);
151 current_block_->AddSuccessor(target);
152 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
153 DCHECK(target != nullptr);
154 current_block_->AddSuccessor(target);
155 current_block_ = nullptr;
156}
157
158template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100159void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000160 int32_t target_offset = instruction.GetTargetOffset();
161 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700162 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
163 T* comparison = new (arena_) T(value, GetIntConstant(0));
164 current_block_->AddInstruction(comparison);
165 HInstruction* ifinst = new (arena_) HIf(comparison);
166 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100168 DCHECK(target != nullptr);
169 current_block_->AddSuccessor(target);
170 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
171 DCHECK(target != nullptr);
172 current_block_->AddSuccessor(target);
173 current_block_ = nullptr;
174}
175
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000176HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000177 if (!CanHandleCodeItem(code_item)) {
178 return nullptr;
179 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000180
181 const uint16_t* code_ptr = code_item.insns_;
182 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
183
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000184 // Setup the graph with the entry block and exit block.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000185 graph_ = new (arena_) HGraph(arena_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000186 entry_block_ = new (arena_) HBasicBlock(graph_);
187 graph_->AddBlock(entry_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000188 exit_block_ = new (arena_) HBasicBlock(graph_);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000189 graph_->SetEntryBlock(entry_block_);
190 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000191
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000192 InitializeLocals(code_item.registers_size_);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100193 graph_->UpdateMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000194
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000195 // To avoid splitting blocks, we compute ahead of time the instructions that
196 // start a new block, and create these blocks.
197 ComputeBranchTargets(code_ptr, code_end);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000198
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100199 if (!InitializeParameters(code_item.ins_size_)) {
200 return nullptr;
201 }
202
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000203 size_t dex_offset = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000204 while (code_ptr < code_end) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000205 // Update the current block if dex_offset starts a new block.
206 MaybeUpdateCurrentBlock(dex_offset);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000207 const Instruction& instruction = *Instruction::At(code_ptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000208 if (!AnalyzeDexInstruction(instruction, dex_offset)) return nullptr;
209 dex_offset += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000210 code_ptr += instruction.SizeInCodeUnits();
211 }
212
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000213 // Add the exit block at the end to give it the highest id.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000214 graph_->AddBlock(exit_block_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000215 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000216 // Add the suspend check to the entry block.
217 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000218 entry_block_->AddInstruction(new (arena_) HGoto());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000219 return graph_;
220}
221
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000222void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) {
223 HBasicBlock* block = FindBlockStartingAt(index);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000224 if (block == nullptr) {
225 return;
226 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000227
228 if (current_block_ != nullptr) {
229 // Branching instructions clear current_block, so we know
230 // the last instruction of the current block is not a branching
231 // instruction. We add an unconditional goto to the found block.
232 current_block_->AddInstruction(new (arena_) HGoto());
233 current_block_->AddSuccessor(block);
234 }
235 graph_->AddBlock(block);
236 current_block_ = block;
237}
238
239void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr, const uint16_t* code_end) {
240 // TODO: Support switch instructions.
241 branch_targets_.SetSize(code_end - code_ptr);
242
243 // Create the first block for the dex instructions, single successor of the entry block.
244 HBasicBlock* block = new (arena_) HBasicBlock(graph_);
245 branch_targets_.Put(0, block);
246 entry_block_->AddSuccessor(block);
247
248 // Iterate over all instructions and find branching instructions. Create blocks for
249 // the locations these instructions branch to.
250 size_t dex_offset = 0;
251 while (code_ptr < code_end) {
252 const Instruction& instruction = *Instruction::At(code_ptr);
253 if (instruction.IsBranch()) {
254 int32_t target = instruction.GetTargetOffset() + dex_offset;
255 // Create a block for the target instruction.
256 if (FindBlockStartingAt(target) == nullptr) {
257 block = new (arena_) HBasicBlock(graph_);
258 branch_targets_.Put(target, block);
259 }
260 dex_offset += instruction.SizeInCodeUnits();
261 code_ptr += instruction.SizeInCodeUnits();
262 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_offset) == nullptr)) {
263 block = new (arena_) HBasicBlock(graph_);
264 branch_targets_.Put(dex_offset, block);
265 }
266 } else {
267 code_ptr += instruction.SizeInCodeUnits();
268 dex_offset += instruction.SizeInCodeUnits();
269 }
270 }
271}
272
273HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const {
274 DCHECK_GE(index, 0);
275 return branch_targets_.Get(index);
276}
277
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100278template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100279void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100280 HInstruction* first = LoadLocal(instruction.VRegB(), type);
281 HInstruction* second = LoadLocal(instruction.VRegC(), type);
282 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100283 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
284}
285
286template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100287void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
288 HInstruction* first = LoadLocal(instruction.VRegA(), type);
289 HInstruction* second = LoadLocal(instruction.VRegB(), type);
290 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100291 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
292}
293
294template<typename T>
295void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100296 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
297 HInstruction* second = GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100298 if (reverse) {
299 std::swap(first, second);
300 }
301 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
302 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
303}
304
305template<typename T>
306void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100307 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
308 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100309 if (reverse) {
310 std::swap(first, second);
311 }
312 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
313 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
314}
315
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100316void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
317 if (type == Primitive::kPrimVoid) {
318 current_block_->AddInstruction(new (arena_) HReturnVoid());
319 } else {
320 HInstruction* value = LoadLocal(instruction.VRegA(), type);
321 current_block_->AddInstruction(new (arena_) HReturn(value));
322 }
323 current_block_->AddSuccessor(exit_block_);
324 current_block_ = nullptr;
325}
326
327bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
328 uint32_t dex_offset,
329 uint32_t method_idx,
330 uint32_t number_of_vreg_arguments,
331 bool is_range,
332 uint32_t* args,
333 uint32_t register_index) {
334 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
335 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
336 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
337 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
338 bool is_instance_call =
339 instruction.Opcode() != Instruction::INVOKE_STATIC
340 && instruction.Opcode() != Instruction::INVOKE_STATIC_RANGE;
341 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
342
343 // Treat invoke-direct like static calls for now.
344 HInvoke* invoke = new (arena_) HInvokeStatic(
345 arena_, number_of_arguments, return_type, dex_offset, method_idx);
346
347 size_t start_index = 0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100348 Temporaries temps(graph_, is_instance_call ? 1 : 0);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100349 if (is_instance_call) {
350 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100351 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_offset);
352 current_block_->AddInstruction(null_check);
353 temps.Add(null_check);
354 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100355 start_index = 1;
356 }
357
358 uint32_t descriptor_index = 1;
359 uint32_t argument_index = start_index;
360 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
361 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100362 if (!IsTypeSupported(type)) {
363 return false;
364 }
365 if (!is_range && type == Primitive::kPrimLong && args[i] + 1 != args[i + 1]) {
366 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
367 << " at " << dex_offset;
368 // We do not implement non sequential register pair.
369 return false;
370 }
371 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
372 invoke->SetArgumentAt(argument_index, arg);
373 if (type == Primitive::kPrimLong) {
374 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100375 }
376 }
377
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100378 if (!IsTypeSupported(return_type)) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100379 return false;
380 }
381
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100382 DCHECK_EQ(argument_index, number_of_arguments);
383 current_block_->AddInstruction(invoke);
384 return true;
385}
386
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100387bool HGraphBuilder::BuildFieldAccess(const Instruction& instruction,
388 uint32_t dex_offset,
389 bool is_put) {
390 uint32_t source_or_dest_reg = instruction.VRegA_22c();
391 uint32_t obj_reg = instruction.VRegB_22c();
392 uint16_t field_index = instruction.VRegC_22c();
393
394 ScopedObjectAccess soa(Thread::Current());
395 StackHandleScope<1> hs(soa.Self());
396 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
397 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
398
399 if (resolved_field.Get() == nullptr) {
400 return false;
401 }
402 if (resolved_field->IsVolatile()) {
403 return false;
404 }
405
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100406 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
407 if (!IsTypeSupported(field_type)) {
408 return false;
409 }
410
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100411 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
412 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_offset));
413 if (is_put) {
414 Temporaries temps(graph_, 1);
415 HInstruction* null_check = current_block_->GetLastInstruction();
416 // We need one temporary for the null check.
417 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100418 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100419 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
420 null_check,
421 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100422 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100423 resolved_field->GetOffset()));
424 } else {
425 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
426 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100427 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100428 resolved_field->GetOffset()));
429
430 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
431 }
432 return true;
433}
434
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100435void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
436 uint32_t dex_offset,
437 bool is_put,
438 Primitive::Type anticipated_type) {
439 uint8_t source_or_dest_reg = instruction.VRegA_23x();
440 uint8_t array_reg = instruction.VRegB_23x();
441 uint8_t index_reg = instruction.VRegC_23x();
442
443 DCHECK(IsTypeSupported(anticipated_type));
444
445 // We need one temporary for the null check, one for the index, and one for the length.
446 Temporaries temps(graph_, 3);
447
448 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
449 object = new (arena_) HNullCheck(object, dex_offset);
450 current_block_->AddInstruction(object);
451 temps.Add(object);
452
453 HInstruction* length = new (arena_) HArrayLength(object);
454 current_block_->AddInstruction(length);
455 temps.Add(length);
456 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
457 index = new (arena_) HBoundsCheck(index, length, dex_offset);
458 current_block_->AddInstruction(index);
459 temps.Add(index);
460 if (is_put) {
461 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
462 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100463 current_block_->AddInstruction(new (arena_) HArraySet(
464 object, index, value, anticipated_type, dex_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100465 } else {
466 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
467 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
468 }
469}
470
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000471void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset) {
472 if (target_offset <= 0) {
473 // Unconditionnally add a suspend check to backward branches. We can remove
474 // them after we recognize loops in the graph.
475 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_offset));
476 }
477}
478
479bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000480 if (current_block_ == nullptr) {
481 return true; // Dead code
482 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000483
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000484 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000485 case Instruction::CONST_4: {
486 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100487 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000488 UpdateLocal(register_index, constant);
489 break;
490 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000491
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100492 case Instruction::CONST_16: {
493 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100494 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
495 UpdateLocal(register_index, constant);
496 break;
497 }
498
Dave Allison20dfc792014-06-16 20:44:29 -0700499 case Instruction::CONST: {
500 int32_t register_index = instruction.VRegA();
501 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
502 UpdateLocal(register_index, constant);
503 break;
504 }
505
506 case Instruction::CONST_HIGH16: {
507 int32_t register_index = instruction.VRegA();
508 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
509 UpdateLocal(register_index, constant);
510 break;
511 }
512
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100513 case Instruction::CONST_WIDE_16: {
514 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700515 // Get 16 bits of constant value, sign extended to 64 bits.
516 int64_t value = instruction.VRegB_21s();
517 value <<= 48;
518 value >>= 48;
519 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100520 UpdateLocal(register_index, constant);
521 break;
522 }
523
524 case Instruction::CONST_WIDE_32: {
525 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700526 // Get 32 bits of constant value, sign extended to 64 bits.
527 int64_t value = instruction.VRegB_31i();
528 value <<= 32;
529 value >>= 32;
530 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100531 UpdateLocal(register_index, constant);
532 break;
533 }
534
535 case Instruction::CONST_WIDE: {
536 int32_t register_index = instruction.VRegA();
537 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100538 UpdateLocal(register_index, constant);
539 break;
540 }
541
Dave Allison20dfc792014-06-16 20:44:29 -0700542 case Instruction::CONST_WIDE_HIGH16: {
543 int32_t register_index = instruction.VRegA();
544 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
545 HLongConstant* constant = GetLongConstant(value);
546 UpdateLocal(register_index, constant);
547 break;
548 }
549
550 // TODO: these instructions are also used to move floating point values, so what is
551 // the type (int or float)?
552 case Instruction::MOVE:
553 case Instruction::MOVE_FROM16:
554 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100555 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100556 UpdateLocal(instruction.VRegA(), value);
557 break;
558 }
559
Dave Allison20dfc792014-06-16 20:44:29 -0700560 // TODO: these instructions are also used to move floating point values, so what is
561 // the type (long or double)?
562 case Instruction::MOVE_WIDE:
563 case Instruction::MOVE_WIDE_FROM16:
564 case Instruction::MOVE_WIDE_16: {
565 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
566 UpdateLocal(instruction.VRegA(), value);
567 break;
568 }
569
570 case Instruction::MOVE_OBJECT:
571 case Instruction::MOVE_OBJECT_16:
572 case Instruction::MOVE_OBJECT_FROM16: {
573 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
574 UpdateLocal(instruction.VRegA(), value);
575 break;
576 }
577
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000578 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100579 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000580 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000581 }
582
Dave Allison20dfc792014-06-16 20:44:29 -0700583#define IF_XX(comparison, cond) \
584 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_offset); break; \
585 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_offset); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100586
Dave Allison20dfc792014-06-16 20:44:29 -0700587 IF_XX(HEqual, EQ);
588 IF_XX(HNotEqual, NE);
589 IF_XX(HLessThan, LT);
590 IF_XX(HLessThanOrEqual, LE);
591 IF_XX(HGreaterThan, GT);
592 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000593
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000594 case Instruction::GOTO:
595 case Instruction::GOTO_16:
596 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000597 int32_t offset = instruction.GetTargetOffset();
598 PotentiallyAddSuspendCheck(offset, dex_offset);
599 HBasicBlock* target = FindBlockStartingAt(offset + dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000600 DCHECK(target != nullptr);
601 current_block_->AddInstruction(new (arena_) HGoto());
602 current_block_->AddSuccessor(target);
603 current_block_ = nullptr;
604 break;
605 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000606
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100607 case Instruction::RETURN: {
608 BuildReturn(instruction, Primitive::kPrimInt);
609 break;
610 }
611
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100612 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100613 BuildReturn(instruction, Primitive::kPrimNot);
614 break;
615 }
616
617 case Instruction::RETURN_WIDE: {
618 BuildReturn(instruction, Primitive::kPrimLong);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000619 break;
620 }
621
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100622 case Instruction::INVOKE_STATIC:
623 case Instruction::INVOKE_DIRECT: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000624 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100625 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100626 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -0700627 instruction.GetVarArgs(args);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 if (!BuildInvoke(instruction, dex_offset, method_idx, number_of_vreg_arguments, false, args, -1)) {
629 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100630 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100631 break;
632 }
633
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100634 case Instruction::INVOKE_STATIC_RANGE:
635 case Instruction::INVOKE_DIRECT_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100636 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100637 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
638 uint32_t register_index = instruction.VRegC();
639 if (!BuildInvoke(instruction, dex_offset, method_idx,
640 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100641 return false;
642 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000643 break;
644 }
645
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000646 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100647 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100648 break;
649 }
650
651 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100652 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100653 break;
654 }
655
656 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100657 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100658 break;
659 }
660
661 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100662 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000663 break;
664 }
665
666 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100667 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
668 break;
669 }
670
671 case Instruction::ADD_LONG_2ADDR: {
672 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100673 break;
674 }
675
676 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100677 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
678 break;
679 }
680
681 case Instruction::SUB_LONG_2ADDR: {
682 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000683 break;
684 }
685
686 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100687 Binop_22s<HAdd>(instruction, false);
688 break;
689 }
690
691 case Instruction::RSUB_INT: {
692 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000693 break;
694 }
695
696 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100697 Binop_22b<HAdd>(instruction, false);
698 break;
699 }
700
701 case Instruction::RSUB_INT_LIT8: {
702 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000703 break;
704 }
705
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100706 case Instruction::NEW_INSTANCE: {
707 current_block_->AddInstruction(
708 new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c()));
709 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
710 break;
711 }
712
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100713 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -0700714 case Instruction::MOVE_RESULT_WIDE:
715 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100716 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
717 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100718
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100719 case Instruction::CMP_LONG: {
720 Binop_23x<HCompare>(instruction, Primitive::kPrimLong);
721 break;
722 }
723
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000724 case Instruction::NOP:
725 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000726
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100727 case Instruction::IGET:
728 case Instruction::IGET_WIDE:
729 case Instruction::IGET_OBJECT:
730 case Instruction::IGET_BOOLEAN:
731 case Instruction::IGET_BYTE:
732 case Instruction::IGET_CHAR:
733 case Instruction::IGET_SHORT: {
734 if (!BuildFieldAccess(instruction, dex_offset, false)) {
735 return false;
736 }
737 break;
738 }
739
740 case Instruction::IPUT:
741 case Instruction::IPUT_WIDE:
742 case Instruction::IPUT_OBJECT:
743 case Instruction::IPUT_BOOLEAN:
744 case Instruction::IPUT_BYTE:
745 case Instruction::IPUT_CHAR:
746 case Instruction::IPUT_SHORT: {
747 if (!BuildFieldAccess(instruction, dex_offset, true)) {
748 return false;
749 }
750 break;
751 }
752
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100753#define ARRAY_XX(kind, anticipated_type) \
754 case Instruction::AGET##kind: { \
755 BuildArrayAccess(instruction, dex_offset, false, anticipated_type); \
756 break; \
757 } \
758 case Instruction::APUT##kind: { \
759 BuildArrayAccess(instruction, dex_offset, true, anticipated_type); \
760 break; \
761 }
762
763 ARRAY_XX(, Primitive::kPrimInt);
764 ARRAY_XX(_WIDE, Primitive::kPrimLong);
765 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
766 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
767 ARRAY_XX(_BYTE, Primitive::kPrimByte);
768 ARRAY_XX(_CHAR, Primitive::kPrimChar);
769 ARRAY_XX(_SHORT, Primitive::kPrimShort);
770
Nicolas Geoffray39468442014-09-02 15:17:15 +0100771 case Instruction::ARRAY_LENGTH: {
772 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
773 current_block_->AddInstruction(new (arena_) HArrayLength(object));
774 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
775 break;
776 }
777
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000778 default:
779 return false;
780 }
781 return true;
782}
783
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100784HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000785 if (constant0_ != nullptr) {
786 return constant0_;
787 }
788 constant0_ = new(arena_) HIntConstant(0);
789 entry_block_->AddInstruction(constant0_);
790 return constant0_;
791}
792
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100793HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000794 if (constant1_ != nullptr) {
795 return constant1_;
796 }
797 constant1_ = new(arena_) HIntConstant(1);
798 entry_block_->AddInstruction(constant1_);
799 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000800}
801
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100802HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000803 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100804 case 0: return GetIntConstant0();
805 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000806 default: {
807 HIntConstant* instruction = new (arena_) HIntConstant(constant);
808 entry_block_->AddInstruction(instruction);
809 return instruction;
810 }
811 }
812}
813
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100814HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
815 HLongConstant* instruction = new (arena_) HLongConstant(constant);
816 entry_block_->AddInstruction(instruction);
817 return instruction;
818}
819
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000820HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
821 return locals_.Get(register_index);
822}
823
824void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
825 HLocal* local = GetLocalAt(register_index);
826 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
827}
828
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100829HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000830 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100831 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000832 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000833}
834
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000835} // namespace art