blob: 35360fef6ce3db896a3b89f7d486239f8c6d288c [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>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000308void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
309 Primitive::Type type) {
310 HInstruction* first = LoadLocal(instruction.VRegB(), type);
311 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
312 current_block_->AddInstruction(new (arena_) T(type, first, second));
313 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
314}
315
316template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100317void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
318 HInstruction* first = LoadLocal(instruction.VRegA(), type);
319 HInstruction* second = LoadLocal(instruction.VRegB(), type);
320 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100321 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
322}
323
324template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000325void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) {
326 HInstruction* first = LoadLocal(instruction.VRegA(), type);
327 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
328 current_block_->AddInstruction(new (arena_) T(type, first, second));
329 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
330}
331
332template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000333void HGraphBuilder::Binop_12x(const Instruction& instruction,
334 Primitive::Type type,
335 uint32_t dex_pc) {
336 HInstruction* first = LoadLocal(instruction.VRegA(), type);
337 HInstruction* second = LoadLocal(instruction.VRegB(), type);
338 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
339 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
340}
341
342template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100343void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100344 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
345 HInstruction* second = GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100346 if (reverse) {
347 std::swap(first, second);
348 }
349 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
350 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
351}
352
353template<typename T>
354void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100355 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
356 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100357 if (reverse) {
358 std::swap(first, second);
359 }
360 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
361 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
362}
363
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100364void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
365 if (type == Primitive::kPrimVoid) {
366 current_block_->AddInstruction(new (arena_) HReturnVoid());
367 } else {
368 HInstruction* value = LoadLocal(instruction.VRegA(), type);
369 current_block_->AddInstruction(new (arena_) HReturn(value));
370 }
371 current_block_->AddSuccessor(exit_block_);
372 current_block_ = nullptr;
373}
374
375bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000376 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100377 uint32_t method_idx,
378 uint32_t number_of_vreg_arguments,
379 bool is_range,
380 uint32_t* args,
381 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100382 Instruction::Code opcode = instruction.Opcode();
383 InvokeType invoke_type;
384 switch (opcode) {
385 case Instruction::INVOKE_STATIC:
386 case Instruction::INVOKE_STATIC_RANGE:
387 invoke_type = kStatic;
388 break;
389 case Instruction::INVOKE_DIRECT:
390 case Instruction::INVOKE_DIRECT_RANGE:
391 invoke_type = kDirect;
392 break;
393 case Instruction::INVOKE_VIRTUAL:
394 case Instruction::INVOKE_VIRTUAL_RANGE:
395 invoke_type = kVirtual;
396 break;
397 case Instruction::INVOKE_INTERFACE:
398 case Instruction::INVOKE_INTERFACE_RANGE:
399 invoke_type = kInterface;
400 break;
401 case Instruction::INVOKE_SUPER_RANGE:
402 case Instruction::INVOKE_SUPER:
403 invoke_type = kSuper;
404 break;
405 default:
406 LOG(FATAL) << "Unexpected invoke op: " << opcode;
407 return false;
408 }
409
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100410 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
411 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
412 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
413 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100414 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100415 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
416
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100417 HInvoke* invoke = nullptr;
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000418 if (invoke_type == kVirtual || invoke_type == kInterface || invoke_type == kSuper) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100419 MethodReference target_method(dex_file_, method_idx);
420 uintptr_t direct_code;
421 uintptr_t direct_method;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000422 int table_index;
423 InvokeType optimized_invoke_type = invoke_type;
Calin Juravle225ff812014-11-13 16:46:39 +0000424 compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000425 &optimized_invoke_type, &target_method, &table_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100426 &direct_code, &direct_method);
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000427 if (table_index == -1) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100428 return false;
429 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000430
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000431 if (optimized_invoke_type == kVirtual) {
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000432 invoke = new (arena_) HInvokeVirtual(
Calin Juravle225ff812014-11-13 16:46:39 +0000433 arena_, number_of_arguments, return_type, dex_pc, table_index);
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000434 } else if (optimized_invoke_type == kInterface) {
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000435 invoke = new (arena_) HInvokeInterface(
Calin Juravle225ff812014-11-13 16:46:39 +0000436 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000437 } else if (optimized_invoke_type == kDirect) {
438 // For this compiler, sharpening only works if we compile PIC.
439 DCHECK(compiler_driver_->GetCompilerOptions().GetCompilePic());
440 // Treat invoke-direct like static calls for now.
441 invoke = new (arena_) HInvokeStatic(
Calin Juravle225ff812014-11-13 16:46:39 +0000442 arena_, number_of_arguments, return_type, dex_pc, target_method.dex_method_index);
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000443 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100444 } else {
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000445 DCHECK(invoke_type == kDirect || invoke_type == kStatic);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100446 // Treat invoke-direct like static calls for now.
447 invoke = new (arena_) HInvokeStatic(
Calin Juravle225ff812014-11-13 16:46:39 +0000448 arena_, number_of_arguments, return_type, dex_pc, method_idx);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100449 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100450
451 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000452 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100453 if (is_instance_call) {
454 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000455 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100456 current_block_->AddInstruction(null_check);
457 temps.Add(null_check);
458 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100459 start_index = 1;
460 }
461
462 uint32_t descriptor_index = 1;
463 uint32_t argument_index = start_index;
464 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
465 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100466 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
467 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100468 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
Calin Juravle225ff812014-11-13 16:46:39 +0000469 << " at " << dex_pc;
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100470 // We do not implement non sequential register pair.
471 return false;
472 }
473 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
474 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100475 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100476 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100477 }
478 }
479
480 DCHECK_EQ(argument_index, number_of_arguments);
481 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100482 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100483 return true;
484}
485
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100486bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000487 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100488 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100489 uint32_t source_or_dest_reg = instruction.VRegA_22c();
490 uint32_t obj_reg = instruction.VRegB_22c();
491 uint16_t field_index = instruction.VRegC_22c();
492
493 ScopedObjectAccess soa(Thread::Current());
494 StackHandleScope<1> hs(soa.Self());
495 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
496 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
497
498 if (resolved_field.Get() == nullptr) {
499 return false;
500 }
501 if (resolved_field->IsVolatile()) {
502 return false;
503 }
504
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100505 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100506
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100507 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000508 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100509 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000510 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100511 HInstruction* null_check = current_block_->GetLastInstruction();
512 // We need one temporary for the null check.
513 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100514 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100515 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
516 null_check,
517 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100518 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100519 resolved_field->GetOffset()));
520 } else {
521 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
522 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100523 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100524 resolved_field->GetOffset()));
525
526 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
527 }
528 return true;
529}
530
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100531
532
533bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000534 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100535 bool is_put) {
536 uint32_t source_or_dest_reg = instruction.VRegA_21c();
537 uint16_t field_index = instruction.VRegB_21c();
538
539 uint32_t storage_index;
540 bool is_referrers_class;
541 bool is_initialized;
542 bool is_volatile;
543 MemberOffset field_offset(0u);
544 Primitive::Type field_type;
545
546 bool fast_path = compiler_driver_->ComputeStaticFieldInfo(field_index,
547 dex_compilation_unit_,
548 is_put,
549 &field_offset,
550 &storage_index,
551 &is_referrers_class,
552 &is_volatile,
553 &is_initialized,
554 &field_type);
555 if (!fast_path) {
556 return false;
557 }
558
559 if (is_volatile) {
560 return false;
561 }
562
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100563 HLoadClass* constant = new (arena_) HLoadClass(
Calin Juravle225ff812014-11-13 16:46:39 +0000564 storage_index, is_referrers_class, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100565 current_block_->AddInstruction(constant);
566
567 HInstruction* cls = constant;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000568 if (!is_initialized) {
Calin Juravle225ff812014-11-13 16:46:39 +0000569 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100570 current_block_->AddInstruction(cls);
571 }
572
573 if (is_put) {
574 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000575 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100576 temps.Add(cls);
577 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
578 DCHECK_EQ(value->GetType(), field_type);
579 current_block_->AddInstruction(
580 new (arena_) HStaticFieldSet(cls, value, field_type, field_offset));
581 } else {
582 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls, field_type, field_offset));
583 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
584 }
585 return true;
586}
587
Calin Juravlebacfec32014-11-14 15:54:36 +0000588void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
589 uint16_t first_vreg,
590 int64_t second_vreg_or_constant,
591 uint32_t dex_pc,
592 Primitive::Type type,
593 bool second_is_constant,
594 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000595 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +0000596
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000597 HInstruction* first = LoadLocal(first_vreg, type);
598 HInstruction* second = nullptr;
599 if (second_is_constant) {
600 if (type == Primitive::kPrimInt) {
601 second = GetIntConstant(second_vreg_or_constant);
602 } else {
603 second = GetLongConstant(second_vreg_or_constant);
604 }
605 } else {
606 second = LoadLocal(second_vreg_or_constant, type);
607 }
608
609 if (!second_is_constant
610 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
611 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
612 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000613 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000614 current_block_->AddInstruction(second);
615 temps.Add(current_block_->GetLastInstruction());
616 }
617
Calin Juravlebacfec32014-11-14 15:54:36 +0000618 if (isDiv) {
619 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
620 } else {
621 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
622 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000623 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +0000624}
625
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100626void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000627 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100628 bool is_put,
629 Primitive::Type anticipated_type) {
630 uint8_t source_or_dest_reg = instruction.VRegA_23x();
631 uint8_t array_reg = instruction.VRegB_23x();
632 uint8_t index_reg = instruction.VRegC_23x();
633
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100634 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000635 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100636
637 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000638 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100639 current_block_->AddInstruction(object);
640 temps.Add(object);
641
642 HInstruction* length = new (arena_) HArrayLength(object);
643 current_block_->AddInstruction(length);
644 temps.Add(length);
645 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +0000646 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100647 current_block_->AddInstruction(index);
648 temps.Add(index);
649 if (is_put) {
650 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
651 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100652 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000653 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100654 } else {
655 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
656 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
657 }
658}
659
Calin Juravle225ff812014-11-13 16:46:39 +0000660void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100661 uint32_t type_index,
662 uint32_t number_of_vreg_arguments,
663 bool is_range,
664 uint32_t* args,
665 uint32_t register_index) {
666 HInstruction* length = GetIntConstant(number_of_vreg_arguments);
Calin Juravle225ff812014-11-13 16:46:39 +0000667 HInstruction* object = new (arena_) HNewArray(length, dex_pc, type_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100668 current_block_->AddInstruction(object);
669
670 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
671 DCHECK_EQ(descriptor[0], '[') << descriptor;
672 char primitive = descriptor[1];
673 DCHECK(primitive == 'I'
674 || primitive == 'L'
675 || primitive == '[') << descriptor;
676 bool is_reference_array = (primitive == 'L') || (primitive == '[');
677 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
678
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000679 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100680 temps.Add(object);
681 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
682 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
683 HInstruction* index = GetIntConstant(i);
684 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000685 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100686 }
687 latest_result_ = object;
688}
689
690template <typename T>
691void HGraphBuilder::BuildFillArrayData(HInstruction* object,
692 const T* data,
693 uint32_t element_count,
694 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +0000695 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100696 for (uint32_t i = 0; i < element_count; ++i) {
697 HInstruction* index = GetIntConstant(i);
698 HInstruction* value = GetIntConstant(data[i]);
699 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000700 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100701 }
702}
703
Calin Juravle225ff812014-11-13 16:46:39 +0000704void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000705 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000706 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000707 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000708 current_block_->AddInstruction(null_check);
709 temps.Add(null_check);
710
711 HInstruction* length = new (arena_) HArrayLength(null_check);
712 current_block_->AddInstruction(length);
713
Calin Juravle225ff812014-11-13 16:46:39 +0000714 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +0000715 const Instruction::ArrayDataPayload* payload =
716 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
717 const uint8_t* data = payload->data;
718 uint32_t element_count = payload->element_count;
719
720 // Implementation of this DEX instruction seems to be that the bounds check is
721 // done before doing any stores.
722 HInstruction* last_index = GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +0000723 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +0000724
725 switch (payload->element_width) {
726 case 1:
727 BuildFillArrayData(null_check,
728 reinterpret_cast<const int8_t*>(data),
729 element_count,
730 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +0000731 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000732 break;
733 case 2:
734 BuildFillArrayData(null_check,
735 reinterpret_cast<const int16_t*>(data),
736 element_count,
737 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +0000738 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000739 break;
740 case 4:
741 BuildFillArrayData(null_check,
742 reinterpret_cast<const int32_t*>(data),
743 element_count,
744 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +0000745 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000746 break;
747 case 8:
748 BuildFillWideArrayData(null_check,
749 reinterpret_cast<const int64_t*>(data),
750 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000751 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000752 break;
753 default:
754 LOG(FATAL) << "Unknown element width for " << payload->element_width;
755 }
756}
757
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100758void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100759 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100760 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000761 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100762 for (uint32_t i = 0; i < element_count; ++i) {
763 HInstruction* index = GetIntConstant(i);
764 HInstruction* value = GetLongConstant(data[i]);
765 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000766 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100767 }
768}
769
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000770bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
771 uint8_t destination,
772 uint8_t reference,
773 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +0000774 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000775 bool type_known_final;
776 bool type_known_abstract;
777 bool is_referrers_class;
778 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
779 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
780 &type_known_final, &type_known_abstract, &is_referrers_class);
781 if (!can_access) {
782 return false;
783 }
784 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000785 HLoadClass* cls = new (arena_) HLoadClass(type_index, is_referrers_class, dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000786 current_block_->AddInstruction(cls);
787 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000788 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000789 temps.Add(cls);
790 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
791 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000792 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000793 UpdateLocal(destination, current_block_->GetLastInstruction());
794 } else {
795 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
796 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000797 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000798 }
799 return true;
800}
801
Calin Juravle225ff812014-11-13 16:46:39 +0000802void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000803 if (target_offset <= 0) {
804 // Unconditionnally add a suspend check to backward branches. We can remove
805 // them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +0000806 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000807 }
808}
809
Calin Juravle225ff812014-11-13 16:46:39 +0000810bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000811 if (current_block_ == nullptr) {
812 return true; // Dead code
813 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000814
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000815 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000816 case Instruction::CONST_4: {
817 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100818 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000819 UpdateLocal(register_index, constant);
820 break;
821 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000822
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100823 case Instruction::CONST_16: {
824 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100825 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
826 UpdateLocal(register_index, constant);
827 break;
828 }
829
Dave Allison20dfc792014-06-16 20:44:29 -0700830 case Instruction::CONST: {
831 int32_t register_index = instruction.VRegA();
832 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
833 UpdateLocal(register_index, constant);
834 break;
835 }
836
837 case Instruction::CONST_HIGH16: {
838 int32_t register_index = instruction.VRegA();
839 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
840 UpdateLocal(register_index, constant);
841 break;
842 }
843
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100844 case Instruction::CONST_WIDE_16: {
845 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700846 // Get 16 bits of constant value, sign extended to 64 bits.
847 int64_t value = instruction.VRegB_21s();
848 value <<= 48;
849 value >>= 48;
850 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100851 UpdateLocal(register_index, constant);
852 break;
853 }
854
855 case Instruction::CONST_WIDE_32: {
856 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700857 // Get 32 bits of constant value, sign extended to 64 bits.
858 int64_t value = instruction.VRegB_31i();
859 value <<= 32;
860 value >>= 32;
861 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100862 UpdateLocal(register_index, constant);
863 break;
864 }
865
866 case Instruction::CONST_WIDE: {
867 int32_t register_index = instruction.VRegA();
868 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100869 UpdateLocal(register_index, constant);
870 break;
871 }
872
Dave Allison20dfc792014-06-16 20:44:29 -0700873 case Instruction::CONST_WIDE_HIGH16: {
874 int32_t register_index = instruction.VRegA();
875 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
876 HLongConstant* constant = GetLongConstant(value);
877 UpdateLocal(register_index, constant);
878 break;
879 }
880
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000881 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -0700882 case Instruction::MOVE:
883 case Instruction::MOVE_FROM16:
884 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100885 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100886 UpdateLocal(instruction.VRegA(), value);
887 break;
888 }
889
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000890 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -0700891 case Instruction::MOVE_WIDE:
892 case Instruction::MOVE_WIDE_FROM16:
893 case Instruction::MOVE_WIDE_16: {
894 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
895 UpdateLocal(instruction.VRegA(), value);
896 break;
897 }
898
899 case Instruction::MOVE_OBJECT:
900 case Instruction::MOVE_OBJECT_16:
901 case Instruction::MOVE_OBJECT_FROM16: {
902 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
903 UpdateLocal(instruction.VRegA(), value);
904 break;
905 }
906
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000907 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100908 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000909 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000910 }
911
Dave Allison20dfc792014-06-16 20:44:29 -0700912#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +0000913 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
914 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100915
Dave Allison20dfc792014-06-16 20:44:29 -0700916 IF_XX(HEqual, EQ);
917 IF_XX(HNotEqual, NE);
918 IF_XX(HLessThan, LT);
919 IF_XX(HLessThanOrEqual, LE);
920 IF_XX(HGreaterThan, GT);
921 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000922
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000923 case Instruction::GOTO:
924 case Instruction::GOTO_16:
925 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000926 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +0000927 PotentiallyAddSuspendCheck(offset, dex_pc);
928 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000929 DCHECK(target != nullptr);
930 current_block_->AddInstruction(new (arena_) HGoto());
931 current_block_->AddSuccessor(target);
932 current_block_ = nullptr;
933 break;
934 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000935
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100936 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100937 DCHECK_NE(return_type_, Primitive::kPrimNot);
938 DCHECK_NE(return_type_, Primitive::kPrimLong);
939 DCHECK_NE(return_type_, Primitive::kPrimDouble);
940 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100941 break;
942 }
943
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100944 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100945 DCHECK(return_type_ == Primitive::kPrimNot);
946 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100947 break;
948 }
949
950 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100951 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
952 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000953 break;
954 }
955
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100956 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000957 case Instruction::INVOKE_INTERFACE:
958 case Instruction::INVOKE_STATIC:
959 case Instruction::INVOKE_SUPER:
960 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000961 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100962 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100963 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -0700964 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +0000965 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +0000966 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100967 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100968 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100969 break;
970 }
971
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100972 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +0000973 case Instruction::INVOKE_INTERFACE_RANGE:
974 case Instruction::INVOKE_STATIC_RANGE:
975 case Instruction::INVOKE_SUPER_RANGE:
976 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100977 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100978 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
979 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +0000980 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100981 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100982 return false;
983 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000984 break;
985 }
986
Roland Levillain88cb1752014-10-20 16:36:47 +0100987 case Instruction::NEG_INT: {
988 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
989 break;
990 }
991
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100992 case Instruction::NEG_LONG: {
993 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
994 break;
995 }
996
Roland Levillain3dbcb382014-10-28 17:30:07 +0000997 case Instruction::NEG_FLOAT: {
998 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
999 break;
1000 }
1001
1002 case Instruction::NEG_DOUBLE: {
1003 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1004 break;
1005 }
1006
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001007 case Instruction::NOT_INT: {
1008 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1009 break;
1010 }
1011
Roland Levillain70566432014-10-24 16:20:17 +01001012 case Instruction::NOT_LONG: {
1013 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1014 break;
1015 }
1016
Roland Levillaindff1f282014-11-05 14:15:05 +00001017 case Instruction::INT_TO_LONG: {
1018 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong);
1019 break;
1020 }
1021
Roland Levillaincff13742014-11-17 14:32:17 +00001022 case Instruction::INT_TO_FLOAT: {
1023 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat);
1024 break;
1025 }
1026
1027 case Instruction::INT_TO_DOUBLE: {
1028 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble);
1029 break;
1030 }
1031
Roland Levillain946e1432014-11-11 17:35:19 +00001032 case Instruction::LONG_TO_INT: {
1033 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt);
1034 break;
1035 }
1036
Roland Levillain647b9ed2014-11-27 12:06:00 +00001037 case Instruction::LONG_TO_DOUBLE: {
1038 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble);
1039 break;
1040 }
1041
Roland Levillain51d3fc42014-11-13 14:11:42 +00001042 case Instruction::INT_TO_BYTE: {
1043 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte);
1044 break;
1045 }
1046
Roland Levillain01a8d712014-11-14 16:27:39 +00001047 case Instruction::INT_TO_SHORT: {
1048 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort);
1049 break;
1050 }
1051
Roland Levillain981e4542014-11-14 11:47:14 +00001052 case Instruction::INT_TO_CHAR: {
1053 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar);
1054 break;
1055 }
1056
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001057 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001058 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001059 break;
1060 }
1061
1062 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001063 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001064 break;
1065 }
1066
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001067 case Instruction::ADD_DOUBLE: {
1068 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1069 break;
1070 }
1071
1072 case Instruction::ADD_FLOAT: {
1073 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1074 break;
1075 }
1076
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001077 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001078 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001079 break;
1080 }
1081
1082 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001083 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001084 break;
1085 }
1086
Calin Juravle096cc022014-10-23 17:01:13 +01001087 case Instruction::SUB_FLOAT: {
1088 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1089 break;
1090 }
1091
1092 case Instruction::SUB_DOUBLE: {
1093 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1094 break;
1095 }
1096
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001097 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1099 break;
1100 }
1101
Calin Juravle34bacdf2014-10-07 20:23:36 +01001102 case Instruction::MUL_INT: {
1103 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1104 break;
1105 }
1106
1107 case Instruction::MUL_LONG: {
1108 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1109 break;
1110 }
1111
Calin Juravleb5bfa962014-10-21 18:02:24 +01001112 case Instruction::MUL_FLOAT: {
1113 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1114 break;
1115 }
1116
1117 case Instruction::MUL_DOUBLE: {
1118 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1119 break;
1120 }
1121
Calin Juravled0d48522014-11-04 16:40:20 +00001122 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001123 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1124 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001125 break;
1126 }
1127
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001128 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001129 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1130 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001131 break;
1132 }
1133
Calin Juravle7c4954d2014-10-28 16:57:40 +00001134 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001135 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001136 break;
1137 }
1138
1139 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001140 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001141 break;
1142 }
1143
Calin Juravlebacfec32014-11-14 15:54:36 +00001144 case Instruction::REM_INT: {
1145 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1146 dex_pc, Primitive::kPrimInt, false, false);
1147 break;
1148 }
1149
1150 case Instruction::REM_LONG: {
1151 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1152 dex_pc, Primitive::kPrimLong, false, false);
1153 break;
1154 }
1155
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001156 case Instruction::AND_INT: {
1157 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1158 break;
1159 }
1160
1161 case Instruction::AND_LONG: {
1162 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1163 break;
1164 }
1165
Calin Juravle9aec02f2014-11-18 23:06:35 +00001166 case Instruction::SHL_INT: {
1167 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
1168 break;
1169 }
1170
1171 case Instruction::SHL_LONG: {
1172 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
1173 break;
1174 }
1175
1176 case Instruction::SHR_INT: {
1177 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
1178 break;
1179 }
1180
1181 case Instruction::SHR_LONG: {
1182 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
1183 break;
1184 }
1185
1186 case Instruction::USHR_INT: {
1187 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
1188 break;
1189 }
1190
1191 case Instruction::USHR_LONG: {
1192 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
1193 break;
1194 }
1195
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001196 case Instruction::OR_INT: {
1197 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1198 break;
1199 }
1200
1201 case Instruction::OR_LONG: {
1202 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1203 break;
1204 }
1205
1206 case Instruction::XOR_INT: {
1207 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1208 break;
1209 }
1210
1211 case Instruction::XOR_LONG: {
1212 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1213 break;
1214 }
1215
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001216 case Instruction::ADD_LONG_2ADDR: {
1217 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001218 break;
1219 }
1220
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001221 case Instruction::ADD_DOUBLE_2ADDR: {
1222 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1223 break;
1224 }
1225
1226 case Instruction::ADD_FLOAT_2ADDR: {
1227 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
1228 break;
1229 }
1230
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001231 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001232 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
1233 break;
1234 }
1235
1236 case Instruction::SUB_LONG_2ADDR: {
1237 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001238 break;
1239 }
1240
Calin Juravle096cc022014-10-23 17:01:13 +01001241 case Instruction::SUB_FLOAT_2ADDR: {
1242 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
1243 break;
1244 }
1245
1246 case Instruction::SUB_DOUBLE_2ADDR: {
1247 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
1248 break;
1249 }
1250
Calin Juravle34bacdf2014-10-07 20:23:36 +01001251 case Instruction::MUL_INT_2ADDR: {
1252 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
1253 break;
1254 }
1255
1256 case Instruction::MUL_LONG_2ADDR: {
1257 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
1258 break;
1259 }
1260
Calin Juravleb5bfa962014-10-21 18:02:24 +01001261 case Instruction::MUL_FLOAT_2ADDR: {
1262 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
1263 break;
1264 }
1265
1266 case Instruction::MUL_DOUBLE_2ADDR: {
1267 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
1268 break;
1269 }
1270
Calin Juravle865fc882014-11-06 17:09:03 +00001271 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001272 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1273 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00001274 break;
1275 }
1276
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001277 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001278 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1279 dex_pc, Primitive::kPrimLong, false, true);
1280 break;
1281 }
1282
1283 case Instruction::REM_INT_2ADDR: {
1284 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1285 dex_pc, Primitive::kPrimInt, false, false);
1286 break;
1287 }
1288
1289 case Instruction::REM_LONG_2ADDR: {
1290 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1291 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001292 break;
1293 }
1294
Calin Juravle9aec02f2014-11-18 23:06:35 +00001295 case Instruction::SHL_INT_2ADDR: {
1296 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
1297 break;
1298 }
1299
1300 case Instruction::SHL_LONG_2ADDR: {
1301 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
1302 break;
1303 }
1304
1305 case Instruction::SHR_INT_2ADDR: {
1306 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
1307 break;
1308 }
1309
1310 case Instruction::SHR_LONG_2ADDR: {
1311 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
1312 break;
1313 }
1314
1315 case Instruction::USHR_INT_2ADDR: {
1316 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
1317 break;
1318 }
1319
1320 case Instruction::USHR_LONG_2ADDR: {
1321 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
1322 break;
1323 }
1324
Calin Juravle7c4954d2014-10-28 16:57:40 +00001325 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001326 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001327 break;
1328 }
1329
1330 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001331 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001332 break;
1333 }
1334
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001335 case Instruction::AND_INT_2ADDR: {
1336 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
1337 break;
1338 }
1339
1340 case Instruction::AND_LONG_2ADDR: {
1341 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
1342 break;
1343 }
1344
1345 case Instruction::OR_INT_2ADDR: {
1346 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
1347 break;
1348 }
1349
1350 case Instruction::OR_LONG_2ADDR: {
1351 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
1352 break;
1353 }
1354
1355 case Instruction::XOR_INT_2ADDR: {
1356 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
1357 break;
1358 }
1359
1360 case Instruction::XOR_LONG_2ADDR: {
1361 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
1362 break;
1363 }
1364
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001365 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001366 Binop_22s<HAdd>(instruction, false);
1367 break;
1368 }
1369
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001370 case Instruction::AND_INT_LIT16: {
1371 Binop_22s<HAnd>(instruction, false);
1372 break;
1373 }
1374
1375 case Instruction::OR_INT_LIT16: {
1376 Binop_22s<HOr>(instruction, false);
1377 break;
1378 }
1379
1380 case Instruction::XOR_INT_LIT16: {
1381 Binop_22s<HXor>(instruction, false);
1382 break;
1383 }
1384
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001385 case Instruction::RSUB_INT: {
1386 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001387 break;
1388 }
1389
Calin Juravle34bacdf2014-10-07 20:23:36 +01001390 case Instruction::MUL_INT_LIT16: {
1391 Binop_22s<HMul>(instruction, false);
1392 break;
1393 }
1394
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001395 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001396 Binop_22b<HAdd>(instruction, false);
1397 break;
1398 }
1399
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001400 case Instruction::AND_INT_LIT8: {
1401 Binop_22b<HAnd>(instruction, false);
1402 break;
1403 }
1404
1405 case Instruction::OR_INT_LIT8: {
1406 Binop_22b<HOr>(instruction, false);
1407 break;
1408 }
1409
1410 case Instruction::XOR_INT_LIT8: {
1411 Binop_22b<HXor>(instruction, false);
1412 break;
1413 }
1414
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001415 case Instruction::RSUB_INT_LIT8: {
1416 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001417 break;
1418 }
1419
Calin Juravle34bacdf2014-10-07 20:23:36 +01001420 case Instruction::MUL_INT_LIT8: {
1421 Binop_22b<HMul>(instruction, false);
1422 break;
1423 }
1424
Calin Juravled0d48522014-11-04 16:40:20 +00001425 case Instruction::DIV_INT_LIT16:
1426 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001427 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1428 dex_pc, Primitive::kPrimInt, true, true);
1429 break;
1430 }
1431
1432 case Instruction::REM_INT_LIT16:
1433 case Instruction::REM_INT_LIT8: {
1434 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1435 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00001436 break;
1437 }
1438
Calin Juravle9aec02f2014-11-18 23:06:35 +00001439 case Instruction::SHL_INT_LIT8: {
1440 Binop_22b<HShl>(instruction, false);
1441 break;
1442 }
1443
1444 case Instruction::SHR_INT_LIT8: {
1445 Binop_22b<HShr>(instruction, false);
1446 break;
1447 }
1448
1449 case Instruction::USHR_INT_LIT8: {
1450 Binop_22b<HUShr>(instruction, false);
1451 break;
1452 }
1453
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001454 case Instruction::NEW_INSTANCE: {
1455 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001456 new (arena_) HNewInstance(dex_pc, instruction.VRegB_21c()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001457 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1458 break;
1459 }
1460
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001461 case Instruction::NEW_ARRAY: {
1462 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
1463 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001464 new (arena_) HNewArray(length, dex_pc, instruction.VRegC_22c()));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001465 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1466 break;
1467 }
1468
1469 case Instruction::FILLED_NEW_ARRAY: {
1470 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1471 uint32_t type_index = instruction.VRegB_35c();
1472 uint32_t args[5];
1473 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001474 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001475 break;
1476 }
1477
1478 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1479 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1480 uint32_t type_index = instruction.VRegB_3rc();
1481 uint32_t register_index = instruction.VRegC_3rc();
1482 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00001483 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001484 break;
1485 }
1486
1487 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00001488 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001489 break;
1490 }
1491
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001492 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001493 case Instruction::MOVE_RESULT_WIDE:
1494 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001495 UpdateLocal(instruction.VRegA(), latest_result_);
1496 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001497 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001498
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001499 case Instruction::CMP_LONG: {
Calin Juravle91debbc2014-11-26 19:01:09 +00001500 Binop_23x<HCompare>(instruction, Primitive::kPrimLong);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001501 break;
1502 }
1503
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001504 case Instruction::NOP:
1505 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001506
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001507 case Instruction::IGET:
1508 case Instruction::IGET_WIDE:
1509 case Instruction::IGET_OBJECT:
1510 case Instruction::IGET_BOOLEAN:
1511 case Instruction::IGET_BYTE:
1512 case Instruction::IGET_CHAR:
1513 case Instruction::IGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001514 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001515 return false;
1516 }
1517 break;
1518 }
1519
1520 case Instruction::IPUT:
1521 case Instruction::IPUT_WIDE:
1522 case Instruction::IPUT_OBJECT:
1523 case Instruction::IPUT_BOOLEAN:
1524 case Instruction::IPUT_BYTE:
1525 case Instruction::IPUT_CHAR:
1526 case Instruction::IPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001527 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001528 return false;
1529 }
1530 break;
1531 }
1532
1533 case Instruction::SGET:
1534 case Instruction::SGET_WIDE:
1535 case Instruction::SGET_OBJECT:
1536 case Instruction::SGET_BOOLEAN:
1537 case Instruction::SGET_BYTE:
1538 case Instruction::SGET_CHAR:
1539 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001540 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001541 return false;
1542 }
1543 break;
1544 }
1545
1546 case Instruction::SPUT:
1547 case Instruction::SPUT_WIDE:
1548 case Instruction::SPUT_OBJECT:
1549 case Instruction::SPUT_BOOLEAN:
1550 case Instruction::SPUT_BYTE:
1551 case Instruction::SPUT_CHAR:
1552 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001553 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001554 return false;
1555 }
1556 break;
1557 }
1558
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001559#define ARRAY_XX(kind, anticipated_type) \
1560 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001561 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001562 break; \
1563 } \
1564 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001565 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001566 break; \
1567 }
1568
1569 ARRAY_XX(, Primitive::kPrimInt);
1570 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1571 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1572 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1573 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1574 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1575 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1576
Nicolas Geoffray39468442014-09-02 15:17:15 +01001577 case Instruction::ARRAY_LENGTH: {
1578 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001579 // No need for a temporary for the null check, it is the only input of the following
1580 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00001581 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001582 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001583 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1584 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1585 break;
1586 }
1587
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001588 case Instruction::CONST_STRING: {
Calin Juravle225ff812014-11-13 16:46:39 +00001589 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001590 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1591 break;
1592 }
1593
1594 case Instruction::CONST_STRING_JUMBO: {
Calin Juravle225ff812014-11-13 16:46:39 +00001595 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001596 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
1597 break;
1598 }
1599
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001600 case Instruction::CONST_CLASS: {
1601 uint16_t type_index = instruction.VRegB_21c();
1602 bool type_known_final;
1603 bool type_known_abstract;
1604 bool is_referrers_class;
1605 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1606 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
1607 &type_known_final, &type_known_abstract, &is_referrers_class);
1608 if (!can_access) {
1609 return false;
1610 }
1611 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001612 new (arena_) HLoadClass(type_index, is_referrers_class, dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001613 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1614 break;
1615 }
1616
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001617 case Instruction::MOVE_EXCEPTION: {
1618 current_block_->AddInstruction(new (arena_) HLoadException());
1619 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
1620 break;
1621 }
1622
1623 case Instruction::THROW: {
1624 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001625 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001626 // A throw instruction must branch to the exit block.
1627 current_block_->AddSuccessor(exit_block_);
1628 // We finished building this block. Set the current block to null to avoid
1629 // adding dead instructions to it.
1630 current_block_ = nullptr;
1631 break;
1632 }
1633
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001634 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001635 uint8_t destination = instruction.VRegA_22c();
1636 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001637 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00001638 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001639 return false;
1640 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001641 break;
1642 }
1643
1644 case Instruction::CHECK_CAST: {
1645 uint8_t reference = instruction.VRegA_21c();
1646 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00001647 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001648 return false;
1649 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00001650 break;
1651 }
1652
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001653 case Instruction::MONITOR_ENTER: {
1654 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1655 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1656 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00001657 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001658 break;
1659 }
1660
1661 case Instruction::MONITOR_EXIT: {
1662 current_block_->AddInstruction(new (arena_) HMonitorOperation(
1663 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
1664 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00001665 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001666 break;
1667 }
1668
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001669 default:
1670 return false;
1671 }
1672 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001673} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001674
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001675HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001676 if (constant0_ != nullptr) {
1677 return constant0_;
1678 }
1679 constant0_ = new(arena_) HIntConstant(0);
1680 entry_block_->AddInstruction(constant0_);
1681 return constant0_;
1682}
1683
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001684HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001685 if (constant1_ != nullptr) {
1686 return constant1_;
1687 }
1688 constant1_ = new(arena_) HIntConstant(1);
1689 entry_block_->AddInstruction(constant1_);
1690 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001691}
1692
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001693HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001694 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001695 case 0: return GetIntConstant0();
1696 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001697 default: {
1698 HIntConstant* instruction = new (arena_) HIntConstant(constant);
1699 entry_block_->AddInstruction(instruction);
1700 return instruction;
1701 }
1702 }
1703}
1704
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001705HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
1706 HLongConstant* instruction = new (arena_) HLongConstant(constant);
1707 entry_block_->AddInstruction(instruction);
1708 return instruction;
1709}
1710
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001711HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
1712 return locals_.Get(register_index);
1713}
1714
1715void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
1716 HLocal* local = GetLocalAt(register_index);
1717 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
1718}
1719
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001720HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001721 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001722 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001723 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001724}
1725
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001726} // namespace art