blob: a3ca061512bc1306b335865f9e80b5284f106683 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Nicolas Geoffraye5038322014-07-04 09:41:32 +010017#include "builder.h"
18
19#include "class_linker.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000020#include "dex_file.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000021#include "dex_file-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000022#include "dex_instruction.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000023#include "dex_instruction-inl.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010024#include "driver/compiler_driver-inl.h"
25#include "mirror/art_field.h"
26#include "mirror/art_field-inl.h"
27#include "mirror/class_loader.h"
28#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000029#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000030#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010031#include "scoped_thread_state_change.h"
32#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033
34namespace art {
35
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036/**
37 * Helper class to add HTemporary instructions. This class is used when
38 * converting a DEX instruction to multiple HInstruction, and where those
39 * instructions do not die at the following instruction, but instead spans
40 * multiple instructions.
41 */
42class Temporaries : public ValueObject {
43 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000044 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010045
46 void Add(HInstruction* instruction) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +000047 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000049
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000051
52 size_t offset;
53 if (instruction->GetType() == Primitive::kPrimLong
54 || instruction->GetType() == Primitive::kPrimDouble) {
55 offset = 2;
56 } else {
57 offset = 1;
58 }
59 index_ += offset;
60
61 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010062 }
63
64 private:
65 HGraph* const graph_;
66
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010067 // Current index in the temporary stack, updated by `Add`.
68 size_t index_;
69};
70
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010071void HGraphBuilder::InitializeLocals(uint16_t count) {
72 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000073 locals_.SetSize(count);
74 for (int i = 0; i < count; i++) {
75 HLocal* local = new (arena_) HLocal(i);
76 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000077 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000078 }
79}
80
Nicolas Geoffray52e832b2014-11-06 15:15:31 +000081void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010082 // dex_compilation_unit_ is null only when unit testing.
83 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +000084 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010085 }
86
87 graph_->SetNumberOfInVRegs(number_of_parameters);
88 const char* shorty = dex_compilation_unit_->GetShorty();
89 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010090 int parameter_index = 0;
91
92 if (!dex_compilation_unit_->IsStatic()) {
93 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010094 HParameterValue* parameter =
95 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010096 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010097 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010098 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010099 number_of_parameters--;
100 }
101
102 uint32_t pos = 1;
103 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100104 HParameterValue* parameter =
105 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
106 entry_block_->AddInstruction(parameter);
107 HLocal* local = GetLocalAt(locals_index++);
108 // Store the parameter value in the local that the dex code will use
109 // to reference that parameter.
110 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
111 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
112 || (parameter->GetType() == Primitive::kPrimDouble);
113 if (is_wide) {
114 i++;
115 locals_index++;
116 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100117 }
118 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100119}
120
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100121template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100122void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000123 int32_t target_offset = instruction.GetTargetOffset();
124 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100125 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
126 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700127 T* comparison = new (arena_) T(first, second);
128 current_block_->AddInstruction(comparison);
129 HInstruction* ifinst = new (arena_) HIf(comparison);
130 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000131 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700132 DCHECK(target != nullptr);
133 current_block_->AddSuccessor(target);
134 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
135 DCHECK(target != nullptr);
136 current_block_->AddSuccessor(target);
137 current_block_ = nullptr;
138}
139
140template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100141void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 int32_t target_offset = instruction.GetTargetOffset();
143 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700144 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
145 T* comparison = new (arena_) T(value, GetIntConstant(0));
146 current_block_->AddInstruction(comparison);
147 HInstruction* ifinst = new (arena_) HIf(comparison);
148 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100150 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
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000158HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000159 const uint16_t* code_ptr = code_item.insns_;
160 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100161 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000162
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000163 // Setup the graph with the entry block and exit block.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000164 graph_ = new (arena_) HGraph(arena_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100165 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000166 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100167 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000168 graph_->SetEntryBlock(entry_block_);
169 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000170
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000171 InitializeLocals(code_item.registers_size_);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100172 graph_->UpdateMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000173
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000174 // To avoid splitting blocks, we compute ahead of time the instructions that
175 // start a new block, and create these blocks.
176 ComputeBranchTargets(code_ptr, code_end);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000177
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000178 // Also create blocks for catch handlers.
179 if (code_item.tries_size_ != 0) {
180 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
181 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
182 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
183 CatchHandlerIterator iterator(handlers_ptr);
184 for (; iterator.HasNext(); iterator.Next()) {
185 uint32_t address = iterator.GetHandlerAddress();
186 HBasicBlock* block = FindBlockStartingAt(address);
187 if (block == nullptr) {
188 block = new (arena_) HBasicBlock(graph_, address);
189 branch_targets_.Put(address, block);
190 }
191 block->SetIsCatchBlock();
192 }
193 handlers_ptr = iterator.EndDataPointer();
194 }
195 }
196
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000197 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100198
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000199 size_t dex_offset = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000200 while (code_ptr < code_end) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000201 // Update the current block if dex_offset starts a new block.
202 MaybeUpdateCurrentBlock(dex_offset);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000203 const Instruction& instruction = *Instruction::At(code_ptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000204 if (!AnalyzeDexInstruction(instruction, dex_offset)) return nullptr;
205 dex_offset += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000206 code_ptr += instruction.SizeInCodeUnits();
207 }
208
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000209 // Add the exit block at the end to give it the highest id.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000210 graph_->AddBlock(exit_block_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000211 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000212 // Add the suspend check to the entry block.
213 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000214 entry_block_->AddInstruction(new (arena_) HGoto());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000215 return graph_;
216}
217
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000218void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) {
219 HBasicBlock* block = FindBlockStartingAt(index);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000220 if (block == nullptr) {
221 return;
222 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000223
224 if (current_block_ != nullptr) {
225 // Branching instructions clear current_block, so we know
226 // the last instruction of the current block is not a branching
227 // instruction. We add an unconditional goto to the found block.
228 current_block_->AddInstruction(new (arena_) HGoto());
229 current_block_->AddSuccessor(block);
230 }
231 graph_->AddBlock(block);
232 current_block_ = block;
233}
234
235void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr, const uint16_t* code_end) {
236 // TODO: Support switch instructions.
237 branch_targets_.SetSize(code_end - code_ptr);
238
239 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100240 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000241 branch_targets_.Put(0, block);
242 entry_block_->AddSuccessor(block);
243
244 // Iterate over all instructions and find branching instructions. Create blocks for
245 // the locations these instructions branch to.
246 size_t dex_offset = 0;
247 while (code_ptr < code_end) {
248 const Instruction& instruction = *Instruction::At(code_ptr);
249 if (instruction.IsBranch()) {
250 int32_t target = instruction.GetTargetOffset() + dex_offset;
251 // Create a block for the target instruction.
252 if (FindBlockStartingAt(target) == nullptr) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100253 block = new (arena_) HBasicBlock(graph_, target);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000254 branch_targets_.Put(target, block);
255 }
256 dex_offset += instruction.SizeInCodeUnits();
257 code_ptr += instruction.SizeInCodeUnits();
258 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_offset) == nullptr)) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100259 block = new (arena_) HBasicBlock(graph_, dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000260 branch_targets_.Put(dex_offset, block);
261 }
262 } else {
263 code_ptr += instruction.SizeInCodeUnits();
264 dex_offset += instruction.SizeInCodeUnits();
265 }
266 }
267}
268
269HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const {
270 DCHECK_GE(index, 0);
271 return branch_targets_.Get(index);
272}
273
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100274template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100275void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
276 HInstruction* first = LoadLocal(instruction.VRegB(), type);
277 current_block_->AddInstruction(new (arena_) T(type, first));
278 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
279}
280
Roland Levillaindff1f282014-11-05 14:15:05 +0000281void HGraphBuilder::Conversion_12x(const Instruction& instruction,
282 Primitive::Type input_type,
283 Primitive::Type result_type) {
284 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
285 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first));
286 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
287}
288
Roland Levillain88cb1752014-10-20 16:36:47 +0100289template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100290void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100291 HInstruction* first = LoadLocal(instruction.VRegB(), type);
292 HInstruction* second = LoadLocal(instruction.VRegC(), type);
293 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100294 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
295}
296
297template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100298void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
299 HInstruction* first = LoadLocal(instruction.VRegA(), type);
300 HInstruction* second = LoadLocal(instruction.VRegB(), type);
301 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100302 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
303}
304
305template<typename T>
306void HGraphBuilder::Binop_22s(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_22s());
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
316template<typename T>
317void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100318 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
319 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100320 if (reverse) {
321 std::swap(first, second);
322 }
323 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
324 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
325}
326
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100327void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
328 if (type == Primitive::kPrimVoid) {
329 current_block_->AddInstruction(new (arena_) HReturnVoid());
330 } else {
331 HInstruction* value = LoadLocal(instruction.VRegA(), type);
332 current_block_->AddInstruction(new (arena_) HReturn(value));
333 }
334 current_block_->AddSuccessor(exit_block_);
335 current_block_ = nullptr;
336}
337
338bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
339 uint32_t dex_offset,
340 uint32_t method_idx,
341 uint32_t number_of_vreg_arguments,
342 bool is_range,
343 uint32_t* args,
344 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100345 Instruction::Code opcode = instruction.Opcode();
346 InvokeType invoke_type;
347 switch (opcode) {
348 case Instruction::INVOKE_STATIC:
349 case Instruction::INVOKE_STATIC_RANGE:
350 invoke_type = kStatic;
351 break;
352 case Instruction::INVOKE_DIRECT:
353 case Instruction::INVOKE_DIRECT_RANGE:
354 invoke_type = kDirect;
355 break;
356 case Instruction::INVOKE_VIRTUAL:
357 case Instruction::INVOKE_VIRTUAL_RANGE:
358 invoke_type = kVirtual;
359 break;
360 case Instruction::INVOKE_INTERFACE:
361 case Instruction::INVOKE_INTERFACE_RANGE:
362 invoke_type = kInterface;
363 break;
364 case Instruction::INVOKE_SUPER_RANGE:
365 case Instruction::INVOKE_SUPER:
366 invoke_type = kSuper;
367 break;
368 default:
369 LOG(FATAL) << "Unexpected invoke op: " << opcode;
370 return false;
371 }
372
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100373 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
374 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
375 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
376 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100377 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100378 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
379
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100380 HInvoke* invoke = nullptr;
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000381 if (invoke_type == kVirtual || invoke_type == kInterface || invoke_type == kSuper) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100382 MethodReference target_method(dex_file_, method_idx);
383 uintptr_t direct_code;
384 uintptr_t direct_method;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000385 int table_index;
386 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100387 compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_offset, true, true,
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000388 &optimized_invoke_type, &target_method, &table_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100389 &direct_code, &direct_method);
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000390 if (table_index == -1) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100391 return false;
392 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000393
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000394 if (optimized_invoke_type == kVirtual) {
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000395 invoke = new (arena_) HInvokeVirtual(
396 arena_, number_of_arguments, return_type, dex_offset, table_index);
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000397 } else if (optimized_invoke_type == kInterface) {
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000398 invoke = new (arena_) HInvokeInterface(
399 arena_, number_of_arguments, return_type, dex_offset, method_idx, table_index);
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000400 } else if (optimized_invoke_type == kDirect) {
401 // For this compiler, sharpening only works if we compile PIC.
402 DCHECK(compiler_driver_->GetCompilerOptions().GetCompilePic());
403 // Treat invoke-direct like static calls for now.
404 invoke = new (arena_) HInvokeStatic(
405 arena_, number_of_arguments, return_type, dex_offset, target_method.dex_method_index);
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000406 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100407 } else {
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000408 DCHECK(invoke_type == kDirect || invoke_type == kStatic);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100409 // Treat invoke-direct like static calls for now.
410 invoke = new (arena_) HInvokeStatic(
411 arena_, number_of_arguments, return_type, dex_offset, method_idx);
412 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100413
414 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000415 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100416 if (is_instance_call) {
417 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100418 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_offset);
419 current_block_->AddInstruction(null_check);
420 temps.Add(null_check);
421 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100422 start_index = 1;
423 }
424
425 uint32_t descriptor_index = 1;
426 uint32_t argument_index = start_index;
427 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
428 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100429 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
430 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100431 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
432 << " at " << dex_offset;
433 // We do not implement non sequential register pair.
434 return false;
435 }
436 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
437 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100438 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100439 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100440 }
441 }
442
443 DCHECK_EQ(argument_index, number_of_arguments);
444 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100445 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100446 return true;
447}
448
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100449bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
450 uint32_t dex_offset,
451 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100452 uint32_t source_or_dest_reg = instruction.VRegA_22c();
453 uint32_t obj_reg = instruction.VRegB_22c();
454 uint16_t field_index = instruction.VRegC_22c();
455
456 ScopedObjectAccess soa(Thread::Current());
457 StackHandleScope<1> hs(soa.Self());
458 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
459 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
460
461 if (resolved_field.Get() == nullptr) {
462 return false;
463 }
464 if (resolved_field->IsVolatile()) {
465 return false;
466 }
467
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100468 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100469
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100470 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
471 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_offset));
472 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000473 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100474 HInstruction* null_check = current_block_->GetLastInstruction();
475 // We need one temporary for the null check.
476 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100477 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100478 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
479 null_check,
480 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100481 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100482 resolved_field->GetOffset()));
483 } else {
484 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
485 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100486 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100487 resolved_field->GetOffset()));
488
489 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
490 }
491 return true;
492}
493
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100494
495
496bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
497 uint32_t dex_offset,
498 bool is_put) {
499 uint32_t source_or_dest_reg = instruction.VRegA_21c();
500 uint16_t field_index = instruction.VRegB_21c();
501
502 uint32_t storage_index;
503 bool is_referrers_class;
504 bool is_initialized;
505 bool is_volatile;
506 MemberOffset field_offset(0u);
507 Primitive::Type field_type;
508
509 bool fast_path = compiler_driver_->ComputeStaticFieldInfo(field_index,
510 dex_compilation_unit_,
511 is_put,
512 &field_offset,
513 &storage_index,
514 &is_referrers_class,
515 &is_volatile,
516 &is_initialized,
517 &field_type);
518 if (!fast_path) {
519 return false;
520 }
521
522 if (is_volatile) {
523 return false;
524 }
525
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100526 HLoadClass* constant = new (arena_) HLoadClass(
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000527 storage_index, is_referrers_class, dex_offset);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100528 current_block_->AddInstruction(constant);
529
530 HInstruction* cls = constant;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000531 if (!is_initialized) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100532 cls = new (arena_) HClinitCheck(constant, dex_offset);
533 current_block_->AddInstruction(cls);
534 }
535
536 if (is_put) {
537 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000538 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100539 temps.Add(cls);
540 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
541 DCHECK_EQ(value->GetType(), field_type);
542 current_block_->AddInstruction(
543 new (arena_) HStaticFieldSet(cls, value, field_type, field_offset));
544 } else {
545 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls, field_type, field_offset));
546 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
547 }
548 return true;
549}
550
Calin Juravle865fc882014-11-06 17:09:03 +0000551void HGraphBuilder::BuildCheckedDiv(uint16_t out_reg,
552 uint16_t first_reg,
Calin Juravle0f00db72014-11-06 18:19:23 +0000553 int32_t second_reg,
Calin Juravled0d48522014-11-04 16:40:20 +0000554 uint32_t dex_offset,
555 Primitive::Type type,
556 bool second_is_lit) {
557 DCHECK(type == Primitive::kPrimInt);
558
Calin Juravle865fc882014-11-06 17:09:03 +0000559 HInstruction* first = LoadLocal(first_reg, type);
560 HInstruction* second = second_is_lit ? GetIntConstant(second_reg) : LoadLocal(second_reg, type);
Calin Juravled0d48522014-11-04 16:40:20 +0000561 if (!second->IsIntConstant() || (second->AsIntConstant()->GetValue() == 0)) {
562 second = new (arena_) HDivZeroCheck(second, dex_offset);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000563 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000564 current_block_->AddInstruction(second);
565 temps.Add(current_block_->GetLastInstruction());
566 }
567
568 current_block_->AddInstruction(new (arena_) HDiv(type, first, second));
Calin Juravle865fc882014-11-06 17:09:03 +0000569 UpdateLocal(out_reg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +0000570}
571
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100572void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
573 uint32_t dex_offset,
574 bool is_put,
575 Primitive::Type anticipated_type) {
576 uint8_t source_or_dest_reg = instruction.VRegA_23x();
577 uint8_t array_reg = instruction.VRegB_23x();
578 uint8_t index_reg = instruction.VRegC_23x();
579
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100580 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000581 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100582
583 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
584 object = new (arena_) HNullCheck(object, dex_offset);
585 current_block_->AddInstruction(object);
586 temps.Add(object);
587
588 HInstruction* length = new (arena_) HArrayLength(object);
589 current_block_->AddInstruction(length);
590 temps.Add(length);
591 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
592 index = new (arena_) HBoundsCheck(index, length, dex_offset);
593 current_block_->AddInstruction(index);
594 temps.Add(index);
595 if (is_put) {
596 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
597 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100598 current_block_->AddInstruction(new (arena_) HArraySet(
599 object, index, value, anticipated_type, dex_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100600 } else {
601 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
602 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
603 }
604}
605
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100606void HGraphBuilder::BuildFilledNewArray(uint32_t dex_offset,
607 uint32_t type_index,
608 uint32_t number_of_vreg_arguments,
609 bool is_range,
610 uint32_t* args,
611 uint32_t register_index) {
612 HInstruction* length = GetIntConstant(number_of_vreg_arguments);
613 HInstruction* object = new (arena_) HNewArray(length, dex_offset, type_index);
614 current_block_->AddInstruction(object);
615
616 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
617 DCHECK_EQ(descriptor[0], '[') << descriptor;
618 char primitive = descriptor[1];
619 DCHECK(primitive == 'I'
620 || primitive == 'L'
621 || primitive == '[') << descriptor;
622 bool is_reference_array = (primitive == 'L') || (primitive == '[');
623 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
624
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000625 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100626 temps.Add(object);
627 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
628 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
629 HInstruction* index = GetIntConstant(i);
630 current_block_->AddInstruction(
631 new (arena_) HArraySet(object, index, value, type, dex_offset));
632 }
633 latest_result_ = object;
634}
635
636template <typename T>
637void HGraphBuilder::BuildFillArrayData(HInstruction* object,
638 const T* data,
639 uint32_t element_count,
640 Primitive::Type anticipated_type,
641 uint32_t dex_offset) {
642 for (uint32_t i = 0; i < element_count; ++i) {
643 HInstruction* index = GetIntConstant(i);
644 HInstruction* value = GetIntConstant(data[i]);
645 current_block_->AddInstruction(new (arena_) HArraySet(
646 object, index, value, anticipated_type, dex_offset));
647 }
648}
649
Calin Juravled0d48522014-11-04 16:40:20 +0000650void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_offset) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000651 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000652 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
653 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_offset);
654 current_block_->AddInstruction(null_check);
655 temps.Add(null_check);
656
657 HInstruction* length = new (arena_) HArrayLength(null_check);
658 current_block_->AddInstruction(length);
659
660 int32_t payload_offset = instruction.VRegB_31t() + dex_offset;
661 const Instruction::ArrayDataPayload* payload =
662 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
663 const uint8_t* data = payload->data;
664 uint32_t element_count = payload->element_count;
665
666 // Implementation of this DEX instruction seems to be that the bounds check is
667 // done before doing any stores.
668 HInstruction* last_index = GetIntConstant(payload->element_count - 1);
669 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_offset));
670
671 switch (payload->element_width) {
672 case 1:
673 BuildFillArrayData(null_check,
674 reinterpret_cast<const int8_t*>(data),
675 element_count,
676 Primitive::kPrimByte,
677 dex_offset);
678 break;
679 case 2:
680 BuildFillArrayData(null_check,
681 reinterpret_cast<const int16_t*>(data),
682 element_count,
683 Primitive::kPrimShort,
684 dex_offset);
685 break;
686 case 4:
687 BuildFillArrayData(null_check,
688 reinterpret_cast<const int32_t*>(data),
689 element_count,
690 Primitive::kPrimInt,
691 dex_offset);
692 break;
693 case 8:
694 BuildFillWideArrayData(null_check,
695 reinterpret_cast<const int64_t*>(data),
696 element_count,
697 dex_offset);
698 break;
699 default:
700 LOG(FATAL) << "Unknown element width for " << payload->element_width;
701 }
702}
703
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100704void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100705 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100706 uint32_t element_count,
707 uint32_t dex_offset) {
708 for (uint32_t i = 0; i < element_count; ++i) {
709 HInstruction* index = GetIntConstant(i);
710 HInstruction* value = GetLongConstant(data[i]);
711 current_block_->AddInstruction(new (arena_) HArraySet(
712 object, index, value, Primitive::kPrimLong, dex_offset));
713 }
714}
715
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000716bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
717 uint8_t destination,
718 uint8_t reference,
719 uint16_t type_index,
720 uint32_t dex_offset) {
721 bool type_known_final;
722 bool type_known_abstract;
723 bool is_referrers_class;
724 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
725 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
726 &type_known_final, &type_known_abstract, &is_referrers_class);
727 if (!can_access) {
728 return false;
729 }
730 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
731 HLoadClass* cls = new (arena_) HLoadClass(type_index, is_referrers_class, dex_offset);
732 current_block_->AddInstruction(cls);
733 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000734 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000735 temps.Add(cls);
736 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
737 current_block_->AddInstruction(
738 new (arena_) HInstanceOf(object, cls, type_known_final, dex_offset));
739 UpdateLocal(destination, current_block_->GetLastInstruction());
740 } else {
741 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
742 current_block_->AddInstruction(
743 new (arena_) HCheckCast(object, cls, type_known_final, dex_offset));
744 }
745 return true;
746}
747
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000748void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset) {
749 if (target_offset <= 0) {
750 // Unconditionnally add a suspend check to backward branches. We can remove
751 // them after we recognize loops in the graph.
752 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_offset));
753 }
754}
755
756bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000757 if (current_block_ == nullptr) {
758 return true; // Dead code
759 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000760
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000761 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000762 case Instruction::CONST_4: {
763 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100764 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000765 UpdateLocal(register_index, constant);
766 break;
767 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000768
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100769 case Instruction::CONST_16: {
770 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100771 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
772 UpdateLocal(register_index, constant);
773 break;
774 }
775
Dave Allison20dfc792014-06-16 20:44:29 -0700776 case Instruction::CONST: {
777 int32_t register_index = instruction.VRegA();
778 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
779 UpdateLocal(register_index, constant);
780 break;
781 }
782
783 case Instruction::CONST_HIGH16: {
784 int32_t register_index = instruction.VRegA();
785 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
786 UpdateLocal(register_index, constant);
787 break;
788 }
789
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100790 case Instruction::CONST_WIDE_16: {
791 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700792 // Get 16 bits of constant value, sign extended to 64 bits.
793 int64_t value = instruction.VRegB_21s();
794 value <<= 48;
795 value >>= 48;
796 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100797 UpdateLocal(register_index, constant);
798 break;
799 }
800
801 case Instruction::CONST_WIDE_32: {
802 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700803 // Get 32 bits of constant value, sign extended to 64 bits.
804 int64_t value = instruction.VRegB_31i();
805 value <<= 32;
806 value >>= 32;
807 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100808 UpdateLocal(register_index, constant);
809 break;
810 }
811
812 case Instruction::CONST_WIDE: {
813 int32_t register_index = instruction.VRegA();
814 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100815 UpdateLocal(register_index, constant);
816 break;
817 }
818
Dave Allison20dfc792014-06-16 20:44:29 -0700819 case Instruction::CONST_WIDE_HIGH16: {
820 int32_t register_index = instruction.VRegA();
821 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
822 HLongConstant* constant = GetLongConstant(value);
823 UpdateLocal(register_index, constant);
824 break;
825 }
826
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000827 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -0700828 case Instruction::MOVE:
829 case Instruction::MOVE_FROM16:
830 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100831 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100832 UpdateLocal(instruction.VRegA(), value);
833 break;
834 }
835
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000836 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -0700837 case Instruction::MOVE_WIDE:
838 case Instruction::MOVE_WIDE_FROM16:
839 case Instruction::MOVE_WIDE_16: {
840 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
841 UpdateLocal(instruction.VRegA(), value);
842 break;
843 }
844
845 case Instruction::MOVE_OBJECT:
846 case Instruction::MOVE_OBJECT_16:
847 case Instruction::MOVE_OBJECT_FROM16: {
848 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
849 UpdateLocal(instruction.VRegA(), value);
850 break;
851 }
852
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000853 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100854 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000855 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000856 }
857
Dave Allison20dfc792014-06-16 20:44:29 -0700858#define IF_XX(comparison, cond) \
859 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_offset); break; \
860 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_offset); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100861
Dave Allison20dfc792014-06-16 20:44:29 -0700862 IF_XX(HEqual, EQ);
863 IF_XX(HNotEqual, NE);
864 IF_XX(HLessThan, LT);
865 IF_XX(HLessThanOrEqual, LE);
866 IF_XX(HGreaterThan, GT);
867 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000868
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000869 case Instruction::GOTO:
870 case Instruction::GOTO_16:
871 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000872 int32_t offset = instruction.GetTargetOffset();
873 PotentiallyAddSuspendCheck(offset, dex_offset);
874 HBasicBlock* target = FindBlockStartingAt(offset + dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000875 DCHECK(target != nullptr);
876 current_block_->AddInstruction(new (arena_) HGoto());
877 current_block_->AddSuccessor(target);
878 current_block_ = nullptr;
879 break;
880 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000881
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100882 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100883 DCHECK_NE(return_type_, Primitive::kPrimNot);
884 DCHECK_NE(return_type_, Primitive::kPrimLong);
885 DCHECK_NE(return_type_, Primitive::kPrimDouble);
886 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100887 break;
888 }
889
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100890 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100891 DCHECK(return_type_ == Primitive::kPrimNot);
892 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100893 break;
894 }
895
896 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100897 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
898 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000899 break;
900 }
901
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100902 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000903 case Instruction::INVOKE_INTERFACE:
904 case Instruction::INVOKE_STATIC:
905 case Instruction::INVOKE_SUPER:
906 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000907 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100908 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100909 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -0700910 instruction.GetVarArgs(args);
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000911 if (!BuildInvoke(instruction, dex_offset, method_idx,
912 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100913 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100914 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100915 break;
916 }
917
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100918 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000919 case Instruction::INVOKE_INTERFACE_RANGE:
920 case Instruction::INVOKE_STATIC_RANGE:
921 case Instruction::INVOKE_SUPER_RANGE:
922 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100923 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100924 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
925 uint32_t register_index = instruction.VRegC();
926 if (!BuildInvoke(instruction, dex_offset, method_idx,
927 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100928 return false;
929 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000930 break;
931 }
932
Roland Levillain88cb1752014-10-20 16:36:47 +0100933 case Instruction::NEG_INT: {
934 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
935 break;
936 }
937
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100938 case Instruction::NEG_LONG: {
939 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
940 break;
941 }
942
Roland Levillain3dbcb382014-10-28 17:30:07 +0000943 case Instruction::NEG_FLOAT: {
944 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
945 break;
946 }
947
948 case Instruction::NEG_DOUBLE: {
949 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
950 break;
951 }
952
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100953 case Instruction::NOT_INT: {
954 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
955 break;
956 }
957
Roland Levillain70566432014-10-24 16:20:17 +0100958 case Instruction::NOT_LONG: {
959 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
960 break;
961 }
962
Roland Levillaindff1f282014-11-05 14:15:05 +0000963 case Instruction::INT_TO_LONG: {
964 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong);
965 break;
966 }
967
Roland Levillain946e1432014-11-11 17:35:19 +0000968 case Instruction::LONG_TO_INT: {
969 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt);
970 break;
971 }
972
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000973 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100974 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100975 break;
976 }
977
978 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100979 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100980 break;
981 }
982
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100983 case Instruction::ADD_DOUBLE: {
984 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
985 break;
986 }
987
988 case Instruction::ADD_FLOAT: {
989 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
990 break;
991 }
992
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100993 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100994 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100995 break;
996 }
997
998 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100999 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001000 break;
1001 }
1002
Calin Juravle096cc022014-10-23 17:01:13 +01001003 case Instruction::SUB_FLOAT: {
1004 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1005 break;
1006 }
1007
1008 case Instruction::SUB_DOUBLE: {
1009 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1010 break;
1011 }
1012
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001013 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001014 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1015 break;
1016 }
1017
Calin Juravle34bacdf2014-10-07 20:23:36 +01001018 case Instruction::MUL_INT: {
1019 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1020 break;
1021 }
1022
1023 case Instruction::MUL_LONG: {
1024 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1025 break;
1026 }
1027
Calin Juravleb5bfa962014-10-21 18:02:24 +01001028 case Instruction::MUL_FLOAT: {
1029 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1030 break;
1031 }
1032
1033 case Instruction::MUL_DOUBLE: {
1034 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1035 break;
1036 }
1037
Calin Juravled0d48522014-11-04 16:40:20 +00001038 case Instruction::DIV_INT: {
Calin Juravle865fc882014-11-06 17:09:03 +00001039 BuildCheckedDiv(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1040 dex_offset, Primitive::kPrimInt, false);
Calin Juravled0d48522014-11-04 16:40:20 +00001041 break;
1042 }
1043
Calin Juravle7c4954d2014-10-28 16:57:40 +00001044 case Instruction::DIV_FLOAT: {
1045 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat);
1046 break;
1047 }
1048
1049 case Instruction::DIV_DOUBLE: {
1050 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble);
1051 break;
1052 }
1053
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001054 case Instruction::AND_INT: {
1055 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1056 break;
1057 }
1058
1059 case Instruction::AND_LONG: {
1060 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1061 break;
1062 }
1063
1064 case Instruction::OR_INT: {
1065 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1066 break;
1067 }
1068
1069 case Instruction::OR_LONG: {
1070 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1071 break;
1072 }
1073
1074 case Instruction::XOR_INT: {
1075 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1076 break;
1077 }
1078
1079 case Instruction::XOR_LONG: {
1080 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1081 break;
1082 }
1083
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001084 case Instruction::ADD_LONG_2ADDR: {
1085 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001086 break;
1087 }
1088
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001089 case Instruction::ADD_DOUBLE_2ADDR: {
1090 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1091 break;
1092 }
1093
1094 case Instruction::ADD_FLOAT_2ADDR: {
1095 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
1096 break;
1097 }
1098
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001099 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001100 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
1101 break;
1102 }
1103
1104 case Instruction::SUB_LONG_2ADDR: {
1105 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001106 break;
1107 }
1108
Calin Juravle096cc022014-10-23 17:01:13 +01001109 case Instruction::SUB_FLOAT_2ADDR: {
1110 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
1111 break;
1112 }
1113
1114 case Instruction::SUB_DOUBLE_2ADDR: {
1115 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
1116 break;
1117 }
1118
Calin Juravle34bacdf2014-10-07 20:23:36 +01001119 case Instruction::MUL_INT_2ADDR: {
1120 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
1121 break;
1122 }
1123
1124 case Instruction::MUL_LONG_2ADDR: {
1125 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
1126 break;
1127 }
1128
Calin Juravleb5bfa962014-10-21 18:02:24 +01001129 case Instruction::MUL_FLOAT_2ADDR: {
1130 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
1131 break;
1132 }
1133
1134 case Instruction::MUL_DOUBLE_2ADDR: {
1135 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
1136 break;
1137 }
1138
Calin Juravle865fc882014-11-06 17:09:03 +00001139 case Instruction::DIV_INT_2ADDR: {
1140 BuildCheckedDiv(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1141 dex_offset, Primitive::kPrimInt, false);
1142 break;
1143 }
1144
Calin Juravle7c4954d2014-10-28 16:57:40 +00001145 case Instruction::DIV_FLOAT_2ADDR: {
1146 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat);
1147 break;
1148 }
1149
1150 case Instruction::DIV_DOUBLE_2ADDR: {
1151 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble);
1152 break;
1153 }
1154
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001155 case Instruction::AND_INT_2ADDR: {
1156 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
1157 break;
1158 }
1159
1160 case Instruction::AND_LONG_2ADDR: {
1161 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
1162 break;
1163 }
1164
1165 case Instruction::OR_INT_2ADDR: {
1166 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
1167 break;
1168 }
1169
1170 case Instruction::OR_LONG_2ADDR: {
1171 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
1172 break;
1173 }
1174
1175 case Instruction::XOR_INT_2ADDR: {
1176 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
1177 break;
1178 }
1179
1180 case Instruction::XOR_LONG_2ADDR: {
1181 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
1182 break;
1183 }
1184
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001185 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001186 Binop_22s<HAdd>(instruction, false);
1187 break;
1188 }
1189
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001190 case Instruction::AND_INT_LIT16: {
1191 Binop_22s<HAnd>(instruction, false);
1192 break;
1193 }
1194
1195 case Instruction::OR_INT_LIT16: {
1196 Binop_22s<HOr>(instruction, false);
1197 break;
1198 }
1199
1200 case Instruction::XOR_INT_LIT16: {
1201 Binop_22s<HXor>(instruction, false);
1202 break;
1203 }
1204
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001205 case Instruction::RSUB_INT: {
1206 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001207 break;
1208 }
1209
Calin Juravle34bacdf2014-10-07 20:23:36 +01001210 case Instruction::MUL_INT_LIT16: {
1211 Binop_22s<HMul>(instruction, false);
1212 break;
1213 }
1214
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001215 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001216 Binop_22b<HAdd>(instruction, false);
1217 break;
1218 }
1219
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001220 case Instruction::AND_INT_LIT8: {
1221 Binop_22b<HAnd>(instruction, false);
1222 break;
1223 }
1224
1225 case Instruction::OR_INT_LIT8: {
1226 Binop_22b<HOr>(instruction, false);
1227 break;
1228 }
1229
1230 case Instruction::XOR_INT_LIT8: {
1231 Binop_22b<HXor>(instruction, false);
1232 break;
1233 }
1234
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001235 case Instruction::RSUB_INT_LIT8: {
1236 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001237 break;
1238 }
1239
Calin Juravle34bacdf2014-10-07 20:23:36 +01001240 case Instruction::MUL_INT_LIT8: {
1241 Binop_22b<HMul>(instruction, false);
1242 break;
1243 }
1244
Calin Juravled0d48522014-11-04 16:40:20 +00001245 case Instruction::DIV_INT_LIT16:
1246 case Instruction::DIV_INT_LIT8: {
Calin Juravle865fc882014-11-06 17:09:03 +00001247 BuildCheckedDiv(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1248 dex_offset, Primitive::kPrimInt, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001249 break;
1250 }
1251
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001252 case Instruction::NEW_INSTANCE: {
1253 current_block_->AddInstruction(
1254 new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c()));
1255 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1256 break;
1257 }
1258
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001259 case Instruction::NEW_ARRAY: {
1260 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
1261 current_block_->AddInstruction(
1262 new (arena_) HNewArray(length, dex_offset, instruction.VRegC_22c()));
1263 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1264 break;
1265 }
1266
1267 case Instruction::FILLED_NEW_ARRAY: {
1268 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1269 uint32_t type_index = instruction.VRegB_35c();
1270 uint32_t args[5];
1271 instruction.GetVarArgs(args);
1272 BuildFilledNewArray(dex_offset, type_index, number_of_vreg_arguments, false, args, 0);
1273 break;
1274 }
1275
1276 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1277 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1278 uint32_t type_index = instruction.VRegB_3rc();
1279 uint32_t register_index = instruction.VRegC_3rc();
1280 BuildFilledNewArray(
1281 dex_offset, type_index, number_of_vreg_arguments, true, nullptr, register_index);
1282 break;
1283 }
1284
1285 case Instruction::FILL_ARRAY_DATA: {
Calin Juravled0d48522014-11-04 16:40:20 +00001286 BuildFillArrayData(instruction, dex_offset);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001287 break;
1288 }
1289
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001290 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001291 case Instruction::MOVE_RESULT_WIDE:
1292 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001293 UpdateLocal(instruction.VRegA(), latest_result_);
1294 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001295 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001296
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001297 case Instruction::CMP_LONG: {
1298 Binop_23x<HCompare>(instruction, Primitive::kPrimLong);
1299 break;
1300 }
1301
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001302 case Instruction::NOP:
1303 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001304
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001305 case Instruction::IGET:
1306 case Instruction::IGET_WIDE:
1307 case Instruction::IGET_OBJECT:
1308 case Instruction::IGET_BOOLEAN:
1309 case Instruction::IGET_BYTE:
1310 case Instruction::IGET_CHAR:
1311 case Instruction::IGET_SHORT: {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001312 if (!BuildInstanceFieldAccess(instruction, dex_offset, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001313 return false;
1314 }
1315 break;
1316 }
1317
1318 case Instruction::IPUT:
1319 case Instruction::IPUT_WIDE:
1320 case Instruction::IPUT_OBJECT:
1321 case Instruction::IPUT_BOOLEAN:
1322 case Instruction::IPUT_BYTE:
1323 case Instruction::IPUT_CHAR:
1324 case Instruction::IPUT_SHORT: {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001325 if (!BuildInstanceFieldAccess(instruction, dex_offset, true)) {
1326 return false;
1327 }
1328 break;
1329 }
1330
1331 case Instruction::SGET:
1332 case Instruction::SGET_WIDE:
1333 case Instruction::SGET_OBJECT:
1334 case Instruction::SGET_BOOLEAN:
1335 case Instruction::SGET_BYTE:
1336 case Instruction::SGET_CHAR:
1337 case Instruction::SGET_SHORT: {
1338 if (!BuildStaticFieldAccess(instruction, dex_offset, false)) {
1339 return false;
1340 }
1341 break;
1342 }
1343
1344 case Instruction::SPUT:
1345 case Instruction::SPUT_WIDE:
1346 case Instruction::SPUT_OBJECT:
1347 case Instruction::SPUT_BOOLEAN:
1348 case Instruction::SPUT_BYTE:
1349 case Instruction::SPUT_CHAR:
1350 case Instruction::SPUT_SHORT: {
1351 if (!BuildStaticFieldAccess(instruction, dex_offset, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001352 return false;
1353 }
1354 break;
1355 }
1356
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001357#define ARRAY_XX(kind, anticipated_type) \
1358 case Instruction::AGET##kind: { \
1359 BuildArrayAccess(instruction, dex_offset, false, anticipated_type); \
1360 break; \
1361 } \
1362 case Instruction::APUT##kind: { \
1363 BuildArrayAccess(instruction, dex_offset, true, anticipated_type); \
1364 break; \
1365 }
1366
1367 ARRAY_XX(, Primitive::kPrimInt);
1368 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1369 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1370 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1371 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1372 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1373 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1374
Nicolas Geoffray39468442014-09-02 15:17:15 +01001375 case Instruction::ARRAY_LENGTH: {
1376 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001377 // No need for a temporary for the null check, it is the only input of the following
1378 // instruction.
1379 object = new (arena_) HNullCheck(object, dex_offset);
1380 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001381 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1382 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1383 break;
1384 }
1385
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001386 case Instruction::CONST_STRING: {
1387 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_offset));
1388 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1389 break;
1390 }
1391
1392 case Instruction::CONST_STRING_JUMBO: {
1393 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_offset));
1394 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
1395 break;
1396 }
1397
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001398 case Instruction::CONST_CLASS: {
1399 uint16_t type_index = instruction.VRegB_21c();
1400 bool type_known_final;
1401 bool type_known_abstract;
1402 bool is_referrers_class;
1403 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1404 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
1405 &type_known_final, &type_known_abstract, &is_referrers_class);
1406 if (!can_access) {
1407 return false;
1408 }
1409 current_block_->AddInstruction(
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001410 new (arena_) HLoadClass(type_index, is_referrers_class, dex_offset));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001411 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1412 break;
1413 }
1414
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001415 case Instruction::MOVE_EXCEPTION: {
1416 current_block_->AddInstruction(new (arena_) HLoadException());
1417 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
1418 break;
1419 }
1420
1421 case Instruction::THROW: {
1422 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
1423 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_offset));
1424 // A throw instruction must branch to the exit block.
1425 current_block_->AddSuccessor(exit_block_);
1426 // We finished building this block. Set the current block to null to avoid
1427 // adding dead instructions to it.
1428 current_block_ = nullptr;
1429 break;
1430 }
1431
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001432 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001433 uint8_t destination = instruction.VRegA_22c();
1434 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001435 uint16_t type_index = instruction.VRegC_22c();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001436 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_offset)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001437 return false;
1438 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001439 break;
1440 }
1441
1442 case Instruction::CHECK_CAST: {
1443 uint8_t reference = instruction.VRegA_21c();
1444 uint16_t type_index = instruction.VRegB_21c();
1445 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_offset)) {
1446 return false;
1447 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001448 break;
1449 }
1450
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001451 case Instruction::MONITOR_ENTER: {
1452 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1453 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1454 HMonitorOperation::kEnter,
1455 dex_offset));
1456 break;
1457 }
1458
1459 case Instruction::MONITOR_EXIT: {
1460 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1461 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1462 HMonitorOperation::kExit,
1463 dex_offset));
1464 break;
1465 }
1466
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001467 default:
1468 return false;
1469 }
1470 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001471} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001472
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001473HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001474 if (constant0_ != nullptr) {
1475 return constant0_;
1476 }
1477 constant0_ = new(arena_) HIntConstant(0);
1478 entry_block_->AddInstruction(constant0_);
1479 return constant0_;
1480}
1481
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001482HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001483 if (constant1_ != nullptr) {
1484 return constant1_;
1485 }
1486 constant1_ = new(arena_) HIntConstant(1);
1487 entry_block_->AddInstruction(constant1_);
1488 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001489}
1490
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001491HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001492 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001493 case 0: return GetIntConstant0();
1494 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001495 default: {
1496 HIntConstant* instruction = new (arena_) HIntConstant(constant);
1497 entry_block_->AddInstruction(instruction);
1498 return instruction;
1499 }
1500 }
1501}
1502
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001503HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
1504 HLongConstant* instruction = new (arena_) HLongConstant(constant);
1505 entry_block_->AddInstruction(instruction);
1506 return instruction;
1507}
1508
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001509HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
1510 return locals_.Get(register_index);
1511}
1512
1513void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
1514 HLocal* local = GetLocalAt(register_index);
1515 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
1516}
1517
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001518HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001519 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001520 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001521 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001522}
1523
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001524} // namespace art