blob: df3d57ebdfe57c9a95285002a043fa1dcd83ed9f [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Nicolas Geoffraye5038322014-07-04 09:41:32 +010017#include "builder.h"
18
19#include "class_linker.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000020#include "dex_file.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000021#include "dex_file-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000022#include "dex_instruction.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000023#include "dex_instruction-inl.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010024#include "driver/compiler_driver-inl.h"
25#include "mirror/art_field.h"
26#include "mirror/art_field-inl.h"
27#include "mirror/class_loader.h"
28#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000029#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000030#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010031#include "scoped_thread_state_change.h"
32#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033
34namespace art {
35
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036/**
37 * Helper class to add HTemporary instructions. This class is used when
38 * converting a DEX instruction to multiple HInstruction, and where those
39 * instructions do not die at the following instruction, but instead spans
40 * multiple instructions.
41 */
42class Temporaries : public ValueObject {
43 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000044 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010045
46 void Add(HInstruction* instruction) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +000047 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000049
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000051
52 size_t offset;
53 if (instruction->GetType() == Primitive::kPrimLong
54 || instruction->GetType() == Primitive::kPrimDouble) {
55 offset = 2;
56 } else {
57 offset = 1;
58 }
59 index_ += offset;
60
61 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010062 }
63
64 private:
65 HGraph* const graph_;
66
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010067 // Current index in the temporary stack, updated by `Add`.
68 size_t index_;
69};
70
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010071void HGraphBuilder::InitializeLocals(uint16_t count) {
72 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000073 locals_.SetSize(count);
74 for (int i = 0; i < count; i++) {
75 HLocal* local = new (arena_) HLocal(i);
76 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000077 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000078 }
79}
80
Nicolas Geoffray52e832b2014-11-06 15:15:31 +000081void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010082 // dex_compilation_unit_ is null only when unit testing.
83 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +000084 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010085 }
86
87 graph_->SetNumberOfInVRegs(number_of_parameters);
88 const char* shorty = dex_compilation_unit_->GetShorty();
89 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010090 int parameter_index = 0;
91
92 if (!dex_compilation_unit_->IsStatic()) {
93 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010094 HParameterValue* parameter =
95 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010096 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010097 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010098 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010099 number_of_parameters--;
100 }
101
102 uint32_t pos = 1;
103 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100104 HParameterValue* parameter =
105 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
106 entry_block_->AddInstruction(parameter);
107 HLocal* local = GetLocalAt(locals_index++);
108 // Store the parameter value in the local that the dex code will use
109 // to reference that parameter.
110 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
111 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
112 || (parameter->GetType() == Primitive::kPrimDouble);
113 if (is_wide) {
114 i++;
115 locals_index++;
116 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100117 }
118 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100119}
120
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100121template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000122void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000123 int32_t target_offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +0000124 PotentiallyAddSuspendCheck(target_offset, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100125 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
126 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700127 T* comparison = new (arena_) T(first, second);
128 current_block_->AddInstruction(comparison);
129 HInstruction* ifinst = new (arena_) HIf(comparison);
130 current_block_->AddInstruction(ifinst);
Calin Juravle225ff812014-11-13 16:46:39 +0000131 HBasicBlock* target = FindBlockStartingAt(dex_pc + target_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700132 DCHECK(target != nullptr);
133 current_block_->AddSuccessor(target);
Calin Juravle225ff812014-11-13 16:46:39 +0000134 target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
Dave Allison20dfc792014-06-16 20:44:29 -0700135 DCHECK(target != nullptr);
136 current_block_->AddSuccessor(target);
137 current_block_ = nullptr;
138}
139
140template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000141void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 int32_t target_offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +0000143 PotentiallyAddSuspendCheck(target_offset, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700144 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
145 T* comparison = new (arena_) T(value, GetIntConstant(0));
146 current_block_->AddInstruction(comparison);
147 HInstruction* ifinst = new (arena_) HIf(comparison);
148 current_block_->AddInstruction(ifinst);
Calin Juravle225ff812014-11-13 16:46:39 +0000149 HBasicBlock* target = FindBlockStartingAt(dex_pc + target_offset);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100150 DCHECK(target != nullptr);
151 current_block_->AddSuccessor(target);
Calin Juravle225ff812014-11-13 16:46:39 +0000152 target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100153 DCHECK(target != nullptr);
154 current_block_->AddSuccessor(target);
155 current_block_ = nullptr;
156}
157
Nicolas 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
Calin Juravle225ff812014-11-13 16:46:39 +0000199 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000200 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000201 // Update the current block if dex_pc starts a new block.
202 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000203 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle225ff812014-11-13 16:46:39 +0000204 if (!AnalyzeDexInstruction(instruction, dex_pc)) return nullptr;
205 dex_pc += 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.
Calin Juravle225ff812014-11-13 16:46:39 +0000246 size_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000247 while (code_ptr < code_end) {
248 const Instruction& instruction = *Instruction::At(code_ptr);
249 if (instruction.IsBranch()) {
Calin Juravle225ff812014-11-13 16:46:39 +0000250 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000251 // 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 }
Calin Juravle225ff812014-11-13 16:46:39 +0000256 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000257 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000258 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_pc) == nullptr)) {
259 block = new (arena_) HBasicBlock(graph_, dex_pc);
260 branch_targets_.Put(dex_pc, block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000261 }
262 } else {
263 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000264 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000265 }
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,
Calin Juravle225ff812014-11-13 16:46:39 +0000359 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100360 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;
Calin Juravle225ff812014-11-13 16:46:39 +0000407 compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, 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(
Calin Juravle225ff812014-11-13 16:46:39 +0000416 arena_, number_of_arguments, return_type, dex_pc, 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(
Calin Juravle225ff812014-11-13 16:46:39 +0000419 arena_, number_of_arguments, return_type, dex_pc, 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(
Calin Juravle225ff812014-11-13 16:46:39 +0000425 arena_, number_of_arguments, return_type, dex_pc, 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(
Calin Juravle225ff812014-11-13 16:46:39 +0000431 arena_, number_of_arguments, return_type, dex_pc, method_idx);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100432 }
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);
Calin Juravle225ff812014-11-13 16:46:39 +0000438 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100439 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()
Calin Juravle225ff812014-11-13 16:46:39 +0000452 << " at " << dex_pc;
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100453 // 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,
Calin Juravle225ff812014-11-13 16:46:39 +0000470 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100471 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);
Calin Juravle225ff812014-11-13 16:46:39 +0000491 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100492 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,
Calin Juravle225ff812014-11-13 16:46:39 +0000517 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100518 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(
Calin Juravle225ff812014-11-13 16:46:39 +0000547 storage_index, is_referrers_class, dex_pc);
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) {
Calin Juravle225ff812014-11-13 16:46:39 +0000552 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100553 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,
Calin Juravle225ff812014-11-13 16:46:39 +0000605 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100606 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);
Calin Juravle225ff812014-11-13 16:46:39 +0000616 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100617 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);
Calin Juravle225ff812014-11-13 16:46:39 +0000624 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100625 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(
Calin Juravle225ff812014-11-13 16:46:39 +0000631 object, index, value, anticipated_type, dex_pc));
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
Calin Juravle225ff812014-11-13 16:46:39 +0000638void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100639 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);
Calin Juravle225ff812014-11-13 16:46:39 +0000645 HInstruction* object = new (arena_) HNewArray(length, dex_pc, type_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100646 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(
Calin Juravle225ff812014-11-13 16:46:39 +0000663 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100664 }
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,
Calin Juravle225ff812014-11-13 16:46:39 +0000673 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100674 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(
Calin Juravle225ff812014-11-13 16:46:39 +0000678 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100679 }
680}
681
Calin Juravle225ff812014-11-13 16:46:39 +0000682void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
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);
Calin Juravle225ff812014-11-13 16:46:39 +0000685 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000686 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
Calin Juravle225ff812014-11-13 16:46:39 +0000692 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +0000693 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);
Calin Juravle225ff812014-11-13 16:46:39 +0000701 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +0000702
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,
Calin Juravle225ff812014-11-13 16:46:39 +0000709 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000710 break;
711 case 2:
712 BuildFillArrayData(null_check,
713 reinterpret_cast<const int16_t*>(data),
714 element_count,
715 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +0000716 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000717 break;
718 case 4:
719 BuildFillArrayData(null_check,
720 reinterpret_cast<const int32_t*>(data),
721 element_count,
722 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +0000723 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000724 break;
725 case 8:
726 BuildFillWideArrayData(null_check,
727 reinterpret_cast<const int64_t*>(data),
728 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000729 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000730 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,
Calin Juravle225ff812014-11-13 16:46:39 +0000739 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100740 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(
Calin Juravle225ff812014-11-13 16:46:39 +0000744 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100745 }
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,
Calin Juravle225ff812014-11-13 16:46:39 +0000752 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000753 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);
Calin Juravle225ff812014-11-13 16:46:39 +0000763 HLoadClass* cls = new (arena_) HLoadClass(type_index, is_referrers_class, dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000764 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(
Calin Juravle225ff812014-11-13 16:46:39 +0000770 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000771 UpdateLocal(destination, current_block_->GetLastInstruction());
772 } else {
773 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
774 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000775 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000776 }
777 return true;
778}
779
Calin Juravle225ff812014-11-13 16:46:39 +0000780void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000781 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.
Calin Juravle225ff812014-11-13 16:46:39 +0000784 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000785 }
786}
787
Calin Juravle225ff812014-11-13 16:46:39 +0000788bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
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) \
Calin Juravle225ff812014-11-13 16:46:39 +0000891 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
892 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); 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();
Calin Juravle225ff812014-11-13 16:46:39 +0000905 PotentiallyAddSuspendCheck(offset, dex_pc);
906 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
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);
Calin Juravle225ff812014-11-13 16:46:39 +0000943 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000944 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();
Calin Juravle225ff812014-11-13 16:46:39 +0000958 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100959 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
Roland Levillain51d3fc42014-11-13 14:11:42 +00001005 case Instruction::INT_TO_BYTE: {
1006 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte);
1007 break;
1008 }
1009
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001010 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001011 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001012 break;
1013 }
1014
1015 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001016 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001017 break;
1018 }
1019
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001020 case Instruction::ADD_DOUBLE: {
1021 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1022 break;
1023 }
1024
1025 case Instruction::ADD_FLOAT: {
1026 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1027 break;
1028 }
1029
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001030 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001031 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001032 break;
1033 }
1034
1035 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001036 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001037 break;
1038 }
1039
Calin Juravle096cc022014-10-23 17:01:13 +01001040 case Instruction::SUB_FLOAT: {
1041 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1042 break;
1043 }
1044
1045 case Instruction::SUB_DOUBLE: {
1046 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1047 break;
1048 }
1049
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001050 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001051 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1052 break;
1053 }
1054
Calin Juravle34bacdf2014-10-07 20:23:36 +01001055 case Instruction::MUL_INT: {
1056 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1057 break;
1058 }
1059
1060 case Instruction::MUL_LONG: {
1061 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1062 break;
1063 }
1064
Calin Juravleb5bfa962014-10-21 18:02:24 +01001065 case Instruction::MUL_FLOAT: {
1066 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1067 break;
1068 }
1069
1070 case Instruction::MUL_DOUBLE: {
1071 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1072 break;
1073 }
1074
Calin Juravled0d48522014-11-04 16:40:20 +00001075 case Instruction::DIV_INT: {
Calin Juravle865fc882014-11-06 17:09:03 +00001076 BuildCheckedDiv(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Calin Juravle225ff812014-11-13 16:46:39 +00001077 dex_pc, Primitive::kPrimInt, false);
Calin Juravled0d48522014-11-04 16:40:20 +00001078 break;
1079 }
1080
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001081 case Instruction::DIV_LONG: {
1082 BuildCheckedDiv(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Calin Juravle225ff812014-11-13 16:46:39 +00001083 dex_pc, Primitive::kPrimLong, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001084 break;
1085 }
1086
Calin Juravle7c4954d2014-10-28 16:57:40 +00001087 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001088 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001089 break;
1090 }
1091
1092 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001093 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001094 break;
1095 }
1096
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001097 case Instruction::AND_INT: {
1098 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1099 break;
1100 }
1101
1102 case Instruction::AND_LONG: {
1103 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1104 break;
1105 }
1106
1107 case Instruction::OR_INT: {
1108 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1109 break;
1110 }
1111
1112 case Instruction::OR_LONG: {
1113 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1114 break;
1115 }
1116
1117 case Instruction::XOR_INT: {
1118 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1119 break;
1120 }
1121
1122 case Instruction::XOR_LONG: {
1123 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1124 break;
1125 }
1126
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001127 case Instruction::ADD_LONG_2ADDR: {
1128 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001129 break;
1130 }
1131
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001132 case Instruction::ADD_DOUBLE_2ADDR: {
1133 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1134 break;
1135 }
1136
1137 case Instruction::ADD_FLOAT_2ADDR: {
1138 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
1139 break;
1140 }
1141
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001142 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001143 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
1144 break;
1145 }
1146
1147 case Instruction::SUB_LONG_2ADDR: {
1148 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001149 break;
1150 }
1151
Calin Juravle096cc022014-10-23 17:01:13 +01001152 case Instruction::SUB_FLOAT_2ADDR: {
1153 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
1154 break;
1155 }
1156
1157 case Instruction::SUB_DOUBLE_2ADDR: {
1158 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
1159 break;
1160 }
1161
Calin Juravle34bacdf2014-10-07 20:23:36 +01001162 case Instruction::MUL_INT_2ADDR: {
1163 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
1164 break;
1165 }
1166
1167 case Instruction::MUL_LONG_2ADDR: {
1168 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
1169 break;
1170 }
1171
Calin Juravleb5bfa962014-10-21 18:02:24 +01001172 case Instruction::MUL_FLOAT_2ADDR: {
1173 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
1174 break;
1175 }
1176
1177 case Instruction::MUL_DOUBLE_2ADDR: {
1178 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
1179 break;
1180 }
1181
Calin Juravle865fc882014-11-06 17:09:03 +00001182 case Instruction::DIV_INT_2ADDR: {
1183 BuildCheckedDiv(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Calin Juravle225ff812014-11-13 16:46:39 +00001184 dex_pc, Primitive::kPrimInt, false);
Calin Juravle865fc882014-11-06 17:09:03 +00001185 break;
1186 }
1187
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001188 case Instruction::DIV_LONG_2ADDR: {
1189 BuildCheckedDiv(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Calin Juravle225ff812014-11-13 16:46:39 +00001190 dex_pc, Primitive::kPrimLong, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001191 break;
1192 }
1193
Calin Juravle7c4954d2014-10-28 16:57:40 +00001194 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001195 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001196 break;
1197 }
1198
1199 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001200 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001201 break;
1202 }
1203
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001204 case Instruction::AND_INT_2ADDR: {
1205 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
1206 break;
1207 }
1208
1209 case Instruction::AND_LONG_2ADDR: {
1210 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
1211 break;
1212 }
1213
1214 case Instruction::OR_INT_2ADDR: {
1215 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
1216 break;
1217 }
1218
1219 case Instruction::OR_LONG_2ADDR: {
1220 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
1221 break;
1222 }
1223
1224 case Instruction::XOR_INT_2ADDR: {
1225 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
1226 break;
1227 }
1228
1229 case Instruction::XOR_LONG_2ADDR: {
1230 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
1231 break;
1232 }
1233
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001234 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001235 Binop_22s<HAdd>(instruction, false);
1236 break;
1237 }
1238
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001239 case Instruction::AND_INT_LIT16: {
1240 Binop_22s<HAnd>(instruction, false);
1241 break;
1242 }
1243
1244 case Instruction::OR_INT_LIT16: {
1245 Binop_22s<HOr>(instruction, false);
1246 break;
1247 }
1248
1249 case Instruction::XOR_INT_LIT16: {
1250 Binop_22s<HXor>(instruction, false);
1251 break;
1252 }
1253
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001254 case Instruction::RSUB_INT: {
1255 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001256 break;
1257 }
1258
Calin Juravle34bacdf2014-10-07 20:23:36 +01001259 case Instruction::MUL_INT_LIT16: {
1260 Binop_22s<HMul>(instruction, false);
1261 break;
1262 }
1263
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001264 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001265 Binop_22b<HAdd>(instruction, false);
1266 break;
1267 }
1268
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001269 case Instruction::AND_INT_LIT8: {
1270 Binop_22b<HAnd>(instruction, false);
1271 break;
1272 }
1273
1274 case Instruction::OR_INT_LIT8: {
1275 Binop_22b<HOr>(instruction, false);
1276 break;
1277 }
1278
1279 case Instruction::XOR_INT_LIT8: {
1280 Binop_22b<HXor>(instruction, false);
1281 break;
1282 }
1283
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001284 case Instruction::RSUB_INT_LIT8: {
1285 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001286 break;
1287 }
1288
Calin Juravle34bacdf2014-10-07 20:23:36 +01001289 case Instruction::MUL_INT_LIT8: {
1290 Binop_22b<HMul>(instruction, false);
1291 break;
1292 }
1293
Calin Juravled0d48522014-11-04 16:40:20 +00001294 case Instruction::DIV_INT_LIT16:
1295 case Instruction::DIV_INT_LIT8: {
Calin Juravle865fc882014-11-06 17:09:03 +00001296 BuildCheckedDiv(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Calin Juravle225ff812014-11-13 16:46:39 +00001297 dex_pc, Primitive::kPrimInt, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001298 break;
1299 }
1300
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001301 case Instruction::NEW_INSTANCE: {
1302 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001303 new (arena_) HNewInstance(dex_pc, instruction.VRegB_21c()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001304 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1305 break;
1306 }
1307
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001308 case Instruction::NEW_ARRAY: {
1309 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
1310 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001311 new (arena_) HNewArray(length, dex_pc, instruction.VRegC_22c()));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001312 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1313 break;
1314 }
1315
1316 case Instruction::FILLED_NEW_ARRAY: {
1317 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1318 uint32_t type_index = instruction.VRegB_35c();
1319 uint32_t args[5];
1320 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001321 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001322 break;
1323 }
1324
1325 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1326 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1327 uint32_t type_index = instruction.VRegB_3rc();
1328 uint32_t register_index = instruction.VRegC_3rc();
1329 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00001330 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001331 break;
1332 }
1333
1334 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00001335 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001336 break;
1337 }
1338
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001339 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001340 case Instruction::MOVE_RESULT_WIDE:
1341 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001342 UpdateLocal(instruction.VRegA(), latest_result_);
1343 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001344 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001345
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001346 case Instruction::CMP_LONG: {
1347 Binop_23x<HCompare>(instruction, Primitive::kPrimLong);
1348 break;
1349 }
1350
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001351 case Instruction::NOP:
1352 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001353
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001354 case Instruction::IGET:
1355 case Instruction::IGET_WIDE:
1356 case Instruction::IGET_OBJECT:
1357 case Instruction::IGET_BOOLEAN:
1358 case Instruction::IGET_BYTE:
1359 case Instruction::IGET_CHAR:
1360 case Instruction::IGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001361 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001362 return false;
1363 }
1364 break;
1365 }
1366
1367 case Instruction::IPUT:
1368 case Instruction::IPUT_WIDE:
1369 case Instruction::IPUT_OBJECT:
1370 case Instruction::IPUT_BOOLEAN:
1371 case Instruction::IPUT_BYTE:
1372 case Instruction::IPUT_CHAR:
1373 case Instruction::IPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001374 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001375 return false;
1376 }
1377 break;
1378 }
1379
1380 case Instruction::SGET:
1381 case Instruction::SGET_WIDE:
1382 case Instruction::SGET_OBJECT:
1383 case Instruction::SGET_BOOLEAN:
1384 case Instruction::SGET_BYTE:
1385 case Instruction::SGET_CHAR:
1386 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001387 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001388 return false;
1389 }
1390 break;
1391 }
1392
1393 case Instruction::SPUT:
1394 case Instruction::SPUT_WIDE:
1395 case Instruction::SPUT_OBJECT:
1396 case Instruction::SPUT_BOOLEAN:
1397 case Instruction::SPUT_BYTE:
1398 case Instruction::SPUT_CHAR:
1399 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001400 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001401 return false;
1402 }
1403 break;
1404 }
1405
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001406#define ARRAY_XX(kind, anticipated_type) \
1407 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001408 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001409 break; \
1410 } \
1411 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001412 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001413 break; \
1414 }
1415
1416 ARRAY_XX(, Primitive::kPrimInt);
1417 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1418 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1419 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1420 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1421 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1422 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1423
Nicolas Geoffray39468442014-09-02 15:17:15 +01001424 case Instruction::ARRAY_LENGTH: {
1425 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001426 // No need for a temporary for the null check, it is the only input of the following
1427 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00001428 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001429 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001430 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1431 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1432 break;
1433 }
1434
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001435 case Instruction::CONST_STRING: {
Calin Juravle225ff812014-11-13 16:46:39 +00001436 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001437 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1438 break;
1439 }
1440
1441 case Instruction::CONST_STRING_JUMBO: {
Calin Juravle225ff812014-11-13 16:46:39 +00001442 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001443 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
1444 break;
1445 }
1446
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001447 case Instruction::CONST_CLASS: {
1448 uint16_t type_index = instruction.VRegB_21c();
1449 bool type_known_final;
1450 bool type_known_abstract;
1451 bool is_referrers_class;
1452 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1453 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
1454 &type_known_final, &type_known_abstract, &is_referrers_class);
1455 if (!can_access) {
1456 return false;
1457 }
1458 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001459 new (arena_) HLoadClass(type_index, is_referrers_class, dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001460 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1461 break;
1462 }
1463
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001464 case Instruction::MOVE_EXCEPTION: {
1465 current_block_->AddInstruction(new (arena_) HLoadException());
1466 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
1467 break;
1468 }
1469
1470 case Instruction::THROW: {
1471 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001472 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001473 // A throw instruction must branch to the exit block.
1474 current_block_->AddSuccessor(exit_block_);
1475 // We finished building this block. Set the current block to null to avoid
1476 // adding dead instructions to it.
1477 current_block_ = nullptr;
1478 break;
1479 }
1480
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001481 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001482 uint8_t destination = instruction.VRegA_22c();
1483 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001484 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00001485 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001486 return false;
1487 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001488 break;
1489 }
1490
1491 case Instruction::CHECK_CAST: {
1492 uint8_t reference = instruction.VRegA_21c();
1493 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00001494 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001495 return false;
1496 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001497 break;
1498 }
1499
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001500 case Instruction::MONITOR_ENTER: {
1501 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1502 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1503 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00001504 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001505 break;
1506 }
1507
1508 case Instruction::MONITOR_EXIT: {
1509 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1510 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1511 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00001512 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001513 break;
1514 }
1515
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001516 default:
1517 return false;
1518 }
1519 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001520} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001521
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001522HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001523 if (constant0_ != nullptr) {
1524 return constant0_;
1525 }
1526 constant0_ = new(arena_) HIntConstant(0);
1527 entry_block_->AddInstruction(constant0_);
1528 return constant0_;
1529}
1530
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001531HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001532 if (constant1_ != nullptr) {
1533 return constant1_;
1534 }
1535 constant1_ = new(arena_) HIntConstant(1);
1536 entry_block_->AddInstruction(constant1_);
1537 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001538}
1539
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001540HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001541 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001542 case 0: return GetIntConstant0();
1543 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001544 default: {
1545 HIntConstant* instruction = new (arena_) HIntConstant(constant);
1546 entry_block_->AddInstruction(instruction);
1547 return instruction;
1548 }
1549 }
1550}
1551
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001552HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
1553 HLongConstant* instruction = new (arena_) HLongConstant(constant);
1554 entry_block_->AddInstruction(instruction);
1555 return instruction;
1556}
1557
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001558HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
1559 return locals_.Get(register_index);
1560}
1561
1562void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
1563 HLocal* local = GetLocalAt(register_index);
1564 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
1565}
1566
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001567HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001568 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001569 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001570 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001571}
1572
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001573} // namespace art