blob: 4e2f1cd9a87129630fbe2c7c47d57ea40d71ac59 [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>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000298void HGraphBuilder::Binop_23x(const Instruction& instruction,
299 Primitive::Type type,
300 uint32_t dex_pc) {
301 HInstruction* first = LoadLocal(instruction.VRegB(), type);
302 HInstruction* second = LoadLocal(instruction.VRegC(), type);
303 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
304 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
305}
306
307template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100308void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
309 HInstruction* first = LoadLocal(instruction.VRegA(), type);
310 HInstruction* second = LoadLocal(instruction.VRegB(), type);
311 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100312 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
313}
314
315template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000316void HGraphBuilder::Binop_12x(const Instruction& instruction,
317 Primitive::Type type,
318 uint32_t dex_pc) {
319 HInstruction* first = LoadLocal(instruction.VRegA(), type);
320 HInstruction* second = LoadLocal(instruction.VRegB(), type);
321 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
322 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
323}
324
325template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100326void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100327 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
328 HInstruction* second = GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100329 if (reverse) {
330 std::swap(first, second);
331 }
332 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
333 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
334}
335
336template<typename T>
337void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100338 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
339 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100340 if (reverse) {
341 std::swap(first, second);
342 }
343 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
344 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
345}
346
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100347void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
348 if (type == Primitive::kPrimVoid) {
349 current_block_->AddInstruction(new (arena_) HReturnVoid());
350 } else {
351 HInstruction* value = LoadLocal(instruction.VRegA(), type);
352 current_block_->AddInstruction(new (arena_) HReturn(value));
353 }
354 current_block_->AddSuccessor(exit_block_);
355 current_block_ = nullptr;
356}
357
358bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
359 uint32_t dex_offset,
360 uint32_t method_idx,
361 uint32_t number_of_vreg_arguments,
362 bool is_range,
363 uint32_t* args,
364 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100365 Instruction::Code opcode = instruction.Opcode();
366 InvokeType invoke_type;
367 switch (opcode) {
368 case Instruction::INVOKE_STATIC:
369 case Instruction::INVOKE_STATIC_RANGE:
370 invoke_type = kStatic;
371 break;
372 case Instruction::INVOKE_DIRECT:
373 case Instruction::INVOKE_DIRECT_RANGE:
374 invoke_type = kDirect;
375 break;
376 case Instruction::INVOKE_VIRTUAL:
377 case Instruction::INVOKE_VIRTUAL_RANGE:
378 invoke_type = kVirtual;
379 break;
380 case Instruction::INVOKE_INTERFACE:
381 case Instruction::INVOKE_INTERFACE_RANGE:
382 invoke_type = kInterface;
383 break;
384 case Instruction::INVOKE_SUPER_RANGE:
385 case Instruction::INVOKE_SUPER:
386 invoke_type = kSuper;
387 break;
388 default:
389 LOG(FATAL) << "Unexpected invoke op: " << opcode;
390 return false;
391 }
392
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100393 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
394 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
395 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
396 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100397 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100398 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
399
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100400 HInvoke* invoke = nullptr;
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000401 if (invoke_type == kVirtual || invoke_type == kInterface || invoke_type == kSuper) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100402 MethodReference target_method(dex_file_, method_idx);
403 uintptr_t direct_code;
404 uintptr_t direct_method;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000405 int table_index;
406 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100407 compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_offset, true, true,
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000408 &optimized_invoke_type, &target_method, &table_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100409 &direct_code, &direct_method);
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000410 if (table_index == -1) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100411 return false;
412 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000413
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000414 if (optimized_invoke_type == kVirtual) {
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000415 invoke = new (arena_) HInvokeVirtual(
416 arena_, number_of_arguments, return_type, dex_offset, table_index);
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000417 } else if (optimized_invoke_type == kInterface) {
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000418 invoke = new (arena_) HInvokeInterface(
419 arena_, number_of_arguments, return_type, dex_offset, method_idx, table_index);
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000420 } else if (optimized_invoke_type == kDirect) {
421 // For this compiler, sharpening only works if we compile PIC.
422 DCHECK(compiler_driver_->GetCompilerOptions().GetCompilePic());
423 // Treat invoke-direct like static calls for now.
424 invoke = new (arena_) HInvokeStatic(
425 arena_, number_of_arguments, return_type, dex_offset, target_method.dex_method_index);
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000426 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100427 } else {
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000428 DCHECK(invoke_type == kDirect || invoke_type == kStatic);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100429 // Treat invoke-direct like static calls for now.
430 invoke = new (arena_) HInvokeStatic(
431 arena_, number_of_arguments, return_type, dex_offset, method_idx);
432 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100433
434 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000435 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100436 if (is_instance_call) {
437 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100438 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_offset);
439 current_block_->AddInstruction(null_check);
440 temps.Add(null_check);
441 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100442 start_index = 1;
443 }
444
445 uint32_t descriptor_index = 1;
446 uint32_t argument_index = start_index;
447 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
448 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100449 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
450 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100451 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
452 << " at " << dex_offset;
453 // We do not implement non sequential register pair.
454 return false;
455 }
456 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
457 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100458 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100459 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100460 }
461 }
462
463 DCHECK_EQ(argument_index, number_of_arguments);
464 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100465 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100466 return true;
467}
468
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100469bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
470 uint32_t dex_offset,
471 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100472 uint32_t source_or_dest_reg = instruction.VRegA_22c();
473 uint32_t obj_reg = instruction.VRegB_22c();
474 uint16_t field_index = instruction.VRegC_22c();
475
476 ScopedObjectAccess soa(Thread::Current());
477 StackHandleScope<1> hs(soa.Self());
478 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
479 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
480
481 if (resolved_field.Get() == nullptr) {
482 return false;
483 }
484 if (resolved_field->IsVolatile()) {
485 return false;
486 }
487
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100488 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100489
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100490 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
491 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_offset));
492 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000493 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100494 HInstruction* null_check = current_block_->GetLastInstruction();
495 // We need one temporary for the null check.
496 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100497 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100498 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
499 null_check,
500 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100501 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100502 resolved_field->GetOffset()));
503 } else {
504 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
505 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100506 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100507 resolved_field->GetOffset()));
508
509 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
510 }
511 return true;
512}
513
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100514
515
516bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
517 uint32_t dex_offset,
518 bool is_put) {
519 uint32_t source_or_dest_reg = instruction.VRegA_21c();
520 uint16_t field_index = instruction.VRegB_21c();
521
522 uint32_t storage_index;
523 bool is_referrers_class;
524 bool is_initialized;
525 bool is_volatile;
526 MemberOffset field_offset(0u);
527 Primitive::Type field_type;
528
529 bool fast_path = compiler_driver_->ComputeStaticFieldInfo(field_index,
530 dex_compilation_unit_,
531 is_put,
532 &field_offset,
533 &storage_index,
534 &is_referrers_class,
535 &is_volatile,
536 &is_initialized,
537 &field_type);
538 if (!fast_path) {
539 return false;
540 }
541
542 if (is_volatile) {
543 return false;
544 }
545
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100546 HLoadClass* constant = new (arena_) HLoadClass(
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000547 storage_index, is_referrers_class, dex_offset);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100548 current_block_->AddInstruction(constant);
549
550 HInstruction* cls = constant;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000551 if (!is_initialized) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100552 cls = new (arena_) HClinitCheck(constant, dex_offset);
553 current_block_->AddInstruction(cls);
554 }
555
556 if (is_put) {
557 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000558 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100559 temps.Add(cls);
560 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
561 DCHECK_EQ(value->GetType(), field_type);
562 current_block_->AddInstruction(
563 new (arena_) HStaticFieldSet(cls, value, field_type, field_offset));
564 } else {
565 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls, field_type, field_offset));
566 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
567 }
568 return true;
569}
570
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000571void HGraphBuilder::BuildCheckedDiv(uint16_t out_vreg,
572 uint16_t first_vreg,
573 int64_t second_vreg_or_constant,
574 uint32_t dex_pc,
Calin Juravled0d48522014-11-04 16:40:20 +0000575 Primitive::Type type,
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000576 bool second_is_constant) {
577 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +0000578
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000579 HInstruction* first = LoadLocal(first_vreg, type);
580 HInstruction* second = nullptr;
581 if (second_is_constant) {
582 if (type == Primitive::kPrimInt) {
583 second = GetIntConstant(second_vreg_or_constant);
584 } else {
585 second = GetLongConstant(second_vreg_or_constant);
586 }
587 } else {
588 second = LoadLocal(second_vreg_or_constant, type);
589 }
590
591 if (!second_is_constant
592 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
593 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
594 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000595 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000596 current_block_->AddInstruction(second);
597 temps.Add(current_block_->GetLastInstruction());
598 }
599
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000600 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
601 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +0000602}
603
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100604void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
605 uint32_t dex_offset,
606 bool is_put,
607 Primitive::Type anticipated_type) {
608 uint8_t source_or_dest_reg = instruction.VRegA_23x();
609 uint8_t array_reg = instruction.VRegB_23x();
610 uint8_t index_reg = instruction.VRegC_23x();
611
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100612 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000613 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100614
615 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
616 object = new (arena_) HNullCheck(object, dex_offset);
617 current_block_->AddInstruction(object);
618 temps.Add(object);
619
620 HInstruction* length = new (arena_) HArrayLength(object);
621 current_block_->AddInstruction(length);
622 temps.Add(length);
623 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
624 index = new (arena_) HBoundsCheck(index, length, dex_offset);
625 current_block_->AddInstruction(index);
626 temps.Add(index);
627 if (is_put) {
628 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
629 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100630 current_block_->AddInstruction(new (arena_) HArraySet(
631 object, index, value, anticipated_type, dex_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100632 } else {
633 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
634 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
635 }
636}
637
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100638void HGraphBuilder::BuildFilledNewArray(uint32_t dex_offset,
639 uint32_t type_index,
640 uint32_t number_of_vreg_arguments,
641 bool is_range,
642 uint32_t* args,
643 uint32_t register_index) {
644 HInstruction* length = GetIntConstant(number_of_vreg_arguments);
645 HInstruction* object = new (arena_) HNewArray(length, dex_offset, type_index);
646 current_block_->AddInstruction(object);
647
648 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
649 DCHECK_EQ(descriptor[0], '[') << descriptor;
650 char primitive = descriptor[1];
651 DCHECK(primitive == 'I'
652 || primitive == 'L'
653 || primitive == '[') << descriptor;
654 bool is_reference_array = (primitive == 'L') || (primitive == '[');
655 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
656
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000657 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100658 temps.Add(object);
659 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
660 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
661 HInstruction* index = GetIntConstant(i);
662 current_block_->AddInstruction(
663 new (arena_) HArraySet(object, index, value, type, dex_offset));
664 }
665 latest_result_ = object;
666}
667
668template <typename T>
669void HGraphBuilder::BuildFillArrayData(HInstruction* object,
670 const T* data,
671 uint32_t element_count,
672 Primitive::Type anticipated_type,
673 uint32_t dex_offset) {
674 for (uint32_t i = 0; i < element_count; ++i) {
675 HInstruction* index = GetIntConstant(i);
676 HInstruction* value = GetIntConstant(data[i]);
677 current_block_->AddInstruction(new (arena_) HArraySet(
678 object, index, value, anticipated_type, dex_offset));
679 }
680}
681
Calin Juravled0d48522014-11-04 16:40:20 +0000682void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_offset) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000683 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000684 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
685 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_offset);
686 current_block_->AddInstruction(null_check);
687 temps.Add(null_check);
688
689 HInstruction* length = new (arena_) HArrayLength(null_check);
690 current_block_->AddInstruction(length);
691
692 int32_t payload_offset = instruction.VRegB_31t() + dex_offset;
693 const Instruction::ArrayDataPayload* payload =
694 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
695 const uint8_t* data = payload->data;
696 uint32_t element_count = payload->element_count;
697
698 // Implementation of this DEX instruction seems to be that the bounds check is
699 // done before doing any stores.
700 HInstruction* last_index = GetIntConstant(payload->element_count - 1);
701 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_offset));
702
703 switch (payload->element_width) {
704 case 1:
705 BuildFillArrayData(null_check,
706 reinterpret_cast<const int8_t*>(data),
707 element_count,
708 Primitive::kPrimByte,
709 dex_offset);
710 break;
711 case 2:
712 BuildFillArrayData(null_check,
713 reinterpret_cast<const int16_t*>(data),
714 element_count,
715 Primitive::kPrimShort,
716 dex_offset);
717 break;
718 case 4:
719 BuildFillArrayData(null_check,
720 reinterpret_cast<const int32_t*>(data),
721 element_count,
722 Primitive::kPrimInt,
723 dex_offset);
724 break;
725 case 8:
726 BuildFillWideArrayData(null_check,
727 reinterpret_cast<const int64_t*>(data),
728 element_count,
729 dex_offset);
730 break;
731 default:
732 LOG(FATAL) << "Unknown element width for " << payload->element_width;
733 }
734}
735
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100736void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100737 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100738 uint32_t element_count,
739 uint32_t dex_offset) {
740 for (uint32_t i = 0; i < element_count; ++i) {
741 HInstruction* index = GetIntConstant(i);
742 HInstruction* value = GetLongConstant(data[i]);
743 current_block_->AddInstruction(new (arena_) HArraySet(
744 object, index, value, Primitive::kPrimLong, dex_offset));
745 }
746}
747
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000748bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
749 uint8_t destination,
750 uint8_t reference,
751 uint16_t type_index,
752 uint32_t dex_offset) {
753 bool type_known_final;
754 bool type_known_abstract;
755 bool is_referrers_class;
756 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
757 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
758 &type_known_final, &type_known_abstract, &is_referrers_class);
759 if (!can_access) {
760 return false;
761 }
762 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
763 HLoadClass* cls = new (arena_) HLoadClass(type_index, is_referrers_class, dex_offset);
764 current_block_->AddInstruction(cls);
765 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000766 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000767 temps.Add(cls);
768 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
769 current_block_->AddInstruction(
770 new (arena_) HInstanceOf(object, cls, type_known_final, dex_offset));
771 UpdateLocal(destination, current_block_->GetLastInstruction());
772 } else {
773 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
774 current_block_->AddInstruction(
775 new (arena_) HCheckCast(object, cls, type_known_final, dex_offset));
776 }
777 return true;
778}
779
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000780void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset) {
781 if (target_offset <= 0) {
782 // Unconditionnally add a suspend check to backward branches. We can remove
783 // them after we recognize loops in the graph.
784 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_offset));
785 }
786}
787
788bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000789 if (current_block_ == nullptr) {
790 return true; // Dead code
791 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000792
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000793 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000794 case Instruction::CONST_4: {
795 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100796 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000797 UpdateLocal(register_index, constant);
798 break;
799 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000800
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100801 case Instruction::CONST_16: {
802 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100803 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
804 UpdateLocal(register_index, constant);
805 break;
806 }
807
Dave Allison20dfc792014-06-16 20:44:29 -0700808 case Instruction::CONST: {
809 int32_t register_index = instruction.VRegA();
810 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
811 UpdateLocal(register_index, constant);
812 break;
813 }
814
815 case Instruction::CONST_HIGH16: {
816 int32_t register_index = instruction.VRegA();
817 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
818 UpdateLocal(register_index, constant);
819 break;
820 }
821
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100822 case Instruction::CONST_WIDE_16: {
823 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700824 // Get 16 bits of constant value, sign extended to 64 bits.
825 int64_t value = instruction.VRegB_21s();
826 value <<= 48;
827 value >>= 48;
828 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100829 UpdateLocal(register_index, constant);
830 break;
831 }
832
833 case Instruction::CONST_WIDE_32: {
834 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700835 // Get 32 bits of constant value, sign extended to 64 bits.
836 int64_t value = instruction.VRegB_31i();
837 value <<= 32;
838 value >>= 32;
839 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100840 UpdateLocal(register_index, constant);
841 break;
842 }
843
844 case Instruction::CONST_WIDE: {
845 int32_t register_index = instruction.VRegA();
846 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100847 UpdateLocal(register_index, constant);
848 break;
849 }
850
Dave Allison20dfc792014-06-16 20:44:29 -0700851 case Instruction::CONST_WIDE_HIGH16: {
852 int32_t register_index = instruction.VRegA();
853 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
854 HLongConstant* constant = GetLongConstant(value);
855 UpdateLocal(register_index, constant);
856 break;
857 }
858
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000859 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -0700860 case Instruction::MOVE:
861 case Instruction::MOVE_FROM16:
862 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100863 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100864 UpdateLocal(instruction.VRegA(), value);
865 break;
866 }
867
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000868 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -0700869 case Instruction::MOVE_WIDE:
870 case Instruction::MOVE_WIDE_FROM16:
871 case Instruction::MOVE_WIDE_16: {
872 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
873 UpdateLocal(instruction.VRegA(), value);
874 break;
875 }
876
877 case Instruction::MOVE_OBJECT:
878 case Instruction::MOVE_OBJECT_16:
879 case Instruction::MOVE_OBJECT_FROM16: {
880 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
881 UpdateLocal(instruction.VRegA(), value);
882 break;
883 }
884
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000885 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100886 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000887 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000888 }
889
Dave Allison20dfc792014-06-16 20:44:29 -0700890#define IF_XX(comparison, cond) \
891 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_offset); break; \
892 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_offset); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100893
Dave Allison20dfc792014-06-16 20:44:29 -0700894 IF_XX(HEqual, EQ);
895 IF_XX(HNotEqual, NE);
896 IF_XX(HLessThan, LT);
897 IF_XX(HLessThanOrEqual, LE);
898 IF_XX(HGreaterThan, GT);
899 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000900
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000901 case Instruction::GOTO:
902 case Instruction::GOTO_16:
903 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000904 int32_t offset = instruction.GetTargetOffset();
905 PotentiallyAddSuspendCheck(offset, dex_offset);
906 HBasicBlock* target = FindBlockStartingAt(offset + dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000907 DCHECK(target != nullptr);
908 current_block_->AddInstruction(new (arena_) HGoto());
909 current_block_->AddSuccessor(target);
910 current_block_ = nullptr;
911 break;
912 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000913
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100914 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100915 DCHECK_NE(return_type_, Primitive::kPrimNot);
916 DCHECK_NE(return_type_, Primitive::kPrimLong);
917 DCHECK_NE(return_type_, Primitive::kPrimDouble);
918 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100919 break;
920 }
921
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100922 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100923 DCHECK(return_type_ == Primitive::kPrimNot);
924 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100925 break;
926 }
927
928 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100929 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
930 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000931 break;
932 }
933
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100934 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000935 case Instruction::INVOKE_INTERFACE:
936 case Instruction::INVOKE_STATIC:
937 case Instruction::INVOKE_SUPER:
938 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000939 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100940 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100941 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -0700942 instruction.GetVarArgs(args);
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000943 if (!BuildInvoke(instruction, dex_offset, method_idx,
944 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100945 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100946 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100947 break;
948 }
949
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100950 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000951 case Instruction::INVOKE_INTERFACE_RANGE:
952 case Instruction::INVOKE_STATIC_RANGE:
953 case Instruction::INVOKE_SUPER_RANGE:
954 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100955 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100956 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
957 uint32_t register_index = instruction.VRegC();
958 if (!BuildInvoke(instruction, dex_offset, method_idx,
959 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100960 return false;
961 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000962 break;
963 }
964
Roland Levillain88cb1752014-10-20 16:36:47 +0100965 case Instruction::NEG_INT: {
966 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
967 break;
968 }
969
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100970 case Instruction::NEG_LONG: {
971 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
972 break;
973 }
974
Roland Levillain3dbcb382014-10-28 17:30:07 +0000975 case Instruction::NEG_FLOAT: {
976 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
977 break;
978 }
979
980 case Instruction::NEG_DOUBLE: {
981 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
982 break;
983 }
984
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100985 case Instruction::NOT_INT: {
986 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
987 break;
988 }
989
Roland Levillain70566432014-10-24 16:20:17 +0100990 case Instruction::NOT_LONG: {
991 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
992 break;
993 }
994
Roland Levillaindff1f282014-11-05 14:15:05 +0000995 case Instruction::INT_TO_LONG: {
996 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong);
997 break;
998 }
999
Roland Levillain946e1432014-11-11 17:35:19 +00001000 case Instruction::LONG_TO_INT: {
1001 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt);
1002 break;
1003 }
1004
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001005 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001006 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001007 break;
1008 }
1009
1010 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001011 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001012 break;
1013 }
1014
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001015 case Instruction::ADD_DOUBLE: {
1016 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1017 break;
1018 }
1019
1020 case Instruction::ADD_FLOAT: {
1021 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1022 break;
1023 }
1024
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001025 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001026 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001027 break;
1028 }
1029
1030 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001031 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001032 break;
1033 }
1034
Calin Juravle096cc022014-10-23 17:01:13 +01001035 case Instruction::SUB_FLOAT: {
1036 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1037 break;
1038 }
1039
1040 case Instruction::SUB_DOUBLE: {
1041 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1042 break;
1043 }
1044
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001045 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001046 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1047 break;
1048 }
1049
Calin Juravle34bacdf2014-10-07 20:23:36 +01001050 case Instruction::MUL_INT: {
1051 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1052 break;
1053 }
1054
1055 case Instruction::MUL_LONG: {
1056 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1057 break;
1058 }
1059
Calin Juravleb5bfa962014-10-21 18:02:24 +01001060 case Instruction::MUL_FLOAT: {
1061 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1062 break;
1063 }
1064
1065 case Instruction::MUL_DOUBLE: {
1066 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1067 break;
1068 }
1069
Calin Juravled0d48522014-11-04 16:40:20 +00001070 case Instruction::DIV_INT: {
Calin Juravle865fc882014-11-06 17:09:03 +00001071 BuildCheckedDiv(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1072 dex_offset, Primitive::kPrimInt, false);
Calin Juravled0d48522014-11-04 16:40:20 +00001073 break;
1074 }
1075
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001076 case Instruction::DIV_LONG: {
1077 BuildCheckedDiv(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1078 dex_offset, Primitive::kPrimLong, false);
1079 break;
1080 }
1081
Calin Juravle7c4954d2014-10-28 16:57:40 +00001082 case Instruction::DIV_FLOAT: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001083 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_offset);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001084 break;
1085 }
1086
1087 case Instruction::DIV_DOUBLE: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001088 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_offset);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001089 break;
1090 }
1091
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001092 case Instruction::AND_INT: {
1093 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1094 break;
1095 }
1096
1097 case Instruction::AND_LONG: {
1098 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1099 break;
1100 }
1101
1102 case Instruction::OR_INT: {
1103 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1104 break;
1105 }
1106
1107 case Instruction::OR_LONG: {
1108 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1109 break;
1110 }
1111
1112 case Instruction::XOR_INT: {
1113 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1114 break;
1115 }
1116
1117 case Instruction::XOR_LONG: {
1118 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1119 break;
1120 }
1121
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001122 case Instruction::ADD_LONG_2ADDR: {
1123 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001124 break;
1125 }
1126
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001127 case Instruction::ADD_DOUBLE_2ADDR: {
1128 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1129 break;
1130 }
1131
1132 case Instruction::ADD_FLOAT_2ADDR: {
1133 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
1134 break;
1135 }
1136
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001137 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001138 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
1139 break;
1140 }
1141
1142 case Instruction::SUB_LONG_2ADDR: {
1143 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001144 break;
1145 }
1146
Calin Juravle096cc022014-10-23 17:01:13 +01001147 case Instruction::SUB_FLOAT_2ADDR: {
1148 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
1149 break;
1150 }
1151
1152 case Instruction::SUB_DOUBLE_2ADDR: {
1153 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
1154 break;
1155 }
1156
Calin Juravle34bacdf2014-10-07 20:23:36 +01001157 case Instruction::MUL_INT_2ADDR: {
1158 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
1159 break;
1160 }
1161
1162 case Instruction::MUL_LONG_2ADDR: {
1163 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
1164 break;
1165 }
1166
Calin Juravleb5bfa962014-10-21 18:02:24 +01001167 case Instruction::MUL_FLOAT_2ADDR: {
1168 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
1169 break;
1170 }
1171
1172 case Instruction::MUL_DOUBLE_2ADDR: {
1173 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
1174 break;
1175 }
1176
Calin Juravle865fc882014-11-06 17:09:03 +00001177 case Instruction::DIV_INT_2ADDR: {
1178 BuildCheckedDiv(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1179 dex_offset, Primitive::kPrimInt, false);
1180 break;
1181 }
1182
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001183 case Instruction::DIV_LONG_2ADDR: {
1184 BuildCheckedDiv(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1185 dex_offset, Primitive::kPrimLong, false);
1186 break;
1187 }
1188
Calin Juravle7c4954d2014-10-28 16:57:40 +00001189 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001190 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_offset);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001191 break;
1192 }
1193
1194 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001195 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_offset);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001196 break;
1197 }
1198
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001199 case Instruction::AND_INT_2ADDR: {
1200 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
1201 break;
1202 }
1203
1204 case Instruction::AND_LONG_2ADDR: {
1205 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
1206 break;
1207 }
1208
1209 case Instruction::OR_INT_2ADDR: {
1210 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
1211 break;
1212 }
1213
1214 case Instruction::OR_LONG_2ADDR: {
1215 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
1216 break;
1217 }
1218
1219 case Instruction::XOR_INT_2ADDR: {
1220 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
1221 break;
1222 }
1223
1224 case Instruction::XOR_LONG_2ADDR: {
1225 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
1226 break;
1227 }
1228
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001229 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001230 Binop_22s<HAdd>(instruction, false);
1231 break;
1232 }
1233
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001234 case Instruction::AND_INT_LIT16: {
1235 Binop_22s<HAnd>(instruction, false);
1236 break;
1237 }
1238
1239 case Instruction::OR_INT_LIT16: {
1240 Binop_22s<HOr>(instruction, false);
1241 break;
1242 }
1243
1244 case Instruction::XOR_INT_LIT16: {
1245 Binop_22s<HXor>(instruction, false);
1246 break;
1247 }
1248
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001249 case Instruction::RSUB_INT: {
1250 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001251 break;
1252 }
1253
Calin Juravle34bacdf2014-10-07 20:23:36 +01001254 case Instruction::MUL_INT_LIT16: {
1255 Binop_22s<HMul>(instruction, false);
1256 break;
1257 }
1258
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001259 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001260 Binop_22b<HAdd>(instruction, false);
1261 break;
1262 }
1263
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001264 case Instruction::AND_INT_LIT8: {
1265 Binop_22b<HAnd>(instruction, false);
1266 break;
1267 }
1268
1269 case Instruction::OR_INT_LIT8: {
1270 Binop_22b<HOr>(instruction, false);
1271 break;
1272 }
1273
1274 case Instruction::XOR_INT_LIT8: {
1275 Binop_22b<HXor>(instruction, false);
1276 break;
1277 }
1278
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001279 case Instruction::RSUB_INT_LIT8: {
1280 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001281 break;
1282 }
1283
Calin Juravle34bacdf2014-10-07 20:23:36 +01001284 case Instruction::MUL_INT_LIT8: {
1285 Binop_22b<HMul>(instruction, false);
1286 break;
1287 }
1288
Calin Juravled0d48522014-11-04 16:40:20 +00001289 case Instruction::DIV_INT_LIT16:
1290 case Instruction::DIV_INT_LIT8: {
Calin Juravle865fc882014-11-06 17:09:03 +00001291 BuildCheckedDiv(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1292 dex_offset, Primitive::kPrimInt, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001293 break;
1294 }
1295
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001296 case Instruction::NEW_INSTANCE: {
1297 current_block_->AddInstruction(
1298 new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c()));
1299 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1300 break;
1301 }
1302
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001303 case Instruction::NEW_ARRAY: {
1304 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
1305 current_block_->AddInstruction(
1306 new (arena_) HNewArray(length, dex_offset, instruction.VRegC_22c()));
1307 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1308 break;
1309 }
1310
1311 case Instruction::FILLED_NEW_ARRAY: {
1312 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1313 uint32_t type_index = instruction.VRegB_35c();
1314 uint32_t args[5];
1315 instruction.GetVarArgs(args);
1316 BuildFilledNewArray(dex_offset, type_index, number_of_vreg_arguments, false, args, 0);
1317 break;
1318 }
1319
1320 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1321 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1322 uint32_t type_index = instruction.VRegB_3rc();
1323 uint32_t register_index = instruction.VRegC_3rc();
1324 BuildFilledNewArray(
1325 dex_offset, type_index, number_of_vreg_arguments, true, nullptr, register_index);
1326 break;
1327 }
1328
1329 case Instruction::FILL_ARRAY_DATA: {
Calin Juravled0d48522014-11-04 16:40:20 +00001330 BuildFillArrayData(instruction, dex_offset);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001331 break;
1332 }
1333
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001334 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001335 case Instruction::MOVE_RESULT_WIDE:
1336 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001337 UpdateLocal(instruction.VRegA(), latest_result_);
1338 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001339 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001340
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001341 case Instruction::CMP_LONG: {
1342 Binop_23x<HCompare>(instruction, Primitive::kPrimLong);
1343 break;
1344 }
1345
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001346 case Instruction::NOP:
1347 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001348
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001349 case Instruction::IGET:
1350 case Instruction::IGET_WIDE:
1351 case Instruction::IGET_OBJECT:
1352 case Instruction::IGET_BOOLEAN:
1353 case Instruction::IGET_BYTE:
1354 case Instruction::IGET_CHAR:
1355 case Instruction::IGET_SHORT: {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001356 if (!BuildInstanceFieldAccess(instruction, dex_offset, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001357 return false;
1358 }
1359 break;
1360 }
1361
1362 case Instruction::IPUT:
1363 case Instruction::IPUT_WIDE:
1364 case Instruction::IPUT_OBJECT:
1365 case Instruction::IPUT_BOOLEAN:
1366 case Instruction::IPUT_BYTE:
1367 case Instruction::IPUT_CHAR:
1368 case Instruction::IPUT_SHORT: {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001369 if (!BuildInstanceFieldAccess(instruction, dex_offset, true)) {
1370 return false;
1371 }
1372 break;
1373 }
1374
1375 case Instruction::SGET:
1376 case Instruction::SGET_WIDE:
1377 case Instruction::SGET_OBJECT:
1378 case Instruction::SGET_BOOLEAN:
1379 case Instruction::SGET_BYTE:
1380 case Instruction::SGET_CHAR:
1381 case Instruction::SGET_SHORT: {
1382 if (!BuildStaticFieldAccess(instruction, dex_offset, false)) {
1383 return false;
1384 }
1385 break;
1386 }
1387
1388 case Instruction::SPUT:
1389 case Instruction::SPUT_WIDE:
1390 case Instruction::SPUT_OBJECT:
1391 case Instruction::SPUT_BOOLEAN:
1392 case Instruction::SPUT_BYTE:
1393 case Instruction::SPUT_CHAR:
1394 case Instruction::SPUT_SHORT: {
1395 if (!BuildStaticFieldAccess(instruction, dex_offset, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001396 return false;
1397 }
1398 break;
1399 }
1400
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001401#define ARRAY_XX(kind, anticipated_type) \
1402 case Instruction::AGET##kind: { \
1403 BuildArrayAccess(instruction, dex_offset, false, anticipated_type); \
1404 break; \
1405 } \
1406 case Instruction::APUT##kind: { \
1407 BuildArrayAccess(instruction, dex_offset, true, anticipated_type); \
1408 break; \
1409 }
1410
1411 ARRAY_XX(, Primitive::kPrimInt);
1412 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1413 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1414 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1415 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1416 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1417 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1418
Nicolas Geoffray39468442014-09-02 15:17:15 +01001419 case Instruction::ARRAY_LENGTH: {
1420 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001421 // No need for a temporary for the null check, it is the only input of the following
1422 // instruction.
1423 object = new (arena_) HNullCheck(object, dex_offset);
1424 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001425 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1426 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1427 break;
1428 }
1429
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001430 case Instruction::CONST_STRING: {
1431 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_offset));
1432 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1433 break;
1434 }
1435
1436 case Instruction::CONST_STRING_JUMBO: {
1437 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_offset));
1438 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
1439 break;
1440 }
1441
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001442 case Instruction::CONST_CLASS: {
1443 uint16_t type_index = instruction.VRegB_21c();
1444 bool type_known_final;
1445 bool type_known_abstract;
1446 bool is_referrers_class;
1447 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1448 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
1449 &type_known_final, &type_known_abstract, &is_referrers_class);
1450 if (!can_access) {
1451 return false;
1452 }
1453 current_block_->AddInstruction(
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001454 new (arena_) HLoadClass(type_index, is_referrers_class, dex_offset));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001455 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1456 break;
1457 }
1458
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001459 case Instruction::MOVE_EXCEPTION: {
1460 current_block_->AddInstruction(new (arena_) HLoadException());
1461 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
1462 break;
1463 }
1464
1465 case Instruction::THROW: {
1466 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
1467 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_offset));
1468 // A throw instruction must branch to the exit block.
1469 current_block_->AddSuccessor(exit_block_);
1470 // We finished building this block. Set the current block to null to avoid
1471 // adding dead instructions to it.
1472 current_block_ = nullptr;
1473 break;
1474 }
1475
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001476 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001477 uint8_t destination = instruction.VRegA_22c();
1478 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001479 uint16_t type_index = instruction.VRegC_22c();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001480 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_offset)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001481 return false;
1482 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001483 break;
1484 }
1485
1486 case Instruction::CHECK_CAST: {
1487 uint8_t reference = instruction.VRegA_21c();
1488 uint16_t type_index = instruction.VRegB_21c();
1489 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_offset)) {
1490 return false;
1491 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001492 break;
1493 }
1494
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001495 case Instruction::MONITOR_ENTER: {
1496 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1497 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1498 HMonitorOperation::kEnter,
1499 dex_offset));
1500 break;
1501 }
1502
1503 case Instruction::MONITOR_EXIT: {
1504 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1505 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1506 HMonitorOperation::kExit,
1507 dex_offset));
1508 break;
1509 }
1510
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001511 default:
1512 return false;
1513 }
1514 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001515} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001516
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001517HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001518 if (constant0_ != nullptr) {
1519 return constant0_;
1520 }
1521 constant0_ = new(arena_) HIntConstant(0);
1522 entry_block_->AddInstruction(constant0_);
1523 return constant0_;
1524}
1525
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001526HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001527 if (constant1_ != nullptr) {
1528 return constant1_;
1529 }
1530 constant1_ = new(arena_) HIntConstant(1);
1531 entry_block_->AddInstruction(constant1_);
1532 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001533}
1534
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001535HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001536 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001537 case 0: return GetIntConstant0();
1538 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001539 default: {
1540 HIntConstant* instruction = new (arena_) HIntConstant(constant);
1541 entry_block_->AddInstruction(instruction);
1542 return instruction;
1543 }
1544 }
1545}
1546
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001547HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
1548 HLongConstant* instruction = new (arena_) HLongConstant(constant);
1549 entry_block_->AddInstruction(instruction);
1550 return instruction;
1551}
1552
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001553HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
1554 return locals_.Get(register_index);
1555}
1556
1557void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
1558 HLocal* local = GetLocalAt(register_index);
1559 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
1560}
1561
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001562HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001563 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001564 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001565 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001566}
1567
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001568} // namespace art