1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "instruction_builder.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
20*795d594fSAndroid Build Coastguard Worker #include "base/arena_bit_vector.h"
21*795d594fSAndroid Build Coastguard Worker #include "base/bit_vector-inl.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/logging.h"
23*795d594fSAndroid Build Coastguard Worker #include "block_builder.h"
24*795d594fSAndroid Build Coastguard Worker #include "class_linker-inl.h"
25*795d594fSAndroid Build Coastguard Worker #include "code_generator.h"
26*795d594fSAndroid Build Coastguard Worker #include "data_type-inl.h"
27*795d594fSAndroid Build Coastguard Worker #include "dex/bytecode_utils.h"
28*795d594fSAndroid Build Coastguard Worker #include "dex/dex_instruction-inl.h"
29*795d594fSAndroid Build Coastguard Worker #include "driver/compiler_options.h"
30*795d594fSAndroid Build Coastguard Worker #include "driver/dex_compilation_unit.h"
31*795d594fSAndroid Build Coastguard Worker #include "entrypoints/entrypoint_utils-inl.h"
32*795d594fSAndroid Build Coastguard Worker #include "handle_cache-inl.h"
33*795d594fSAndroid Build Coastguard Worker #include "imtable-inl.h"
34*795d594fSAndroid Build Coastguard Worker #include "intrinsics.h"
35*795d594fSAndroid Build Coastguard Worker #include "intrinsics_utils.h"
36*795d594fSAndroid Build Coastguard Worker #include "jit/jit.h"
37*795d594fSAndroid Build Coastguard Worker #include "jit/profiling_info.h"
38*795d594fSAndroid Build Coastguard Worker #include "mirror/dex_cache.h"
39*795d594fSAndroid Build Coastguard Worker #include "oat/oat_file.h"
40*795d594fSAndroid Build Coastguard Worker #include "optimizing/data_type.h"
41*795d594fSAndroid Build Coastguard Worker #include "optimizing_compiler_stats.h"
42*795d594fSAndroid Build Coastguard Worker #include "reflective_handle_scope-inl.h"
43*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
44*795d594fSAndroid Build Coastguard Worker #include "sharpening.h"
45*795d594fSAndroid Build Coastguard Worker #include "ssa_builder.h"
46*795d594fSAndroid Build Coastguard Worker #include "well_known_classes.h"
47*795d594fSAndroid Build Coastguard Worker
48*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
49*795d594fSAndroid Build Coastguard Worker
50*795d594fSAndroid Build Coastguard Worker namespace {
51*795d594fSAndroid Build Coastguard Worker
52*795d594fSAndroid Build Coastguard Worker class SamePackageCompare {
53*795d594fSAndroid Build Coastguard Worker public:
SamePackageCompare(const DexCompilationUnit & dex_compilation_unit)54*795d594fSAndroid Build Coastguard Worker explicit SamePackageCompare(const DexCompilationUnit& dex_compilation_unit)
55*795d594fSAndroid Build Coastguard Worker : dex_compilation_unit_(dex_compilation_unit) {}
56*795d594fSAndroid Build Coastguard Worker
operator ()(ObjPtr<mirror::Class> klass)57*795d594fSAndroid Build Coastguard Worker bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) {
58*795d594fSAndroid Build Coastguard Worker if (klass->GetClassLoader() != dex_compilation_unit_.GetClassLoader().Get()) {
59*795d594fSAndroid Build Coastguard Worker return false;
60*795d594fSAndroid Build Coastguard Worker }
61*795d594fSAndroid Build Coastguard Worker if (referrers_descriptor_ == nullptr) {
62*795d594fSAndroid Build Coastguard Worker const DexFile* dex_file = dex_compilation_unit_.GetDexFile();
63*795d594fSAndroid Build Coastguard Worker uint32_t referrers_method_idx = dex_compilation_unit_.GetDexMethodIndex();
64*795d594fSAndroid Build Coastguard Worker referrers_descriptor_ =
65*795d594fSAndroid Build Coastguard Worker dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(referrers_method_idx));
66*795d594fSAndroid Build Coastguard Worker referrers_package_length_ = PackageLength(referrers_descriptor_);
67*795d594fSAndroid Build Coastguard Worker }
68*795d594fSAndroid Build Coastguard Worker std::string temp;
69*795d594fSAndroid Build Coastguard Worker const char* klass_descriptor = klass->GetDescriptor(&temp);
70*795d594fSAndroid Build Coastguard Worker size_t klass_package_length = PackageLength(klass_descriptor);
71*795d594fSAndroid Build Coastguard Worker return (referrers_package_length_ == klass_package_length) &&
72*795d594fSAndroid Build Coastguard Worker memcmp(referrers_descriptor_, klass_descriptor, referrers_package_length_) == 0;
73*795d594fSAndroid Build Coastguard Worker };
74*795d594fSAndroid Build Coastguard Worker
75*795d594fSAndroid Build Coastguard Worker private:
PackageLength(const char * descriptor)76*795d594fSAndroid Build Coastguard Worker static size_t PackageLength(const char* descriptor) {
77*795d594fSAndroid Build Coastguard Worker const char* slash_pos = strrchr(descriptor, '/');
78*795d594fSAndroid Build Coastguard Worker return (slash_pos != nullptr) ? static_cast<size_t>(slash_pos - descriptor) : 0u;
79*795d594fSAndroid Build Coastguard Worker }
80*795d594fSAndroid Build Coastguard Worker
81*795d594fSAndroid Build Coastguard Worker const DexCompilationUnit& dex_compilation_unit_;
82*795d594fSAndroid Build Coastguard Worker const char* referrers_descriptor_ = nullptr;
83*795d594fSAndroid Build Coastguard Worker size_t referrers_package_length_ = 0u;
84*795d594fSAndroid Build Coastguard Worker };
85*795d594fSAndroid Build Coastguard Worker
86*795d594fSAndroid Build Coastguard Worker } // anonymous namespace
87*795d594fSAndroid Build Coastguard Worker
HInstructionBuilder(HGraph * graph,HBasicBlockBuilder * block_builder,SsaBuilder * ssa_builder,const DexFile * dex_file,const CodeItemDebugInfoAccessor & accessor,DataType::Type return_type,const DexCompilationUnit * dex_compilation_unit,const DexCompilationUnit * outer_compilation_unit,CodeGenerator * code_generator,OptimizingCompilerStats * compiler_stats,ScopedArenaAllocator * local_allocator)88*795d594fSAndroid Build Coastguard Worker HInstructionBuilder::HInstructionBuilder(HGraph* graph,
89*795d594fSAndroid Build Coastguard Worker HBasicBlockBuilder* block_builder,
90*795d594fSAndroid Build Coastguard Worker SsaBuilder* ssa_builder,
91*795d594fSAndroid Build Coastguard Worker const DexFile* dex_file,
92*795d594fSAndroid Build Coastguard Worker const CodeItemDebugInfoAccessor& accessor,
93*795d594fSAndroid Build Coastguard Worker DataType::Type return_type,
94*795d594fSAndroid Build Coastguard Worker const DexCompilationUnit* dex_compilation_unit,
95*795d594fSAndroid Build Coastguard Worker const DexCompilationUnit* outer_compilation_unit,
96*795d594fSAndroid Build Coastguard Worker CodeGenerator* code_generator,
97*795d594fSAndroid Build Coastguard Worker OptimizingCompilerStats* compiler_stats,
98*795d594fSAndroid Build Coastguard Worker ScopedArenaAllocator* local_allocator)
99*795d594fSAndroid Build Coastguard Worker : allocator_(graph->GetAllocator()),
100*795d594fSAndroid Build Coastguard Worker graph_(graph),
101*795d594fSAndroid Build Coastguard Worker dex_file_(dex_file),
102*795d594fSAndroid Build Coastguard Worker code_item_accessor_(accessor),
103*795d594fSAndroid Build Coastguard Worker return_type_(return_type),
104*795d594fSAndroid Build Coastguard Worker block_builder_(block_builder),
105*795d594fSAndroid Build Coastguard Worker ssa_builder_(ssa_builder),
106*795d594fSAndroid Build Coastguard Worker code_generator_(code_generator),
107*795d594fSAndroid Build Coastguard Worker dex_compilation_unit_(dex_compilation_unit),
108*795d594fSAndroid Build Coastguard Worker outer_compilation_unit_(outer_compilation_unit),
109*795d594fSAndroid Build Coastguard Worker compilation_stats_(compiler_stats),
110*795d594fSAndroid Build Coastguard Worker local_allocator_(local_allocator),
111*795d594fSAndroid Build Coastguard Worker locals_for_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
112*795d594fSAndroid Build Coastguard Worker current_block_(nullptr),
113*795d594fSAndroid Build Coastguard Worker current_locals_(nullptr),
114*795d594fSAndroid Build Coastguard Worker latest_result_(nullptr),
115*795d594fSAndroid Build Coastguard Worker current_this_parameter_(nullptr),
116*795d594fSAndroid Build Coastguard Worker loop_headers_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
117*795d594fSAndroid Build Coastguard Worker class_cache_(std::less<dex::TypeIndex>(), local_allocator->Adapter(kArenaAllocGraphBuilder)) {
118*795d594fSAndroid Build Coastguard Worker loop_headers_.reserve(kDefaultNumberOfLoops);
119*795d594fSAndroid Build Coastguard Worker }
120*795d594fSAndroid Build Coastguard Worker
FindBlockStartingAt(uint32_t dex_pc) const121*795d594fSAndroid Build Coastguard Worker HBasicBlock* HInstructionBuilder::FindBlockStartingAt(uint32_t dex_pc) const {
122*795d594fSAndroid Build Coastguard Worker return block_builder_->GetBlockAt(dex_pc);
123*795d594fSAndroid Build Coastguard Worker }
124*795d594fSAndroid Build Coastguard Worker
GetLocalsFor(HBasicBlock * block)125*795d594fSAndroid Build Coastguard Worker inline ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsFor(HBasicBlock* block) {
126*795d594fSAndroid Build Coastguard Worker ScopedArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
127*795d594fSAndroid Build Coastguard Worker const size_t vregs = graph_->GetNumberOfVRegs();
128*795d594fSAndroid Build Coastguard Worker if (locals->size() == vregs) {
129*795d594fSAndroid Build Coastguard Worker return locals;
130*795d594fSAndroid Build Coastguard Worker }
131*795d594fSAndroid Build Coastguard Worker return GetLocalsForWithAllocation(block, locals, vregs);
132*795d594fSAndroid Build Coastguard Worker }
133*795d594fSAndroid Build Coastguard Worker
GetLocalsForWithAllocation(HBasicBlock * block,ScopedArenaVector<HInstruction * > * locals,const size_t vregs)134*795d594fSAndroid Build Coastguard Worker ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsForWithAllocation(
135*795d594fSAndroid Build Coastguard Worker HBasicBlock* block,
136*795d594fSAndroid Build Coastguard Worker ScopedArenaVector<HInstruction*>* locals,
137*795d594fSAndroid Build Coastguard Worker const size_t vregs) {
138*795d594fSAndroid Build Coastguard Worker DCHECK_NE(locals->size(), vregs);
139*795d594fSAndroid Build Coastguard Worker locals->resize(vregs, nullptr);
140*795d594fSAndroid Build Coastguard Worker if (block->IsCatchBlock()) {
141*795d594fSAndroid Build Coastguard Worker // We record incoming inputs of catch phis at throwing instructions and
142*795d594fSAndroid Build Coastguard Worker // must therefore eagerly create the phis. Phis for undefined vregs will
143*795d594fSAndroid Build Coastguard Worker // be deleted when the first throwing instruction with the vreg undefined
144*795d594fSAndroid Build Coastguard Worker // is encountered. Unused phis will be removed by dead phi analysis.
145*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < vregs; ++i) {
146*795d594fSAndroid Build Coastguard Worker // No point in creating the catch phi if it is already undefined at
147*795d594fSAndroid Build Coastguard Worker // the first throwing instruction.
148*795d594fSAndroid Build Coastguard Worker HInstruction* current_local_value = (*current_locals_)[i];
149*795d594fSAndroid Build Coastguard Worker if (current_local_value != nullptr) {
150*795d594fSAndroid Build Coastguard Worker HPhi* phi = new (allocator_) HPhi(
151*795d594fSAndroid Build Coastguard Worker allocator_,
152*795d594fSAndroid Build Coastguard Worker i,
153*795d594fSAndroid Build Coastguard Worker 0,
154*795d594fSAndroid Build Coastguard Worker current_local_value->GetType());
155*795d594fSAndroid Build Coastguard Worker block->AddPhi(phi);
156*795d594fSAndroid Build Coastguard Worker (*locals)[i] = phi;
157*795d594fSAndroid Build Coastguard Worker }
158*795d594fSAndroid Build Coastguard Worker }
159*795d594fSAndroid Build Coastguard Worker }
160*795d594fSAndroid Build Coastguard Worker return locals;
161*795d594fSAndroid Build Coastguard Worker }
162*795d594fSAndroid Build Coastguard Worker
ValueOfLocalAt(HBasicBlock * block,size_t local)163*795d594fSAndroid Build Coastguard Worker inline HInstruction* HInstructionBuilder::ValueOfLocalAt(HBasicBlock* block, size_t local) {
164*795d594fSAndroid Build Coastguard Worker ScopedArenaVector<HInstruction*>* locals = GetLocalsFor(block);
165*795d594fSAndroid Build Coastguard Worker return (*locals)[local];
166*795d594fSAndroid Build Coastguard Worker }
167*795d594fSAndroid Build Coastguard Worker
InitializeBlockLocals()168*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::InitializeBlockLocals() {
169*795d594fSAndroid Build Coastguard Worker current_locals_ = GetLocalsFor(current_block_);
170*795d594fSAndroid Build Coastguard Worker
171*795d594fSAndroid Build Coastguard Worker if (current_block_->IsCatchBlock()) {
172*795d594fSAndroid Build Coastguard Worker // Catch phis were already created and inputs collected from throwing sites.
173*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
174*795d594fSAndroid Build Coastguard Worker // Make sure there was at least one throwing instruction which initialized
175*795d594fSAndroid Build Coastguard Worker // locals (guaranteed by HGraphBuilder) and that all try blocks have been
176*795d594fSAndroid Build Coastguard Worker // visited already (from HTryBoundary scoping and reverse post order).
177*795d594fSAndroid Build Coastguard Worker bool catch_block_visited = false;
178*795d594fSAndroid Build Coastguard Worker for (HBasicBlock* current : graph_->GetReversePostOrder()) {
179*795d594fSAndroid Build Coastguard Worker if (current == current_block_) {
180*795d594fSAndroid Build Coastguard Worker catch_block_visited = true;
181*795d594fSAndroid Build Coastguard Worker } else if (current->IsTryBlock()) {
182*795d594fSAndroid Build Coastguard Worker const HTryBoundary& try_entry = current->GetTryCatchInformation()->GetTryEntry();
183*795d594fSAndroid Build Coastguard Worker if (try_entry.HasExceptionHandler(*current_block_)) {
184*795d594fSAndroid Build Coastguard Worker DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
185*795d594fSAndroid Build Coastguard Worker }
186*795d594fSAndroid Build Coastguard Worker }
187*795d594fSAndroid Build Coastguard Worker }
188*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(current_locals_->size(), graph_->GetNumberOfVRegs())
189*795d594fSAndroid Build Coastguard Worker << "No instructions throwing into a live catch block.";
190*795d594fSAndroid Build Coastguard Worker }
191*795d594fSAndroid Build Coastguard Worker } else if (current_block_->IsLoopHeader()) {
192*795d594fSAndroid Build Coastguard Worker // If the block is a loop header, we know we only have visited the pre header
193*795d594fSAndroid Build Coastguard Worker // because we are visiting in reverse post order. We create phis for all initialized
194*795d594fSAndroid Build Coastguard Worker // locals from the pre header. Their inputs will be populated at the end of
195*795d594fSAndroid Build Coastguard Worker // the analysis.
196*795d594fSAndroid Build Coastguard Worker for (size_t local = 0; local < current_locals_->size(); ++local) {
197*795d594fSAndroid Build Coastguard Worker HInstruction* incoming =
198*795d594fSAndroid Build Coastguard Worker ValueOfLocalAt(current_block_->GetLoopInformation()->GetPreHeader(), local);
199*795d594fSAndroid Build Coastguard Worker if (incoming != nullptr) {
200*795d594fSAndroid Build Coastguard Worker HPhi* phi = new (allocator_) HPhi(
201*795d594fSAndroid Build Coastguard Worker allocator_,
202*795d594fSAndroid Build Coastguard Worker local,
203*795d594fSAndroid Build Coastguard Worker 0,
204*795d594fSAndroid Build Coastguard Worker incoming->GetType());
205*795d594fSAndroid Build Coastguard Worker current_block_->AddPhi(phi);
206*795d594fSAndroid Build Coastguard Worker (*current_locals_)[local] = phi;
207*795d594fSAndroid Build Coastguard Worker }
208*795d594fSAndroid Build Coastguard Worker }
209*795d594fSAndroid Build Coastguard Worker
210*795d594fSAndroid Build Coastguard Worker // Save the loop header so that the last phase of the analysis knows which
211*795d594fSAndroid Build Coastguard Worker // blocks need to be updated.
212*795d594fSAndroid Build Coastguard Worker loop_headers_.push_back(current_block_);
213*795d594fSAndroid Build Coastguard Worker } else if (current_block_->GetPredecessors().size() > 0) {
214*795d594fSAndroid Build Coastguard Worker // All predecessors have already been visited because we are visiting in reverse post order.
215*795d594fSAndroid Build Coastguard Worker // We merge the values of all locals, creating phis if those values differ.
216*795d594fSAndroid Build Coastguard Worker for (size_t local = 0; local < current_locals_->size(); ++local) {
217*795d594fSAndroid Build Coastguard Worker bool one_predecessor_has_no_value = false;
218*795d594fSAndroid Build Coastguard Worker bool is_different = false;
219*795d594fSAndroid Build Coastguard Worker HInstruction* value = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
220*795d594fSAndroid Build Coastguard Worker
221*795d594fSAndroid Build Coastguard Worker for (HBasicBlock* predecessor : current_block_->GetPredecessors()) {
222*795d594fSAndroid Build Coastguard Worker HInstruction* current = ValueOfLocalAt(predecessor, local);
223*795d594fSAndroid Build Coastguard Worker if (current == nullptr) {
224*795d594fSAndroid Build Coastguard Worker one_predecessor_has_no_value = true;
225*795d594fSAndroid Build Coastguard Worker break;
226*795d594fSAndroid Build Coastguard Worker } else if (current != value) {
227*795d594fSAndroid Build Coastguard Worker is_different = true;
228*795d594fSAndroid Build Coastguard Worker }
229*795d594fSAndroid Build Coastguard Worker }
230*795d594fSAndroid Build Coastguard Worker
231*795d594fSAndroid Build Coastguard Worker if (one_predecessor_has_no_value) {
232*795d594fSAndroid Build Coastguard Worker // If one predecessor has no value for this local, we trust the verifier has
233*795d594fSAndroid Build Coastguard Worker // successfully checked that there is a store dominating any read after this block.
234*795d594fSAndroid Build Coastguard Worker continue;
235*795d594fSAndroid Build Coastguard Worker }
236*795d594fSAndroid Build Coastguard Worker
237*795d594fSAndroid Build Coastguard Worker if (is_different) {
238*795d594fSAndroid Build Coastguard Worker HInstruction* first_input = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
239*795d594fSAndroid Build Coastguard Worker HPhi* phi = new (allocator_) HPhi(
240*795d594fSAndroid Build Coastguard Worker allocator_,
241*795d594fSAndroid Build Coastguard Worker local,
242*795d594fSAndroid Build Coastguard Worker current_block_->GetPredecessors().size(),
243*795d594fSAndroid Build Coastguard Worker first_input->GetType());
244*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < current_block_->GetPredecessors().size(); i++) {
245*795d594fSAndroid Build Coastguard Worker HInstruction* pred_value = ValueOfLocalAt(current_block_->GetPredecessors()[i], local);
246*795d594fSAndroid Build Coastguard Worker phi->SetRawInputAt(i, pred_value);
247*795d594fSAndroid Build Coastguard Worker }
248*795d594fSAndroid Build Coastguard Worker current_block_->AddPhi(phi);
249*795d594fSAndroid Build Coastguard Worker value = phi;
250*795d594fSAndroid Build Coastguard Worker }
251*795d594fSAndroid Build Coastguard Worker (*current_locals_)[local] = value;
252*795d594fSAndroid Build Coastguard Worker }
253*795d594fSAndroid Build Coastguard Worker }
254*795d594fSAndroid Build Coastguard Worker }
255*795d594fSAndroid Build Coastguard Worker
PropagateLocalsToCatchBlocks()256*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::PropagateLocalsToCatchBlocks() {
257*795d594fSAndroid Build Coastguard Worker const HTryBoundary& try_entry = current_block_->GetTryCatchInformation()->GetTryEntry();
258*795d594fSAndroid Build Coastguard Worker for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) {
259*795d594fSAndroid Build Coastguard Worker ScopedArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
260*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(handler_locals->size(), current_locals_->size());
261*795d594fSAndroid Build Coastguard Worker for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
262*795d594fSAndroid Build Coastguard Worker HInstruction* handler_value = (*handler_locals)[vreg];
263*795d594fSAndroid Build Coastguard Worker if (handler_value == nullptr) {
264*795d594fSAndroid Build Coastguard Worker // Vreg was undefined at a previously encountered throwing instruction
265*795d594fSAndroid Build Coastguard Worker // and the catch phi was deleted. Do not record the local value.
266*795d594fSAndroid Build Coastguard Worker continue;
267*795d594fSAndroid Build Coastguard Worker }
268*795d594fSAndroid Build Coastguard Worker DCHECK(handler_value->IsPhi());
269*795d594fSAndroid Build Coastguard Worker
270*795d594fSAndroid Build Coastguard Worker HInstruction* local_value = (*current_locals_)[vreg];
271*795d594fSAndroid Build Coastguard Worker if (local_value == nullptr) {
272*795d594fSAndroid Build Coastguard Worker // This is the first instruction throwing into `catch_block` where
273*795d594fSAndroid Build Coastguard Worker // `vreg` is undefined. Delete the catch phi.
274*795d594fSAndroid Build Coastguard Worker catch_block->RemovePhi(handler_value->AsPhi());
275*795d594fSAndroid Build Coastguard Worker (*handler_locals)[vreg] = nullptr;
276*795d594fSAndroid Build Coastguard Worker } else {
277*795d594fSAndroid Build Coastguard Worker // Vreg has been defined at all instructions throwing into `catch_block`
278*795d594fSAndroid Build Coastguard Worker // encountered so far. Record the local value in the catch phi.
279*795d594fSAndroid Build Coastguard Worker handler_value->AsPhi()->AddInput(local_value);
280*795d594fSAndroid Build Coastguard Worker }
281*795d594fSAndroid Build Coastguard Worker }
282*795d594fSAndroid Build Coastguard Worker }
283*795d594fSAndroid Build Coastguard Worker }
284*795d594fSAndroid Build Coastguard Worker
AppendInstruction(HInstruction * instruction)285*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::AppendInstruction(HInstruction* instruction) {
286*795d594fSAndroid Build Coastguard Worker current_block_->AddInstruction(instruction);
287*795d594fSAndroid Build Coastguard Worker InitializeInstruction(instruction);
288*795d594fSAndroid Build Coastguard Worker }
289*795d594fSAndroid Build Coastguard Worker
InsertInstructionAtTop(HInstruction * instruction)290*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::InsertInstructionAtTop(HInstruction* instruction) {
291*795d594fSAndroid Build Coastguard Worker if (current_block_->GetInstructions().IsEmpty()) {
292*795d594fSAndroid Build Coastguard Worker current_block_->AddInstruction(instruction);
293*795d594fSAndroid Build Coastguard Worker } else {
294*795d594fSAndroid Build Coastguard Worker current_block_->InsertInstructionBefore(instruction, current_block_->GetFirstInstruction());
295*795d594fSAndroid Build Coastguard Worker }
296*795d594fSAndroid Build Coastguard Worker InitializeInstruction(instruction);
297*795d594fSAndroid Build Coastguard Worker }
298*795d594fSAndroid Build Coastguard Worker
InitializeInstruction(HInstruction * instruction)299*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::InitializeInstruction(HInstruction* instruction) {
300*795d594fSAndroid Build Coastguard Worker if (instruction->NeedsEnvironment()) {
301*795d594fSAndroid Build Coastguard Worker HEnvironment* environment = HEnvironment::Create(
302*795d594fSAndroid Build Coastguard Worker allocator_,
303*795d594fSAndroid Build Coastguard Worker current_locals_->size(),
304*795d594fSAndroid Build Coastguard Worker graph_->GetArtMethod(),
305*795d594fSAndroid Build Coastguard Worker instruction->GetDexPc(),
306*795d594fSAndroid Build Coastguard Worker instruction);
307*795d594fSAndroid Build Coastguard Worker environment->CopyFrom(ArrayRef<HInstruction* const>(*current_locals_));
308*795d594fSAndroid Build Coastguard Worker instruction->SetRawEnvironment(environment);
309*795d594fSAndroid Build Coastguard Worker }
310*795d594fSAndroid Build Coastguard Worker }
311*795d594fSAndroid Build Coastguard Worker
LoadNullCheckedLocal(uint32_t register_index,uint32_t dex_pc)312*795d594fSAndroid Build Coastguard Worker HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
313*795d594fSAndroid Build Coastguard Worker HInstruction* ref = LoadLocal(register_index, DataType::Type::kReference);
314*795d594fSAndroid Build Coastguard Worker if (!ref->CanBeNull()) {
315*795d594fSAndroid Build Coastguard Worker return ref;
316*795d594fSAndroid Build Coastguard Worker }
317*795d594fSAndroid Build Coastguard Worker
318*795d594fSAndroid Build Coastguard Worker HNullCheck* null_check = new (allocator_) HNullCheck(ref, dex_pc);
319*795d594fSAndroid Build Coastguard Worker AppendInstruction(null_check);
320*795d594fSAndroid Build Coastguard Worker return null_check;
321*795d594fSAndroid Build Coastguard Worker }
322*795d594fSAndroid Build Coastguard Worker
SetLoopHeaderPhiInputs()323*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::SetLoopHeaderPhiInputs() {
324*795d594fSAndroid Build Coastguard Worker for (size_t i = loop_headers_.size(); i > 0; --i) {
325*795d594fSAndroid Build Coastguard Worker HBasicBlock* block = loop_headers_[i - 1];
326*795d594fSAndroid Build Coastguard Worker for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
327*795d594fSAndroid Build Coastguard Worker HPhi* phi = it.Current()->AsPhi();
328*795d594fSAndroid Build Coastguard Worker size_t vreg = phi->GetRegNumber();
329*795d594fSAndroid Build Coastguard Worker for (HBasicBlock* predecessor : block->GetPredecessors()) {
330*795d594fSAndroid Build Coastguard Worker HInstruction* value = ValueOfLocalAt(predecessor, vreg);
331*795d594fSAndroid Build Coastguard Worker if (value == nullptr) {
332*795d594fSAndroid Build Coastguard Worker // Vreg is undefined at this predecessor. Mark it dead and leave with
333*795d594fSAndroid Build Coastguard Worker // fewer inputs than predecessors. SsaChecker will fail if not removed.
334*795d594fSAndroid Build Coastguard Worker phi->SetDead();
335*795d594fSAndroid Build Coastguard Worker break;
336*795d594fSAndroid Build Coastguard Worker } else {
337*795d594fSAndroid Build Coastguard Worker phi->AddInput(value);
338*795d594fSAndroid Build Coastguard Worker }
339*795d594fSAndroid Build Coastguard Worker }
340*795d594fSAndroid Build Coastguard Worker }
341*795d594fSAndroid Build Coastguard Worker }
342*795d594fSAndroid Build Coastguard Worker }
343*795d594fSAndroid Build Coastguard Worker
IsBlockPopulated(HBasicBlock * block)344*795d594fSAndroid Build Coastguard Worker static bool IsBlockPopulated(HBasicBlock* block) {
345*795d594fSAndroid Build Coastguard Worker if (block->IsLoopHeader()) {
346*795d594fSAndroid Build Coastguard Worker // Suspend checks were inserted into loop headers during building of dominator tree.
347*795d594fSAndroid Build Coastguard Worker DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
348*795d594fSAndroid Build Coastguard Worker return block->GetFirstInstruction() != block->GetLastInstruction();
349*795d594fSAndroid Build Coastguard Worker } else if (block->IsCatchBlock()) {
350*795d594fSAndroid Build Coastguard Worker // Nops were inserted into the beginning of catch blocks.
351*795d594fSAndroid Build Coastguard Worker DCHECK(block->GetFirstInstruction()->IsNop());
352*795d594fSAndroid Build Coastguard Worker return block->GetFirstInstruction() != block->GetLastInstruction();
353*795d594fSAndroid Build Coastguard Worker } else {
354*795d594fSAndroid Build Coastguard Worker return !block->GetInstructions().IsEmpty();
355*795d594fSAndroid Build Coastguard Worker }
356*795d594fSAndroid Build Coastguard Worker }
357*795d594fSAndroid Build Coastguard Worker
Build()358*795d594fSAndroid Build Coastguard Worker bool HInstructionBuilder::Build() {
359*795d594fSAndroid Build Coastguard Worker DCHECK(code_item_accessor_.HasCodeItem());
360*795d594fSAndroid Build Coastguard Worker locals_for_.resize(
361*795d594fSAndroid Build Coastguard Worker graph_->GetBlocks().size(),
362*795d594fSAndroid Build Coastguard Worker ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
363*795d594fSAndroid Build Coastguard Worker
364*795d594fSAndroid Build Coastguard Worker // Find locations where we want to generate extra stackmaps for native debugging.
365*795d594fSAndroid Build Coastguard Worker // This allows us to generate the info only at interesting points (for example,
366*795d594fSAndroid Build Coastguard Worker // at start of java statement) rather than before every dex instruction.
367*795d594fSAndroid Build Coastguard Worker const bool native_debuggable = code_generator_ != nullptr &&
368*795d594fSAndroid Build Coastguard Worker code_generator_->GetCompilerOptions().GetNativeDebuggable();
369*795d594fSAndroid Build Coastguard Worker ArenaBitVector* native_debug_info_locations = nullptr;
370*795d594fSAndroid Build Coastguard Worker if (native_debuggable) {
371*795d594fSAndroid Build Coastguard Worker native_debug_info_locations = FindNativeDebugInfoLocations();
372*795d594fSAndroid Build Coastguard Worker }
373*795d594fSAndroid Build Coastguard Worker
374*795d594fSAndroid Build Coastguard Worker for (HBasicBlock* block : graph_->GetReversePostOrder()) {
375*795d594fSAndroid Build Coastguard Worker current_block_ = block;
376*795d594fSAndroid Build Coastguard Worker uint32_t block_dex_pc = current_block_->GetDexPc();
377*795d594fSAndroid Build Coastguard Worker
378*795d594fSAndroid Build Coastguard Worker InitializeBlockLocals();
379*795d594fSAndroid Build Coastguard Worker
380*795d594fSAndroid Build Coastguard Worker if (current_block_->IsEntryBlock()) {
381*795d594fSAndroid Build Coastguard Worker InitializeParameters();
382*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HSuspendCheck(0u));
383*795d594fSAndroid Build Coastguard Worker if (graph_->IsDebuggable() && code_generator_->GetCompilerOptions().IsJitCompiler()) {
384*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HMethodEntryHook(0u));
385*795d594fSAndroid Build Coastguard Worker }
386*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HGoto(0u));
387*795d594fSAndroid Build Coastguard Worker continue;
388*795d594fSAndroid Build Coastguard Worker } else if (current_block_->IsExitBlock()) {
389*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HExit());
390*795d594fSAndroid Build Coastguard Worker continue;
391*795d594fSAndroid Build Coastguard Worker } else if (current_block_->IsLoopHeader()) {
392*795d594fSAndroid Build Coastguard Worker HSuspendCheck* suspend_check = new (allocator_) HSuspendCheck(current_block_->GetDexPc());
393*795d594fSAndroid Build Coastguard Worker current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
394*795d594fSAndroid Build Coastguard Worker // This is slightly odd because the loop header might not be empty (TryBoundary).
395*795d594fSAndroid Build Coastguard Worker // But we're still creating the environment with locals from the top of the block.
396*795d594fSAndroid Build Coastguard Worker InsertInstructionAtTop(suspend_check);
397*795d594fSAndroid Build Coastguard Worker } else if (current_block_->IsCatchBlock()) {
398*795d594fSAndroid Build Coastguard Worker // We add an environment emitting instruction at the beginning of each catch block, in order
399*795d594fSAndroid Build Coastguard Worker // to support try catch inlining.
400*795d594fSAndroid Build Coastguard Worker // This is slightly odd because the catch block might not be empty (TryBoundary).
401*795d594fSAndroid Build Coastguard Worker InsertInstructionAtTop(new (allocator_) HNop(block_dex_pc, /* needs_environment= */ true));
402*795d594fSAndroid Build Coastguard Worker }
403*795d594fSAndroid Build Coastguard Worker
404*795d594fSAndroid Build Coastguard Worker if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
405*795d594fSAndroid Build Coastguard Worker // Synthetic block that does not need to be populated.
406*795d594fSAndroid Build Coastguard Worker DCHECK(IsBlockPopulated(current_block_));
407*795d594fSAndroid Build Coastguard Worker continue;
408*795d594fSAndroid Build Coastguard Worker }
409*795d594fSAndroid Build Coastguard Worker
410*795d594fSAndroid Build Coastguard Worker DCHECK(!IsBlockPopulated(current_block_));
411*795d594fSAndroid Build Coastguard Worker
412*795d594fSAndroid Build Coastguard Worker for (const DexInstructionPcPair& pair : code_item_accessor_.InstructionsFrom(block_dex_pc)) {
413*795d594fSAndroid Build Coastguard Worker if (current_block_ == nullptr) {
414*795d594fSAndroid Build Coastguard Worker // The previous instruction ended this block.
415*795d594fSAndroid Build Coastguard Worker break;
416*795d594fSAndroid Build Coastguard Worker }
417*795d594fSAndroid Build Coastguard Worker
418*795d594fSAndroid Build Coastguard Worker const uint32_t dex_pc = pair.DexPc();
419*795d594fSAndroid Build Coastguard Worker if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
420*795d594fSAndroid Build Coastguard Worker // This dex_pc starts a new basic block.
421*795d594fSAndroid Build Coastguard Worker break;
422*795d594fSAndroid Build Coastguard Worker }
423*795d594fSAndroid Build Coastguard Worker
424*795d594fSAndroid Build Coastguard Worker if (current_block_->IsTryBlock() && IsThrowingDexInstruction(pair.Inst())) {
425*795d594fSAndroid Build Coastguard Worker PropagateLocalsToCatchBlocks();
426*795d594fSAndroid Build Coastguard Worker }
427*795d594fSAndroid Build Coastguard Worker
428*795d594fSAndroid Build Coastguard Worker if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
429*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HNop(dex_pc, /* needs_environment= */ true));
430*795d594fSAndroid Build Coastguard Worker }
431*795d594fSAndroid Build Coastguard Worker
432*795d594fSAndroid Build Coastguard Worker // Note: There may be no Thread for gtests.
433*795d594fSAndroid Build Coastguard Worker DCHECK(Thread::Current() == nullptr || !Thread::Current()->IsExceptionPending())
434*795d594fSAndroid Build Coastguard Worker << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
435*795d594fSAndroid Build Coastguard Worker << " " << pair.Inst().Name() << "@" << dex_pc;
436*795d594fSAndroid Build Coastguard Worker if (!ProcessDexInstruction(pair.Inst(), dex_pc)) {
437*795d594fSAndroid Build Coastguard Worker return false;
438*795d594fSAndroid Build Coastguard Worker }
439*795d594fSAndroid Build Coastguard Worker DCHECK(Thread::Current() == nullptr || !Thread::Current()->IsExceptionPending())
440*795d594fSAndroid Build Coastguard Worker << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
441*795d594fSAndroid Build Coastguard Worker << " " << pair.Inst().Name() << "@" << dex_pc;
442*795d594fSAndroid Build Coastguard Worker }
443*795d594fSAndroid Build Coastguard Worker
444*795d594fSAndroid Build Coastguard Worker if (current_block_ != nullptr) {
445*795d594fSAndroid Build Coastguard Worker // Branching instructions clear current_block, so we know the last
446*795d594fSAndroid Build Coastguard Worker // instruction of the current block is not a branching instruction.
447*795d594fSAndroid Build Coastguard Worker // We add an unconditional Goto to the next block.
448*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
449*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HGoto());
450*795d594fSAndroid Build Coastguard Worker }
451*795d594fSAndroid Build Coastguard Worker }
452*795d594fSAndroid Build Coastguard Worker
453*795d594fSAndroid Build Coastguard Worker SetLoopHeaderPhiInputs();
454*795d594fSAndroid Build Coastguard Worker
455*795d594fSAndroid Build Coastguard Worker return true;
456*795d594fSAndroid Build Coastguard Worker }
457*795d594fSAndroid Build Coastguard Worker
BuildIntrinsic(ArtMethod * method)458*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::BuildIntrinsic(ArtMethod* method) {
459*795d594fSAndroid Build Coastguard Worker DCHECK(!code_item_accessor_.HasCodeItem());
460*795d594fSAndroid Build Coastguard Worker DCHECK(method->IsIntrinsic());
461*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
462*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
463*795d594fSAndroid Build Coastguard Worker CHECK(!method->IsSignaturePolymorphic());
464*795d594fSAndroid Build Coastguard Worker }
465*795d594fSAndroid Build Coastguard Worker
466*795d594fSAndroid Build Coastguard Worker locals_for_.resize(
467*795d594fSAndroid Build Coastguard Worker graph_->GetBlocks().size(),
468*795d594fSAndroid Build Coastguard Worker ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
469*795d594fSAndroid Build Coastguard Worker
470*795d594fSAndroid Build Coastguard Worker // Fill the entry block. Do not add suspend check, we do not want a suspend
471*795d594fSAndroid Build Coastguard Worker // check in intrinsics; intrinsic methods are supposed to be fast.
472*795d594fSAndroid Build Coastguard Worker current_block_ = graph_->GetEntryBlock();
473*795d594fSAndroid Build Coastguard Worker InitializeBlockLocals();
474*795d594fSAndroid Build Coastguard Worker InitializeParameters();
475*795d594fSAndroid Build Coastguard Worker if (graph_->IsDebuggable() && code_generator_->GetCompilerOptions().IsJitCompiler()) {
476*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HMethodEntryHook(0u));
477*795d594fSAndroid Build Coastguard Worker }
478*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HGoto(0u));
479*795d594fSAndroid Build Coastguard Worker
480*795d594fSAndroid Build Coastguard Worker // Fill the body.
481*795d594fSAndroid Build Coastguard Worker current_block_ = current_block_->GetSingleSuccessor();
482*795d594fSAndroid Build Coastguard Worker InitializeBlockLocals();
483*795d594fSAndroid Build Coastguard Worker DCHECK(!IsBlockPopulated(current_block_));
484*795d594fSAndroid Build Coastguard Worker
485*795d594fSAndroid Build Coastguard Worker // Add the intermediate representation, if available, or invoke instruction.
486*795d594fSAndroid Build Coastguard Worker size_t in_vregs = graph_->GetNumberOfInVRegs();
487*795d594fSAndroid Build Coastguard Worker size_t number_of_arguments =
488*795d594fSAndroid Build Coastguard Worker in_vregs - std::count(current_locals_->end() - in_vregs, current_locals_->end(), nullptr);
489*795d594fSAndroid Build Coastguard Worker uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex();
490*795d594fSAndroid Build Coastguard Worker const char* shorty = dex_file_->GetMethodShorty(method_idx);
491*795d594fSAndroid Build Coastguard Worker RangeInstructionOperands operands(graph_->GetNumberOfVRegs() - in_vregs, in_vregs);
492*795d594fSAndroid Build Coastguard Worker if (!BuildSimpleIntrinsic(method, kNoDexPc, operands, shorty)) {
493*795d594fSAndroid Build Coastguard Worker // Some intrinsics without intermediate representation still yield a leaf method,
494*795d594fSAndroid Build Coastguard Worker // so build the invoke. Use HInvokeStaticOrDirect even for methods that would
495*795d594fSAndroid Build Coastguard Worker // normally use an HInvokeVirtual (sharpen the call).
496*795d594fSAndroid Build Coastguard Worker MethodReference target_method(dex_file_, method_idx);
497*795d594fSAndroid Build Coastguard Worker HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
498*795d594fSAndroid Build Coastguard Worker MethodLoadKind::kRuntimeCall,
499*795d594fSAndroid Build Coastguard Worker CodePtrLocation::kCallArtMethod,
500*795d594fSAndroid Build Coastguard Worker /* method_load_data= */ 0u
501*795d594fSAndroid Build Coastguard Worker };
502*795d594fSAndroid Build Coastguard Worker InvokeType invoke_type = dex_compilation_unit_->IsStatic() ? kStatic : kDirect;
503*795d594fSAndroid Build Coastguard Worker HInvokeStaticOrDirect* invoke = new (allocator_) HInvokeStaticOrDirect(
504*795d594fSAndroid Build Coastguard Worker allocator_,
505*795d594fSAndroid Build Coastguard Worker number_of_arguments,
506*795d594fSAndroid Build Coastguard Worker /* number_of_out_vregs= */ in_vregs,
507*795d594fSAndroid Build Coastguard Worker return_type_,
508*795d594fSAndroid Build Coastguard Worker kNoDexPc,
509*795d594fSAndroid Build Coastguard Worker target_method,
510*795d594fSAndroid Build Coastguard Worker method,
511*795d594fSAndroid Build Coastguard Worker dispatch_info,
512*795d594fSAndroid Build Coastguard Worker invoke_type,
513*795d594fSAndroid Build Coastguard Worker target_method,
514*795d594fSAndroid Build Coastguard Worker HInvokeStaticOrDirect::ClinitCheckRequirement::kNone,
515*795d594fSAndroid Build Coastguard Worker !graph_->IsDebuggable());
516*795d594fSAndroid Build Coastguard Worker HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
517*795d594fSAndroid Build Coastguard Worker }
518*795d594fSAndroid Build Coastguard Worker
519*795d594fSAndroid Build Coastguard Worker // Add the return instruction.
520*795d594fSAndroid Build Coastguard Worker if (return_type_ == DataType::Type::kVoid) {
521*795d594fSAndroid Build Coastguard Worker if (graph_->IsDebuggable() && code_generator_->GetCompilerOptions().IsJitCompiler()) {
522*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HMethodExitHook(graph_->GetNullConstant(), kNoDexPc));
523*795d594fSAndroid Build Coastguard Worker }
524*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HReturnVoid());
525*795d594fSAndroid Build Coastguard Worker } else {
526*795d594fSAndroid Build Coastguard Worker if (graph_->IsDebuggable() && code_generator_->GetCompilerOptions().IsJitCompiler()) {
527*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HMethodExitHook(latest_result_, kNoDexPc));
528*795d594fSAndroid Build Coastguard Worker }
529*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HReturn(latest_result_));
530*795d594fSAndroid Build Coastguard Worker }
531*795d594fSAndroid Build Coastguard Worker
532*795d594fSAndroid Build Coastguard Worker // Fill the exit block.
533*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(current_block_->GetSingleSuccessor(), graph_->GetExitBlock());
534*795d594fSAndroid Build Coastguard Worker current_block_ = graph_->GetExitBlock();
535*795d594fSAndroid Build Coastguard Worker InitializeBlockLocals();
536*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HExit());
537*795d594fSAndroid Build Coastguard Worker }
538*795d594fSAndroid Build Coastguard Worker
FindNativeDebugInfoLocations()539*795d594fSAndroid Build Coastguard Worker ArenaBitVector* HInstructionBuilder::FindNativeDebugInfoLocations() {
540*795d594fSAndroid Build Coastguard Worker ArenaBitVector* locations = ArenaBitVector::Create(local_allocator_,
541*795d594fSAndroid Build Coastguard Worker code_item_accessor_.InsnsSizeInCodeUnits(),
542*795d594fSAndroid Build Coastguard Worker /* expandable= */ false,
543*795d594fSAndroid Build Coastguard Worker kArenaAllocGraphBuilder);
544*795d594fSAndroid Build Coastguard Worker // The visitor gets called when the line number changes.
545*795d594fSAndroid Build Coastguard Worker // In other words, it marks the start of new java statement.
546*795d594fSAndroid Build Coastguard Worker code_item_accessor_.DecodeDebugPositionInfo([&](const DexFile::PositionInfo& entry) {
547*795d594fSAndroid Build Coastguard Worker locations->SetBit(entry.address_);
548*795d594fSAndroid Build Coastguard Worker return false;
549*795d594fSAndroid Build Coastguard Worker });
550*795d594fSAndroid Build Coastguard Worker // Instruction-specific tweaks.
551*795d594fSAndroid Build Coastguard Worker for (const DexInstructionPcPair& inst : code_item_accessor_) {
552*795d594fSAndroid Build Coastguard Worker switch (inst->Opcode()) {
553*795d594fSAndroid Build Coastguard Worker case Instruction::MOVE_EXCEPTION: {
554*795d594fSAndroid Build Coastguard Worker // Stop in native debugger after the exception has been moved.
555*795d594fSAndroid Build Coastguard Worker // The compiler also expects the move at the start of basic block so
556*795d594fSAndroid Build Coastguard Worker // we do not want to interfere by inserting native-debug-info before it.
557*795d594fSAndroid Build Coastguard Worker locations->ClearBit(inst.DexPc());
558*795d594fSAndroid Build Coastguard Worker DexInstructionIterator next = std::next(DexInstructionIterator(inst));
559*795d594fSAndroid Build Coastguard Worker DCHECK(next.DexPc() != inst.DexPc());
560*795d594fSAndroid Build Coastguard Worker if (next != code_item_accessor_.end()) {
561*795d594fSAndroid Build Coastguard Worker locations->SetBit(next.DexPc());
562*795d594fSAndroid Build Coastguard Worker }
563*795d594fSAndroid Build Coastguard Worker break;
564*795d594fSAndroid Build Coastguard Worker }
565*795d594fSAndroid Build Coastguard Worker default:
566*795d594fSAndroid Build Coastguard Worker break;
567*795d594fSAndroid Build Coastguard Worker }
568*795d594fSAndroid Build Coastguard Worker }
569*795d594fSAndroid Build Coastguard Worker return locations;
570*795d594fSAndroid Build Coastguard Worker }
571*795d594fSAndroid Build Coastguard Worker
LoadLocal(uint32_t reg_number,DataType::Type type) const572*795d594fSAndroid Build Coastguard Worker HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
573*795d594fSAndroid Build Coastguard Worker HInstruction* value = (*current_locals_)[reg_number];
574*795d594fSAndroid Build Coastguard Worker DCHECK(value != nullptr);
575*795d594fSAndroid Build Coastguard Worker
576*795d594fSAndroid Build Coastguard Worker // If the operation requests a specific type, we make sure its input is of that type.
577*795d594fSAndroid Build Coastguard Worker if (type != value->GetType()) {
578*795d594fSAndroid Build Coastguard Worker if (DataType::IsFloatingPointType(type)) {
579*795d594fSAndroid Build Coastguard Worker value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
580*795d594fSAndroid Build Coastguard Worker } else if (type == DataType::Type::kReference) {
581*795d594fSAndroid Build Coastguard Worker value = ssa_builder_->GetReferenceTypeEquivalent(value);
582*795d594fSAndroid Build Coastguard Worker }
583*795d594fSAndroid Build Coastguard Worker DCHECK(value != nullptr);
584*795d594fSAndroid Build Coastguard Worker }
585*795d594fSAndroid Build Coastguard Worker
586*795d594fSAndroid Build Coastguard Worker return value;
587*795d594fSAndroid Build Coastguard Worker }
588*795d594fSAndroid Build Coastguard Worker
UpdateLocal(uint32_t reg_number,HInstruction * stored_value)589*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
590*795d594fSAndroid Build Coastguard Worker DataType::Type stored_type = stored_value->GetType();
591*795d594fSAndroid Build Coastguard Worker DCHECK_NE(stored_type, DataType::Type::kVoid);
592*795d594fSAndroid Build Coastguard Worker
593*795d594fSAndroid Build Coastguard Worker // Storing into vreg `reg_number` may implicitly invalidate the surrounding
594*795d594fSAndroid Build Coastguard Worker // registers. Consider the following cases:
595*795d594fSAndroid Build Coastguard Worker // (1) Storing a wide value must overwrite previous values in both `reg_number`
596*795d594fSAndroid Build Coastguard Worker // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
597*795d594fSAndroid Build Coastguard Worker // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
598*795d594fSAndroid Build Coastguard Worker // must invalidate it. We store `nullptr` in `reg_number-1`.
599*795d594fSAndroid Build Coastguard Worker // Consequently, storing a wide value into the high vreg of another wide value
600*795d594fSAndroid Build Coastguard Worker // will invalidate both `reg_number-1` and `reg_number+1`.
601*795d594fSAndroid Build Coastguard Worker
602*795d594fSAndroid Build Coastguard Worker if (reg_number != 0) {
603*795d594fSAndroid Build Coastguard Worker HInstruction* local_low = (*current_locals_)[reg_number - 1];
604*795d594fSAndroid Build Coastguard Worker if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
605*795d594fSAndroid Build Coastguard Worker // The vreg we are storing into was previously the high vreg of a pair.
606*795d594fSAndroid Build Coastguard Worker // We need to invalidate its low vreg.
607*795d594fSAndroid Build Coastguard Worker DCHECK((*current_locals_)[reg_number] == nullptr);
608*795d594fSAndroid Build Coastguard Worker (*current_locals_)[reg_number - 1] = nullptr;
609*795d594fSAndroid Build Coastguard Worker }
610*795d594fSAndroid Build Coastguard Worker }
611*795d594fSAndroid Build Coastguard Worker
612*795d594fSAndroid Build Coastguard Worker (*current_locals_)[reg_number] = stored_value;
613*795d594fSAndroid Build Coastguard Worker if (DataType::Is64BitType(stored_type)) {
614*795d594fSAndroid Build Coastguard Worker // We are storing a pair. Invalidate the instruction in the high vreg.
615*795d594fSAndroid Build Coastguard Worker (*current_locals_)[reg_number + 1] = nullptr;
616*795d594fSAndroid Build Coastguard Worker }
617*795d594fSAndroid Build Coastguard Worker }
618*795d594fSAndroid Build Coastguard Worker
InitializeParameters()619*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::InitializeParameters() {
620*795d594fSAndroid Build Coastguard Worker DCHECK(current_block_->IsEntryBlock());
621*795d594fSAndroid Build Coastguard Worker
622*795d594fSAndroid Build Coastguard Worker // outer_compilation_unit_ is null only when unit testing.
623*795d594fSAndroid Build Coastguard Worker if (outer_compilation_unit_ == nullptr) {
624*795d594fSAndroid Build Coastguard Worker return;
625*795d594fSAndroid Build Coastguard Worker }
626*795d594fSAndroid Build Coastguard Worker
627*795d594fSAndroid Build Coastguard Worker const char* shorty = dex_compilation_unit_->GetShorty();
628*795d594fSAndroid Build Coastguard Worker uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
629*795d594fSAndroid Build Coastguard Worker uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
630*795d594fSAndroid Build Coastguard Worker uint16_t parameter_index = 0;
631*795d594fSAndroid Build Coastguard Worker
632*795d594fSAndroid Build Coastguard Worker const dex::MethodId& referrer_method_id =
633*795d594fSAndroid Build Coastguard Worker dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
634*795d594fSAndroid Build Coastguard Worker if (!dex_compilation_unit_->IsStatic()) {
635*795d594fSAndroid Build Coastguard Worker // Add the implicit 'this' argument, not expressed in the signature.
636*795d594fSAndroid Build Coastguard Worker HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
637*795d594fSAndroid Build Coastguard Worker referrer_method_id.class_idx_,
638*795d594fSAndroid Build Coastguard Worker parameter_index++,
639*795d594fSAndroid Build Coastguard Worker DataType::Type::kReference,
640*795d594fSAndroid Build Coastguard Worker /* is_this= */ true);
641*795d594fSAndroid Build Coastguard Worker AppendInstruction(parameter);
642*795d594fSAndroid Build Coastguard Worker UpdateLocal(locals_index++, parameter);
643*795d594fSAndroid Build Coastguard Worker number_of_parameters--;
644*795d594fSAndroid Build Coastguard Worker current_this_parameter_ = parameter;
645*795d594fSAndroid Build Coastguard Worker } else {
646*795d594fSAndroid Build Coastguard Worker DCHECK(current_this_parameter_ == nullptr);
647*795d594fSAndroid Build Coastguard Worker }
648*795d594fSAndroid Build Coastguard Worker
649*795d594fSAndroid Build Coastguard Worker const dex::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
650*795d594fSAndroid Build Coastguard Worker const dex::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
651*795d594fSAndroid Build Coastguard Worker for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
652*795d594fSAndroid Build Coastguard Worker HParameterValue* parameter = new (allocator_) HParameterValue(
653*795d594fSAndroid Build Coastguard Worker *dex_file_,
654*795d594fSAndroid Build Coastguard Worker arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
655*795d594fSAndroid Build Coastguard Worker parameter_index++,
656*795d594fSAndroid Build Coastguard Worker DataType::FromShorty(shorty[shorty_pos]),
657*795d594fSAndroid Build Coastguard Worker /* is_this= */ false);
658*795d594fSAndroid Build Coastguard Worker ++shorty_pos;
659*795d594fSAndroid Build Coastguard Worker AppendInstruction(parameter);
660*795d594fSAndroid Build Coastguard Worker // Store the parameter value in the local that the dex code will use
661*795d594fSAndroid Build Coastguard Worker // to reference that parameter.
662*795d594fSAndroid Build Coastguard Worker UpdateLocal(locals_index++, parameter);
663*795d594fSAndroid Build Coastguard Worker if (DataType::Is64BitType(parameter->GetType())) {
664*795d594fSAndroid Build Coastguard Worker i++;
665*795d594fSAndroid Build Coastguard Worker locals_index++;
666*795d594fSAndroid Build Coastguard Worker parameter_index++;
667*795d594fSAndroid Build Coastguard Worker }
668*795d594fSAndroid Build Coastguard Worker }
669*795d594fSAndroid Build Coastguard Worker }
670*795d594fSAndroid Build Coastguard Worker
671*795d594fSAndroid Build Coastguard Worker template<typename T, bool kCompareWithZero>
If_21_22t(const Instruction & instruction,uint32_t dex_pc)672*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::If_21_22t(const Instruction& instruction, uint32_t dex_pc) {
673*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(kCompareWithZero ? Instruction::Format::k21t : Instruction::Format::k22t,
674*795d594fSAndroid Build Coastguard Worker Instruction::FormatOf(instruction.Opcode()));
675*795d594fSAndroid Build Coastguard Worker HInstruction* value = LoadLocal(
676*795d594fSAndroid Build Coastguard Worker kCompareWithZero ? instruction.VRegA_21t() : instruction.VRegA_22t(),
677*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt32);
678*795d594fSAndroid Build Coastguard Worker T* comparison = nullptr;
679*795d594fSAndroid Build Coastguard Worker if (kCompareWithZero) {
680*795d594fSAndroid Build Coastguard Worker comparison = new (allocator_) T(value, graph_->GetIntConstant(0), dex_pc);
681*795d594fSAndroid Build Coastguard Worker } else {
682*795d594fSAndroid Build Coastguard Worker HInstruction* second = LoadLocal(instruction.VRegB_22t(), DataType::Type::kInt32);
683*795d594fSAndroid Build Coastguard Worker comparison = new (allocator_) T(value, second, dex_pc);
684*795d594fSAndroid Build Coastguard Worker }
685*795d594fSAndroid Build Coastguard Worker AppendInstruction(comparison);
686*795d594fSAndroid Build Coastguard Worker HIf* if_instr = new (allocator_) HIf(comparison, dex_pc);
687*795d594fSAndroid Build Coastguard Worker
688*795d594fSAndroid Build Coastguard Worker ProfilingInfo* info = graph_->GetProfilingInfo();
689*795d594fSAndroid Build Coastguard Worker if (info != nullptr && !graph_->IsCompilingBaseline()) {
690*795d594fSAndroid Build Coastguard Worker BranchCache* cache = info->GetBranchCache(dex_pc);
691*795d594fSAndroid Build Coastguard Worker if (cache != nullptr) {
692*795d594fSAndroid Build Coastguard Worker if_instr->SetTrueCount(cache->GetTrue());
693*795d594fSAndroid Build Coastguard Worker if_instr->SetFalseCount(cache->GetFalse());
694*795d594fSAndroid Build Coastguard Worker }
695*795d594fSAndroid Build Coastguard Worker }
696*795d594fSAndroid Build Coastguard Worker
697*795d594fSAndroid Build Coastguard Worker // Append after setting true/false count, so that the builder knows if the
698*795d594fSAndroid Build Coastguard Worker // instruction needs an environment.
699*795d594fSAndroid Build Coastguard Worker AppendInstruction(if_instr);
700*795d594fSAndroid Build Coastguard Worker current_block_ = nullptr;
701*795d594fSAndroid Build Coastguard Worker }
702*795d594fSAndroid Build Coastguard Worker
703*795d594fSAndroid Build Coastguard Worker template<typename T>
Unop_12x(const Instruction & instruction,DataType::Type type,uint32_t dex_pc)704*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::Unop_12x(const Instruction& instruction,
705*795d594fSAndroid Build Coastguard Worker DataType::Type type,
706*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc) {
707*795d594fSAndroid Build Coastguard Worker HInstruction* first = LoadLocal(instruction.VRegB_12x(), type);
708*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) T(type, first, dex_pc));
709*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
710*795d594fSAndroid Build Coastguard Worker }
711*795d594fSAndroid Build Coastguard Worker
Conversion_12x(const Instruction & instruction,DataType::Type input_type,DataType::Type result_type,uint32_t dex_pc)712*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
713*795d594fSAndroid Build Coastguard Worker DataType::Type input_type,
714*795d594fSAndroid Build Coastguard Worker DataType::Type result_type,
715*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc) {
716*795d594fSAndroid Build Coastguard Worker HInstruction* first = LoadLocal(instruction.VRegB_12x(), input_type);
717*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
718*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
719*795d594fSAndroid Build Coastguard Worker }
720*795d594fSAndroid Build Coastguard Worker
721*795d594fSAndroid Build Coastguard Worker template<typename T>
Binop_23x(const Instruction & instruction,DataType::Type type,uint32_t dex_pc)722*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::Binop_23x(const Instruction& instruction,
723*795d594fSAndroid Build Coastguard Worker DataType::Type type,
724*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc) {
725*795d594fSAndroid Build Coastguard Worker HInstruction* first = LoadLocal(instruction.VRegB_23x(), type);
726*795d594fSAndroid Build Coastguard Worker HInstruction* second = LoadLocal(instruction.VRegC_23x(), type);
727*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
728*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_23x(), current_block_->GetLastInstruction());
729*795d594fSAndroid Build Coastguard Worker }
730*795d594fSAndroid Build Coastguard Worker
731*795d594fSAndroid Build Coastguard Worker template<typename T>
Binop_23x_shift(const Instruction & instruction,DataType::Type type,uint32_t dex_pc)732*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
733*795d594fSAndroid Build Coastguard Worker DataType::Type type,
734*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc) {
735*795d594fSAndroid Build Coastguard Worker HInstruction* first = LoadLocal(instruction.VRegB_23x(), type);
736*795d594fSAndroid Build Coastguard Worker HInstruction* second = LoadLocal(instruction.VRegC_23x(), DataType::Type::kInt32);
737*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
738*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_23x(), current_block_->GetLastInstruction());
739*795d594fSAndroid Build Coastguard Worker }
740*795d594fSAndroid Build Coastguard Worker
Binop_23x_cmp(const Instruction & instruction,DataType::Type type,ComparisonBias bias,uint32_t dex_pc)741*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
742*795d594fSAndroid Build Coastguard Worker DataType::Type type,
743*795d594fSAndroid Build Coastguard Worker ComparisonBias bias,
744*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc) {
745*795d594fSAndroid Build Coastguard Worker HInstruction* first = LoadLocal(instruction.VRegB_23x(), type);
746*795d594fSAndroid Build Coastguard Worker HInstruction* second = LoadLocal(instruction.VRegC_23x(), type);
747*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
748*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_23x(), current_block_->GetLastInstruction());
749*795d594fSAndroid Build Coastguard Worker }
750*795d594fSAndroid Build Coastguard Worker
751*795d594fSAndroid Build Coastguard Worker template<typename T>
Binop_12x_shift(const Instruction & instruction,DataType::Type type,uint32_t dex_pc)752*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
753*795d594fSAndroid Build Coastguard Worker DataType::Type type,
754*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc) {
755*795d594fSAndroid Build Coastguard Worker HInstruction* first = LoadLocal(instruction.VRegA_12x(), type);
756*795d594fSAndroid Build Coastguard Worker HInstruction* second = LoadLocal(instruction.VRegB_12x(), DataType::Type::kInt32);
757*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
758*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
759*795d594fSAndroid Build Coastguard Worker }
760*795d594fSAndroid Build Coastguard Worker
761*795d594fSAndroid Build Coastguard Worker template<typename T>
Binop_12x(const Instruction & instruction,DataType::Type type,uint32_t dex_pc)762*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::Binop_12x(const Instruction& instruction,
763*795d594fSAndroid Build Coastguard Worker DataType::Type type,
764*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc) {
765*795d594fSAndroid Build Coastguard Worker HInstruction* first = LoadLocal(instruction.VRegA_12x(), type);
766*795d594fSAndroid Build Coastguard Worker HInstruction* second = LoadLocal(instruction.VRegB_12x(), type);
767*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
768*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
769*795d594fSAndroid Build Coastguard Worker }
770*795d594fSAndroid Build Coastguard Worker
771*795d594fSAndroid Build Coastguard Worker template<typename T>
Binop_22s(const Instruction & instruction,bool reverse,uint32_t dex_pc)772*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
773*795d594fSAndroid Build Coastguard Worker HInstruction* first = LoadLocal(instruction.VRegB_22s(), DataType::Type::kInt32);
774*795d594fSAndroid Build Coastguard Worker HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s());
775*795d594fSAndroid Build Coastguard Worker if (reverse) {
776*795d594fSAndroid Build Coastguard Worker std::swap(first, second);
777*795d594fSAndroid Build Coastguard Worker }
778*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
779*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_22s(), current_block_->GetLastInstruction());
780*795d594fSAndroid Build Coastguard Worker }
781*795d594fSAndroid Build Coastguard Worker
782*795d594fSAndroid Build Coastguard Worker template<typename T>
Binop_22b(const Instruction & instruction,bool reverse,uint32_t dex_pc)783*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
784*795d594fSAndroid Build Coastguard Worker HInstruction* first = LoadLocal(instruction.VRegB_22b(), DataType::Type::kInt32);
785*795d594fSAndroid Build Coastguard Worker HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b());
786*795d594fSAndroid Build Coastguard Worker if (reverse) {
787*795d594fSAndroid Build Coastguard Worker std::swap(first, second);
788*795d594fSAndroid Build Coastguard Worker }
789*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
790*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_22b(), current_block_->GetLastInstruction());
791*795d594fSAndroid Build Coastguard Worker }
792*795d594fSAndroid Build Coastguard Worker
793*795d594fSAndroid Build Coastguard Worker // Does the method being compiled need any constructor barriers being inserted?
794*795d594fSAndroid Build Coastguard Worker // (Always 'false' for methods that aren't <init>.)
RequiresConstructorBarrier(const DexCompilationUnit * cu)795*795d594fSAndroid Build Coastguard Worker static bool RequiresConstructorBarrier(const DexCompilationUnit* cu) {
796*795d594fSAndroid Build Coastguard Worker // Can be null in unit tests only.
797*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(cu == nullptr)) {
798*795d594fSAndroid Build Coastguard Worker return false;
799*795d594fSAndroid Build Coastguard Worker }
800*795d594fSAndroid Build Coastguard Worker
801*795d594fSAndroid Build Coastguard Worker // Constructor barriers are applicable only for <init> methods.
802*795d594fSAndroid Build Coastguard Worker if (LIKELY(!cu->IsConstructor() || cu->IsStatic())) {
803*795d594fSAndroid Build Coastguard Worker return false;
804*795d594fSAndroid Build Coastguard Worker }
805*795d594fSAndroid Build Coastguard Worker
806*795d594fSAndroid Build Coastguard Worker return cu->RequiresConstructorBarrier();
807*795d594fSAndroid Build Coastguard Worker }
808*795d594fSAndroid Build Coastguard Worker
809*795d594fSAndroid Build Coastguard Worker // Returns true if `block` has only one successor which starts at the next
810*795d594fSAndroid Build Coastguard Worker // dex_pc after `instruction` at `dex_pc`.
IsFallthroughInstruction(const Instruction & instruction,uint32_t dex_pc,HBasicBlock * block)811*795d594fSAndroid Build Coastguard Worker static bool IsFallthroughInstruction(const Instruction& instruction,
812*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc,
813*795d594fSAndroid Build Coastguard Worker HBasicBlock* block) {
814*795d594fSAndroid Build Coastguard Worker uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
815*795d594fSAndroid Build Coastguard Worker return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
816*795d594fSAndroid Build Coastguard Worker }
817*795d594fSAndroid Build Coastguard Worker
BuildSwitch(const Instruction & instruction,uint32_t dex_pc)818*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
819*795d594fSAndroid Build Coastguard Worker HInstruction* value = LoadLocal(instruction.VRegA_31t(), DataType::Type::kInt32);
820*795d594fSAndroid Build Coastguard Worker DexSwitchTable table(instruction, dex_pc);
821*795d594fSAndroid Build Coastguard Worker
822*795d594fSAndroid Build Coastguard Worker if (table.GetNumEntries() == 0) {
823*795d594fSAndroid Build Coastguard Worker // Empty Switch. Code falls through to the next block.
824*795d594fSAndroid Build Coastguard Worker DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
825*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HGoto(dex_pc));
826*795d594fSAndroid Build Coastguard Worker } else if (table.ShouldBuildDecisionTree()) {
827*795d594fSAndroid Build Coastguard Worker for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
828*795d594fSAndroid Build Coastguard Worker HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey());
829*795d594fSAndroid Build Coastguard Worker HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
830*795d594fSAndroid Build Coastguard Worker AppendInstruction(comparison);
831*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
832*795d594fSAndroid Build Coastguard Worker
833*795d594fSAndroid Build Coastguard Worker if (!it.IsLast()) {
834*795d594fSAndroid Build Coastguard Worker current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
835*795d594fSAndroid Build Coastguard Worker }
836*795d594fSAndroid Build Coastguard Worker }
837*795d594fSAndroid Build Coastguard Worker } else {
838*795d594fSAndroid Build Coastguard Worker AppendInstruction(
839*795d594fSAndroid Build Coastguard Worker new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
840*795d594fSAndroid Build Coastguard Worker }
841*795d594fSAndroid Build Coastguard Worker
842*795d594fSAndroid Build Coastguard Worker current_block_ = nullptr;
843*795d594fSAndroid Build Coastguard Worker }
844*795d594fSAndroid Build Coastguard Worker
845*795d594fSAndroid Build Coastguard Worker template <DataType::Type type>
BuildMove(uint32_t dest_reg,uint32_t src_reg)846*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE inline void HInstructionBuilder::BuildMove(uint32_t dest_reg, uint32_t src_reg) {
847*795d594fSAndroid Build Coastguard Worker // The verifier has no notion of a null type, so a move-object of constant 0
848*795d594fSAndroid Build Coastguard Worker // will lead to the same constant 0 in the destination register. To mimic
849*795d594fSAndroid Build Coastguard Worker // this behavior, we just pretend we haven't seen a type change (int to reference)
850*795d594fSAndroid Build Coastguard Worker // for the 0 constant and phis. We rely on our type propagation to eventually get the
851*795d594fSAndroid Build Coastguard Worker // types correct.
852*795d594fSAndroid Build Coastguard Worker constexpr bool is_reference = type == DataType::Type::kReference;
853*795d594fSAndroid Build Coastguard Worker HInstruction* value = is_reference ? (*current_locals_)[src_reg] : /* not needed */ nullptr;
854*795d594fSAndroid Build Coastguard Worker if (is_reference && value->IsIntConstant()) {
855*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
856*795d594fSAndroid Build Coastguard Worker } else if (is_reference && value->IsPhi()) {
857*795d594fSAndroid Build Coastguard Worker DCHECK(value->GetType() == DataType::Type::kInt32 ||
858*795d594fSAndroid Build Coastguard Worker value->GetType() == DataType::Type::kReference);
859*795d594fSAndroid Build Coastguard Worker } else {
860*795d594fSAndroid Build Coastguard Worker value = LoadLocal(src_reg, type);
861*795d594fSAndroid Build Coastguard Worker }
862*795d594fSAndroid Build Coastguard Worker UpdateLocal(dest_reg, value);
863*795d594fSAndroid Build Coastguard Worker }
864*795d594fSAndroid Build Coastguard Worker
BuildReturn(const Instruction & instruction,DataType::Type type,uint32_t dex_pc)865*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::BuildReturn(const Instruction& instruction,
866*795d594fSAndroid Build Coastguard Worker DataType::Type type,
867*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc) {
868*795d594fSAndroid Build Coastguard Worker if (type == DataType::Type::kVoid) {
869*795d594fSAndroid Build Coastguard Worker // Only <init> (which is a return-void) could possibly have a constructor fence.
870*795d594fSAndroid Build Coastguard Worker // This may insert additional redundant constructor fences from the super constructors.
871*795d594fSAndroid Build Coastguard Worker // TODO: remove redundant constructor fences (b/36656456).
872*795d594fSAndroid Build Coastguard Worker if (RequiresConstructorBarrier(dex_compilation_unit_)) {
873*795d594fSAndroid Build Coastguard Worker // Compiling instance constructor.
874*795d594fSAndroid Build Coastguard Worker DCHECK_STREQ("<init>", graph_->GetMethodName());
875*795d594fSAndroid Build Coastguard Worker
876*795d594fSAndroid Build Coastguard Worker HInstruction* fence_target = current_this_parameter_;
877*795d594fSAndroid Build Coastguard Worker DCHECK(fence_target != nullptr);
878*795d594fSAndroid Build Coastguard Worker
879*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
880*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(
881*795d594fSAndroid Build Coastguard Worker compilation_stats_,
882*795d594fSAndroid Build Coastguard Worker MethodCompilationStat::kConstructorFenceGeneratedFinal);
883*795d594fSAndroid Build Coastguard Worker }
884*795d594fSAndroid Build Coastguard Worker if (graph_->IsDebuggable() && code_generator_->GetCompilerOptions().IsJitCompiler()) {
885*795d594fSAndroid Build Coastguard Worker // Return value is not used for void functions. We pass NullConstant to
886*795d594fSAndroid Build Coastguard Worker // avoid special cases when generating code.
887*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HMethodExitHook(graph_->GetNullConstant(), dex_pc));
888*795d594fSAndroid Build Coastguard Worker }
889*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
890*795d594fSAndroid Build Coastguard Worker } else {
891*795d594fSAndroid Build Coastguard Worker DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_));
892*795d594fSAndroid Build Coastguard Worker HInstruction* value = LoadLocal(instruction.VRegA_11x(), type);
893*795d594fSAndroid Build Coastguard Worker if (graph_->IsDebuggable() && code_generator_->GetCompilerOptions().IsJitCompiler()) {
894*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HMethodExitHook(value, dex_pc));
895*795d594fSAndroid Build Coastguard Worker }
896*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HReturn(value, dex_pc));
897*795d594fSAndroid Build Coastguard Worker }
898*795d594fSAndroid Build Coastguard Worker current_block_ = nullptr;
899*795d594fSAndroid Build Coastguard Worker }
900*795d594fSAndroid Build Coastguard Worker
GetInvokeTypeFromOpCode(Instruction::Code opcode)901*795d594fSAndroid Build Coastguard Worker static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
902*795d594fSAndroid Build Coastguard Worker switch (opcode) {
903*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_STATIC:
904*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_STATIC_RANGE:
905*795d594fSAndroid Build Coastguard Worker return kStatic;
906*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_DIRECT:
907*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_DIRECT_RANGE:
908*795d594fSAndroid Build Coastguard Worker return kDirect;
909*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_VIRTUAL:
910*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_VIRTUAL_RANGE:
911*795d594fSAndroid Build Coastguard Worker return kVirtual;
912*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_INTERFACE:
913*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_INTERFACE_RANGE:
914*795d594fSAndroid Build Coastguard Worker return kInterface;
915*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_SUPER_RANGE:
916*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_SUPER:
917*795d594fSAndroid Build Coastguard Worker return kSuper;
918*795d594fSAndroid Build Coastguard Worker default:
919*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
920*795d594fSAndroid Build Coastguard Worker UNREACHABLE();
921*795d594fSAndroid Build Coastguard Worker }
922*795d594fSAndroid Build Coastguard Worker }
923*795d594fSAndroid Build Coastguard Worker
924*795d594fSAndroid Build Coastguard Worker // Try to resolve a method using the class linker. Return null if a method could
925*795d594fSAndroid Build Coastguard Worker // not be resolved or the resolved method cannot be used for some reason.
926*795d594fSAndroid Build Coastguard Worker // Also retrieve method data needed for creating the invoke intermediate
927*795d594fSAndroid Build Coastguard Worker // representation while we hold the mutator lock here.
ResolveMethod(uint16_t method_idx,ArtMethod * referrer,const DexCompilationUnit & dex_compilation_unit,InvokeType * invoke_type,MethodReference * resolved_method_info,uint16_t * imt_or_vtable_index,bool * is_string_constructor)928*795d594fSAndroid Build Coastguard Worker static ArtMethod* ResolveMethod(uint16_t method_idx,
929*795d594fSAndroid Build Coastguard Worker ArtMethod* referrer,
930*795d594fSAndroid Build Coastguard Worker const DexCompilationUnit& dex_compilation_unit,
931*795d594fSAndroid Build Coastguard Worker /*inout*/InvokeType* invoke_type,
932*795d594fSAndroid Build Coastguard Worker /*out*/MethodReference* resolved_method_info,
933*795d594fSAndroid Build Coastguard Worker /*out*/uint16_t* imt_or_vtable_index,
934*795d594fSAndroid Build Coastguard Worker /*out*/bool* is_string_constructor) {
935*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
936*795d594fSAndroid Build Coastguard Worker
937*795d594fSAndroid Build Coastguard Worker ClassLinker* class_linker = dex_compilation_unit.GetClassLinker();
938*795d594fSAndroid Build Coastguard Worker Handle<mirror::ClassLoader> class_loader = dex_compilation_unit.GetClassLoader();
939*795d594fSAndroid Build Coastguard Worker
940*795d594fSAndroid Build Coastguard Worker ArtMethod* resolved_method = nullptr;
941*795d594fSAndroid Build Coastguard Worker if (referrer == nullptr) {
942*795d594fSAndroid Build Coastguard Worker // The referrer may be unresolved for AOT if we're compiling a class that cannot be
943*795d594fSAndroid Build Coastguard Worker // resolved because, for example, we don't find a superclass in the classpath.
944*795d594fSAndroid Build Coastguard Worker resolved_method = class_linker->ResolveMethodId(
945*795d594fSAndroid Build Coastguard Worker method_idx, dex_compilation_unit.GetDexCache(), class_loader);
946*795d594fSAndroid Build Coastguard Worker } else if (referrer->SkipAccessChecks()) {
947*795d594fSAndroid Build Coastguard Worker resolved_method = class_linker->ResolveMethodId(method_idx, referrer);
948*795d594fSAndroid Build Coastguard Worker } else {
949*795d594fSAndroid Build Coastguard Worker resolved_method = class_linker->ResolveMethodWithChecks(
950*795d594fSAndroid Build Coastguard Worker method_idx,
951*795d594fSAndroid Build Coastguard Worker referrer,
952*795d594fSAndroid Build Coastguard Worker *invoke_type);
953*795d594fSAndroid Build Coastguard Worker }
954*795d594fSAndroid Build Coastguard Worker
955*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(resolved_method == nullptr)) {
956*795d594fSAndroid Build Coastguard Worker // Clean up any exception left by type resolution.
957*795d594fSAndroid Build Coastguard Worker soa.Self()->ClearException();
958*795d594fSAndroid Build Coastguard Worker return nullptr;
959*795d594fSAndroid Build Coastguard Worker }
960*795d594fSAndroid Build Coastguard Worker DCHECK(!soa.Self()->IsExceptionPending());
961*795d594fSAndroid Build Coastguard Worker
962*795d594fSAndroid Build Coastguard Worker if (referrer == nullptr) {
963*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
964*795d594fSAndroid Build Coastguard Worker dex_compilation_unit.GetDexFile()->GetMethodId(method_idx).class_idx_,
965*795d594fSAndroid Build Coastguard Worker dex_compilation_unit.GetDexCache().Get(),
966*795d594fSAndroid Build Coastguard Worker class_loader.Get());
967*795d594fSAndroid Build Coastguard Worker DCHECK(referenced_class != nullptr); // Must have been resolved when resolving the method.
968*795d594fSAndroid Build Coastguard Worker if (class_linker->ThrowIfInvokeClassMismatch(referenced_class,
969*795d594fSAndroid Build Coastguard Worker *dex_compilation_unit.GetDexFile(),
970*795d594fSAndroid Build Coastguard Worker *invoke_type)) {
971*795d594fSAndroid Build Coastguard Worker soa.Self()->ClearException();
972*795d594fSAndroid Build Coastguard Worker return nullptr;
973*795d594fSAndroid Build Coastguard Worker }
974*795d594fSAndroid Build Coastguard Worker // The class linker cannot check access without a referrer, so we have to do it.
975*795d594fSAndroid Build Coastguard Worker // Check if the declaring class or referencing class is accessible.
976*795d594fSAndroid Build Coastguard Worker SamePackageCompare same_package(dex_compilation_unit);
977*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> declaring_class = resolved_method->GetDeclaringClass();
978*795d594fSAndroid Build Coastguard Worker bool declaring_class_accessible = declaring_class->IsPublic() || same_package(declaring_class);
979*795d594fSAndroid Build Coastguard Worker if (!declaring_class_accessible) {
980*795d594fSAndroid Build Coastguard Worker // It is possible to access members from an inaccessible superclass
981*795d594fSAndroid Build Coastguard Worker // by referencing them through an accessible subclass.
982*795d594fSAndroid Build Coastguard Worker if (!referenced_class->IsPublic() && !same_package(referenced_class)) {
983*795d594fSAndroid Build Coastguard Worker return nullptr;
984*795d594fSAndroid Build Coastguard Worker }
985*795d594fSAndroid Build Coastguard Worker }
986*795d594fSAndroid Build Coastguard Worker // Check whether the method itself is accessible.
987*795d594fSAndroid Build Coastguard Worker // Since the referrer is unresolved but the method is resolved, it cannot be
988*795d594fSAndroid Build Coastguard Worker // inside the same class, so a private method is known to be inaccessible.
989*795d594fSAndroid Build Coastguard Worker // And without a resolved referrer, we cannot check for protected member access
990*795d594fSAndroid Build Coastguard Worker // in superlass, so we handle only access to public member or within the package.
991*795d594fSAndroid Build Coastguard Worker if (resolved_method->IsPrivate() ||
992*795d594fSAndroid Build Coastguard Worker (!resolved_method->IsPublic() && !declaring_class_accessible)) {
993*795d594fSAndroid Build Coastguard Worker return nullptr;
994*795d594fSAndroid Build Coastguard Worker }
995*795d594fSAndroid Build Coastguard Worker }
996*795d594fSAndroid Build Coastguard Worker
997*795d594fSAndroid Build Coastguard Worker // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
998*795d594fSAndroid Build Coastguard Worker // We need to look at the referrer's super class vtable. We need to do this to know if we need to
999*795d594fSAndroid Build Coastguard Worker // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
1000*795d594fSAndroid Build Coastguard Worker // which require runtime handling.
1001*795d594fSAndroid Build Coastguard Worker if (*invoke_type == kSuper) {
1002*795d594fSAndroid Build Coastguard Worker if (referrer == nullptr) {
1003*795d594fSAndroid Build Coastguard Worker // We could not determine the method's class we need to wait until runtime.
1004*795d594fSAndroid Build Coastguard Worker DCHECK(Runtime::Current()->IsAotCompiler());
1005*795d594fSAndroid Build Coastguard Worker return nullptr;
1006*795d594fSAndroid Build Coastguard Worker }
1007*795d594fSAndroid Build Coastguard Worker ArtMethod* actual_method = FindSuperMethodToCall</*access_check=*/true>(
1008*795d594fSAndroid Build Coastguard Worker method_idx, resolved_method, referrer, soa.Self());
1009*795d594fSAndroid Build Coastguard Worker if (actual_method == nullptr) {
1010*795d594fSAndroid Build Coastguard Worker // Clean up any exception left by method resolution.
1011*795d594fSAndroid Build Coastguard Worker soa.Self()->ClearException();
1012*795d594fSAndroid Build Coastguard Worker return nullptr;
1013*795d594fSAndroid Build Coastguard Worker }
1014*795d594fSAndroid Build Coastguard Worker if (!actual_method->IsInvokable()) {
1015*795d594fSAndroid Build Coastguard Worker // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
1016*795d594fSAndroid Build Coastguard Worker // could resolve the callee to the wrong method.
1017*795d594fSAndroid Build Coastguard Worker return nullptr;
1018*795d594fSAndroid Build Coastguard Worker }
1019*795d594fSAndroid Build Coastguard Worker // Call GetCanonicalMethod in case the resolved method is a copy: for super calls, the encoding
1020*795d594fSAndroid Build Coastguard Worker // of ArtMethod in BSS relies on not having copies there.
1021*795d594fSAndroid Build Coastguard Worker resolved_method = actual_method->GetCanonicalMethod(class_linker->GetImagePointerSize());
1022*795d594fSAndroid Build Coastguard Worker }
1023*795d594fSAndroid Build Coastguard Worker
1024*795d594fSAndroid Build Coastguard Worker if (*invoke_type == kInterface) {
1025*795d594fSAndroid Build Coastguard Worker if (resolved_method->GetDeclaringClass()->IsObjectClass()) {
1026*795d594fSAndroid Build Coastguard Worker // If the resolved method is from j.l.Object, emit a virtual call instead.
1027*795d594fSAndroid Build Coastguard Worker // The IMT conflict stub only handles interface methods.
1028*795d594fSAndroid Build Coastguard Worker *invoke_type = kVirtual;
1029*795d594fSAndroid Build Coastguard Worker } else {
1030*795d594fSAndroid Build Coastguard Worker DCHECK(resolved_method->GetDeclaringClass()->IsInterface());
1031*795d594fSAndroid Build Coastguard Worker }
1032*795d594fSAndroid Build Coastguard Worker }
1033*795d594fSAndroid Build Coastguard Worker
1034*795d594fSAndroid Build Coastguard Worker *resolved_method_info =
1035*795d594fSAndroid Build Coastguard Worker MethodReference(resolved_method->GetDexFile(), resolved_method->GetDexMethodIndex());
1036*795d594fSAndroid Build Coastguard Worker if (*invoke_type == kVirtual) {
1037*795d594fSAndroid Build Coastguard Worker // For HInvokeVirtual we need the vtable index.
1038*795d594fSAndroid Build Coastguard Worker *imt_or_vtable_index = resolved_method->GetVtableIndex();
1039*795d594fSAndroid Build Coastguard Worker } else if (*invoke_type == kInterface) {
1040*795d594fSAndroid Build Coastguard Worker // For HInvokeInterface we need the IMT index.
1041*795d594fSAndroid Build Coastguard Worker *imt_or_vtable_index = resolved_method->GetImtIndex();
1042*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(*imt_or_vtable_index, ImTable::GetImtIndex(resolved_method));
1043*795d594fSAndroid Build Coastguard Worker }
1044*795d594fSAndroid Build Coastguard Worker
1045*795d594fSAndroid Build Coastguard Worker *is_string_constructor = resolved_method->IsStringConstructor();
1046*795d594fSAndroid Build Coastguard Worker
1047*795d594fSAndroid Build Coastguard Worker return resolved_method;
1048*795d594fSAndroid Build Coastguard Worker }
1049*795d594fSAndroid Build Coastguard Worker
BuildInvoke(const Instruction & instruction,uint32_t dex_pc,uint32_t method_idx,const InstructionOperands & operands)1050*795d594fSAndroid Build Coastguard Worker bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
1051*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc,
1052*795d594fSAndroid Build Coastguard Worker uint32_t method_idx,
1053*795d594fSAndroid Build Coastguard Worker const InstructionOperands& operands) {
1054*795d594fSAndroid Build Coastguard Worker InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
1055*795d594fSAndroid Build Coastguard Worker const char* shorty = dex_file_->GetMethodShorty(method_idx);
1056*795d594fSAndroid Build Coastguard Worker DataType::Type return_type = DataType::FromShorty(shorty[0]);
1057*795d594fSAndroid Build Coastguard Worker
1058*795d594fSAndroid Build Coastguard Worker // Remove the return type from the 'proto'.
1059*795d594fSAndroid Build Coastguard Worker size_t number_of_arguments = strlen(shorty) - 1;
1060*795d594fSAndroid Build Coastguard Worker if (invoke_type != kStatic) { // instance call
1061*795d594fSAndroid Build Coastguard Worker // One extra argument for 'this'.
1062*795d594fSAndroid Build Coastguard Worker number_of_arguments++;
1063*795d594fSAndroid Build Coastguard Worker }
1064*795d594fSAndroid Build Coastguard Worker
1065*795d594fSAndroid Build Coastguard Worker MethodReference resolved_method_reference(nullptr, 0u);
1066*795d594fSAndroid Build Coastguard Worker bool is_string_constructor = false;
1067*795d594fSAndroid Build Coastguard Worker uint16_t imt_or_vtable_index = DexFile::kDexNoIndex16;
1068*795d594fSAndroid Build Coastguard Worker ArtMethod* resolved_method = ResolveMethod(method_idx,
1069*795d594fSAndroid Build Coastguard Worker graph_->GetArtMethod(),
1070*795d594fSAndroid Build Coastguard Worker *dex_compilation_unit_,
1071*795d594fSAndroid Build Coastguard Worker &invoke_type,
1072*795d594fSAndroid Build Coastguard Worker &resolved_method_reference,
1073*795d594fSAndroid Build Coastguard Worker &imt_or_vtable_index,
1074*795d594fSAndroid Build Coastguard Worker &is_string_constructor);
1075*795d594fSAndroid Build Coastguard Worker
1076*795d594fSAndroid Build Coastguard Worker MethodReference method_reference(&graph_->GetDexFile(), method_idx);
1077*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(resolved_method == nullptr)) {
1078*795d594fSAndroid Build Coastguard Worker DCHECK(!Thread::Current()->IsExceptionPending());
1079*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(compilation_stats_,
1080*795d594fSAndroid Build Coastguard Worker MethodCompilationStat::kUnresolvedMethod);
1081*795d594fSAndroid Build Coastguard Worker HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
1082*795d594fSAndroid Build Coastguard Worker number_of_arguments,
1083*795d594fSAndroid Build Coastguard Worker operands.GetNumberOfOperands(),
1084*795d594fSAndroid Build Coastguard Worker return_type,
1085*795d594fSAndroid Build Coastguard Worker dex_pc,
1086*795d594fSAndroid Build Coastguard Worker method_reference,
1087*795d594fSAndroid Build Coastguard Worker invoke_type);
1088*795d594fSAndroid Build Coastguard Worker return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ true);
1089*795d594fSAndroid Build Coastguard Worker }
1090*795d594fSAndroid Build Coastguard Worker
1091*795d594fSAndroid Build Coastguard Worker // Replace calls to String.<init> with StringFactory.
1092*795d594fSAndroid Build Coastguard Worker if (is_string_constructor) {
1093*795d594fSAndroid Build Coastguard Worker uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
1094*795d594fSAndroid Build Coastguard Worker HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
1095*795d594fSAndroid Build Coastguard Worker MethodLoadKind::kStringInit,
1096*795d594fSAndroid Build Coastguard Worker CodePtrLocation::kCallArtMethod,
1097*795d594fSAndroid Build Coastguard Worker dchecked_integral_cast<uint64_t>(string_init_entry_point)
1098*795d594fSAndroid Build Coastguard Worker };
1099*795d594fSAndroid Build Coastguard Worker // We pass null for the resolved_method to ensure optimizations
1100*795d594fSAndroid Build Coastguard Worker // don't rely on it.
1101*795d594fSAndroid Build Coastguard Worker HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
1102*795d594fSAndroid Build Coastguard Worker allocator_,
1103*795d594fSAndroid Build Coastguard Worker number_of_arguments - 1,
1104*795d594fSAndroid Build Coastguard Worker operands.GetNumberOfOperands() - 1,
1105*795d594fSAndroid Build Coastguard Worker /* return_type= */ DataType::Type::kReference,
1106*795d594fSAndroid Build Coastguard Worker dex_pc,
1107*795d594fSAndroid Build Coastguard Worker method_reference,
1108*795d594fSAndroid Build Coastguard Worker /* resolved_method= */ nullptr,
1109*795d594fSAndroid Build Coastguard Worker dispatch_info,
1110*795d594fSAndroid Build Coastguard Worker invoke_type,
1111*795d594fSAndroid Build Coastguard Worker resolved_method_reference,
1112*795d594fSAndroid Build Coastguard Worker HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit,
1113*795d594fSAndroid Build Coastguard Worker !graph_->IsDebuggable());
1114*795d594fSAndroid Build Coastguard Worker return HandleStringInit(invoke, operands, shorty);
1115*795d594fSAndroid Build Coastguard Worker }
1116*795d594fSAndroid Build Coastguard Worker
1117*795d594fSAndroid Build Coastguard Worker // Potential class initialization check, in the case of a static method call.
1118*795d594fSAndroid Build Coastguard Worker HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement =
1119*795d594fSAndroid Build Coastguard Worker HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
1120*795d594fSAndroid Build Coastguard Worker HClinitCheck* clinit_check = nullptr;
1121*795d594fSAndroid Build Coastguard Worker if (invoke_type == kStatic) {
1122*795d594fSAndroid Build Coastguard Worker clinit_check = ProcessClinitCheckForInvoke(dex_pc, resolved_method, &clinit_check_requirement);
1123*795d594fSAndroid Build Coastguard Worker }
1124*795d594fSAndroid Build Coastguard Worker
1125*795d594fSAndroid Build Coastguard Worker // Try to build an HIR replacement for the intrinsic.
1126*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(resolved_method->IsIntrinsic()) && !graph_->IsDebuggable()) {
1127*795d594fSAndroid Build Coastguard Worker // All intrinsics are in the primary boot image, so their class can always be referenced
1128*795d594fSAndroid Build Coastguard Worker // and we do not need to rely on the implicit class initialization check. The class should
1129*795d594fSAndroid Build Coastguard Worker // be initialized but we do not require that here.
1130*795d594fSAndroid Build Coastguard Worker DCHECK_NE(clinit_check_requirement, HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
1131*795d594fSAndroid Build Coastguard Worker if (BuildSimpleIntrinsic(resolved_method, dex_pc, operands, shorty)) {
1132*795d594fSAndroid Build Coastguard Worker return true;
1133*795d594fSAndroid Build Coastguard Worker }
1134*795d594fSAndroid Build Coastguard Worker }
1135*795d594fSAndroid Build Coastguard Worker
1136*795d594fSAndroid Build Coastguard Worker HInvoke* invoke = nullptr;
1137*795d594fSAndroid Build Coastguard Worker if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
1138*795d594fSAndroid Build Coastguard Worker // For sharpening, we create another MethodReference, to account for the
1139*795d594fSAndroid Build Coastguard Worker // kSuper case below where we cannot find a dex method index.
1140*795d594fSAndroid Build Coastguard Worker bool has_method_id = true;
1141*795d594fSAndroid Build Coastguard Worker if (invoke_type == kSuper) {
1142*795d594fSAndroid Build Coastguard Worker uint32_t dex_method_index = method_reference.index;
1143*795d594fSAndroid Build Coastguard Worker if (IsSameDexFile(*resolved_method_reference.dex_file,
1144*795d594fSAndroid Build Coastguard Worker *dex_compilation_unit_->GetDexFile())) {
1145*795d594fSAndroid Build Coastguard Worker // Update the method index to the one resolved. Note that this may be a no-op if
1146*795d594fSAndroid Build Coastguard Worker // we resolved to the method referenced by the instruction.
1147*795d594fSAndroid Build Coastguard Worker dex_method_index = resolved_method_reference.index;
1148*795d594fSAndroid Build Coastguard Worker } else {
1149*795d594fSAndroid Build Coastguard Worker // Try to find a dex method index in this caller's dex file.
1150*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
1151*795d594fSAndroid Build Coastguard Worker dex_method_index = resolved_method->FindDexMethodIndexInOtherDexFile(
1152*795d594fSAndroid Build Coastguard Worker *dex_compilation_unit_->GetDexFile(), method_idx);
1153*795d594fSAndroid Build Coastguard Worker }
1154*795d594fSAndroid Build Coastguard Worker if (dex_method_index == dex::kDexNoIndex) {
1155*795d594fSAndroid Build Coastguard Worker has_method_id = false;
1156*795d594fSAndroid Build Coastguard Worker } else {
1157*795d594fSAndroid Build Coastguard Worker method_reference.index = dex_method_index;
1158*795d594fSAndroid Build Coastguard Worker }
1159*795d594fSAndroid Build Coastguard Worker }
1160*795d594fSAndroid Build Coastguard Worker HInvokeStaticOrDirect::DispatchInfo dispatch_info =
1161*795d594fSAndroid Build Coastguard Worker HSharpening::SharpenLoadMethod(resolved_method,
1162*795d594fSAndroid Build Coastguard Worker has_method_id,
1163*795d594fSAndroid Build Coastguard Worker /* for_interface_call= */ false,
1164*795d594fSAndroid Build Coastguard Worker code_generator_);
1165*795d594fSAndroid Build Coastguard Worker if (dispatch_info.code_ptr_location == CodePtrLocation::kCallCriticalNative) {
1166*795d594fSAndroid Build Coastguard Worker graph_->SetHasDirectCriticalNativeCall(true);
1167*795d594fSAndroid Build Coastguard Worker }
1168*795d594fSAndroid Build Coastguard Worker invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
1169*795d594fSAndroid Build Coastguard Worker number_of_arguments,
1170*795d594fSAndroid Build Coastguard Worker operands.GetNumberOfOperands(),
1171*795d594fSAndroid Build Coastguard Worker return_type,
1172*795d594fSAndroid Build Coastguard Worker dex_pc,
1173*795d594fSAndroid Build Coastguard Worker method_reference,
1174*795d594fSAndroid Build Coastguard Worker resolved_method,
1175*795d594fSAndroid Build Coastguard Worker dispatch_info,
1176*795d594fSAndroid Build Coastguard Worker invoke_type,
1177*795d594fSAndroid Build Coastguard Worker resolved_method_reference,
1178*795d594fSAndroid Build Coastguard Worker clinit_check_requirement,
1179*795d594fSAndroid Build Coastguard Worker !graph_->IsDebuggable());
1180*795d594fSAndroid Build Coastguard Worker if (clinit_check != nullptr) {
1181*795d594fSAndroid Build Coastguard Worker // Add the class initialization check as last input of `invoke`.
1182*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(clinit_check_requirement, HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1183*795d594fSAndroid Build Coastguard Worker size_t clinit_check_index = invoke->InputCount() - 1u;
1184*795d594fSAndroid Build Coastguard Worker DCHECK(invoke->InputAt(clinit_check_index) == nullptr);
1185*795d594fSAndroid Build Coastguard Worker invoke->SetArgumentAt(clinit_check_index, clinit_check);
1186*795d594fSAndroid Build Coastguard Worker }
1187*795d594fSAndroid Build Coastguard Worker } else if (invoke_type == kVirtual) {
1188*795d594fSAndroid Build Coastguard Worker invoke = new (allocator_) HInvokeVirtual(allocator_,
1189*795d594fSAndroid Build Coastguard Worker number_of_arguments,
1190*795d594fSAndroid Build Coastguard Worker operands.GetNumberOfOperands(),
1191*795d594fSAndroid Build Coastguard Worker return_type,
1192*795d594fSAndroid Build Coastguard Worker dex_pc,
1193*795d594fSAndroid Build Coastguard Worker method_reference,
1194*795d594fSAndroid Build Coastguard Worker resolved_method,
1195*795d594fSAndroid Build Coastguard Worker resolved_method_reference,
1196*795d594fSAndroid Build Coastguard Worker /*vtable_index=*/ imt_or_vtable_index,
1197*795d594fSAndroid Build Coastguard Worker !graph_->IsDebuggable());
1198*795d594fSAndroid Build Coastguard Worker } else {
1199*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(invoke_type, kInterface);
1200*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
1201*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
1202*795d594fSAndroid Build Coastguard Worker DCHECK(resolved_method->GetDeclaringClass()->IsInterface());
1203*795d594fSAndroid Build Coastguard Worker }
1204*795d594fSAndroid Build Coastguard Worker MethodLoadKind load_kind = HSharpening::SharpenLoadMethod(
1205*795d594fSAndroid Build Coastguard Worker resolved_method,
1206*795d594fSAndroid Build Coastguard Worker /* has_method_id= */ true,
1207*795d594fSAndroid Build Coastguard Worker /* for_interface_call= */ true,
1208*795d594fSAndroid Build Coastguard Worker code_generator_)
1209*795d594fSAndroid Build Coastguard Worker .method_load_kind;
1210*795d594fSAndroid Build Coastguard Worker invoke = new (allocator_) HInvokeInterface(allocator_,
1211*795d594fSAndroid Build Coastguard Worker number_of_arguments,
1212*795d594fSAndroid Build Coastguard Worker operands.GetNumberOfOperands(),
1213*795d594fSAndroid Build Coastguard Worker return_type,
1214*795d594fSAndroid Build Coastguard Worker dex_pc,
1215*795d594fSAndroid Build Coastguard Worker method_reference,
1216*795d594fSAndroid Build Coastguard Worker resolved_method,
1217*795d594fSAndroid Build Coastguard Worker resolved_method_reference,
1218*795d594fSAndroid Build Coastguard Worker /*imt_index=*/ imt_or_vtable_index,
1219*795d594fSAndroid Build Coastguard Worker load_kind,
1220*795d594fSAndroid Build Coastguard Worker !graph_->IsDebuggable());
1221*795d594fSAndroid Build Coastguard Worker }
1222*795d594fSAndroid Build Coastguard Worker return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
1223*795d594fSAndroid Build Coastguard Worker }
1224*795d594fSAndroid Build Coastguard Worker
VarHandleAccessorNeedsReturnTypeCheck(HInvoke * invoke,DataType::Type return_type)1225*795d594fSAndroid Build Coastguard Worker static bool VarHandleAccessorNeedsReturnTypeCheck(HInvoke* invoke, DataType::Type return_type) {
1226*795d594fSAndroid Build Coastguard Worker mirror::VarHandle::AccessModeTemplate access_mode_template =
1227*795d594fSAndroid Build Coastguard Worker mirror::VarHandle::GetAccessModeTemplateByIntrinsic(invoke->GetIntrinsic());
1228*795d594fSAndroid Build Coastguard Worker
1229*795d594fSAndroid Build Coastguard Worker switch (access_mode_template) {
1230*795d594fSAndroid Build Coastguard Worker case mirror::VarHandle::AccessModeTemplate::kGet:
1231*795d594fSAndroid Build Coastguard Worker case mirror::VarHandle::AccessModeTemplate::kGetAndUpdate:
1232*795d594fSAndroid Build Coastguard Worker case mirror::VarHandle::AccessModeTemplate::kCompareAndExchange:
1233*795d594fSAndroid Build Coastguard Worker return return_type == DataType::Type::kReference;
1234*795d594fSAndroid Build Coastguard Worker case mirror::VarHandle::AccessModeTemplate::kSet:
1235*795d594fSAndroid Build Coastguard Worker case mirror::VarHandle::AccessModeTemplate::kCompareAndSet:
1236*795d594fSAndroid Build Coastguard Worker return false;
1237*795d594fSAndroid Build Coastguard Worker }
1238*795d594fSAndroid Build Coastguard Worker }
1239*795d594fSAndroid Build Coastguard Worker
1240*795d594fSAndroid Build Coastguard Worker // This function initializes `VarHandleOptimizations`, does a number of static checks and disables
1241*795d594fSAndroid Build Coastguard Worker // the intrinsic if some of the checks fail. This is necessary for the code generator to work (for
1242*795d594fSAndroid Build Coastguard Worker // both the baseline and the optimizing compiler).
DecideVarHandleIntrinsic(HInvoke * invoke)1243*795d594fSAndroid Build Coastguard Worker static void DecideVarHandleIntrinsic(HInvoke* invoke) {
1244*795d594fSAndroid Build Coastguard Worker switch (invoke->GetIntrinsic()) {
1245*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleCompareAndExchange:
1246*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleCompareAndExchangeAcquire:
1247*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleCompareAndExchangeRelease:
1248*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleCompareAndSet:
1249*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGet:
1250*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetAcquire:
1251*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetAndAdd:
1252*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetAndAddAcquire:
1253*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetAndAddRelease:
1254*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetAndBitwiseAnd:
1255*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetAndBitwiseAndAcquire:
1256*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetAndBitwiseAndRelease:
1257*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetAndBitwiseOr:
1258*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetAndBitwiseOrAcquire:
1259*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetAndBitwiseOrRelease:
1260*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetAndBitwiseXor:
1261*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetAndBitwiseXorAcquire:
1262*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetAndBitwiseXorRelease:
1263*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetAndSet:
1264*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetAndSetAcquire:
1265*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetAndSetRelease:
1266*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetOpaque:
1267*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleGetVolatile:
1268*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleSet:
1269*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleSetOpaque:
1270*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleSetRelease:
1271*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleSetVolatile:
1272*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleWeakCompareAndSet:
1273*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleWeakCompareAndSetAcquire:
1274*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleWeakCompareAndSetPlain:
1275*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleWeakCompareAndSetRelease:
1276*795d594fSAndroid Build Coastguard Worker break;
1277*795d594fSAndroid Build Coastguard Worker default:
1278*795d594fSAndroid Build Coastguard Worker return; // Not a VarHandle intrinsic, skip.
1279*795d594fSAndroid Build Coastguard Worker }
1280*795d594fSAndroid Build Coastguard Worker
1281*795d594fSAndroid Build Coastguard Worker DCHECK(invoke->IsInvokePolymorphic());
1282*795d594fSAndroid Build Coastguard Worker VarHandleOptimizations optimizations(invoke);
1283*795d594fSAndroid Build Coastguard Worker
1284*795d594fSAndroid Build Coastguard Worker // Do only simple static checks here (those for which we have enough information). More complex
1285*795d594fSAndroid Build Coastguard Worker // checks should be done in instruction simplifier, which runs after other optimization passes
1286*795d594fSAndroid Build Coastguard Worker // that may provide useful information.
1287*795d594fSAndroid Build Coastguard Worker
1288*795d594fSAndroid Build Coastguard Worker size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke);
1289*795d594fSAndroid Build Coastguard Worker if (expected_coordinates_count > 2u) {
1290*795d594fSAndroid Build Coastguard Worker optimizations.SetDoNotIntrinsify();
1291*795d594fSAndroid Build Coastguard Worker return;
1292*795d594fSAndroid Build Coastguard Worker }
1293*795d594fSAndroid Build Coastguard Worker if (expected_coordinates_count != 0u) {
1294*795d594fSAndroid Build Coastguard Worker // Except for static fields (no coordinates), the first coordinate must be a reference.
1295*795d594fSAndroid Build Coastguard Worker // Do not intrinsify if the reference is null as we would always go to slow path anyway.
1296*795d594fSAndroid Build Coastguard Worker HInstruction* object = invoke->InputAt(1);
1297*795d594fSAndroid Build Coastguard Worker if (object->GetType() != DataType::Type::kReference || object->IsNullConstant()) {
1298*795d594fSAndroid Build Coastguard Worker optimizations.SetDoNotIntrinsify();
1299*795d594fSAndroid Build Coastguard Worker return;
1300*795d594fSAndroid Build Coastguard Worker }
1301*795d594fSAndroid Build Coastguard Worker }
1302*795d594fSAndroid Build Coastguard Worker if (expected_coordinates_count == 2u) {
1303*795d594fSAndroid Build Coastguard Worker // For arrays and views, the second coordinate must be convertible to `int`.
1304*795d594fSAndroid Build Coastguard Worker // In this context, `boolean` is not convertible but we have to look at the shorty
1305*795d594fSAndroid Build Coastguard Worker // as compiler transformations can give the invoke a valid boolean input.
1306*795d594fSAndroid Build Coastguard Worker DataType::Type index_type = GetDataTypeFromShorty(invoke, 2);
1307*795d594fSAndroid Build Coastguard Worker if (index_type == DataType::Type::kBool ||
1308*795d594fSAndroid Build Coastguard Worker DataType::Kind(index_type) != DataType::Type::kInt32) {
1309*795d594fSAndroid Build Coastguard Worker optimizations.SetDoNotIntrinsify();
1310*795d594fSAndroid Build Coastguard Worker return;
1311*795d594fSAndroid Build Coastguard Worker }
1312*795d594fSAndroid Build Coastguard Worker }
1313*795d594fSAndroid Build Coastguard Worker
1314*795d594fSAndroid Build Coastguard Worker uint32_t number_of_arguments = invoke->GetNumberOfArguments();
1315*795d594fSAndroid Build Coastguard Worker DataType::Type return_type = invoke->GetType();
1316*795d594fSAndroid Build Coastguard Worker mirror::VarHandle::AccessModeTemplate access_mode_template =
1317*795d594fSAndroid Build Coastguard Worker mirror::VarHandle::GetAccessModeTemplateByIntrinsic(invoke->GetIntrinsic());
1318*795d594fSAndroid Build Coastguard Worker switch (access_mode_template) {
1319*795d594fSAndroid Build Coastguard Worker case mirror::VarHandle::AccessModeTemplate::kGet:
1320*795d594fSAndroid Build Coastguard Worker // The return type should be the same as varType, so it shouldn't be void.
1321*795d594fSAndroid Build Coastguard Worker if (return_type == DataType::Type::kVoid) {
1322*795d594fSAndroid Build Coastguard Worker optimizations.SetDoNotIntrinsify();
1323*795d594fSAndroid Build Coastguard Worker return;
1324*795d594fSAndroid Build Coastguard Worker }
1325*795d594fSAndroid Build Coastguard Worker break;
1326*795d594fSAndroid Build Coastguard Worker case mirror::VarHandle::AccessModeTemplate::kSet:
1327*795d594fSAndroid Build Coastguard Worker if (return_type != DataType::Type::kVoid) {
1328*795d594fSAndroid Build Coastguard Worker optimizations.SetDoNotIntrinsify();
1329*795d594fSAndroid Build Coastguard Worker return;
1330*795d594fSAndroid Build Coastguard Worker }
1331*795d594fSAndroid Build Coastguard Worker break;
1332*795d594fSAndroid Build Coastguard Worker case mirror::VarHandle::AccessModeTemplate::kCompareAndSet: {
1333*795d594fSAndroid Build Coastguard Worker if (return_type != DataType::Type::kBool) {
1334*795d594fSAndroid Build Coastguard Worker optimizations.SetDoNotIntrinsify();
1335*795d594fSAndroid Build Coastguard Worker return;
1336*795d594fSAndroid Build Coastguard Worker }
1337*795d594fSAndroid Build Coastguard Worker uint32_t expected_value_index = number_of_arguments - 2;
1338*795d594fSAndroid Build Coastguard Worker uint32_t new_value_index = number_of_arguments - 1;
1339*795d594fSAndroid Build Coastguard Worker DataType::Type expected_value_type = GetDataTypeFromShorty(invoke, expected_value_index);
1340*795d594fSAndroid Build Coastguard Worker DataType::Type new_value_type = GetDataTypeFromShorty(invoke, new_value_index);
1341*795d594fSAndroid Build Coastguard Worker if (expected_value_type != new_value_type) {
1342*795d594fSAndroid Build Coastguard Worker optimizations.SetDoNotIntrinsify();
1343*795d594fSAndroid Build Coastguard Worker return;
1344*795d594fSAndroid Build Coastguard Worker }
1345*795d594fSAndroid Build Coastguard Worker break;
1346*795d594fSAndroid Build Coastguard Worker }
1347*795d594fSAndroid Build Coastguard Worker case mirror::VarHandle::AccessModeTemplate::kCompareAndExchange: {
1348*795d594fSAndroid Build Coastguard Worker uint32_t expected_value_index = number_of_arguments - 2;
1349*795d594fSAndroid Build Coastguard Worker uint32_t new_value_index = number_of_arguments - 1;
1350*795d594fSAndroid Build Coastguard Worker DataType::Type expected_value_type = GetDataTypeFromShorty(invoke, expected_value_index);
1351*795d594fSAndroid Build Coastguard Worker DataType::Type new_value_type = GetDataTypeFromShorty(invoke, new_value_index);
1352*795d594fSAndroid Build Coastguard Worker if (expected_value_type != new_value_type || return_type != expected_value_type) {
1353*795d594fSAndroid Build Coastguard Worker optimizations.SetDoNotIntrinsify();
1354*795d594fSAndroid Build Coastguard Worker return;
1355*795d594fSAndroid Build Coastguard Worker }
1356*795d594fSAndroid Build Coastguard Worker break;
1357*795d594fSAndroid Build Coastguard Worker }
1358*795d594fSAndroid Build Coastguard Worker case mirror::VarHandle::AccessModeTemplate::kGetAndUpdate: {
1359*795d594fSAndroid Build Coastguard Worker DataType::Type value_type = GetDataTypeFromShorty(invoke, number_of_arguments - 1);
1360*795d594fSAndroid Build Coastguard Worker if (IsVarHandleGetAndAdd(invoke) &&
1361*795d594fSAndroid Build Coastguard Worker (value_type == DataType::Type::kReference || value_type == DataType::Type::kBool)) {
1362*795d594fSAndroid Build Coastguard Worker // We should only add numerical types.
1363*795d594fSAndroid Build Coastguard Worker //
1364*795d594fSAndroid Build Coastguard Worker // For byte array views floating-point types are not allowed, see javadoc comments for
1365*795d594fSAndroid Build Coastguard Worker // java.lang.invoke.MethodHandles.byteArrayViewVarHandle(). But ART treats them as numeric
1366*795d594fSAndroid Build Coastguard Worker // types in ByteArrayViewVarHandle::Access(). Consequently we do generate intrinsic code,
1367*795d594fSAndroid Build Coastguard Worker // but it always fails access mode check at runtime.
1368*795d594fSAndroid Build Coastguard Worker optimizations.SetDoNotIntrinsify();
1369*795d594fSAndroid Build Coastguard Worker return;
1370*795d594fSAndroid Build Coastguard Worker } else if (IsVarHandleGetAndBitwiseOp(invoke) && !DataType::IsIntegralType(value_type)) {
1371*795d594fSAndroid Build Coastguard Worker // We can only apply operators to bitwise integral types.
1372*795d594fSAndroid Build Coastguard Worker // Note that bitwise VarHandle operations accept a non-integral boolean type and
1373*795d594fSAndroid Build Coastguard Worker // perform the appropriate logical operation. However, the result is the same as
1374*795d594fSAndroid Build Coastguard Worker // using the bitwise operation on our boolean representation and this fits well
1375*795d594fSAndroid Build Coastguard Worker // with DataType::IsIntegralType() treating the compiler type kBool as integral.
1376*795d594fSAndroid Build Coastguard Worker optimizations.SetDoNotIntrinsify();
1377*795d594fSAndroid Build Coastguard Worker return;
1378*795d594fSAndroid Build Coastguard Worker }
1379*795d594fSAndroid Build Coastguard Worker if (value_type != return_type && return_type != DataType::Type::kVoid) {
1380*795d594fSAndroid Build Coastguard Worker optimizations.SetDoNotIntrinsify();
1381*795d594fSAndroid Build Coastguard Worker return;
1382*795d594fSAndroid Build Coastguard Worker }
1383*795d594fSAndroid Build Coastguard Worker break;
1384*795d594fSAndroid Build Coastguard Worker }
1385*795d594fSAndroid Build Coastguard Worker }
1386*795d594fSAndroid Build Coastguard Worker }
1387*795d594fSAndroid Build Coastguard Worker
BuildInvokePolymorphic(uint32_t dex_pc,uint32_t method_idx,dex::ProtoIndex proto_idx,const InstructionOperands & operands)1388*795d594fSAndroid Build Coastguard Worker bool HInstructionBuilder::BuildInvokePolymorphic(uint32_t dex_pc,
1389*795d594fSAndroid Build Coastguard Worker uint32_t method_idx,
1390*795d594fSAndroid Build Coastguard Worker dex::ProtoIndex proto_idx,
1391*795d594fSAndroid Build Coastguard Worker const InstructionOperands& operands) {
1392*795d594fSAndroid Build Coastguard Worker const char* shorty = dex_file_->GetShorty(proto_idx);
1393*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(1 + ArtMethod::NumArgRegisters(shorty), operands.GetNumberOfOperands());
1394*795d594fSAndroid Build Coastguard Worker DataType::Type return_type = DataType::FromShorty(shorty[0]);
1395*795d594fSAndroid Build Coastguard Worker size_t number_of_arguments = strlen(shorty);
1396*795d594fSAndroid Build Coastguard Worker // We use ResolveMethod which is also used in BuildInvoke in order to
1397*795d594fSAndroid Build Coastguard Worker // not duplicate code. As such, we need to provide is_string_constructor
1398*795d594fSAndroid Build Coastguard Worker // even if we don't need it afterwards.
1399*795d594fSAndroid Build Coastguard Worker InvokeType invoke_type = InvokeType::kPolymorphic;
1400*795d594fSAndroid Build Coastguard Worker bool is_string_constructor = false;
1401*795d594fSAndroid Build Coastguard Worker uint16_t imt_or_vtable_index = DexFile::kDexNoIndex16;
1402*795d594fSAndroid Build Coastguard Worker MethodReference resolved_method_reference(nullptr, 0u);
1403*795d594fSAndroid Build Coastguard Worker ArtMethod* resolved_method = ResolveMethod(method_idx,
1404*795d594fSAndroid Build Coastguard Worker graph_->GetArtMethod(),
1405*795d594fSAndroid Build Coastguard Worker *dex_compilation_unit_,
1406*795d594fSAndroid Build Coastguard Worker &invoke_type,
1407*795d594fSAndroid Build Coastguard Worker &resolved_method_reference,
1408*795d594fSAndroid Build Coastguard Worker &imt_or_vtable_index,
1409*795d594fSAndroid Build Coastguard Worker &is_string_constructor);
1410*795d594fSAndroid Build Coastguard Worker
1411*795d594fSAndroid Build Coastguard Worker MethodReference method_reference(&graph_->GetDexFile(), method_idx);
1412*795d594fSAndroid Build Coastguard Worker
1413*795d594fSAndroid Build Coastguard Worker // MethodHandle.invokeExact intrinsic needs to check whether call-site matches with MethodHandle's
1414*795d594fSAndroid Build Coastguard Worker // type. To do that, MethodType corresponding to the call-site is passed as an extra input.
1415*795d594fSAndroid Build Coastguard Worker // Other invoke-polymorphic calls do not need it.
1416*795d594fSAndroid Build Coastguard Worker bool can_be_intrinsified =
1417*795d594fSAndroid Build Coastguard Worker static_cast<Intrinsics>(resolved_method->GetIntrinsic()) ==
1418*795d594fSAndroid Build Coastguard Worker Intrinsics::kMethodHandleInvokeExact;
1419*795d594fSAndroid Build Coastguard Worker
1420*795d594fSAndroid Build Coastguard Worker uint32_t number_of_other_inputs = can_be_intrinsified ? 1u : 0u;
1421*795d594fSAndroid Build Coastguard Worker
1422*795d594fSAndroid Build Coastguard Worker HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
1423*795d594fSAndroid Build Coastguard Worker number_of_arguments,
1424*795d594fSAndroid Build Coastguard Worker operands.GetNumberOfOperands(),
1425*795d594fSAndroid Build Coastguard Worker number_of_other_inputs,
1426*795d594fSAndroid Build Coastguard Worker return_type,
1427*795d594fSAndroid Build Coastguard Worker dex_pc,
1428*795d594fSAndroid Build Coastguard Worker method_reference,
1429*795d594fSAndroid Build Coastguard Worker resolved_method,
1430*795d594fSAndroid Build Coastguard Worker resolved_method_reference,
1431*795d594fSAndroid Build Coastguard Worker proto_idx);
1432*795d594fSAndroid Build Coastguard Worker if (!HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false)) {
1433*795d594fSAndroid Build Coastguard Worker return false;
1434*795d594fSAndroid Build Coastguard Worker }
1435*795d594fSAndroid Build Coastguard Worker
1436*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(invoke->AsInvokePolymorphic()->IsMethodHandleInvokeExact(), can_be_intrinsified);
1437*795d594fSAndroid Build Coastguard Worker
1438*795d594fSAndroid Build Coastguard Worker if (invoke->GetIntrinsic() != Intrinsics::kNone &&
1439*795d594fSAndroid Build Coastguard Worker invoke->GetIntrinsic() != Intrinsics::kMethodHandleInvoke &&
1440*795d594fSAndroid Build Coastguard Worker invoke->GetIntrinsic() != Intrinsics::kMethodHandleInvokeExact &&
1441*795d594fSAndroid Build Coastguard Worker VarHandleAccessorNeedsReturnTypeCheck(invoke, return_type)) {
1442*795d594fSAndroid Build Coastguard Worker // Type check is needed because VarHandle intrinsics do not type check the retrieved reference.
1443*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
1444*795d594fSAndroid Build Coastguard Worker ArtMethod* referrer = graph_->GetArtMethod();
1445*795d594fSAndroid Build Coastguard Worker dex::TypeIndex return_type_index =
1446*795d594fSAndroid Build Coastguard Worker referrer->GetDexFile()->GetProtoId(proto_idx).return_type_idx_;
1447*795d594fSAndroid Build Coastguard Worker
1448*795d594fSAndroid Build Coastguard Worker BuildTypeCheck(/* is_instance_of= */ false, invoke, return_type_index, dex_pc);
1449*795d594fSAndroid Build Coastguard Worker latest_result_ = current_block_->GetLastInstruction();
1450*795d594fSAndroid Build Coastguard Worker }
1451*795d594fSAndroid Build Coastguard Worker
1452*795d594fSAndroid Build Coastguard Worker DecideVarHandleIntrinsic(invoke);
1453*795d594fSAndroid Build Coastguard Worker
1454*795d594fSAndroid Build Coastguard Worker return true;
1455*795d594fSAndroid Build Coastguard Worker }
1456*795d594fSAndroid Build Coastguard Worker
1457*795d594fSAndroid Build Coastguard Worker
BuildInvokeCustom(uint32_t dex_pc,uint32_t call_site_idx,const InstructionOperands & operands)1458*795d594fSAndroid Build Coastguard Worker bool HInstructionBuilder::BuildInvokeCustom(uint32_t dex_pc,
1459*795d594fSAndroid Build Coastguard Worker uint32_t call_site_idx,
1460*795d594fSAndroid Build Coastguard Worker const InstructionOperands& operands) {
1461*795d594fSAndroid Build Coastguard Worker dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
1462*795d594fSAndroid Build Coastguard Worker const char* shorty = dex_file_->GetShorty(proto_idx);
1463*795d594fSAndroid Build Coastguard Worker DataType::Type return_type = DataType::FromShorty(shorty[0]);
1464*795d594fSAndroid Build Coastguard Worker size_t number_of_arguments = strlen(shorty) - 1;
1465*795d594fSAndroid Build Coastguard Worker // HInvokeCustom takes a DexNoNoIndex method reference.
1466*795d594fSAndroid Build Coastguard Worker MethodReference method_reference(&graph_->GetDexFile(), dex::kDexNoIndex);
1467*795d594fSAndroid Build Coastguard Worker HInvoke* invoke = new (allocator_) HInvokeCustom(allocator_,
1468*795d594fSAndroid Build Coastguard Worker number_of_arguments,
1469*795d594fSAndroid Build Coastguard Worker operands.GetNumberOfOperands(),
1470*795d594fSAndroid Build Coastguard Worker call_site_idx,
1471*795d594fSAndroid Build Coastguard Worker return_type,
1472*795d594fSAndroid Build Coastguard Worker dex_pc,
1473*795d594fSAndroid Build Coastguard Worker method_reference,
1474*795d594fSAndroid Build Coastguard Worker !graph_->IsDebuggable());
1475*795d594fSAndroid Build Coastguard Worker return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
1476*795d594fSAndroid Build Coastguard Worker }
1477*795d594fSAndroid Build Coastguard Worker
BuildNewInstance(dex::TypeIndex type_index,uint32_t dex_pc)1478*795d594fSAndroid Build Coastguard Worker HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
1479*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
1480*795d594fSAndroid Build Coastguard Worker
1481*795d594fSAndroid Build Coastguard Worker HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
1482*795d594fSAndroid Build Coastguard Worker
1483*795d594fSAndroid Build Coastguard Worker HInstruction* cls = load_class;
1484*795d594fSAndroid Build Coastguard Worker Handle<mirror::Class> klass = load_class->GetClass();
1485*795d594fSAndroid Build Coastguard Worker
1486*795d594fSAndroid Build Coastguard Worker if (!IsInitialized(klass.Get())) {
1487*795d594fSAndroid Build Coastguard Worker cls = new (allocator_) HClinitCheck(load_class, dex_pc);
1488*795d594fSAndroid Build Coastguard Worker AppendInstruction(cls);
1489*795d594fSAndroid Build Coastguard Worker }
1490*795d594fSAndroid Build Coastguard Worker
1491*795d594fSAndroid Build Coastguard Worker // Only the access check entrypoint handles the finalizable class case. If we
1492*795d594fSAndroid Build Coastguard Worker // need access checks, then we haven't resolved the method and the class may
1493*795d594fSAndroid Build Coastguard Worker // again be finalizable.
1494*795d594fSAndroid Build Coastguard Worker QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
1495*795d594fSAndroid Build Coastguard Worker if (load_class->NeedsAccessCheck() ||
1496*795d594fSAndroid Build Coastguard Worker klass == nullptr || // Finalizable/instantiable is unknown.
1497*795d594fSAndroid Build Coastguard Worker klass->IsFinalizable() ||
1498*795d594fSAndroid Build Coastguard Worker klass.Get() == klass->GetClass() || // Classes cannot be allocated in code
1499*795d594fSAndroid Build Coastguard Worker !klass->IsInstantiable()) {
1500*795d594fSAndroid Build Coastguard Worker entrypoint = kQuickAllocObjectWithChecks;
1501*795d594fSAndroid Build Coastguard Worker }
1502*795d594fSAndroid Build Coastguard Worker // We will always be able to resolve the string class since it is in the BCP.
1503*795d594fSAndroid Build Coastguard Worker if (!klass.IsNull() && klass->IsStringClass()) {
1504*795d594fSAndroid Build Coastguard Worker entrypoint = kQuickAllocStringObject;
1505*795d594fSAndroid Build Coastguard Worker }
1506*795d594fSAndroid Build Coastguard Worker
1507*795d594fSAndroid Build Coastguard Worker // Consider classes we haven't resolved as potentially finalizable.
1508*795d594fSAndroid Build Coastguard Worker bool finalizable = (klass == nullptr) || klass->IsFinalizable();
1509*795d594fSAndroid Build Coastguard Worker
1510*795d594fSAndroid Build Coastguard Worker HNewInstance* new_instance = new (allocator_) HNewInstance(
1511*795d594fSAndroid Build Coastguard Worker cls,
1512*795d594fSAndroid Build Coastguard Worker dex_pc,
1513*795d594fSAndroid Build Coastguard Worker type_index,
1514*795d594fSAndroid Build Coastguard Worker *dex_compilation_unit_->GetDexFile(),
1515*795d594fSAndroid Build Coastguard Worker finalizable,
1516*795d594fSAndroid Build Coastguard Worker entrypoint);
1517*795d594fSAndroid Build Coastguard Worker AppendInstruction(new_instance);
1518*795d594fSAndroid Build Coastguard Worker
1519*795d594fSAndroid Build Coastguard Worker return new_instance;
1520*795d594fSAndroid Build Coastguard Worker }
1521*795d594fSAndroid Build Coastguard Worker
BuildConstructorFenceForAllocation(HInstruction * allocation)1522*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1523*795d594fSAndroid Build Coastguard Worker DCHECK(allocation != nullptr &&
1524*795d594fSAndroid Build Coastguard Worker (allocation->IsNewInstance() ||
1525*795d594fSAndroid Build Coastguard Worker allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
1526*795d594fSAndroid Build Coastguard Worker
1527*795d594fSAndroid Build Coastguard Worker if (allocation->IsNewInstance()) {
1528*795d594fSAndroid Build Coastguard Worker // STRING SPECIAL HANDLING:
1529*795d594fSAndroid Build Coastguard Worker // -------------------------------
1530*795d594fSAndroid Build Coastguard Worker // Strings have a real HNewInstance node but they end up always having 0 uses.
1531*795d594fSAndroid Build Coastguard Worker // All uses of a String HNewInstance are always transformed to replace their input
1532*795d594fSAndroid Build Coastguard Worker // of the HNewInstance with an input of the invoke to StringFactory.
1533*795d594fSAndroid Build Coastguard Worker //
1534*795d594fSAndroid Build Coastguard Worker // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1535*795d594fSAndroid Build Coastguard Worker // optimizations (to pass checker tests that rely on those optimizations).
1536*795d594fSAndroid Build Coastguard Worker HNewInstance* new_inst = allocation->AsNewInstance();
1537*795d594fSAndroid Build Coastguard Worker HLoadClass* load_class = new_inst->GetLoadClass();
1538*795d594fSAndroid Build Coastguard Worker
1539*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
1540*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(self);
1541*795d594fSAndroid Build Coastguard Worker StackHandleScope<1> hs(self);
1542*795d594fSAndroid Build Coastguard Worker Handle<mirror::Class> klass = load_class->GetClass();
1543*795d594fSAndroid Build Coastguard Worker if (klass != nullptr && klass->IsStringClass()) {
1544*795d594fSAndroid Build Coastguard Worker return;
1545*795d594fSAndroid Build Coastguard Worker // Note: Do not use allocation->IsStringAlloc which requires
1546*795d594fSAndroid Build Coastguard Worker // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1547*795d594fSAndroid Build Coastguard Worker // propagation (and instruction builder is too early).
1548*795d594fSAndroid Build Coastguard Worker }
1549*795d594fSAndroid Build Coastguard Worker // (In terms of correctness, the StringFactory needs to provide its own
1550*795d594fSAndroid Build Coastguard Worker // default initialization barrier, see below.)
1551*795d594fSAndroid Build Coastguard Worker }
1552*795d594fSAndroid Build Coastguard Worker
1553*795d594fSAndroid Build Coastguard Worker // JLS 17.4.5 "Happens-before Order" describes:
1554*795d594fSAndroid Build Coastguard Worker //
1555*795d594fSAndroid Build Coastguard Worker // The default initialization of any object happens-before any other actions (other than
1556*795d594fSAndroid Build Coastguard Worker // default-writes) of a program.
1557*795d594fSAndroid Build Coastguard Worker //
1558*795d594fSAndroid Build Coastguard Worker // In our implementation the default initialization of an object to type T means
1559*795d594fSAndroid Build Coastguard Worker // setting all of its initial data (object[0..size)) to 0, and setting the
1560*795d594fSAndroid Build Coastguard Worker // object's class header (i.e. object.getClass() == T.class).
1561*795d594fSAndroid Build Coastguard Worker //
1562*795d594fSAndroid Build Coastguard Worker // In practice this fence ensures that the writes to the object header
1563*795d594fSAndroid Build Coastguard Worker // are visible to other threads if this object escapes the current thread.
1564*795d594fSAndroid Build Coastguard Worker // (and in theory the 0-initializing, but that happens automatically
1565*795d594fSAndroid Build Coastguard Worker // when new memory pages are mapped in by the OS).
1566*795d594fSAndroid Build Coastguard Worker HConstructorFence* ctor_fence =
1567*795d594fSAndroid Build Coastguard Worker new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
1568*795d594fSAndroid Build Coastguard Worker AppendInstruction(ctor_fence);
1569*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(
1570*795d594fSAndroid Build Coastguard Worker compilation_stats_,
1571*795d594fSAndroid Build Coastguard Worker MethodCompilationStat::kConstructorFenceGeneratedNew);
1572*795d594fSAndroid Build Coastguard Worker }
1573*795d594fSAndroid Build Coastguard Worker
IsInImage(ObjPtr<mirror::Class> cls,const CompilerOptions & compiler_options)1574*795d594fSAndroid Build Coastguard Worker static bool IsInImage(ObjPtr<mirror::Class> cls, const CompilerOptions& compiler_options)
1575*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1576*795d594fSAndroid Build Coastguard Worker if (Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(cls)) {
1577*795d594fSAndroid Build Coastguard Worker return true;
1578*795d594fSAndroid Build Coastguard Worker }
1579*795d594fSAndroid Build Coastguard Worker if (compiler_options.IsGeneratingImage()) {
1580*795d594fSAndroid Build Coastguard Worker std::string temp;
1581*795d594fSAndroid Build Coastguard Worker const char* descriptor = cls->GetDescriptor(&temp);
1582*795d594fSAndroid Build Coastguard Worker return compiler_options.IsImageClass(descriptor);
1583*795d594fSAndroid Build Coastguard Worker } else {
1584*795d594fSAndroid Build Coastguard Worker return false;
1585*795d594fSAndroid Build Coastguard Worker }
1586*795d594fSAndroid Build Coastguard Worker }
1587*795d594fSAndroid Build Coastguard Worker
IsSubClass(ObjPtr<mirror::Class> to_test,ObjPtr<mirror::Class> super_class)1588*795d594fSAndroid Build Coastguard Worker static bool IsSubClass(ObjPtr<mirror::Class> to_test, ObjPtr<mirror::Class> super_class)
1589*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1590*795d594fSAndroid Build Coastguard Worker return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1591*795d594fSAndroid Build Coastguard Worker }
1592*795d594fSAndroid Build Coastguard Worker
HasTrivialClinit(ObjPtr<mirror::Class> klass,PointerSize pointer_size)1593*795d594fSAndroid Build Coastguard Worker static bool HasTrivialClinit(ObjPtr<mirror::Class> klass, PointerSize pointer_size)
1594*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1595*795d594fSAndroid Build Coastguard Worker // Check if the class has encoded fields that trigger bytecode execution.
1596*795d594fSAndroid Build Coastguard Worker // (Encoded fields are just a different representation of <clinit>.)
1597*795d594fSAndroid Build Coastguard Worker if (klass->NumStaticFields() != 0u) {
1598*795d594fSAndroid Build Coastguard Worker DCHECK(klass->GetClassDef() != nullptr);
1599*795d594fSAndroid Build Coastguard Worker EncodedStaticFieldValueIterator it(klass->GetDexFile(), *klass->GetClassDef());
1600*795d594fSAndroid Build Coastguard Worker for (; it.HasNext(); it.Next()) {
1601*795d594fSAndroid Build Coastguard Worker switch (it.GetValueType()) {
1602*795d594fSAndroid Build Coastguard Worker case EncodedArrayValueIterator::ValueType::kBoolean:
1603*795d594fSAndroid Build Coastguard Worker case EncodedArrayValueIterator::ValueType::kByte:
1604*795d594fSAndroid Build Coastguard Worker case EncodedArrayValueIterator::ValueType::kShort:
1605*795d594fSAndroid Build Coastguard Worker case EncodedArrayValueIterator::ValueType::kChar:
1606*795d594fSAndroid Build Coastguard Worker case EncodedArrayValueIterator::ValueType::kInt:
1607*795d594fSAndroid Build Coastguard Worker case EncodedArrayValueIterator::ValueType::kLong:
1608*795d594fSAndroid Build Coastguard Worker case EncodedArrayValueIterator::ValueType::kFloat:
1609*795d594fSAndroid Build Coastguard Worker case EncodedArrayValueIterator::ValueType::kDouble:
1610*795d594fSAndroid Build Coastguard Worker case EncodedArrayValueIterator::ValueType::kNull:
1611*795d594fSAndroid Build Coastguard Worker case EncodedArrayValueIterator::ValueType::kString:
1612*795d594fSAndroid Build Coastguard Worker // Primitive, null or j.l.String initialization is permitted.
1613*795d594fSAndroid Build Coastguard Worker break;
1614*795d594fSAndroid Build Coastguard Worker case EncodedArrayValueIterator::ValueType::kType:
1615*795d594fSAndroid Build Coastguard Worker // Type initialization can load classes and execute bytecode through a class loader
1616*795d594fSAndroid Build Coastguard Worker // which can execute arbitrary bytecode. We do not optimize for known class loaders;
1617*795d594fSAndroid Build Coastguard Worker // kType is rarely used (if ever).
1618*795d594fSAndroid Build Coastguard Worker return false;
1619*795d594fSAndroid Build Coastguard Worker default:
1620*795d594fSAndroid Build Coastguard Worker // Other types in the encoded static field list are rejected by the DexFileVerifier.
1621*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Unexpected type " << it.GetValueType();
1622*795d594fSAndroid Build Coastguard Worker UNREACHABLE();
1623*795d594fSAndroid Build Coastguard Worker }
1624*795d594fSAndroid Build Coastguard Worker }
1625*795d594fSAndroid Build Coastguard Worker }
1626*795d594fSAndroid Build Coastguard Worker // Check if the class has <clinit> that executes arbitrary code.
1627*795d594fSAndroid Build Coastguard Worker // Initialization of static fields of the class itself with constants is allowed.
1628*795d594fSAndroid Build Coastguard Worker ArtMethod* clinit = klass->FindClassInitializer(pointer_size);
1629*795d594fSAndroid Build Coastguard Worker if (clinit != nullptr) {
1630*795d594fSAndroid Build Coastguard Worker const DexFile& dex_file = *clinit->GetDexFile();
1631*795d594fSAndroid Build Coastguard Worker CodeItemInstructionAccessor accessor(dex_file, clinit->GetCodeItem());
1632*795d594fSAndroid Build Coastguard Worker for (DexInstructionPcPair it : accessor) {
1633*795d594fSAndroid Build Coastguard Worker switch (it->Opcode()) {
1634*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_4:
1635*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_16:
1636*795d594fSAndroid Build Coastguard Worker case Instruction::CONST:
1637*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_HIGH16:
1638*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_WIDE_16:
1639*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_WIDE_32:
1640*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_WIDE:
1641*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_WIDE_HIGH16:
1642*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_STRING:
1643*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_STRING_JUMBO:
1644*795d594fSAndroid Build Coastguard Worker // Primitive, null or j.l.String initialization is permitted.
1645*795d594fSAndroid Build Coastguard Worker break;
1646*795d594fSAndroid Build Coastguard Worker case Instruction::RETURN_VOID:
1647*795d594fSAndroid Build Coastguard Worker break;
1648*795d594fSAndroid Build Coastguard Worker case Instruction::SPUT:
1649*795d594fSAndroid Build Coastguard Worker case Instruction::SPUT_WIDE:
1650*795d594fSAndroid Build Coastguard Worker case Instruction::SPUT_OBJECT:
1651*795d594fSAndroid Build Coastguard Worker case Instruction::SPUT_BOOLEAN:
1652*795d594fSAndroid Build Coastguard Worker case Instruction::SPUT_BYTE:
1653*795d594fSAndroid Build Coastguard Worker case Instruction::SPUT_CHAR:
1654*795d594fSAndroid Build Coastguard Worker case Instruction::SPUT_SHORT:
1655*795d594fSAndroid Build Coastguard Worker // Only initialization of a static field of the same class is permitted.
1656*795d594fSAndroid Build Coastguard Worker if (dex_file.GetFieldId(it->VRegB_21c()).class_idx_ != klass->GetDexTypeIndex()) {
1657*795d594fSAndroid Build Coastguard Worker return false;
1658*795d594fSAndroid Build Coastguard Worker }
1659*795d594fSAndroid Build Coastguard Worker break;
1660*795d594fSAndroid Build Coastguard Worker case Instruction::NEW_ARRAY:
1661*795d594fSAndroid Build Coastguard Worker // Only primitive arrays are permitted.
1662*795d594fSAndroid Build Coastguard Worker if (Primitive::GetType(dex_file.GetTypeDescriptor(dex_file.GetTypeId(
1663*795d594fSAndroid Build Coastguard Worker dex::TypeIndex(it->VRegC_22c())))[1]) == Primitive::kPrimNot) {
1664*795d594fSAndroid Build Coastguard Worker return false;
1665*795d594fSAndroid Build Coastguard Worker }
1666*795d594fSAndroid Build Coastguard Worker break;
1667*795d594fSAndroid Build Coastguard Worker case Instruction::APUT:
1668*795d594fSAndroid Build Coastguard Worker case Instruction::APUT_WIDE:
1669*795d594fSAndroid Build Coastguard Worker case Instruction::APUT_BOOLEAN:
1670*795d594fSAndroid Build Coastguard Worker case Instruction::APUT_BYTE:
1671*795d594fSAndroid Build Coastguard Worker case Instruction::APUT_CHAR:
1672*795d594fSAndroid Build Coastguard Worker case Instruction::APUT_SHORT:
1673*795d594fSAndroid Build Coastguard Worker case Instruction::FILL_ARRAY_DATA:
1674*795d594fSAndroid Build Coastguard Worker case Instruction::NOP:
1675*795d594fSAndroid Build Coastguard Worker // Allow initialization of primitive arrays (only constants can be stored).
1676*795d594fSAndroid Build Coastguard Worker // Note: We expect NOPs used for fill-array-data-payload but accept all NOPs
1677*795d594fSAndroid Build Coastguard Worker // (even unreferenced switch payloads if they make it through the verifier).
1678*795d594fSAndroid Build Coastguard Worker break;
1679*795d594fSAndroid Build Coastguard Worker default:
1680*795d594fSAndroid Build Coastguard Worker return false;
1681*795d594fSAndroid Build Coastguard Worker }
1682*795d594fSAndroid Build Coastguard Worker }
1683*795d594fSAndroid Build Coastguard Worker }
1684*795d594fSAndroid Build Coastguard Worker return true;
1685*795d594fSAndroid Build Coastguard Worker }
1686*795d594fSAndroid Build Coastguard Worker
HasTrivialInitialization(ObjPtr<mirror::Class> cls,const CompilerOptions & compiler_options)1687*795d594fSAndroid Build Coastguard Worker static bool HasTrivialInitialization(ObjPtr<mirror::Class> cls,
1688*795d594fSAndroid Build Coastguard Worker const CompilerOptions& compiler_options)
1689*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1690*795d594fSAndroid Build Coastguard Worker Runtime* runtime = Runtime::Current();
1691*795d594fSAndroid Build Coastguard Worker PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
1692*795d594fSAndroid Build Coastguard Worker
1693*795d594fSAndroid Build Coastguard Worker // Check the superclass chain.
1694*795d594fSAndroid Build Coastguard Worker for (ObjPtr<mirror::Class> klass = cls; klass != nullptr; klass = klass->GetSuperClass()) {
1695*795d594fSAndroid Build Coastguard Worker if (klass->IsInitialized() && IsInImage(klass, compiler_options)) {
1696*795d594fSAndroid Build Coastguard Worker break; // `klass` and its superclasses are already initialized in the boot or app image.
1697*795d594fSAndroid Build Coastguard Worker }
1698*795d594fSAndroid Build Coastguard Worker if (!HasTrivialClinit(klass, pointer_size)) {
1699*795d594fSAndroid Build Coastguard Worker return false;
1700*795d594fSAndroid Build Coastguard Worker }
1701*795d594fSAndroid Build Coastguard Worker }
1702*795d594fSAndroid Build Coastguard Worker
1703*795d594fSAndroid Build Coastguard Worker // Also check interfaces with default methods as they need to be initialized as well.
1704*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::IfTable> iftable = cls->GetIfTable();
1705*795d594fSAndroid Build Coastguard Worker DCHECK(iftable != nullptr);
1706*795d594fSAndroid Build Coastguard Worker for (int32_t i = 0, count = iftable->Count(); i != count; ++i) {
1707*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> iface = iftable->GetInterface(i);
1708*795d594fSAndroid Build Coastguard Worker if (!iface->HasDefaultMethods()) {
1709*795d594fSAndroid Build Coastguard Worker continue; // Initializing `cls` does not initialize this interface.
1710*795d594fSAndroid Build Coastguard Worker }
1711*795d594fSAndroid Build Coastguard Worker if (iface->IsInitialized() && IsInImage(iface, compiler_options)) {
1712*795d594fSAndroid Build Coastguard Worker continue; // This interface is already initialized in the boot or app image.
1713*795d594fSAndroid Build Coastguard Worker }
1714*795d594fSAndroid Build Coastguard Worker if (!HasTrivialClinit(iface, pointer_size)) {
1715*795d594fSAndroid Build Coastguard Worker return false;
1716*795d594fSAndroid Build Coastguard Worker }
1717*795d594fSAndroid Build Coastguard Worker }
1718*795d594fSAndroid Build Coastguard Worker return true;
1719*795d594fSAndroid Build Coastguard Worker }
1720*795d594fSAndroid Build Coastguard Worker
IsInitialized(ObjPtr<mirror::Class> cls) const1721*795d594fSAndroid Build Coastguard Worker bool HInstructionBuilder::IsInitialized(ObjPtr<mirror::Class> cls) const {
1722*795d594fSAndroid Build Coastguard Worker if (cls == nullptr) {
1723*795d594fSAndroid Build Coastguard Worker return false;
1724*795d594fSAndroid Build Coastguard Worker }
1725*795d594fSAndroid Build Coastguard Worker
1726*795d594fSAndroid Build Coastguard Worker // Check if the class will be initialized at runtime.
1727*795d594fSAndroid Build Coastguard Worker if (cls->IsInitialized()) {
1728*795d594fSAndroid Build Coastguard Worker const CompilerOptions& compiler_options = code_generator_->GetCompilerOptions();
1729*795d594fSAndroid Build Coastguard Worker if (compiler_options.IsAotCompiler()) {
1730*795d594fSAndroid Build Coastguard Worker // Assume loaded only if klass is in the boot or app image.
1731*795d594fSAndroid Build Coastguard Worker if (IsInImage(cls, compiler_options)) {
1732*795d594fSAndroid Build Coastguard Worker return true;
1733*795d594fSAndroid Build Coastguard Worker }
1734*795d594fSAndroid Build Coastguard Worker } else {
1735*795d594fSAndroid Build Coastguard Worker DCHECK(compiler_options.IsJitCompiler());
1736*795d594fSAndroid Build Coastguard Worker if (Runtime::Current()->GetJit()->CanAssumeInitialized(
1737*795d594fSAndroid Build Coastguard Worker cls,
1738*795d594fSAndroid Build Coastguard Worker compiler_options.IsJitCompilerForSharedCode())) {
1739*795d594fSAndroid Build Coastguard Worker // For JIT, the class cannot revert to an uninitialized state.
1740*795d594fSAndroid Build Coastguard Worker return true;
1741*795d594fSAndroid Build Coastguard Worker }
1742*795d594fSAndroid Build Coastguard Worker }
1743*795d594fSAndroid Build Coastguard Worker }
1744*795d594fSAndroid Build Coastguard Worker
1745*795d594fSAndroid Build Coastguard Worker // We can avoid the class initialization check for `cls` in static methods and constructors
1746*795d594fSAndroid Build Coastguard Worker // in the very same class; invoking a static method involves a class initialization check
1747*795d594fSAndroid Build Coastguard Worker // and so does the instance allocation that must be executed before invoking a constructor.
1748*795d594fSAndroid Build Coastguard Worker // Other instance methods of the same class can run on an escaped instance
1749*795d594fSAndroid Build Coastguard Worker // of an erroneous class. Even a superclass may need to be checked as the subclass
1750*795d594fSAndroid Build Coastguard Worker // can be completely initialized while the superclass is initializing and the subclass
1751*795d594fSAndroid Build Coastguard Worker // remains initialized when the superclass initializer throws afterwards. b/62478025
1752*795d594fSAndroid Build Coastguard Worker // Note: The HClinitCheck+HInvokeStaticOrDirect merging can still apply.
1753*795d594fSAndroid Build Coastguard Worker auto is_static_method_or_constructor_of_cls = [cls](const DexCompilationUnit& compilation_unit)
1754*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1755*795d594fSAndroid Build Coastguard Worker return (compilation_unit.GetAccessFlags() & (kAccStatic | kAccConstructor)) != 0u &&
1756*795d594fSAndroid Build Coastguard Worker compilation_unit.GetCompilingClass().Get() == cls;
1757*795d594fSAndroid Build Coastguard Worker };
1758*795d594fSAndroid Build Coastguard Worker if (is_static_method_or_constructor_of_cls(*outer_compilation_unit_) ||
1759*795d594fSAndroid Build Coastguard Worker // Check also the innermost method. Though excessive copies of ClinitCheck can be
1760*795d594fSAndroid Build Coastguard Worker // eliminated by GVN, that happens only after the decision whether to inline the
1761*795d594fSAndroid Build Coastguard Worker // graph or not and that may depend on the presence of the ClinitCheck.
1762*795d594fSAndroid Build Coastguard Worker // TODO: We should walk over the entire inlined method chain, but we don't pass that
1763*795d594fSAndroid Build Coastguard Worker // information to the builder.
1764*795d594fSAndroid Build Coastguard Worker is_static_method_or_constructor_of_cls(*dex_compilation_unit_)) {
1765*795d594fSAndroid Build Coastguard Worker return true;
1766*795d594fSAndroid Build Coastguard Worker }
1767*795d594fSAndroid Build Coastguard Worker
1768*795d594fSAndroid Build Coastguard Worker // Otherwise, we may be able to avoid the check if `cls` is a superclass of a method being
1769*795d594fSAndroid Build Coastguard Worker // compiled here (anywhere in the inlining chain) as the `cls` must have started initializing
1770*795d594fSAndroid Build Coastguard Worker // before calling any `cls` or subclass methods. Static methods require a clinit check and
1771*795d594fSAndroid Build Coastguard Worker // instance methods require an instance which cannot be created before doing a clinit check.
1772*795d594fSAndroid Build Coastguard Worker // When a subclass of `cls` starts initializing, it starts initializing its superclass
1773*795d594fSAndroid Build Coastguard Worker // chain up to `cls` without running any bytecode, i.e. without any opportunity for circular
1774*795d594fSAndroid Build Coastguard Worker // initialization weirdness.
1775*795d594fSAndroid Build Coastguard Worker //
1776*795d594fSAndroid Build Coastguard Worker // If the initialization of `cls` is trivial (`cls` and its superclasses and superinterfaces
1777*795d594fSAndroid Build Coastguard Worker // with default methods initialize only their own static fields using constant values), it must
1778*795d594fSAndroid Build Coastguard Worker // complete, either successfully or by throwing and marking `cls` erroneous, without allocating
1779*795d594fSAndroid Build Coastguard Worker // any instances of `cls` or subclasses (or any other class) and without calling any methods.
1780*795d594fSAndroid Build Coastguard Worker // If it completes by throwing, no instances of `cls` shall be created and no subclass method
1781*795d594fSAndroid Build Coastguard Worker // bytecode shall execute (see above), therefore the instruction we're building shall be
1782*795d594fSAndroid Build Coastguard Worker // unreachable. By reaching the instruction, we know that `cls` was initialized successfully.
1783*795d594fSAndroid Build Coastguard Worker //
1784*795d594fSAndroid Build Coastguard Worker // TODO: We should walk over the entire inlined methods chain, but we don't pass that
1785*795d594fSAndroid Build Coastguard Worker // information to the builder. (We could also check if we're guaranteed a non-null instance
1786*795d594fSAndroid Build Coastguard Worker // of `cls` at this location but that's outside the scope of the instruction builder.)
1787*795d594fSAndroid Build Coastguard Worker bool is_subclass = IsSubClass(outer_compilation_unit_->GetCompilingClass().Get(), cls);
1788*795d594fSAndroid Build Coastguard Worker if (dex_compilation_unit_ != outer_compilation_unit_) {
1789*795d594fSAndroid Build Coastguard Worker is_subclass = is_subclass ||
1790*795d594fSAndroid Build Coastguard Worker IsSubClass(dex_compilation_unit_->GetCompilingClass().Get(), cls);
1791*795d594fSAndroid Build Coastguard Worker }
1792*795d594fSAndroid Build Coastguard Worker if (is_subclass && HasTrivialInitialization(cls, code_generator_->GetCompilerOptions())) {
1793*795d594fSAndroid Build Coastguard Worker return true;
1794*795d594fSAndroid Build Coastguard Worker }
1795*795d594fSAndroid Build Coastguard Worker
1796*795d594fSAndroid Build Coastguard Worker return false;
1797*795d594fSAndroid Build Coastguard Worker }
1798*795d594fSAndroid Build Coastguard Worker
ProcessClinitCheckForInvoke(uint32_t dex_pc,ArtMethod * resolved_method,HInvokeStaticOrDirect::ClinitCheckRequirement * clinit_check_requirement)1799*795d594fSAndroid Build Coastguard Worker HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1800*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc,
1801*795d594fSAndroid Build Coastguard Worker ArtMethod* resolved_method,
1802*795d594fSAndroid Build Coastguard Worker HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
1803*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
1804*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> klass = resolved_method->GetDeclaringClass();
1805*795d594fSAndroid Build Coastguard Worker
1806*795d594fSAndroid Build Coastguard Worker HClinitCheck* clinit_check = nullptr;
1807*795d594fSAndroid Build Coastguard Worker if (IsInitialized(klass)) {
1808*795d594fSAndroid Build Coastguard Worker *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
1809*795d594fSAndroid Build Coastguard Worker } else {
1810*795d594fSAndroid Build Coastguard Worker Handle<mirror::Class> h_klass = graph_->GetHandleCache()->NewHandle(klass);
1811*795d594fSAndroid Build Coastguard Worker HLoadClass* cls = BuildLoadClass(h_klass->GetDexTypeIndex(),
1812*795d594fSAndroid Build Coastguard Worker h_klass->GetDexFile(),
1813*795d594fSAndroid Build Coastguard Worker h_klass,
1814*795d594fSAndroid Build Coastguard Worker dex_pc,
1815*795d594fSAndroid Build Coastguard Worker /* needs_access_check= */ false);
1816*795d594fSAndroid Build Coastguard Worker if (cls != nullptr) {
1817*795d594fSAndroid Build Coastguard Worker *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
1818*795d594fSAndroid Build Coastguard Worker clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
1819*795d594fSAndroid Build Coastguard Worker AppendInstruction(clinit_check);
1820*795d594fSAndroid Build Coastguard Worker } else {
1821*795d594fSAndroid Build Coastguard Worker // Let the invoke handle this with an implicit class initialization check.
1822*795d594fSAndroid Build Coastguard Worker *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
1823*795d594fSAndroid Build Coastguard Worker }
1824*795d594fSAndroid Build Coastguard Worker }
1825*795d594fSAndroid Build Coastguard Worker return clinit_check;
1826*795d594fSAndroid Build Coastguard Worker }
1827*795d594fSAndroid Build Coastguard Worker
SetupInvokeArguments(HInstruction * invoke,const InstructionOperands & operands,const char * shorty,ReceiverArg receiver_arg)1828*795d594fSAndroid Build Coastguard Worker bool HInstructionBuilder::SetupInvokeArguments(HInstruction* invoke,
1829*795d594fSAndroid Build Coastguard Worker const InstructionOperands& operands,
1830*795d594fSAndroid Build Coastguard Worker const char* shorty,
1831*795d594fSAndroid Build Coastguard Worker ReceiverArg receiver_arg) {
1832*795d594fSAndroid Build Coastguard Worker // Note: The `invoke` can be an intrinsic replacement, so not necessaritly HInvoke.
1833*795d594fSAndroid Build Coastguard Worker // In that case, do not log errors, they shall be reported when we try to build the HInvoke.
1834*795d594fSAndroid Build Coastguard Worker uint32_t shorty_index = 1; // Skip the return type.
1835*795d594fSAndroid Build Coastguard Worker const size_t number_of_operands = operands.GetNumberOfOperands();
1836*795d594fSAndroid Build Coastguard Worker bool argument_length_error = false;
1837*795d594fSAndroid Build Coastguard Worker
1838*795d594fSAndroid Build Coastguard Worker size_t start_index = 0u;
1839*795d594fSAndroid Build Coastguard Worker size_t argument_index = 0u;
1840*795d594fSAndroid Build Coastguard Worker if (receiver_arg != ReceiverArg::kNone) {
1841*795d594fSAndroid Build Coastguard Worker if (number_of_operands == 0u) {
1842*795d594fSAndroid Build Coastguard Worker argument_length_error = true;
1843*795d594fSAndroid Build Coastguard Worker } else {
1844*795d594fSAndroid Build Coastguard Worker start_index = 1u;
1845*795d594fSAndroid Build Coastguard Worker if (receiver_arg != ReceiverArg::kIgnored) {
1846*795d594fSAndroid Build Coastguard Worker uint32_t obj_reg = operands.GetOperand(0u);
1847*795d594fSAndroid Build Coastguard Worker HInstruction* arg = (receiver_arg == ReceiverArg::kPlainArg)
1848*795d594fSAndroid Build Coastguard Worker ? LoadLocal(obj_reg, DataType::Type::kReference)
1849*795d594fSAndroid Build Coastguard Worker : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
1850*795d594fSAndroid Build Coastguard Worker if (receiver_arg != ReceiverArg::kNullCheckedOnly) {
1851*795d594fSAndroid Build Coastguard Worker invoke->SetRawInputAt(0u, arg);
1852*795d594fSAndroid Build Coastguard Worker argument_index = 1u;
1853*795d594fSAndroid Build Coastguard Worker }
1854*795d594fSAndroid Build Coastguard Worker }
1855*795d594fSAndroid Build Coastguard Worker }
1856*795d594fSAndroid Build Coastguard Worker }
1857*795d594fSAndroid Build Coastguard Worker
1858*795d594fSAndroid Build Coastguard Worker for (size_t i = start_index; i < number_of_operands; ++i, ++argument_index) {
1859*795d594fSAndroid Build Coastguard Worker // Make sure we don't go over the expected arguments or over the number of
1860*795d594fSAndroid Build Coastguard Worker // dex registers given. If the instruction was seen as dead by the verifier,
1861*795d594fSAndroid Build Coastguard Worker // it hasn't been properly checked.
1862*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(shorty[shorty_index] == 0)) {
1863*795d594fSAndroid Build Coastguard Worker argument_length_error = true;
1864*795d594fSAndroid Build Coastguard Worker break;
1865*795d594fSAndroid Build Coastguard Worker }
1866*795d594fSAndroid Build Coastguard Worker DataType::Type type = DataType::FromShorty(shorty[shorty_index++]);
1867*795d594fSAndroid Build Coastguard Worker bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
1868*795d594fSAndroid Build Coastguard Worker if (is_wide && ((i + 1 == number_of_operands) ||
1869*795d594fSAndroid Build Coastguard Worker (operands.GetOperand(i) + 1 != operands.GetOperand(i + 1)))) {
1870*795d594fSAndroid Build Coastguard Worker if (invoke->IsInvoke()) {
1871*795d594fSAndroid Build Coastguard Worker // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1872*795d594fSAndroid Build Coastguard Worker // reject any class where this is violated. However, the verifier only does these checks
1873*795d594fSAndroid Build Coastguard Worker // on non trivially dead instructions, so we just bailout the compilation.
1874*795d594fSAndroid Build Coastguard Worker VLOG(compiler) << "Did not compile "
1875*795d594fSAndroid Build Coastguard Worker << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
1876*795d594fSAndroid Build Coastguard Worker << " because of non-sequential dex register pair in wide argument";
1877*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(compilation_stats_,
1878*795d594fSAndroid Build Coastguard Worker MethodCompilationStat::kNotCompiledMalformedOpcode);
1879*795d594fSAndroid Build Coastguard Worker }
1880*795d594fSAndroid Build Coastguard Worker return false;
1881*795d594fSAndroid Build Coastguard Worker }
1882*795d594fSAndroid Build Coastguard Worker HInstruction* arg = LoadLocal(operands.GetOperand(i), type);
1883*795d594fSAndroid Build Coastguard Worker DCHECK(invoke->InputAt(argument_index) == nullptr);
1884*795d594fSAndroid Build Coastguard Worker invoke->SetRawInputAt(argument_index, arg);
1885*795d594fSAndroid Build Coastguard Worker if (is_wide) {
1886*795d594fSAndroid Build Coastguard Worker ++i;
1887*795d594fSAndroid Build Coastguard Worker }
1888*795d594fSAndroid Build Coastguard Worker }
1889*795d594fSAndroid Build Coastguard Worker
1890*795d594fSAndroid Build Coastguard Worker argument_length_error = argument_length_error || shorty[shorty_index] != 0;
1891*795d594fSAndroid Build Coastguard Worker if (argument_length_error) {
1892*795d594fSAndroid Build Coastguard Worker if (invoke->IsInvoke()) {
1893*795d594fSAndroid Build Coastguard Worker VLOG(compiler) << "Did not compile "
1894*795d594fSAndroid Build Coastguard Worker << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
1895*795d594fSAndroid Build Coastguard Worker << " because of wrong number of arguments in invoke instruction";
1896*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(compilation_stats_,
1897*795d594fSAndroid Build Coastguard Worker MethodCompilationStat::kNotCompiledMalformedOpcode);
1898*795d594fSAndroid Build Coastguard Worker }
1899*795d594fSAndroid Build Coastguard Worker return false;
1900*795d594fSAndroid Build Coastguard Worker }
1901*795d594fSAndroid Build Coastguard Worker
1902*795d594fSAndroid Build Coastguard Worker if (invoke->IsInvokeStaticOrDirect() &&
1903*795d594fSAndroid Build Coastguard Worker HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1904*795d594fSAndroid Build Coastguard Worker invoke->AsInvokeStaticOrDirect()->GetDispatchInfo())) {
1905*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(argument_index, invoke->AsInvokeStaticOrDirect()->GetCurrentMethodIndex());
1906*795d594fSAndroid Build Coastguard Worker DCHECK(invoke->InputAt(argument_index) == nullptr);
1907*795d594fSAndroid Build Coastguard Worker invoke->SetRawInputAt(argument_index, graph_->GetCurrentMethod());
1908*795d594fSAndroid Build Coastguard Worker }
1909*795d594fSAndroid Build Coastguard Worker
1910*795d594fSAndroid Build Coastguard Worker if (invoke->IsInvokeInterface() &&
1911*795d594fSAndroid Build Coastguard Worker (invoke->AsInvokeInterface()->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive)) {
1912*795d594fSAndroid Build Coastguard Worker invoke->SetRawInputAt(invoke->AsInvokeInterface()->GetNumberOfArguments() - 1,
1913*795d594fSAndroid Build Coastguard Worker graph_->GetCurrentMethod());
1914*795d594fSAndroid Build Coastguard Worker }
1915*795d594fSAndroid Build Coastguard Worker
1916*795d594fSAndroid Build Coastguard Worker if (invoke->IsInvokePolymorphic()) {
1917*795d594fSAndroid Build Coastguard Worker HInvokePolymorphic* invoke_polymorphic = invoke->AsInvokePolymorphic();
1918*795d594fSAndroid Build Coastguard Worker
1919*795d594fSAndroid Build Coastguard Worker // MethodHandle.invokeExact intrinsic expects MethodType corresponding to the call-site as an
1920*795d594fSAndroid Build Coastguard Worker // extra input to determine whether to throw WrongMethodTypeException or execute target method.
1921*795d594fSAndroid Build Coastguard Worker if (invoke_polymorphic->IsMethodHandleInvokeExact()) {
1922*795d594fSAndroid Build Coastguard Worker HLoadMethodType* load_method_type =
1923*795d594fSAndroid Build Coastguard Worker new (allocator_) HLoadMethodType(graph_->GetCurrentMethod(),
1924*795d594fSAndroid Build Coastguard Worker invoke_polymorphic->GetProtoIndex(),
1925*795d594fSAndroid Build Coastguard Worker graph_->GetDexFile(),
1926*795d594fSAndroid Build Coastguard Worker invoke_polymorphic->GetDexPc());
1927*795d594fSAndroid Build Coastguard Worker HSharpening::ProcessLoadMethodType(load_method_type,
1928*795d594fSAndroid Build Coastguard Worker code_generator_,
1929*795d594fSAndroid Build Coastguard Worker *dex_compilation_unit_,
1930*795d594fSAndroid Build Coastguard Worker graph_->GetHandleCache()->GetHandles());
1931*795d594fSAndroid Build Coastguard Worker invoke->SetRawInputAt(invoke_polymorphic->GetNumberOfArguments(), load_method_type);
1932*795d594fSAndroid Build Coastguard Worker AppendInstruction(load_method_type);
1933*795d594fSAndroid Build Coastguard Worker }
1934*795d594fSAndroid Build Coastguard Worker }
1935*795d594fSAndroid Build Coastguard Worker
1936*795d594fSAndroid Build Coastguard Worker return true;
1937*795d594fSAndroid Build Coastguard Worker }
1938*795d594fSAndroid Build Coastguard Worker
HandleInvoke(HInvoke * invoke,const InstructionOperands & operands,const char * shorty,bool is_unresolved)1939*795d594fSAndroid Build Coastguard Worker bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1940*795d594fSAndroid Build Coastguard Worker const InstructionOperands& operands,
1941*795d594fSAndroid Build Coastguard Worker const char* shorty,
1942*795d594fSAndroid Build Coastguard Worker bool is_unresolved) {
1943*795d594fSAndroid Build Coastguard Worker DCHECK_IMPLIES(invoke->IsInvokeStaticOrDirect(),
1944*795d594fSAndroid Build Coastguard Worker !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1945*795d594fSAndroid Build Coastguard Worker
1946*795d594fSAndroid Build Coastguard Worker ReceiverArg receiver_arg = (invoke->GetInvokeType() == InvokeType::kStatic)
1947*795d594fSAndroid Build Coastguard Worker ? ReceiverArg::kNone
1948*795d594fSAndroid Build Coastguard Worker : (is_unresolved ? ReceiverArg::kPlainArg : ReceiverArg::kNullCheckedArg);
1949*795d594fSAndroid Build Coastguard Worker if (!SetupInvokeArguments(invoke, operands, shorty, receiver_arg)) {
1950*795d594fSAndroid Build Coastguard Worker return false;
1951*795d594fSAndroid Build Coastguard Worker }
1952*795d594fSAndroid Build Coastguard Worker
1953*795d594fSAndroid Build Coastguard Worker AppendInstruction(invoke);
1954*795d594fSAndroid Build Coastguard Worker latest_result_ = invoke;
1955*795d594fSAndroid Build Coastguard Worker
1956*795d594fSAndroid Build Coastguard Worker return true;
1957*795d594fSAndroid Build Coastguard Worker }
1958*795d594fSAndroid Build Coastguard Worker
BuildSimpleIntrinsic(ArtMethod * method,uint32_t dex_pc,const InstructionOperands & operands,const char * shorty)1959*795d594fSAndroid Build Coastguard Worker bool HInstructionBuilder::BuildSimpleIntrinsic(ArtMethod* method,
1960*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc,
1961*795d594fSAndroid Build Coastguard Worker const InstructionOperands& operands,
1962*795d594fSAndroid Build Coastguard Worker const char* shorty) {
1963*795d594fSAndroid Build Coastguard Worker Intrinsics intrinsic = method->GetIntrinsic();
1964*795d594fSAndroid Build Coastguard Worker DCHECK_NE(intrinsic, Intrinsics::kNone);
1965*795d594fSAndroid Build Coastguard Worker constexpr DataType::Type kInt32 = DataType::Type::kInt32;
1966*795d594fSAndroid Build Coastguard Worker constexpr DataType::Type kInt64 = DataType::Type::kInt64;
1967*795d594fSAndroid Build Coastguard Worker constexpr DataType::Type kFloat32 = DataType::Type::kFloat32;
1968*795d594fSAndroid Build Coastguard Worker constexpr DataType::Type kFloat64 = DataType::Type::kFloat64;
1969*795d594fSAndroid Build Coastguard Worker ReceiverArg receiver_arg = method->IsStatic() ? ReceiverArg::kNone : ReceiverArg::kNullCheckedArg;
1970*795d594fSAndroid Build Coastguard Worker HInstruction* instruction = nullptr;
1971*795d594fSAndroid Build Coastguard Worker switch (intrinsic) {
1972*795d594fSAndroid Build Coastguard Worker case Intrinsics::kIntegerRotateLeft:
1973*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HRol(kInt32, /*value=*/ nullptr, /*distance=*/ nullptr);
1974*795d594fSAndroid Build Coastguard Worker break;
1975*795d594fSAndroid Build Coastguard Worker case Intrinsics::kIntegerRotateRight:
1976*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HRor(kInt32, /*value=*/ nullptr, /*distance=*/ nullptr);
1977*795d594fSAndroid Build Coastguard Worker break;
1978*795d594fSAndroid Build Coastguard Worker case Intrinsics::kLongRotateLeft:
1979*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HRol(kInt64, /*value=*/ nullptr, /*distance=*/ nullptr);
1980*795d594fSAndroid Build Coastguard Worker break;
1981*795d594fSAndroid Build Coastguard Worker case Intrinsics::kLongRotateRight:
1982*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HRor(kInt64, /*value=*/ nullptr, /*distance=*/ nullptr);
1983*795d594fSAndroid Build Coastguard Worker break;
1984*795d594fSAndroid Build Coastguard Worker case Intrinsics::kIntegerCompare:
1985*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HCompare(
1986*795d594fSAndroid Build Coastguard Worker kInt32, /*first=*/ nullptr, /*second=*/ nullptr, ComparisonBias::kNoBias, dex_pc);
1987*795d594fSAndroid Build Coastguard Worker break;
1988*795d594fSAndroid Build Coastguard Worker case Intrinsics::kLongCompare:
1989*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HCompare(
1990*795d594fSAndroid Build Coastguard Worker kInt64, /*first=*/ nullptr, /*second=*/ nullptr, ComparisonBias::kNoBias, dex_pc);
1991*795d594fSAndroid Build Coastguard Worker break;
1992*795d594fSAndroid Build Coastguard Worker case Intrinsics::kIntegerSignum:
1993*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HCompare(
1994*795d594fSAndroid Build Coastguard Worker kInt32, /*first=*/ nullptr, graph_->GetIntConstant(0), ComparisonBias::kNoBias, dex_pc);
1995*795d594fSAndroid Build Coastguard Worker break;
1996*795d594fSAndroid Build Coastguard Worker case Intrinsics::kLongSignum:
1997*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HCompare(
1998*795d594fSAndroid Build Coastguard Worker kInt64, /*first=*/ nullptr, graph_->GetLongConstant(0), ComparisonBias::kNoBias, dex_pc);
1999*795d594fSAndroid Build Coastguard Worker break;
2000*795d594fSAndroid Build Coastguard Worker case Intrinsics::kFloatIsNaN:
2001*795d594fSAndroid Build Coastguard Worker case Intrinsics::kDoubleIsNaN: {
2002*795d594fSAndroid Build Coastguard Worker // IsNaN(x) is the same as x != x.
2003*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HNotEqual(/*first=*/ nullptr, /*second=*/ nullptr, dex_pc);
2004*795d594fSAndroid Build Coastguard Worker instruction->AsCondition()->SetBias(ComparisonBias::kLtBias);
2005*795d594fSAndroid Build Coastguard Worker break;
2006*795d594fSAndroid Build Coastguard Worker }
2007*795d594fSAndroid Build Coastguard Worker case Intrinsics::kStringCharAt:
2008*795d594fSAndroid Build Coastguard Worker // We treat String as an array to allow DCE and BCE to seamlessly work on strings.
2009*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HArrayGet(/*array=*/ nullptr,
2010*795d594fSAndroid Build Coastguard Worker /*index=*/ nullptr,
2011*795d594fSAndroid Build Coastguard Worker DataType::Type::kUint16,
2012*795d594fSAndroid Build Coastguard Worker SideEffects::None(), // Strings are immutable.
2013*795d594fSAndroid Build Coastguard Worker dex_pc,
2014*795d594fSAndroid Build Coastguard Worker /*is_string_char_at=*/ true);
2015*795d594fSAndroid Build Coastguard Worker break;
2016*795d594fSAndroid Build Coastguard Worker case Intrinsics::kStringIsEmpty:
2017*795d594fSAndroid Build Coastguard Worker case Intrinsics::kStringLength:
2018*795d594fSAndroid Build Coastguard Worker // We treat String as an array to allow DCE and BCE to seamlessly work on strings.
2019*795d594fSAndroid Build Coastguard Worker // For String.isEmpty(), we add a comparison with 0 below.
2020*795d594fSAndroid Build Coastguard Worker instruction =
2021*795d594fSAndroid Build Coastguard Worker new (allocator_) HArrayLength(/*array=*/ nullptr, dex_pc, /* is_string_length= */ true);
2022*795d594fSAndroid Build Coastguard Worker break;
2023*795d594fSAndroid Build Coastguard Worker case Intrinsics::kUnsafeLoadFence:
2024*795d594fSAndroid Build Coastguard Worker case Intrinsics::kJdkUnsafeLoadFence:
2025*795d594fSAndroid Build Coastguard Worker receiver_arg = ReceiverArg::kNullCheckedOnly;
2026*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kLoadAny, dex_pc);
2027*795d594fSAndroid Build Coastguard Worker break;
2028*795d594fSAndroid Build Coastguard Worker case Intrinsics::kUnsafeStoreFence:
2029*795d594fSAndroid Build Coastguard Worker case Intrinsics::kJdkUnsafeStoreFence:
2030*795d594fSAndroid Build Coastguard Worker receiver_arg = ReceiverArg::kNullCheckedOnly;
2031*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyStore, dex_pc);
2032*795d594fSAndroid Build Coastguard Worker break;
2033*795d594fSAndroid Build Coastguard Worker case Intrinsics::kUnsafeFullFence:
2034*795d594fSAndroid Build Coastguard Worker case Intrinsics::kJdkUnsafeFullFence:
2035*795d594fSAndroid Build Coastguard Worker receiver_arg = ReceiverArg::kNullCheckedOnly;
2036*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyAny, dex_pc);
2037*795d594fSAndroid Build Coastguard Worker break;
2038*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleFullFence:
2039*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyAny, dex_pc);
2040*795d594fSAndroid Build Coastguard Worker break;
2041*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleAcquireFence:
2042*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kLoadAny, dex_pc);
2043*795d594fSAndroid Build Coastguard Worker break;
2044*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleReleaseFence:
2045*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyStore, dex_pc);
2046*795d594fSAndroid Build Coastguard Worker break;
2047*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleLoadLoadFence:
2048*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kLoadAny, dex_pc);
2049*795d594fSAndroid Build Coastguard Worker break;
2050*795d594fSAndroid Build Coastguard Worker case Intrinsics::kVarHandleStoreStoreFence:
2051*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kStoreStore, dex_pc);
2052*795d594fSAndroid Build Coastguard Worker break;
2053*795d594fSAndroid Build Coastguard Worker case Intrinsics::kMathMinIntInt:
2054*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HMin(kInt32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
2055*795d594fSAndroid Build Coastguard Worker break;
2056*795d594fSAndroid Build Coastguard Worker case Intrinsics::kMathMinLongLong:
2057*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HMin(kInt64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
2058*795d594fSAndroid Build Coastguard Worker break;
2059*795d594fSAndroid Build Coastguard Worker case Intrinsics::kMathMinFloatFloat:
2060*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HMin(kFloat32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
2061*795d594fSAndroid Build Coastguard Worker break;
2062*795d594fSAndroid Build Coastguard Worker case Intrinsics::kMathMinDoubleDouble:
2063*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HMin(kFloat64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
2064*795d594fSAndroid Build Coastguard Worker break;
2065*795d594fSAndroid Build Coastguard Worker case Intrinsics::kMathMaxIntInt:
2066*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HMax(kInt32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
2067*795d594fSAndroid Build Coastguard Worker break;
2068*795d594fSAndroid Build Coastguard Worker case Intrinsics::kMathMaxLongLong:
2069*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HMax(kInt64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
2070*795d594fSAndroid Build Coastguard Worker break;
2071*795d594fSAndroid Build Coastguard Worker case Intrinsics::kMathMaxFloatFloat:
2072*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HMax(kFloat32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
2073*795d594fSAndroid Build Coastguard Worker break;
2074*795d594fSAndroid Build Coastguard Worker case Intrinsics::kMathMaxDoubleDouble:
2075*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HMax(kFloat64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
2076*795d594fSAndroid Build Coastguard Worker break;
2077*795d594fSAndroid Build Coastguard Worker case Intrinsics::kMathAbsInt:
2078*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HAbs(kInt32, /*input=*/ nullptr, dex_pc);
2079*795d594fSAndroid Build Coastguard Worker break;
2080*795d594fSAndroid Build Coastguard Worker case Intrinsics::kMathAbsLong:
2081*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HAbs(kInt64, /*input=*/ nullptr, dex_pc);
2082*795d594fSAndroid Build Coastguard Worker break;
2083*795d594fSAndroid Build Coastguard Worker case Intrinsics::kMathAbsFloat:
2084*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HAbs(kFloat32, /*input=*/ nullptr, dex_pc);
2085*795d594fSAndroid Build Coastguard Worker break;
2086*795d594fSAndroid Build Coastguard Worker case Intrinsics::kMathAbsDouble:
2087*795d594fSAndroid Build Coastguard Worker instruction = new (allocator_) HAbs(kFloat64, /*input=*/ nullptr, dex_pc);
2088*795d594fSAndroid Build Coastguard Worker break;
2089*795d594fSAndroid Build Coastguard Worker default:
2090*795d594fSAndroid Build Coastguard Worker // We do not have intermediate representation for other intrinsics.
2091*795d594fSAndroid Build Coastguard Worker DCHECK(!IsIntrinsicWithSpecializedHir(intrinsic));
2092*795d594fSAndroid Build Coastguard Worker return false;
2093*795d594fSAndroid Build Coastguard Worker }
2094*795d594fSAndroid Build Coastguard Worker DCHECK(instruction != nullptr);
2095*795d594fSAndroid Build Coastguard Worker if (!SetupInvokeArguments(instruction, operands, shorty, receiver_arg)) {
2096*795d594fSAndroid Build Coastguard Worker return false;
2097*795d594fSAndroid Build Coastguard Worker }
2098*795d594fSAndroid Build Coastguard Worker
2099*795d594fSAndroid Build Coastguard Worker switch (intrinsic) {
2100*795d594fSAndroid Build Coastguard Worker case Intrinsics::kFloatIsNaN:
2101*795d594fSAndroid Build Coastguard Worker case Intrinsics::kDoubleIsNaN:
2102*795d594fSAndroid Build Coastguard Worker // Set the second input to be the same as first.
2103*795d594fSAndroid Build Coastguard Worker DCHECK(instruction->IsNotEqual());
2104*795d594fSAndroid Build Coastguard Worker DCHECK(instruction->InputAt(1u) == nullptr);
2105*795d594fSAndroid Build Coastguard Worker instruction->SetRawInputAt(1u, instruction->InputAt(0u));
2106*795d594fSAndroid Build Coastguard Worker break;
2107*795d594fSAndroid Build Coastguard Worker case Intrinsics::kStringCharAt: {
2108*795d594fSAndroid Build Coastguard Worker // Add bounds check.
2109*795d594fSAndroid Build Coastguard Worker HInstruction* array = instruction->InputAt(0u);
2110*795d594fSAndroid Build Coastguard Worker HInstruction* index = instruction->InputAt(1u);
2111*795d594fSAndroid Build Coastguard Worker HInstruction* length =
2112*795d594fSAndroid Build Coastguard Worker new (allocator_) HArrayLength(array, dex_pc, /*is_string_length=*/ true);
2113*795d594fSAndroid Build Coastguard Worker AppendInstruction(length);
2114*795d594fSAndroid Build Coastguard Worker HBoundsCheck* bounds_check =
2115*795d594fSAndroid Build Coastguard Worker new (allocator_) HBoundsCheck(index, length, dex_pc, /*is_string_char_at=*/ true);
2116*795d594fSAndroid Build Coastguard Worker AppendInstruction(bounds_check);
2117*795d594fSAndroid Build Coastguard Worker graph_->SetHasBoundsChecks(true);
2118*795d594fSAndroid Build Coastguard Worker instruction->SetRawInputAt(1u, bounds_check);
2119*795d594fSAndroid Build Coastguard Worker break;
2120*795d594fSAndroid Build Coastguard Worker }
2121*795d594fSAndroid Build Coastguard Worker case Intrinsics::kStringIsEmpty: {
2122*795d594fSAndroid Build Coastguard Worker // Compare the length with 0.
2123*795d594fSAndroid Build Coastguard Worker DCHECK(instruction->IsArrayLength());
2124*795d594fSAndroid Build Coastguard Worker AppendInstruction(instruction);
2125*795d594fSAndroid Build Coastguard Worker HEqual* equal = new (allocator_) HEqual(instruction, graph_->GetIntConstant(0), dex_pc);
2126*795d594fSAndroid Build Coastguard Worker instruction = equal;
2127*795d594fSAndroid Build Coastguard Worker break;
2128*795d594fSAndroid Build Coastguard Worker }
2129*795d594fSAndroid Build Coastguard Worker default:
2130*795d594fSAndroid Build Coastguard Worker break;
2131*795d594fSAndroid Build Coastguard Worker }
2132*795d594fSAndroid Build Coastguard Worker
2133*795d594fSAndroid Build Coastguard Worker AppendInstruction(instruction);
2134*795d594fSAndroid Build Coastguard Worker latest_result_ = instruction;
2135*795d594fSAndroid Build Coastguard Worker
2136*795d594fSAndroid Build Coastguard Worker return true;
2137*795d594fSAndroid Build Coastguard Worker }
2138*795d594fSAndroid Build Coastguard Worker
HandleStringInit(HInvoke * invoke,const InstructionOperands & operands,const char * shorty)2139*795d594fSAndroid Build Coastguard Worker bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
2140*795d594fSAndroid Build Coastguard Worker const InstructionOperands& operands,
2141*795d594fSAndroid Build Coastguard Worker const char* shorty) {
2142*795d594fSAndroid Build Coastguard Worker DCHECK(invoke->IsInvokeStaticOrDirect());
2143*795d594fSAndroid Build Coastguard Worker DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
2144*795d594fSAndroid Build Coastguard Worker
2145*795d594fSAndroid Build Coastguard Worker if (!SetupInvokeArguments(invoke, operands, shorty, ReceiverArg::kIgnored)) {
2146*795d594fSAndroid Build Coastguard Worker return false;
2147*795d594fSAndroid Build Coastguard Worker }
2148*795d594fSAndroid Build Coastguard Worker
2149*795d594fSAndroid Build Coastguard Worker AppendInstruction(invoke);
2150*795d594fSAndroid Build Coastguard Worker
2151*795d594fSAndroid Build Coastguard Worker // This is a StringFactory call, not an actual String constructor. Its result
2152*795d594fSAndroid Build Coastguard Worker // replaces the empty String pre-allocated by NewInstance.
2153*795d594fSAndroid Build Coastguard Worker uint32_t orig_this_reg = operands.GetOperand(0);
2154*795d594fSAndroid Build Coastguard Worker HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
2155*795d594fSAndroid Build Coastguard Worker
2156*795d594fSAndroid Build Coastguard Worker // Replacing the NewInstance might render it redundant. Keep a list of these
2157*795d594fSAndroid Build Coastguard Worker // to be visited once it is clear whether it has remaining uses.
2158*795d594fSAndroid Build Coastguard Worker if (arg_this->IsNewInstance()) {
2159*795d594fSAndroid Build Coastguard Worker ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
2160*795d594fSAndroid Build Coastguard Worker } else {
2161*795d594fSAndroid Build Coastguard Worker DCHECK(arg_this->IsPhi());
2162*795d594fSAndroid Build Coastguard Worker // We can get a phi as input of a String.<init> if there is a loop between the
2163*795d594fSAndroid Build Coastguard Worker // allocation and the String.<init> call. As we don't know which other phis might alias
2164*795d594fSAndroid Build Coastguard Worker // with `arg_this`, we keep a record of those invocations so we can later replace
2165*795d594fSAndroid Build Coastguard Worker // the allocation with the invocation.
2166*795d594fSAndroid Build Coastguard Worker // Add the actual 'this' input so the analysis knows what is the allocation instruction.
2167*795d594fSAndroid Build Coastguard Worker // The input will be removed during the analysis.
2168*795d594fSAndroid Build Coastguard Worker invoke->AddInput(arg_this);
2169*795d594fSAndroid Build Coastguard Worker ssa_builder_->AddUninitializedStringPhi(invoke);
2170*795d594fSAndroid Build Coastguard Worker }
2171*795d594fSAndroid Build Coastguard Worker // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
2172*795d594fSAndroid Build Coastguard Worker for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
2173*795d594fSAndroid Build Coastguard Worker if ((*current_locals_)[vreg] == arg_this) {
2174*795d594fSAndroid Build Coastguard Worker (*current_locals_)[vreg] = invoke;
2175*795d594fSAndroid Build Coastguard Worker }
2176*795d594fSAndroid Build Coastguard Worker }
2177*795d594fSAndroid Build Coastguard Worker return true;
2178*795d594fSAndroid Build Coastguard Worker }
2179*795d594fSAndroid Build Coastguard Worker
GetFieldAccessType(const DexFile & dex_file,uint16_t field_index)2180*795d594fSAndroid Build Coastguard Worker static DataType::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
2181*795d594fSAndroid Build Coastguard Worker const dex::FieldId& field_id = dex_file.GetFieldId(field_index);
2182*795d594fSAndroid Build Coastguard Worker const char* type = dex_file.GetFieldTypeDescriptor(field_id);
2183*795d594fSAndroid Build Coastguard Worker return DataType::FromShorty(type[0]);
2184*795d594fSAndroid Build Coastguard Worker }
2185*795d594fSAndroid Build Coastguard Worker
BuildInstanceFieldAccess(const Instruction & instruction,uint32_t dex_pc,bool is_put)2186*795d594fSAndroid Build Coastguard Worker bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
2187*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc,
2188*795d594fSAndroid Build Coastguard Worker bool is_put) {
2189*795d594fSAndroid Build Coastguard Worker uint32_t source_or_dest_reg = instruction.VRegA_22c();
2190*795d594fSAndroid Build Coastguard Worker uint32_t obj_reg = instruction.VRegB_22c();
2191*795d594fSAndroid Build Coastguard Worker uint16_t field_index = instruction.VRegC_22c();
2192*795d594fSAndroid Build Coastguard Worker
2193*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
2194*795d594fSAndroid Build Coastguard Worker ArtField* resolved_field = ResolveField(field_index, /* is_static= */ false, is_put);
2195*795d594fSAndroid Build Coastguard Worker
2196*795d594fSAndroid Build Coastguard Worker // Generate an explicit null check on the reference, unless the field access
2197*795d594fSAndroid Build Coastguard Worker // is unresolved. In that case, we rely on the runtime to perform various
2198*795d594fSAndroid Build Coastguard Worker // checks first, followed by a null check.
2199*795d594fSAndroid Build Coastguard Worker HInstruction* object = (resolved_field == nullptr)
2200*795d594fSAndroid Build Coastguard Worker ? LoadLocal(obj_reg, DataType::Type::kReference)
2201*795d594fSAndroid Build Coastguard Worker : LoadNullCheckedLocal(obj_reg, dex_pc);
2202*795d594fSAndroid Build Coastguard Worker
2203*795d594fSAndroid Build Coastguard Worker DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
2204*795d594fSAndroid Build Coastguard Worker if (is_put) {
2205*795d594fSAndroid Build Coastguard Worker HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
2206*795d594fSAndroid Build Coastguard Worker HInstruction* field_set = nullptr;
2207*795d594fSAndroid Build Coastguard Worker if (resolved_field == nullptr) {
2208*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(compilation_stats_,
2209*795d594fSAndroid Build Coastguard Worker MethodCompilationStat::kUnresolvedField);
2210*795d594fSAndroid Build Coastguard Worker field_set = new (allocator_) HUnresolvedInstanceFieldSet(object,
2211*795d594fSAndroid Build Coastguard Worker value,
2212*795d594fSAndroid Build Coastguard Worker field_type,
2213*795d594fSAndroid Build Coastguard Worker field_index,
2214*795d594fSAndroid Build Coastguard Worker dex_pc);
2215*795d594fSAndroid Build Coastguard Worker } else {
2216*795d594fSAndroid Build Coastguard Worker uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
2217*795d594fSAndroid Build Coastguard Worker field_set = new (allocator_) HInstanceFieldSet(object,
2218*795d594fSAndroid Build Coastguard Worker value,
2219*795d594fSAndroid Build Coastguard Worker resolved_field,
2220*795d594fSAndroid Build Coastguard Worker field_type,
2221*795d594fSAndroid Build Coastguard Worker resolved_field->GetOffset(),
2222*795d594fSAndroid Build Coastguard Worker resolved_field->IsVolatile(),
2223*795d594fSAndroid Build Coastguard Worker field_index,
2224*795d594fSAndroid Build Coastguard Worker class_def_index,
2225*795d594fSAndroid Build Coastguard Worker *dex_file_,
2226*795d594fSAndroid Build Coastguard Worker dex_pc);
2227*795d594fSAndroid Build Coastguard Worker }
2228*795d594fSAndroid Build Coastguard Worker AppendInstruction(field_set);
2229*795d594fSAndroid Build Coastguard Worker } else {
2230*795d594fSAndroid Build Coastguard Worker HInstruction* field_get = nullptr;
2231*795d594fSAndroid Build Coastguard Worker if (resolved_field == nullptr) {
2232*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(compilation_stats_,
2233*795d594fSAndroid Build Coastguard Worker MethodCompilationStat::kUnresolvedField);
2234*795d594fSAndroid Build Coastguard Worker field_get = new (allocator_) HUnresolvedInstanceFieldGet(object,
2235*795d594fSAndroid Build Coastguard Worker field_type,
2236*795d594fSAndroid Build Coastguard Worker field_index,
2237*795d594fSAndroid Build Coastguard Worker dex_pc);
2238*795d594fSAndroid Build Coastguard Worker } else {
2239*795d594fSAndroid Build Coastguard Worker uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
2240*795d594fSAndroid Build Coastguard Worker field_get = new (allocator_) HInstanceFieldGet(object,
2241*795d594fSAndroid Build Coastguard Worker resolved_field,
2242*795d594fSAndroid Build Coastguard Worker field_type,
2243*795d594fSAndroid Build Coastguard Worker resolved_field->GetOffset(),
2244*795d594fSAndroid Build Coastguard Worker resolved_field->IsVolatile(),
2245*795d594fSAndroid Build Coastguard Worker field_index,
2246*795d594fSAndroid Build Coastguard Worker class_def_index,
2247*795d594fSAndroid Build Coastguard Worker *dex_file_,
2248*795d594fSAndroid Build Coastguard Worker dex_pc);
2249*795d594fSAndroid Build Coastguard Worker }
2250*795d594fSAndroid Build Coastguard Worker AppendInstruction(field_get);
2251*795d594fSAndroid Build Coastguard Worker UpdateLocal(source_or_dest_reg, field_get);
2252*795d594fSAndroid Build Coastguard Worker }
2253*795d594fSAndroid Build Coastguard Worker
2254*795d594fSAndroid Build Coastguard Worker return true;
2255*795d594fSAndroid Build Coastguard Worker }
2256*795d594fSAndroid Build Coastguard Worker
BuildUnresolvedStaticFieldAccess(const Instruction & instruction,uint32_t dex_pc,bool is_put,DataType::Type field_type)2257*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
2258*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc,
2259*795d594fSAndroid Build Coastguard Worker bool is_put,
2260*795d594fSAndroid Build Coastguard Worker DataType::Type field_type) {
2261*795d594fSAndroid Build Coastguard Worker uint32_t source_or_dest_reg = instruction.VRegA_21c();
2262*795d594fSAndroid Build Coastguard Worker uint16_t field_index = instruction.VRegB_21c();
2263*795d594fSAndroid Build Coastguard Worker
2264*795d594fSAndroid Build Coastguard Worker if (is_put) {
2265*795d594fSAndroid Build Coastguard Worker HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
2266*795d594fSAndroid Build Coastguard Worker AppendInstruction(
2267*795d594fSAndroid Build Coastguard Worker new (allocator_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
2268*795d594fSAndroid Build Coastguard Worker } else {
2269*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
2270*795d594fSAndroid Build Coastguard Worker UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
2271*795d594fSAndroid Build Coastguard Worker }
2272*795d594fSAndroid Build Coastguard Worker }
2273*795d594fSAndroid Build Coastguard Worker
ResolveField(uint16_t field_idx,bool is_static,bool is_put)2274*795d594fSAndroid Build Coastguard Worker ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
2275*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
2276*795d594fSAndroid Build Coastguard Worker
2277*795d594fSAndroid Build Coastguard Worker ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
2278*795d594fSAndroid Build Coastguard Worker Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
2279*795d594fSAndroid Build Coastguard Worker
2280*795d594fSAndroid Build Coastguard Worker ArtField* resolved_field = class_linker->ResolveFieldJLS(field_idx,
2281*795d594fSAndroid Build Coastguard Worker dex_compilation_unit_->GetDexCache(),
2282*795d594fSAndroid Build Coastguard Worker class_loader);
2283*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(resolved_field == nullptr, soa.Self()->IsExceptionPending())
2284*795d594fSAndroid Build Coastguard Worker << "field="
2285*795d594fSAndroid Build Coastguard Worker << ((resolved_field == nullptr) ? "null" : resolved_field->PrettyField())
2286*795d594fSAndroid Build Coastguard Worker << ", exception="
2287*795d594fSAndroid Build Coastguard Worker << (soa.Self()->IsExceptionPending() ? soa.Self()->GetException()->Dump() : "null");
2288*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(resolved_field == nullptr)) {
2289*795d594fSAndroid Build Coastguard Worker // Clean up any exception left by field resolution.
2290*795d594fSAndroid Build Coastguard Worker soa.Self()->ClearException();
2291*795d594fSAndroid Build Coastguard Worker return nullptr;
2292*795d594fSAndroid Build Coastguard Worker }
2293*795d594fSAndroid Build Coastguard Worker
2294*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
2295*795d594fSAndroid Build Coastguard Worker return nullptr;
2296*795d594fSAndroid Build Coastguard Worker }
2297*795d594fSAndroid Build Coastguard Worker
2298*795d594fSAndroid Build Coastguard Worker // Check access.
2299*795d594fSAndroid Build Coastguard Worker Handle<mirror::Class> compiling_class = dex_compilation_unit_->GetCompilingClass();
2300*795d594fSAndroid Build Coastguard Worker if (compiling_class == nullptr) {
2301*795d594fSAndroid Build Coastguard Worker // Check if the declaring class or referencing class is accessible.
2302*795d594fSAndroid Build Coastguard Worker SamePackageCompare same_package(*dex_compilation_unit_);
2303*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> declaring_class = resolved_field->GetDeclaringClass();
2304*795d594fSAndroid Build Coastguard Worker bool declaring_class_accessible = declaring_class->IsPublic() || same_package(declaring_class);
2305*795d594fSAndroid Build Coastguard Worker if (!declaring_class_accessible) {
2306*795d594fSAndroid Build Coastguard Worker // It is possible to access members from an inaccessible superclass
2307*795d594fSAndroid Build Coastguard Worker // by referencing them through an accessible subclass.
2308*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
2309*795d594fSAndroid Build Coastguard Worker dex_compilation_unit_->GetDexFile()->GetFieldId(field_idx).class_idx_,
2310*795d594fSAndroid Build Coastguard Worker dex_compilation_unit_->GetDexCache().Get(),
2311*795d594fSAndroid Build Coastguard Worker class_loader.Get());
2312*795d594fSAndroid Build Coastguard Worker DCHECK(referenced_class != nullptr); // Must have been resolved when resolving the field.
2313*795d594fSAndroid Build Coastguard Worker if (!referenced_class->IsPublic() && !same_package(referenced_class)) {
2314*795d594fSAndroid Build Coastguard Worker return nullptr;
2315*795d594fSAndroid Build Coastguard Worker }
2316*795d594fSAndroid Build Coastguard Worker }
2317*795d594fSAndroid Build Coastguard Worker // Check whether the field itself is accessible.
2318*795d594fSAndroid Build Coastguard Worker // Since the referrer is unresolved but the field is resolved, it cannot be
2319*795d594fSAndroid Build Coastguard Worker // inside the same class, so a private field is known to be inaccessible.
2320*795d594fSAndroid Build Coastguard Worker // And without a resolved referrer, we cannot check for protected member access
2321*795d594fSAndroid Build Coastguard Worker // in superlass, so we handle only access to public member or within the package.
2322*795d594fSAndroid Build Coastguard Worker if (resolved_field->IsPrivate() ||
2323*795d594fSAndroid Build Coastguard Worker (!resolved_field->IsPublic() && !declaring_class_accessible)) {
2324*795d594fSAndroid Build Coastguard Worker return nullptr;
2325*795d594fSAndroid Build Coastguard Worker }
2326*795d594fSAndroid Build Coastguard Worker } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
2327*795d594fSAndroid Build Coastguard Worker resolved_field,
2328*795d594fSAndroid Build Coastguard Worker dex_compilation_unit_->GetDexCache().Get(),
2329*795d594fSAndroid Build Coastguard Worker field_idx)) {
2330*795d594fSAndroid Build Coastguard Worker return nullptr;
2331*795d594fSAndroid Build Coastguard Worker }
2332*795d594fSAndroid Build Coastguard Worker
2333*795d594fSAndroid Build Coastguard Worker if (is_put) {
2334*795d594fSAndroid Build Coastguard Worker if (resolved_field->IsFinal() &&
2335*795d594fSAndroid Build Coastguard Worker (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
2336*795d594fSAndroid Build Coastguard Worker // Final fields can only be updated within their own class.
2337*795d594fSAndroid Build Coastguard Worker // TODO: Only allow it in constructors. b/34966607.
2338*795d594fSAndroid Build Coastguard Worker return nullptr;
2339*795d594fSAndroid Build Coastguard Worker }
2340*795d594fSAndroid Build Coastguard Worker
2341*795d594fSAndroid Build Coastguard Worker // Note: We do not need to resolve the field type for `get` opcodes.
2342*795d594fSAndroid Build Coastguard Worker StackArtFieldHandleScope<1> rhs(soa.Self());
2343*795d594fSAndroid Build Coastguard Worker ReflectiveHandle<ArtField> resolved_field_handle(rhs.NewHandle(resolved_field));
2344*795d594fSAndroid Build Coastguard Worker if (resolved_field->ResolveType().IsNull()) {
2345*795d594fSAndroid Build Coastguard Worker // ArtField::ResolveType() may fail as evidenced with a dexing bug (b/78788577).
2346*795d594fSAndroid Build Coastguard Worker soa.Self()->ClearException();
2347*795d594fSAndroid Build Coastguard Worker return nullptr; // Failure
2348*795d594fSAndroid Build Coastguard Worker }
2349*795d594fSAndroid Build Coastguard Worker resolved_field = resolved_field_handle.Get();
2350*795d594fSAndroid Build Coastguard Worker }
2351*795d594fSAndroid Build Coastguard Worker
2352*795d594fSAndroid Build Coastguard Worker return resolved_field;
2353*795d594fSAndroid Build Coastguard Worker }
2354*795d594fSAndroid Build Coastguard Worker
BuildStaticFieldAccess(const Instruction & instruction,uint32_t dex_pc,bool is_put)2355*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
2356*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc,
2357*795d594fSAndroid Build Coastguard Worker bool is_put) {
2358*795d594fSAndroid Build Coastguard Worker uint32_t source_or_dest_reg = instruction.VRegA_21c();
2359*795d594fSAndroid Build Coastguard Worker uint16_t field_index = instruction.VRegB_21c();
2360*795d594fSAndroid Build Coastguard Worker
2361*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
2362*795d594fSAndroid Build Coastguard Worker ArtField* resolved_field = ResolveField(field_index, /* is_static= */ true, is_put);
2363*795d594fSAndroid Build Coastguard Worker
2364*795d594fSAndroid Build Coastguard Worker if (resolved_field == nullptr) {
2365*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(compilation_stats_,
2366*795d594fSAndroid Build Coastguard Worker MethodCompilationStat::kUnresolvedField);
2367*795d594fSAndroid Build Coastguard Worker DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
2368*795d594fSAndroid Build Coastguard Worker BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
2369*795d594fSAndroid Build Coastguard Worker return;
2370*795d594fSAndroid Build Coastguard Worker }
2371*795d594fSAndroid Build Coastguard Worker
2372*795d594fSAndroid Build Coastguard Worker DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
2373*795d594fSAndroid Build Coastguard Worker
2374*795d594fSAndroid Build Coastguard Worker Handle<mirror::Class> klass =
2375*795d594fSAndroid Build Coastguard Worker graph_->GetHandleCache()->NewHandle(resolved_field->GetDeclaringClass());
2376*795d594fSAndroid Build Coastguard Worker HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
2377*795d594fSAndroid Build Coastguard Worker klass->GetDexFile(),
2378*795d594fSAndroid Build Coastguard Worker klass,
2379*795d594fSAndroid Build Coastguard Worker dex_pc,
2380*795d594fSAndroid Build Coastguard Worker /* needs_access_check= */ false);
2381*795d594fSAndroid Build Coastguard Worker
2382*795d594fSAndroid Build Coastguard Worker if (constant == nullptr) {
2383*795d594fSAndroid Build Coastguard Worker // The class cannot be referenced from this compiled code. Generate
2384*795d594fSAndroid Build Coastguard Worker // an unresolved access.
2385*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(compilation_stats_,
2386*795d594fSAndroid Build Coastguard Worker MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
2387*795d594fSAndroid Build Coastguard Worker BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
2388*795d594fSAndroid Build Coastguard Worker return;
2389*795d594fSAndroid Build Coastguard Worker }
2390*795d594fSAndroid Build Coastguard Worker
2391*795d594fSAndroid Build Coastguard Worker HInstruction* cls = constant;
2392*795d594fSAndroid Build Coastguard Worker if (!IsInitialized(klass.Get())) {
2393*795d594fSAndroid Build Coastguard Worker cls = new (allocator_) HClinitCheck(constant, dex_pc);
2394*795d594fSAndroid Build Coastguard Worker AppendInstruction(cls);
2395*795d594fSAndroid Build Coastguard Worker }
2396*795d594fSAndroid Build Coastguard Worker
2397*795d594fSAndroid Build Coastguard Worker uint16_t class_def_index = klass->GetDexClassDefIndex();
2398*795d594fSAndroid Build Coastguard Worker if (is_put) {
2399*795d594fSAndroid Build Coastguard Worker // We need to keep the class alive before loading the value.
2400*795d594fSAndroid Build Coastguard Worker HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
2401*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
2402*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HStaticFieldSet(cls,
2403*795d594fSAndroid Build Coastguard Worker value,
2404*795d594fSAndroid Build Coastguard Worker resolved_field,
2405*795d594fSAndroid Build Coastguard Worker field_type,
2406*795d594fSAndroid Build Coastguard Worker resolved_field->GetOffset(),
2407*795d594fSAndroid Build Coastguard Worker resolved_field->IsVolatile(),
2408*795d594fSAndroid Build Coastguard Worker field_index,
2409*795d594fSAndroid Build Coastguard Worker class_def_index,
2410*795d594fSAndroid Build Coastguard Worker *dex_file_,
2411*795d594fSAndroid Build Coastguard Worker dex_pc));
2412*795d594fSAndroid Build Coastguard Worker } else {
2413*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HStaticFieldGet(cls,
2414*795d594fSAndroid Build Coastguard Worker resolved_field,
2415*795d594fSAndroid Build Coastguard Worker field_type,
2416*795d594fSAndroid Build Coastguard Worker resolved_field->GetOffset(),
2417*795d594fSAndroid Build Coastguard Worker resolved_field->IsVolatile(),
2418*795d594fSAndroid Build Coastguard Worker field_index,
2419*795d594fSAndroid Build Coastguard Worker class_def_index,
2420*795d594fSAndroid Build Coastguard Worker *dex_file_,
2421*795d594fSAndroid Build Coastguard Worker dex_pc));
2422*795d594fSAndroid Build Coastguard Worker UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
2423*795d594fSAndroid Build Coastguard Worker }
2424*795d594fSAndroid Build Coastguard Worker }
2425*795d594fSAndroid Build Coastguard Worker
BuildCheckedDivRem(uint16_t out_vreg,uint16_t first_vreg,int64_t second_vreg_or_constant,uint32_t dex_pc,DataType::Type type,bool second_is_constant,bool is_div)2426*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
2427*795d594fSAndroid Build Coastguard Worker uint16_t first_vreg,
2428*795d594fSAndroid Build Coastguard Worker int64_t second_vreg_or_constant,
2429*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc,
2430*795d594fSAndroid Build Coastguard Worker DataType::Type type,
2431*795d594fSAndroid Build Coastguard Worker bool second_is_constant,
2432*795d594fSAndroid Build Coastguard Worker bool is_div) {
2433*795d594fSAndroid Build Coastguard Worker DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
2434*795d594fSAndroid Build Coastguard Worker
2435*795d594fSAndroid Build Coastguard Worker HInstruction* first = LoadLocal(first_vreg, type);
2436*795d594fSAndroid Build Coastguard Worker HInstruction* second = nullptr;
2437*795d594fSAndroid Build Coastguard Worker if (second_is_constant) {
2438*795d594fSAndroid Build Coastguard Worker if (type == DataType::Type::kInt32) {
2439*795d594fSAndroid Build Coastguard Worker second = graph_->GetIntConstant(second_vreg_or_constant);
2440*795d594fSAndroid Build Coastguard Worker } else {
2441*795d594fSAndroid Build Coastguard Worker second = graph_->GetLongConstant(second_vreg_or_constant);
2442*795d594fSAndroid Build Coastguard Worker }
2443*795d594fSAndroid Build Coastguard Worker } else {
2444*795d594fSAndroid Build Coastguard Worker second = LoadLocal(second_vreg_or_constant, type);
2445*795d594fSAndroid Build Coastguard Worker }
2446*795d594fSAndroid Build Coastguard Worker
2447*795d594fSAndroid Build Coastguard Worker if (!second_is_constant ||
2448*795d594fSAndroid Build Coastguard Worker (type == DataType::Type::kInt32 && second->AsIntConstant()->GetValue() == 0) ||
2449*795d594fSAndroid Build Coastguard Worker (type == DataType::Type::kInt64 && second->AsLongConstant()->GetValue() == 0)) {
2450*795d594fSAndroid Build Coastguard Worker second = new (allocator_) HDivZeroCheck(second, dex_pc);
2451*795d594fSAndroid Build Coastguard Worker AppendInstruction(second);
2452*795d594fSAndroid Build Coastguard Worker }
2453*795d594fSAndroid Build Coastguard Worker
2454*795d594fSAndroid Build Coastguard Worker if (is_div) {
2455*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HDiv(type, first, second, dex_pc));
2456*795d594fSAndroid Build Coastguard Worker } else {
2457*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HRem(type, first, second, dex_pc));
2458*795d594fSAndroid Build Coastguard Worker }
2459*795d594fSAndroid Build Coastguard Worker UpdateLocal(out_vreg, current_block_->GetLastInstruction());
2460*795d594fSAndroid Build Coastguard Worker }
2461*795d594fSAndroid Build Coastguard Worker
BuildArrayAccess(const Instruction & instruction,uint32_t dex_pc,bool is_put,DataType::Type anticipated_type)2462*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
2463*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc,
2464*795d594fSAndroid Build Coastguard Worker bool is_put,
2465*795d594fSAndroid Build Coastguard Worker DataType::Type anticipated_type) {
2466*795d594fSAndroid Build Coastguard Worker uint8_t source_or_dest_reg = instruction.VRegA_23x();
2467*795d594fSAndroid Build Coastguard Worker uint8_t array_reg = instruction.VRegB_23x();
2468*795d594fSAndroid Build Coastguard Worker uint8_t index_reg = instruction.VRegC_23x();
2469*795d594fSAndroid Build Coastguard Worker
2470*795d594fSAndroid Build Coastguard Worker HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
2471*795d594fSAndroid Build Coastguard Worker HInstruction* length = new (allocator_) HArrayLength(object, dex_pc);
2472*795d594fSAndroid Build Coastguard Worker AppendInstruction(length);
2473*795d594fSAndroid Build Coastguard Worker HInstruction* index = LoadLocal(index_reg, DataType::Type::kInt32);
2474*795d594fSAndroid Build Coastguard Worker index = new (allocator_) HBoundsCheck(index, length, dex_pc);
2475*795d594fSAndroid Build Coastguard Worker AppendInstruction(index);
2476*795d594fSAndroid Build Coastguard Worker if (is_put) {
2477*795d594fSAndroid Build Coastguard Worker HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
2478*795d594fSAndroid Build Coastguard Worker // TODO: Insert a type check node if the type is Object.
2479*795d594fSAndroid Build Coastguard Worker HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
2480*795d594fSAndroid Build Coastguard Worker ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2481*795d594fSAndroid Build Coastguard Worker AppendInstruction(aset);
2482*795d594fSAndroid Build Coastguard Worker } else {
2483*795d594fSAndroid Build Coastguard Worker HArrayGet* aget = new (allocator_) HArrayGet(object, index, anticipated_type, dex_pc);
2484*795d594fSAndroid Build Coastguard Worker ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
2485*795d594fSAndroid Build Coastguard Worker AppendInstruction(aget);
2486*795d594fSAndroid Build Coastguard Worker UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
2487*795d594fSAndroid Build Coastguard Worker }
2488*795d594fSAndroid Build Coastguard Worker graph_->SetHasBoundsChecks(true);
2489*795d594fSAndroid Build Coastguard Worker }
2490*795d594fSAndroid Build Coastguard Worker
BuildNewArray(uint32_t dex_pc,dex::TypeIndex type_index,HInstruction * length)2491*795d594fSAndroid Build Coastguard Worker HNewArray* HInstructionBuilder::BuildNewArray(uint32_t dex_pc,
2492*795d594fSAndroid Build Coastguard Worker dex::TypeIndex type_index,
2493*795d594fSAndroid Build Coastguard Worker HInstruction* length) {
2494*795d594fSAndroid Build Coastguard Worker HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
2495*795d594fSAndroid Build Coastguard Worker
2496*795d594fSAndroid Build Coastguard Worker const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(type_index));
2497*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(descriptor[0], '[');
2498*795d594fSAndroid Build Coastguard Worker size_t component_type_shift = Primitive::ComponentSizeShift(Primitive::GetType(descriptor[1]));
2499*795d594fSAndroid Build Coastguard Worker
2500*795d594fSAndroid Build Coastguard Worker HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc, component_type_shift);
2501*795d594fSAndroid Build Coastguard Worker AppendInstruction(new_array);
2502*795d594fSAndroid Build Coastguard Worker return new_array;
2503*795d594fSAndroid Build Coastguard Worker }
2504*795d594fSAndroid Build Coastguard Worker
BuildFilledNewArray(uint32_t dex_pc,dex::TypeIndex type_index,const InstructionOperands & operands)2505*795d594fSAndroid Build Coastguard Worker HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
2506*795d594fSAndroid Build Coastguard Worker dex::TypeIndex type_index,
2507*795d594fSAndroid Build Coastguard Worker const InstructionOperands& operands) {
2508*795d594fSAndroid Build Coastguard Worker const size_t number_of_operands = operands.GetNumberOfOperands();
2509*795d594fSAndroid Build Coastguard Worker HInstruction* length = graph_->GetIntConstant(number_of_operands);
2510*795d594fSAndroid Build Coastguard Worker
2511*795d594fSAndroid Build Coastguard Worker HNewArray* new_array = BuildNewArray(dex_pc, type_index, length);
2512*795d594fSAndroid Build Coastguard Worker const char* descriptor = dex_file_->GetTypeDescriptor(type_index);
2513*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(descriptor[0], '[') << descriptor;
2514*795d594fSAndroid Build Coastguard Worker char primitive = descriptor[1];
2515*795d594fSAndroid Build Coastguard Worker DCHECK(primitive == 'I'
2516*795d594fSAndroid Build Coastguard Worker || primitive == 'L'
2517*795d594fSAndroid Build Coastguard Worker || primitive == '[') << descriptor;
2518*795d594fSAndroid Build Coastguard Worker bool is_reference_array = (primitive == 'L') || (primitive == '[');
2519*795d594fSAndroid Build Coastguard Worker DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
2520*795d594fSAndroid Build Coastguard Worker
2521*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < number_of_operands; ++i) {
2522*795d594fSAndroid Build Coastguard Worker HInstruction* value = LoadLocal(operands.GetOperand(i), type);
2523*795d594fSAndroid Build Coastguard Worker HInstruction* index = graph_->GetIntConstant(i);
2524*795d594fSAndroid Build Coastguard Worker HArraySet* aset = new (allocator_) HArraySet(new_array, index, value, type, dex_pc);
2525*795d594fSAndroid Build Coastguard Worker ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2526*795d594fSAndroid Build Coastguard Worker AppendInstruction(aset);
2527*795d594fSAndroid Build Coastguard Worker }
2528*795d594fSAndroid Build Coastguard Worker latest_result_ = new_array;
2529*795d594fSAndroid Build Coastguard Worker
2530*795d594fSAndroid Build Coastguard Worker return new_array;
2531*795d594fSAndroid Build Coastguard Worker }
2532*795d594fSAndroid Build Coastguard Worker
2533*795d594fSAndroid Build Coastguard Worker template <typename T>
BuildFillArrayData(HInstruction * object,const T * data,uint32_t element_count,DataType::Type anticipated_type,uint32_t dex_pc)2534*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
2535*795d594fSAndroid Build Coastguard Worker const T* data,
2536*795d594fSAndroid Build Coastguard Worker uint32_t element_count,
2537*795d594fSAndroid Build Coastguard Worker DataType::Type anticipated_type,
2538*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc) {
2539*795d594fSAndroid Build Coastguard Worker for (uint32_t i = 0; i < element_count; ++i) {
2540*795d594fSAndroid Build Coastguard Worker HInstruction* index = graph_->GetIntConstant(i);
2541*795d594fSAndroid Build Coastguard Worker HInstruction* value = graph_->GetIntConstant(data[i]);
2542*795d594fSAndroid Build Coastguard Worker HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
2543*795d594fSAndroid Build Coastguard Worker ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2544*795d594fSAndroid Build Coastguard Worker AppendInstruction(aset);
2545*795d594fSAndroid Build Coastguard Worker }
2546*795d594fSAndroid Build Coastguard Worker }
2547*795d594fSAndroid Build Coastguard Worker
BuildFillArrayData(const Instruction & instruction,uint32_t dex_pc)2548*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
2549*795d594fSAndroid Build Coastguard Worker HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
2550*795d594fSAndroid Build Coastguard Worker
2551*795d594fSAndroid Build Coastguard Worker int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
2552*795d594fSAndroid Build Coastguard Worker const Instruction::ArrayDataPayload* payload =
2553*795d594fSAndroid Build Coastguard Worker reinterpret_cast<const Instruction::ArrayDataPayload*>(
2554*795d594fSAndroid Build Coastguard Worker code_item_accessor_.Insns() + payload_offset);
2555*795d594fSAndroid Build Coastguard Worker const uint8_t* data = payload->data;
2556*795d594fSAndroid Build Coastguard Worker uint32_t element_count = payload->element_count;
2557*795d594fSAndroid Build Coastguard Worker
2558*795d594fSAndroid Build Coastguard Worker if (element_count == 0u) {
2559*795d594fSAndroid Build Coastguard Worker // For empty payload we emit only the null check above.
2560*795d594fSAndroid Build Coastguard Worker return;
2561*795d594fSAndroid Build Coastguard Worker }
2562*795d594fSAndroid Build Coastguard Worker
2563*795d594fSAndroid Build Coastguard Worker HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
2564*795d594fSAndroid Build Coastguard Worker AppendInstruction(length);
2565*795d594fSAndroid Build Coastguard Worker
2566*795d594fSAndroid Build Coastguard Worker // Implementation of this DEX instruction seems to be that the bounds check is
2567*795d594fSAndroid Build Coastguard Worker // done before doing any stores.
2568*795d594fSAndroid Build Coastguard Worker HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1);
2569*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
2570*795d594fSAndroid Build Coastguard Worker
2571*795d594fSAndroid Build Coastguard Worker switch (payload->element_width) {
2572*795d594fSAndroid Build Coastguard Worker case 1:
2573*795d594fSAndroid Build Coastguard Worker BuildFillArrayData(array,
2574*795d594fSAndroid Build Coastguard Worker reinterpret_cast<const int8_t*>(data),
2575*795d594fSAndroid Build Coastguard Worker element_count,
2576*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt8,
2577*795d594fSAndroid Build Coastguard Worker dex_pc);
2578*795d594fSAndroid Build Coastguard Worker break;
2579*795d594fSAndroid Build Coastguard Worker case 2:
2580*795d594fSAndroid Build Coastguard Worker BuildFillArrayData(array,
2581*795d594fSAndroid Build Coastguard Worker reinterpret_cast<const int16_t*>(data),
2582*795d594fSAndroid Build Coastguard Worker element_count,
2583*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt16,
2584*795d594fSAndroid Build Coastguard Worker dex_pc);
2585*795d594fSAndroid Build Coastguard Worker break;
2586*795d594fSAndroid Build Coastguard Worker case 4:
2587*795d594fSAndroid Build Coastguard Worker BuildFillArrayData(array,
2588*795d594fSAndroid Build Coastguard Worker reinterpret_cast<const int32_t*>(data),
2589*795d594fSAndroid Build Coastguard Worker element_count,
2590*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt32,
2591*795d594fSAndroid Build Coastguard Worker dex_pc);
2592*795d594fSAndroid Build Coastguard Worker break;
2593*795d594fSAndroid Build Coastguard Worker case 8:
2594*795d594fSAndroid Build Coastguard Worker BuildFillWideArrayData(array,
2595*795d594fSAndroid Build Coastguard Worker reinterpret_cast<const int64_t*>(data),
2596*795d594fSAndroid Build Coastguard Worker element_count,
2597*795d594fSAndroid Build Coastguard Worker dex_pc);
2598*795d594fSAndroid Build Coastguard Worker break;
2599*795d594fSAndroid Build Coastguard Worker default:
2600*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Unknown element width for " << payload->element_width;
2601*795d594fSAndroid Build Coastguard Worker }
2602*795d594fSAndroid Build Coastguard Worker graph_->SetHasBoundsChecks(true);
2603*795d594fSAndroid Build Coastguard Worker }
2604*795d594fSAndroid Build Coastguard Worker
BuildFillWideArrayData(HInstruction * object,const int64_t * data,uint32_t element_count,uint32_t dex_pc)2605*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
2606*795d594fSAndroid Build Coastguard Worker const int64_t* data,
2607*795d594fSAndroid Build Coastguard Worker uint32_t element_count,
2608*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc) {
2609*795d594fSAndroid Build Coastguard Worker for (uint32_t i = 0; i < element_count; ++i) {
2610*795d594fSAndroid Build Coastguard Worker HInstruction* index = graph_->GetIntConstant(i);
2611*795d594fSAndroid Build Coastguard Worker HInstruction* value = graph_->GetLongConstant(data[i]);
2612*795d594fSAndroid Build Coastguard Worker HArraySet* aset =
2613*795d594fSAndroid Build Coastguard Worker new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
2614*795d594fSAndroid Build Coastguard Worker ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2615*795d594fSAndroid Build Coastguard Worker AppendInstruction(aset);
2616*795d594fSAndroid Build Coastguard Worker }
2617*795d594fSAndroid Build Coastguard Worker }
2618*795d594fSAndroid Build Coastguard Worker
BuildLoadString(dex::StringIndex string_index,uint32_t dex_pc)2619*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc) {
2620*795d594fSAndroid Build Coastguard Worker HLoadString* load_string =
2621*795d594fSAndroid Build Coastguard Worker new (allocator_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc);
2622*795d594fSAndroid Build Coastguard Worker HSharpening::ProcessLoadString(load_string,
2623*795d594fSAndroid Build Coastguard Worker code_generator_,
2624*795d594fSAndroid Build Coastguard Worker *dex_compilation_unit_,
2625*795d594fSAndroid Build Coastguard Worker graph_->GetHandleCache()->GetHandles());
2626*795d594fSAndroid Build Coastguard Worker AppendInstruction(load_string);
2627*795d594fSAndroid Build Coastguard Worker }
2628*795d594fSAndroid Build Coastguard Worker
BuildLoadClass(dex::TypeIndex type_index,uint32_t dex_pc)2629*795d594fSAndroid Build Coastguard Worker HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
2630*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
2631*795d594fSAndroid Build Coastguard Worker const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2632*795d594fSAndroid Build Coastguard Worker Handle<mirror::Class> klass = ResolveClass(soa, type_index);
2633*795d594fSAndroid Build Coastguard Worker bool needs_access_check = LoadClassNeedsAccessCheck(type_index, klass.Get());
2634*795d594fSAndroid Build Coastguard Worker return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
2635*795d594fSAndroid Build Coastguard Worker }
2636*795d594fSAndroid Build Coastguard Worker
BuildLoadClass(dex::TypeIndex type_index,const DexFile & dex_file,Handle<mirror::Class> klass,uint32_t dex_pc,bool needs_access_check)2637*795d594fSAndroid Build Coastguard Worker HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
2638*795d594fSAndroid Build Coastguard Worker const DexFile& dex_file,
2639*795d594fSAndroid Build Coastguard Worker Handle<mirror::Class> klass,
2640*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc,
2641*795d594fSAndroid Build Coastguard Worker bool needs_access_check) {
2642*795d594fSAndroid Build Coastguard Worker // Try to find a reference in the compiling dex file.
2643*795d594fSAndroid Build Coastguard Worker const DexFile* actual_dex_file = &dex_file;
2644*795d594fSAndroid Build Coastguard Worker if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
2645*795d594fSAndroid Build Coastguard Worker dex::TypeIndex local_type_index =
2646*795d594fSAndroid Build Coastguard Worker klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
2647*795d594fSAndroid Build Coastguard Worker if (local_type_index.IsValid()) {
2648*795d594fSAndroid Build Coastguard Worker type_index = local_type_index;
2649*795d594fSAndroid Build Coastguard Worker actual_dex_file = dex_compilation_unit_->GetDexFile();
2650*795d594fSAndroid Build Coastguard Worker }
2651*795d594fSAndroid Build Coastguard Worker }
2652*795d594fSAndroid Build Coastguard Worker
2653*795d594fSAndroid Build Coastguard Worker // We cannot use the referrer's class load kind if we need to do an access check.
2654*795d594fSAndroid Build Coastguard Worker // If the `klass` is unresolved, we need access check with the exception of the referrer's
2655*795d594fSAndroid Build Coastguard Worker // class, see LoadClassNeedsAccessCheck(), so the `!needs_access_check` check is enough.
2656*795d594fSAndroid Build Coastguard Worker // Otherwise, also check if the `klass` is the same as the compiling class, which also
2657*795d594fSAndroid Build Coastguard Worker // conveniently rejects the case of unresolved compiling class.
2658*795d594fSAndroid Build Coastguard Worker bool is_referrers_class =
2659*795d594fSAndroid Build Coastguard Worker !needs_access_check &&
2660*795d594fSAndroid Build Coastguard Worker (klass == nullptr || outer_compilation_unit_->GetCompilingClass().Get() == klass.Get());
2661*795d594fSAndroid Build Coastguard Worker // Note: `klass` must be from `graph_->GetHandleCache()`.
2662*795d594fSAndroid Build Coastguard Worker HLoadClass* load_class = new (allocator_) HLoadClass(
2663*795d594fSAndroid Build Coastguard Worker graph_->GetCurrentMethod(),
2664*795d594fSAndroid Build Coastguard Worker type_index,
2665*795d594fSAndroid Build Coastguard Worker *actual_dex_file,
2666*795d594fSAndroid Build Coastguard Worker klass,
2667*795d594fSAndroid Build Coastguard Worker is_referrers_class,
2668*795d594fSAndroid Build Coastguard Worker dex_pc,
2669*795d594fSAndroid Build Coastguard Worker needs_access_check);
2670*795d594fSAndroid Build Coastguard Worker
2671*795d594fSAndroid Build Coastguard Worker HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
2672*795d594fSAndroid Build Coastguard Worker code_generator_,
2673*795d594fSAndroid Build Coastguard Worker *dex_compilation_unit_);
2674*795d594fSAndroid Build Coastguard Worker
2675*795d594fSAndroid Build Coastguard Worker if (load_kind == HLoadClass::LoadKind::kInvalid) {
2676*795d594fSAndroid Build Coastguard Worker // We actually cannot reference this class, we're forced to bail.
2677*795d594fSAndroid Build Coastguard Worker return nullptr;
2678*795d594fSAndroid Build Coastguard Worker }
2679*795d594fSAndroid Build Coastguard Worker // Load kind must be set before inserting the instruction into the graph.
2680*795d594fSAndroid Build Coastguard Worker load_class->SetLoadKind(load_kind);
2681*795d594fSAndroid Build Coastguard Worker AppendInstruction(load_class);
2682*795d594fSAndroid Build Coastguard Worker return load_class;
2683*795d594fSAndroid Build Coastguard Worker }
2684*795d594fSAndroid Build Coastguard Worker
ResolveClass(ScopedObjectAccess & soa,dex::TypeIndex type_index)2685*795d594fSAndroid Build Coastguard Worker Handle<mirror::Class> HInstructionBuilder::ResolveClass(ScopedObjectAccess& soa,
2686*795d594fSAndroid Build Coastguard Worker dex::TypeIndex type_index) {
2687*795d594fSAndroid Build Coastguard Worker auto it = class_cache_.find(type_index);
2688*795d594fSAndroid Build Coastguard Worker if (it != class_cache_.end()) {
2689*795d594fSAndroid Build Coastguard Worker return it->second;
2690*795d594fSAndroid Build Coastguard Worker }
2691*795d594fSAndroid Build Coastguard Worker
2692*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> klass = dex_compilation_unit_->GetClassLinker()->ResolveType(
2693*795d594fSAndroid Build Coastguard Worker type_index, dex_compilation_unit_->GetDexCache(), dex_compilation_unit_->GetClassLoader());
2694*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(klass == nullptr, soa.Self()->IsExceptionPending());
2695*795d594fSAndroid Build Coastguard Worker soa.Self()->ClearException(); // Clean up the exception left by type resolution if any.
2696*795d594fSAndroid Build Coastguard Worker
2697*795d594fSAndroid Build Coastguard Worker Handle<mirror::Class> h_klass = graph_->GetHandleCache()->NewHandle(klass);
2698*795d594fSAndroid Build Coastguard Worker class_cache_.Put(type_index, h_klass);
2699*795d594fSAndroid Build Coastguard Worker return h_klass;
2700*795d594fSAndroid Build Coastguard Worker }
2701*795d594fSAndroid Build Coastguard Worker
LoadClassNeedsAccessCheck(dex::TypeIndex type_index,ObjPtr<mirror::Class> klass)2702*795d594fSAndroid Build Coastguard Worker bool HInstructionBuilder::LoadClassNeedsAccessCheck(dex::TypeIndex type_index,
2703*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> klass) {
2704*795d594fSAndroid Build Coastguard Worker if (klass == nullptr) {
2705*795d594fSAndroid Build Coastguard Worker // If the class is unresolved, we can avoid access checks only for references to
2706*795d594fSAndroid Build Coastguard Worker // the compiling class as determined by checking the descriptor and ClassLoader.
2707*795d594fSAndroid Build Coastguard Worker if (outer_compilation_unit_->GetCompilingClass() != nullptr) {
2708*795d594fSAndroid Build Coastguard Worker // Compiling class is resolved, so different from the unresolved class.
2709*795d594fSAndroid Build Coastguard Worker return true;
2710*795d594fSAndroid Build Coastguard Worker }
2711*795d594fSAndroid Build Coastguard Worker if (dex_compilation_unit_->GetClassLoader().Get() !=
2712*795d594fSAndroid Build Coastguard Worker outer_compilation_unit_->GetClassLoader().Get()) {
2713*795d594fSAndroid Build Coastguard Worker // Resolving the same descriptor in a different ClassLoader than the
2714*795d594fSAndroid Build Coastguard Worker // defining loader of the compiling class shall either fail to find
2715*795d594fSAndroid Build Coastguard Worker // the class definition, or find a different one.
2716*795d594fSAndroid Build Coastguard Worker // (Assuming no custom ClassLoader hierarchy with circular delegation.)
2717*795d594fSAndroid Build Coastguard Worker return true;
2718*795d594fSAndroid Build Coastguard Worker }
2719*795d594fSAndroid Build Coastguard Worker // Check if the class is the outer method's class.
2720*795d594fSAndroid Build Coastguard Worker // For the same dex file compare type indexes, otherwise descriptors.
2721*795d594fSAndroid Build Coastguard Worker const DexFile* outer_dex_file = outer_compilation_unit_->GetDexFile();
2722*795d594fSAndroid Build Coastguard Worker const DexFile* inner_dex_file = dex_compilation_unit_->GetDexFile();
2723*795d594fSAndroid Build Coastguard Worker const dex::ClassDef& outer_class_def =
2724*795d594fSAndroid Build Coastguard Worker outer_dex_file->GetClassDef(outer_compilation_unit_->GetClassDefIndex());
2725*795d594fSAndroid Build Coastguard Worker if (IsSameDexFile(*inner_dex_file, *outer_dex_file)) {
2726*795d594fSAndroid Build Coastguard Worker if (type_index != outer_class_def.class_idx_) {
2727*795d594fSAndroid Build Coastguard Worker return true;
2728*795d594fSAndroid Build Coastguard Worker }
2729*795d594fSAndroid Build Coastguard Worker } else {
2730*795d594fSAndroid Build Coastguard Worker const std::string_view outer_descriptor =
2731*795d594fSAndroid Build Coastguard Worker outer_dex_file->GetTypeDescriptorView(outer_class_def.class_idx_);
2732*795d594fSAndroid Build Coastguard Worker const std::string_view target_descriptor =
2733*795d594fSAndroid Build Coastguard Worker inner_dex_file->GetTypeDescriptorView(type_index);
2734*795d594fSAndroid Build Coastguard Worker if (outer_descriptor != target_descriptor) {
2735*795d594fSAndroid Build Coastguard Worker return true;
2736*795d594fSAndroid Build Coastguard Worker }
2737*795d594fSAndroid Build Coastguard Worker }
2738*795d594fSAndroid Build Coastguard Worker // For inlined methods we also need to check if the compiling class
2739*795d594fSAndroid Build Coastguard Worker // is public or in the same package as the inlined method's class.
2740*795d594fSAndroid Build Coastguard Worker if (dex_compilation_unit_ != outer_compilation_unit_ &&
2741*795d594fSAndroid Build Coastguard Worker (outer_class_def.access_flags_ & kAccPublic) == 0) {
2742*795d594fSAndroid Build Coastguard Worker DCHECK(dex_compilation_unit_->GetCompilingClass() != nullptr);
2743*795d594fSAndroid Build Coastguard Worker SamePackageCompare same_package(*outer_compilation_unit_);
2744*795d594fSAndroid Build Coastguard Worker if (!same_package(dex_compilation_unit_->GetCompilingClass().Get())) {
2745*795d594fSAndroid Build Coastguard Worker return true;
2746*795d594fSAndroid Build Coastguard Worker }
2747*795d594fSAndroid Build Coastguard Worker }
2748*795d594fSAndroid Build Coastguard Worker return false;
2749*795d594fSAndroid Build Coastguard Worker } else if (klass->IsPublic()) {
2750*795d594fSAndroid Build Coastguard Worker return false;
2751*795d594fSAndroid Build Coastguard Worker } else if (dex_compilation_unit_->GetCompilingClass() != nullptr) {
2752*795d594fSAndroid Build Coastguard Worker return !dex_compilation_unit_->GetCompilingClass()->CanAccess(klass);
2753*795d594fSAndroid Build Coastguard Worker } else {
2754*795d594fSAndroid Build Coastguard Worker SamePackageCompare same_package(*dex_compilation_unit_);
2755*795d594fSAndroid Build Coastguard Worker return !same_package(klass);
2756*795d594fSAndroid Build Coastguard Worker }
2757*795d594fSAndroid Build Coastguard Worker }
2758*795d594fSAndroid Build Coastguard Worker
BuildLoadMethodHandle(uint16_t method_handle_index,uint32_t dex_pc)2759*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::BuildLoadMethodHandle(uint16_t method_handle_index, uint32_t dex_pc) {
2760*795d594fSAndroid Build Coastguard Worker const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2761*795d594fSAndroid Build Coastguard Worker HLoadMethodHandle* load_method_handle = new (allocator_) HLoadMethodHandle(
2762*795d594fSAndroid Build Coastguard Worker graph_->GetCurrentMethod(), method_handle_index, dex_file, dex_pc);
2763*795d594fSAndroid Build Coastguard Worker AppendInstruction(load_method_handle);
2764*795d594fSAndroid Build Coastguard Worker }
2765*795d594fSAndroid Build Coastguard Worker
BuildLoadMethodType(dex::ProtoIndex proto_index,uint32_t dex_pc)2766*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::BuildLoadMethodType(dex::ProtoIndex proto_index, uint32_t dex_pc) {
2767*795d594fSAndroid Build Coastguard Worker const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2768*795d594fSAndroid Build Coastguard Worker HLoadMethodType* load_method_type =
2769*795d594fSAndroid Build Coastguard Worker new (allocator_) HLoadMethodType(graph_->GetCurrentMethod(), proto_index, dex_file, dex_pc);
2770*795d594fSAndroid Build Coastguard Worker HSharpening::ProcessLoadMethodType(load_method_type,
2771*795d594fSAndroid Build Coastguard Worker code_generator_,
2772*795d594fSAndroid Build Coastguard Worker *dex_compilation_unit_,
2773*795d594fSAndroid Build Coastguard Worker graph_->GetHandleCache()->GetHandles());
2774*795d594fSAndroid Build Coastguard Worker AppendInstruction(load_method_type);
2775*795d594fSAndroid Build Coastguard Worker }
2776*795d594fSAndroid Build Coastguard Worker
BuildTypeCheck(bool is_instance_of,HInstruction * object,dex::TypeIndex type_index,uint32_t dex_pc)2777*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::BuildTypeCheck(bool is_instance_of,
2778*795d594fSAndroid Build Coastguard Worker HInstruction* object,
2779*795d594fSAndroid Build Coastguard Worker dex::TypeIndex type_index,
2780*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc) {
2781*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
2782*795d594fSAndroid Build Coastguard Worker const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2783*795d594fSAndroid Build Coastguard Worker Handle<mirror::Class> klass = ResolveClass(soa, type_index);
2784*795d594fSAndroid Build Coastguard Worker bool needs_access_check = LoadClassNeedsAccessCheck(type_index, klass.Get());
2785*795d594fSAndroid Build Coastguard Worker TypeCheckKind check_kind = HSharpening::ComputeTypeCheckKind(
2786*795d594fSAndroid Build Coastguard Worker klass.Get(), code_generator_, needs_access_check);
2787*795d594fSAndroid Build Coastguard Worker
2788*795d594fSAndroid Build Coastguard Worker HInstruction* class_or_null = nullptr;
2789*795d594fSAndroid Build Coastguard Worker HIntConstant* bitstring_path_to_root = nullptr;
2790*795d594fSAndroid Build Coastguard Worker HIntConstant* bitstring_mask = nullptr;
2791*795d594fSAndroid Build Coastguard Worker if (check_kind == TypeCheckKind::kBitstringCheck) {
2792*795d594fSAndroid Build Coastguard Worker // TODO: Allow using the bitstring check also if we need an access check.
2793*795d594fSAndroid Build Coastguard Worker DCHECK(!needs_access_check);
2794*795d594fSAndroid Build Coastguard Worker class_or_null = graph_->GetNullConstant();
2795*795d594fSAndroid Build Coastguard Worker MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
2796*795d594fSAndroid Build Coastguard Worker uint32_t path_to_root =
2797*795d594fSAndroid Build Coastguard Worker SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootForTarget(klass.Get());
2798*795d594fSAndroid Build Coastguard Worker uint32_t mask = SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootMask(klass.Get());
2799*795d594fSAndroid Build Coastguard Worker bitstring_path_to_root = graph_->GetIntConstant(static_cast<int32_t>(path_to_root));
2800*795d594fSAndroid Build Coastguard Worker bitstring_mask = graph_->GetIntConstant(static_cast<int32_t>(mask));
2801*795d594fSAndroid Build Coastguard Worker } else {
2802*795d594fSAndroid Build Coastguard Worker class_or_null = BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
2803*795d594fSAndroid Build Coastguard Worker }
2804*795d594fSAndroid Build Coastguard Worker DCHECK(class_or_null != nullptr);
2805*795d594fSAndroid Build Coastguard Worker
2806*795d594fSAndroid Build Coastguard Worker if (is_instance_of) {
2807*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HInstanceOf(object,
2808*795d594fSAndroid Build Coastguard Worker class_or_null,
2809*795d594fSAndroid Build Coastguard Worker check_kind,
2810*795d594fSAndroid Build Coastguard Worker klass,
2811*795d594fSAndroid Build Coastguard Worker dex_pc,
2812*795d594fSAndroid Build Coastguard Worker allocator_,
2813*795d594fSAndroid Build Coastguard Worker bitstring_path_to_root,
2814*795d594fSAndroid Build Coastguard Worker bitstring_mask));
2815*795d594fSAndroid Build Coastguard Worker } else {
2816*795d594fSAndroid Build Coastguard Worker // We emit a CheckCast followed by a BoundType. CheckCast is a statement
2817*795d594fSAndroid Build Coastguard Worker // which may throw. If it succeeds BoundType sets the new type of `object`
2818*795d594fSAndroid Build Coastguard Worker // for all subsequent uses.
2819*795d594fSAndroid Build Coastguard Worker AppendInstruction(
2820*795d594fSAndroid Build Coastguard Worker new (allocator_) HCheckCast(object,
2821*795d594fSAndroid Build Coastguard Worker class_or_null,
2822*795d594fSAndroid Build Coastguard Worker check_kind,
2823*795d594fSAndroid Build Coastguard Worker klass,
2824*795d594fSAndroid Build Coastguard Worker dex_pc,
2825*795d594fSAndroid Build Coastguard Worker allocator_,
2826*795d594fSAndroid Build Coastguard Worker bitstring_path_to_root,
2827*795d594fSAndroid Build Coastguard Worker bitstring_mask));
2828*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
2829*795d594fSAndroid Build Coastguard Worker }
2830*795d594fSAndroid Build Coastguard Worker }
2831*795d594fSAndroid Build Coastguard Worker
BuildTypeCheck(const Instruction & instruction,uint8_t destination,uint8_t reference,dex::TypeIndex type_index,uint32_t dex_pc)2832*795d594fSAndroid Build Coastguard Worker void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
2833*795d594fSAndroid Build Coastguard Worker uint8_t destination,
2834*795d594fSAndroid Build Coastguard Worker uint8_t reference,
2835*795d594fSAndroid Build Coastguard Worker dex::TypeIndex type_index,
2836*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc) {
2837*795d594fSAndroid Build Coastguard Worker HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
2838*795d594fSAndroid Build Coastguard Worker bool is_instance_of = instruction.Opcode() == Instruction::INSTANCE_OF;
2839*795d594fSAndroid Build Coastguard Worker
2840*795d594fSAndroid Build Coastguard Worker BuildTypeCheck(is_instance_of, object, type_index, dex_pc);
2841*795d594fSAndroid Build Coastguard Worker
2842*795d594fSAndroid Build Coastguard Worker if (is_instance_of) {
2843*795d594fSAndroid Build Coastguard Worker UpdateLocal(destination, current_block_->GetLastInstruction());
2844*795d594fSAndroid Build Coastguard Worker } else {
2845*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
2846*795d594fSAndroid Build Coastguard Worker UpdateLocal(reference, current_block_->GetLastInstruction());
2847*795d594fSAndroid Build Coastguard Worker }
2848*795d594fSAndroid Build Coastguard Worker }
2849*795d594fSAndroid Build Coastguard Worker
ProcessDexInstruction(const Instruction & instruction,uint32_t dex_pc)2850*795d594fSAndroid Build Coastguard Worker bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
2851*795d594fSAndroid Build Coastguard Worker switch (instruction.Opcode()) {
2852*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_4: {
2853*795d594fSAndroid Build Coastguard Worker int32_t register_index = instruction.VRegA_11n();
2854*795d594fSAndroid Build Coastguard Worker HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n());
2855*795d594fSAndroid Build Coastguard Worker UpdateLocal(register_index, constant);
2856*795d594fSAndroid Build Coastguard Worker break;
2857*795d594fSAndroid Build Coastguard Worker }
2858*795d594fSAndroid Build Coastguard Worker
2859*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_16: {
2860*795d594fSAndroid Build Coastguard Worker int32_t register_index = instruction.VRegA_21s();
2861*795d594fSAndroid Build Coastguard Worker HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s());
2862*795d594fSAndroid Build Coastguard Worker UpdateLocal(register_index, constant);
2863*795d594fSAndroid Build Coastguard Worker break;
2864*795d594fSAndroid Build Coastguard Worker }
2865*795d594fSAndroid Build Coastguard Worker
2866*795d594fSAndroid Build Coastguard Worker case Instruction::CONST: {
2867*795d594fSAndroid Build Coastguard Worker int32_t register_index = instruction.VRegA_31i();
2868*795d594fSAndroid Build Coastguard Worker HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i());
2869*795d594fSAndroid Build Coastguard Worker UpdateLocal(register_index, constant);
2870*795d594fSAndroid Build Coastguard Worker break;
2871*795d594fSAndroid Build Coastguard Worker }
2872*795d594fSAndroid Build Coastguard Worker
2873*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_HIGH16: {
2874*795d594fSAndroid Build Coastguard Worker int32_t register_index = instruction.VRegA_21h();
2875*795d594fSAndroid Build Coastguard Worker HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16);
2876*795d594fSAndroid Build Coastguard Worker UpdateLocal(register_index, constant);
2877*795d594fSAndroid Build Coastguard Worker break;
2878*795d594fSAndroid Build Coastguard Worker }
2879*795d594fSAndroid Build Coastguard Worker
2880*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_WIDE_16: {
2881*795d594fSAndroid Build Coastguard Worker int32_t register_index = instruction.VRegA_21s();
2882*795d594fSAndroid Build Coastguard Worker // Get 16 bits of constant value, sign extended to 64 bits.
2883*795d594fSAndroid Build Coastguard Worker int64_t value = instruction.VRegB_21s();
2884*795d594fSAndroid Build Coastguard Worker value <<= 48;
2885*795d594fSAndroid Build Coastguard Worker value >>= 48;
2886*795d594fSAndroid Build Coastguard Worker HLongConstant* constant = graph_->GetLongConstant(value);
2887*795d594fSAndroid Build Coastguard Worker UpdateLocal(register_index, constant);
2888*795d594fSAndroid Build Coastguard Worker break;
2889*795d594fSAndroid Build Coastguard Worker }
2890*795d594fSAndroid Build Coastguard Worker
2891*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_WIDE_32: {
2892*795d594fSAndroid Build Coastguard Worker int32_t register_index = instruction.VRegA_31i();
2893*795d594fSAndroid Build Coastguard Worker // Get 32 bits of constant value, sign extended to 64 bits.
2894*795d594fSAndroid Build Coastguard Worker int64_t value = instruction.VRegB_31i();
2895*795d594fSAndroid Build Coastguard Worker value <<= 32;
2896*795d594fSAndroid Build Coastguard Worker value >>= 32;
2897*795d594fSAndroid Build Coastguard Worker HLongConstant* constant = graph_->GetLongConstant(value);
2898*795d594fSAndroid Build Coastguard Worker UpdateLocal(register_index, constant);
2899*795d594fSAndroid Build Coastguard Worker break;
2900*795d594fSAndroid Build Coastguard Worker }
2901*795d594fSAndroid Build Coastguard Worker
2902*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_WIDE: {
2903*795d594fSAndroid Build Coastguard Worker int32_t register_index = instruction.VRegA_51l();
2904*795d594fSAndroid Build Coastguard Worker HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l());
2905*795d594fSAndroid Build Coastguard Worker UpdateLocal(register_index, constant);
2906*795d594fSAndroid Build Coastguard Worker break;
2907*795d594fSAndroid Build Coastguard Worker }
2908*795d594fSAndroid Build Coastguard Worker
2909*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_WIDE_HIGH16: {
2910*795d594fSAndroid Build Coastguard Worker int32_t register_index = instruction.VRegA_21h();
2911*795d594fSAndroid Build Coastguard Worker int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
2912*795d594fSAndroid Build Coastguard Worker HLongConstant* constant = graph_->GetLongConstant(value);
2913*795d594fSAndroid Build Coastguard Worker UpdateLocal(register_index, constant);
2914*795d594fSAndroid Build Coastguard Worker break;
2915*795d594fSAndroid Build Coastguard Worker }
2916*795d594fSAndroid Build Coastguard Worker
2917*795d594fSAndroid Build Coastguard Worker // Note that the SSA building will refine the types for moves.
2918*795d594fSAndroid Build Coastguard Worker
2919*795d594fSAndroid Build Coastguard Worker case Instruction::MOVE: {
2920*795d594fSAndroid Build Coastguard Worker BuildMove<DataType::Type::kInt32>(instruction.VRegA_12x(), instruction.VRegB_12x());
2921*795d594fSAndroid Build Coastguard Worker break;
2922*795d594fSAndroid Build Coastguard Worker }
2923*795d594fSAndroid Build Coastguard Worker
2924*795d594fSAndroid Build Coastguard Worker case Instruction::MOVE_FROM16: {
2925*795d594fSAndroid Build Coastguard Worker BuildMove<DataType::Type::kInt32>(instruction.VRegA_22x(), instruction.VRegB_22x());
2926*795d594fSAndroid Build Coastguard Worker break;
2927*795d594fSAndroid Build Coastguard Worker }
2928*795d594fSAndroid Build Coastguard Worker
2929*795d594fSAndroid Build Coastguard Worker case Instruction::MOVE_16: {
2930*795d594fSAndroid Build Coastguard Worker BuildMove<DataType::Type::kInt32>(instruction.VRegA_32x(), instruction.VRegB_32x());
2931*795d594fSAndroid Build Coastguard Worker break;
2932*795d594fSAndroid Build Coastguard Worker }
2933*795d594fSAndroid Build Coastguard Worker
2934*795d594fSAndroid Build Coastguard Worker case Instruction::MOVE_WIDE: {
2935*795d594fSAndroid Build Coastguard Worker BuildMove<DataType::Type::kInt64>(instruction.VRegA_12x(), instruction.VRegB_12x());
2936*795d594fSAndroid Build Coastguard Worker break;
2937*795d594fSAndroid Build Coastguard Worker }
2938*795d594fSAndroid Build Coastguard Worker
2939*795d594fSAndroid Build Coastguard Worker case Instruction::MOVE_WIDE_FROM16: {
2940*795d594fSAndroid Build Coastguard Worker BuildMove<DataType::Type::kInt64>(instruction.VRegA_22x(), instruction.VRegB_22x());
2941*795d594fSAndroid Build Coastguard Worker break;
2942*795d594fSAndroid Build Coastguard Worker }
2943*795d594fSAndroid Build Coastguard Worker
2944*795d594fSAndroid Build Coastguard Worker case Instruction::MOVE_WIDE_16: {
2945*795d594fSAndroid Build Coastguard Worker BuildMove<DataType::Type::kInt64>(instruction.VRegA_32x(), instruction.VRegB_32x());
2946*795d594fSAndroid Build Coastguard Worker break;
2947*795d594fSAndroid Build Coastguard Worker }
2948*795d594fSAndroid Build Coastguard Worker
2949*795d594fSAndroid Build Coastguard Worker case Instruction::MOVE_OBJECT: {
2950*795d594fSAndroid Build Coastguard Worker BuildMove<DataType::Type::kReference>(instruction.VRegA_12x(), instruction.VRegB_12x());
2951*795d594fSAndroid Build Coastguard Worker break;
2952*795d594fSAndroid Build Coastguard Worker }
2953*795d594fSAndroid Build Coastguard Worker
2954*795d594fSAndroid Build Coastguard Worker case Instruction::MOVE_OBJECT_FROM16: {
2955*795d594fSAndroid Build Coastguard Worker BuildMove<DataType::Type::kReference>(instruction.VRegA_22x(), instruction.VRegB_22x());
2956*795d594fSAndroid Build Coastguard Worker break;
2957*795d594fSAndroid Build Coastguard Worker }
2958*795d594fSAndroid Build Coastguard Worker
2959*795d594fSAndroid Build Coastguard Worker case Instruction::MOVE_OBJECT_16: {
2960*795d594fSAndroid Build Coastguard Worker BuildMove<DataType::Type::kReference>(instruction.VRegA_32x(), instruction.VRegB_32x());
2961*795d594fSAndroid Build Coastguard Worker break;
2962*795d594fSAndroid Build Coastguard Worker }
2963*795d594fSAndroid Build Coastguard Worker
2964*795d594fSAndroid Build Coastguard Worker case Instruction::RETURN_VOID: {
2965*795d594fSAndroid Build Coastguard Worker BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
2966*795d594fSAndroid Build Coastguard Worker break;
2967*795d594fSAndroid Build Coastguard Worker }
2968*795d594fSAndroid Build Coastguard Worker
2969*795d594fSAndroid Build Coastguard Worker #define IF_XX(comparison, cond) \
2970*795d594fSAndroid Build Coastguard Worker case Instruction::IF_##cond: \
2971*795d594fSAndroid Build Coastguard Worker If_21_22t<comparison, /* kCompareWithZero= */ false>(instruction, dex_pc); \
2972*795d594fSAndroid Build Coastguard Worker break; \
2973*795d594fSAndroid Build Coastguard Worker case Instruction::IF_##cond##Z: \
2974*795d594fSAndroid Build Coastguard Worker If_21_22t<comparison, /* kCompareWithZero= */ true>(instruction, dex_pc); \
2975*795d594fSAndroid Build Coastguard Worker break;
2976*795d594fSAndroid Build Coastguard Worker
2977*795d594fSAndroid Build Coastguard Worker IF_XX(HEqual, EQ);
2978*795d594fSAndroid Build Coastguard Worker IF_XX(HNotEqual, NE);
2979*795d594fSAndroid Build Coastguard Worker IF_XX(HLessThan, LT);
2980*795d594fSAndroid Build Coastguard Worker IF_XX(HLessThanOrEqual, LE);
2981*795d594fSAndroid Build Coastguard Worker IF_XX(HGreaterThan, GT);
2982*795d594fSAndroid Build Coastguard Worker IF_XX(HGreaterThanOrEqual, GE);
2983*795d594fSAndroid Build Coastguard Worker #undef IF_XX
2984*795d594fSAndroid Build Coastguard Worker
2985*795d594fSAndroid Build Coastguard Worker case Instruction::GOTO:
2986*795d594fSAndroid Build Coastguard Worker case Instruction::GOTO_16:
2987*795d594fSAndroid Build Coastguard Worker case Instruction::GOTO_32: {
2988*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HGoto(dex_pc));
2989*795d594fSAndroid Build Coastguard Worker current_block_ = nullptr;
2990*795d594fSAndroid Build Coastguard Worker break;
2991*795d594fSAndroid Build Coastguard Worker }
2992*795d594fSAndroid Build Coastguard Worker
2993*795d594fSAndroid Build Coastguard Worker case Instruction::RETURN: {
2994*795d594fSAndroid Build Coastguard Worker BuildReturn(instruction, return_type_, dex_pc);
2995*795d594fSAndroid Build Coastguard Worker break;
2996*795d594fSAndroid Build Coastguard Worker }
2997*795d594fSAndroid Build Coastguard Worker
2998*795d594fSAndroid Build Coastguard Worker case Instruction::RETURN_OBJECT: {
2999*795d594fSAndroid Build Coastguard Worker BuildReturn(instruction, return_type_, dex_pc);
3000*795d594fSAndroid Build Coastguard Worker break;
3001*795d594fSAndroid Build Coastguard Worker }
3002*795d594fSAndroid Build Coastguard Worker
3003*795d594fSAndroid Build Coastguard Worker case Instruction::RETURN_WIDE: {
3004*795d594fSAndroid Build Coastguard Worker BuildReturn(instruction, return_type_, dex_pc);
3005*795d594fSAndroid Build Coastguard Worker break;
3006*795d594fSAndroid Build Coastguard Worker }
3007*795d594fSAndroid Build Coastguard Worker
3008*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_DIRECT:
3009*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_INTERFACE:
3010*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_STATIC:
3011*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_SUPER:
3012*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_VIRTUAL: {
3013*795d594fSAndroid Build Coastguard Worker uint16_t method_idx = instruction.VRegB_35c();
3014*795d594fSAndroid Build Coastguard Worker uint32_t args[5];
3015*795d594fSAndroid Build Coastguard Worker uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
3016*795d594fSAndroid Build Coastguard Worker VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
3017*795d594fSAndroid Build Coastguard Worker if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
3018*795d594fSAndroid Build Coastguard Worker return false;
3019*795d594fSAndroid Build Coastguard Worker }
3020*795d594fSAndroid Build Coastguard Worker break;
3021*795d594fSAndroid Build Coastguard Worker }
3022*795d594fSAndroid Build Coastguard Worker
3023*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_DIRECT_RANGE:
3024*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_INTERFACE_RANGE:
3025*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_STATIC_RANGE:
3026*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_SUPER_RANGE:
3027*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_VIRTUAL_RANGE: {
3028*795d594fSAndroid Build Coastguard Worker uint16_t method_idx = instruction.VRegB_3rc();
3029*795d594fSAndroid Build Coastguard Worker RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
3030*795d594fSAndroid Build Coastguard Worker if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
3031*795d594fSAndroid Build Coastguard Worker return false;
3032*795d594fSAndroid Build Coastguard Worker }
3033*795d594fSAndroid Build Coastguard Worker break;
3034*795d594fSAndroid Build Coastguard Worker }
3035*795d594fSAndroid Build Coastguard Worker
3036*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_POLYMORPHIC: {
3037*795d594fSAndroid Build Coastguard Worker uint16_t method_idx = instruction.VRegB_45cc();
3038*795d594fSAndroid Build Coastguard Worker dex::ProtoIndex proto_idx(instruction.VRegH_45cc());
3039*795d594fSAndroid Build Coastguard Worker uint32_t args[5];
3040*795d594fSAndroid Build Coastguard Worker uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
3041*795d594fSAndroid Build Coastguard Worker VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
3042*795d594fSAndroid Build Coastguard Worker return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
3043*795d594fSAndroid Build Coastguard Worker }
3044*795d594fSAndroid Build Coastguard Worker
3045*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_POLYMORPHIC_RANGE: {
3046*795d594fSAndroid Build Coastguard Worker uint16_t method_idx = instruction.VRegB_4rcc();
3047*795d594fSAndroid Build Coastguard Worker dex::ProtoIndex proto_idx(instruction.VRegH_4rcc());
3048*795d594fSAndroid Build Coastguard Worker RangeInstructionOperands operands(instruction.VRegC_4rcc(), instruction.VRegA_4rcc());
3049*795d594fSAndroid Build Coastguard Worker return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
3050*795d594fSAndroid Build Coastguard Worker }
3051*795d594fSAndroid Build Coastguard Worker
3052*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_CUSTOM: {
3053*795d594fSAndroid Build Coastguard Worker uint16_t call_site_idx = instruction.VRegB_35c();
3054*795d594fSAndroid Build Coastguard Worker uint32_t args[5];
3055*795d594fSAndroid Build Coastguard Worker uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
3056*795d594fSAndroid Build Coastguard Worker VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
3057*795d594fSAndroid Build Coastguard Worker return BuildInvokeCustom(dex_pc, call_site_idx, operands);
3058*795d594fSAndroid Build Coastguard Worker }
3059*795d594fSAndroid Build Coastguard Worker
3060*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_CUSTOM_RANGE: {
3061*795d594fSAndroid Build Coastguard Worker uint16_t call_site_idx = instruction.VRegB_3rc();
3062*795d594fSAndroid Build Coastguard Worker RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
3063*795d594fSAndroid Build Coastguard Worker return BuildInvokeCustom(dex_pc, call_site_idx, operands);
3064*795d594fSAndroid Build Coastguard Worker }
3065*795d594fSAndroid Build Coastguard Worker
3066*795d594fSAndroid Build Coastguard Worker case Instruction::NEG_INT: {
3067*795d594fSAndroid Build Coastguard Worker Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
3068*795d594fSAndroid Build Coastguard Worker break;
3069*795d594fSAndroid Build Coastguard Worker }
3070*795d594fSAndroid Build Coastguard Worker
3071*795d594fSAndroid Build Coastguard Worker case Instruction::NEG_LONG: {
3072*795d594fSAndroid Build Coastguard Worker Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
3073*795d594fSAndroid Build Coastguard Worker break;
3074*795d594fSAndroid Build Coastguard Worker }
3075*795d594fSAndroid Build Coastguard Worker
3076*795d594fSAndroid Build Coastguard Worker case Instruction::NEG_FLOAT: {
3077*795d594fSAndroid Build Coastguard Worker Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
3078*795d594fSAndroid Build Coastguard Worker break;
3079*795d594fSAndroid Build Coastguard Worker }
3080*795d594fSAndroid Build Coastguard Worker
3081*795d594fSAndroid Build Coastguard Worker case Instruction::NEG_DOUBLE: {
3082*795d594fSAndroid Build Coastguard Worker Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
3083*795d594fSAndroid Build Coastguard Worker break;
3084*795d594fSAndroid Build Coastguard Worker }
3085*795d594fSAndroid Build Coastguard Worker
3086*795d594fSAndroid Build Coastguard Worker case Instruction::NOT_INT: {
3087*795d594fSAndroid Build Coastguard Worker Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
3088*795d594fSAndroid Build Coastguard Worker break;
3089*795d594fSAndroid Build Coastguard Worker }
3090*795d594fSAndroid Build Coastguard Worker
3091*795d594fSAndroid Build Coastguard Worker case Instruction::NOT_LONG: {
3092*795d594fSAndroid Build Coastguard Worker Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
3093*795d594fSAndroid Build Coastguard Worker break;
3094*795d594fSAndroid Build Coastguard Worker }
3095*795d594fSAndroid Build Coastguard Worker
3096*795d594fSAndroid Build Coastguard Worker case Instruction::INT_TO_LONG: {
3097*795d594fSAndroid Build Coastguard Worker Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
3098*795d594fSAndroid Build Coastguard Worker break;
3099*795d594fSAndroid Build Coastguard Worker }
3100*795d594fSAndroid Build Coastguard Worker
3101*795d594fSAndroid Build Coastguard Worker case Instruction::INT_TO_FLOAT: {
3102*795d594fSAndroid Build Coastguard Worker Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
3103*795d594fSAndroid Build Coastguard Worker break;
3104*795d594fSAndroid Build Coastguard Worker }
3105*795d594fSAndroid Build Coastguard Worker
3106*795d594fSAndroid Build Coastguard Worker case Instruction::INT_TO_DOUBLE: {
3107*795d594fSAndroid Build Coastguard Worker Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
3108*795d594fSAndroid Build Coastguard Worker break;
3109*795d594fSAndroid Build Coastguard Worker }
3110*795d594fSAndroid Build Coastguard Worker
3111*795d594fSAndroid Build Coastguard Worker case Instruction::LONG_TO_INT: {
3112*795d594fSAndroid Build Coastguard Worker Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
3113*795d594fSAndroid Build Coastguard Worker break;
3114*795d594fSAndroid Build Coastguard Worker }
3115*795d594fSAndroid Build Coastguard Worker
3116*795d594fSAndroid Build Coastguard Worker case Instruction::LONG_TO_FLOAT: {
3117*795d594fSAndroid Build Coastguard Worker Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
3118*795d594fSAndroid Build Coastguard Worker break;
3119*795d594fSAndroid Build Coastguard Worker }
3120*795d594fSAndroid Build Coastguard Worker
3121*795d594fSAndroid Build Coastguard Worker case Instruction::LONG_TO_DOUBLE: {
3122*795d594fSAndroid Build Coastguard Worker Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
3123*795d594fSAndroid Build Coastguard Worker break;
3124*795d594fSAndroid Build Coastguard Worker }
3125*795d594fSAndroid Build Coastguard Worker
3126*795d594fSAndroid Build Coastguard Worker case Instruction::FLOAT_TO_INT: {
3127*795d594fSAndroid Build Coastguard Worker Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
3128*795d594fSAndroid Build Coastguard Worker break;
3129*795d594fSAndroid Build Coastguard Worker }
3130*795d594fSAndroid Build Coastguard Worker
3131*795d594fSAndroid Build Coastguard Worker case Instruction::FLOAT_TO_LONG: {
3132*795d594fSAndroid Build Coastguard Worker Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
3133*795d594fSAndroid Build Coastguard Worker break;
3134*795d594fSAndroid Build Coastguard Worker }
3135*795d594fSAndroid Build Coastguard Worker
3136*795d594fSAndroid Build Coastguard Worker case Instruction::FLOAT_TO_DOUBLE: {
3137*795d594fSAndroid Build Coastguard Worker Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
3138*795d594fSAndroid Build Coastguard Worker break;
3139*795d594fSAndroid Build Coastguard Worker }
3140*795d594fSAndroid Build Coastguard Worker
3141*795d594fSAndroid Build Coastguard Worker case Instruction::DOUBLE_TO_INT: {
3142*795d594fSAndroid Build Coastguard Worker Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
3143*795d594fSAndroid Build Coastguard Worker break;
3144*795d594fSAndroid Build Coastguard Worker }
3145*795d594fSAndroid Build Coastguard Worker
3146*795d594fSAndroid Build Coastguard Worker case Instruction::DOUBLE_TO_LONG: {
3147*795d594fSAndroid Build Coastguard Worker Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
3148*795d594fSAndroid Build Coastguard Worker break;
3149*795d594fSAndroid Build Coastguard Worker }
3150*795d594fSAndroid Build Coastguard Worker
3151*795d594fSAndroid Build Coastguard Worker case Instruction::DOUBLE_TO_FLOAT: {
3152*795d594fSAndroid Build Coastguard Worker Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
3153*795d594fSAndroid Build Coastguard Worker break;
3154*795d594fSAndroid Build Coastguard Worker }
3155*795d594fSAndroid Build Coastguard Worker
3156*795d594fSAndroid Build Coastguard Worker case Instruction::INT_TO_BYTE: {
3157*795d594fSAndroid Build Coastguard Worker Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
3158*795d594fSAndroid Build Coastguard Worker break;
3159*795d594fSAndroid Build Coastguard Worker }
3160*795d594fSAndroid Build Coastguard Worker
3161*795d594fSAndroid Build Coastguard Worker case Instruction::INT_TO_SHORT: {
3162*795d594fSAndroid Build Coastguard Worker Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
3163*795d594fSAndroid Build Coastguard Worker break;
3164*795d594fSAndroid Build Coastguard Worker }
3165*795d594fSAndroid Build Coastguard Worker
3166*795d594fSAndroid Build Coastguard Worker case Instruction::INT_TO_CHAR: {
3167*795d594fSAndroid Build Coastguard Worker Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
3168*795d594fSAndroid Build Coastguard Worker break;
3169*795d594fSAndroid Build Coastguard Worker }
3170*795d594fSAndroid Build Coastguard Worker
3171*795d594fSAndroid Build Coastguard Worker case Instruction::ADD_INT: {
3172*795d594fSAndroid Build Coastguard Worker Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
3173*795d594fSAndroid Build Coastguard Worker break;
3174*795d594fSAndroid Build Coastguard Worker }
3175*795d594fSAndroid Build Coastguard Worker
3176*795d594fSAndroid Build Coastguard Worker case Instruction::ADD_LONG: {
3177*795d594fSAndroid Build Coastguard Worker Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
3178*795d594fSAndroid Build Coastguard Worker break;
3179*795d594fSAndroid Build Coastguard Worker }
3180*795d594fSAndroid Build Coastguard Worker
3181*795d594fSAndroid Build Coastguard Worker case Instruction::ADD_DOUBLE: {
3182*795d594fSAndroid Build Coastguard Worker Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
3183*795d594fSAndroid Build Coastguard Worker break;
3184*795d594fSAndroid Build Coastguard Worker }
3185*795d594fSAndroid Build Coastguard Worker
3186*795d594fSAndroid Build Coastguard Worker case Instruction::ADD_FLOAT: {
3187*795d594fSAndroid Build Coastguard Worker Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
3188*795d594fSAndroid Build Coastguard Worker break;
3189*795d594fSAndroid Build Coastguard Worker }
3190*795d594fSAndroid Build Coastguard Worker
3191*795d594fSAndroid Build Coastguard Worker case Instruction::SUB_INT: {
3192*795d594fSAndroid Build Coastguard Worker Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
3193*795d594fSAndroid Build Coastguard Worker break;
3194*795d594fSAndroid Build Coastguard Worker }
3195*795d594fSAndroid Build Coastguard Worker
3196*795d594fSAndroid Build Coastguard Worker case Instruction::SUB_LONG: {
3197*795d594fSAndroid Build Coastguard Worker Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
3198*795d594fSAndroid Build Coastguard Worker break;
3199*795d594fSAndroid Build Coastguard Worker }
3200*795d594fSAndroid Build Coastguard Worker
3201*795d594fSAndroid Build Coastguard Worker case Instruction::SUB_FLOAT: {
3202*795d594fSAndroid Build Coastguard Worker Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
3203*795d594fSAndroid Build Coastguard Worker break;
3204*795d594fSAndroid Build Coastguard Worker }
3205*795d594fSAndroid Build Coastguard Worker
3206*795d594fSAndroid Build Coastguard Worker case Instruction::SUB_DOUBLE: {
3207*795d594fSAndroid Build Coastguard Worker Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
3208*795d594fSAndroid Build Coastguard Worker break;
3209*795d594fSAndroid Build Coastguard Worker }
3210*795d594fSAndroid Build Coastguard Worker
3211*795d594fSAndroid Build Coastguard Worker case Instruction::ADD_INT_2ADDR: {
3212*795d594fSAndroid Build Coastguard Worker Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
3213*795d594fSAndroid Build Coastguard Worker break;
3214*795d594fSAndroid Build Coastguard Worker }
3215*795d594fSAndroid Build Coastguard Worker
3216*795d594fSAndroid Build Coastguard Worker case Instruction::MUL_INT: {
3217*795d594fSAndroid Build Coastguard Worker Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
3218*795d594fSAndroid Build Coastguard Worker break;
3219*795d594fSAndroid Build Coastguard Worker }
3220*795d594fSAndroid Build Coastguard Worker
3221*795d594fSAndroid Build Coastguard Worker case Instruction::MUL_LONG: {
3222*795d594fSAndroid Build Coastguard Worker Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
3223*795d594fSAndroid Build Coastguard Worker break;
3224*795d594fSAndroid Build Coastguard Worker }
3225*795d594fSAndroid Build Coastguard Worker
3226*795d594fSAndroid Build Coastguard Worker case Instruction::MUL_FLOAT: {
3227*795d594fSAndroid Build Coastguard Worker Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
3228*795d594fSAndroid Build Coastguard Worker break;
3229*795d594fSAndroid Build Coastguard Worker }
3230*795d594fSAndroid Build Coastguard Worker
3231*795d594fSAndroid Build Coastguard Worker case Instruction::MUL_DOUBLE: {
3232*795d594fSAndroid Build Coastguard Worker Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
3233*795d594fSAndroid Build Coastguard Worker break;
3234*795d594fSAndroid Build Coastguard Worker }
3235*795d594fSAndroid Build Coastguard Worker
3236*795d594fSAndroid Build Coastguard Worker case Instruction::DIV_INT: {
3237*795d594fSAndroid Build Coastguard Worker BuildCheckedDivRem(instruction.VRegA_23x(),
3238*795d594fSAndroid Build Coastguard Worker instruction.VRegB_23x(),
3239*795d594fSAndroid Build Coastguard Worker instruction.VRegC_23x(),
3240*795d594fSAndroid Build Coastguard Worker dex_pc,
3241*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt32,
3242*795d594fSAndroid Build Coastguard Worker /* second_is_constant= */ false,
3243*795d594fSAndroid Build Coastguard Worker /* is_div=*/ true);
3244*795d594fSAndroid Build Coastguard Worker break;
3245*795d594fSAndroid Build Coastguard Worker }
3246*795d594fSAndroid Build Coastguard Worker
3247*795d594fSAndroid Build Coastguard Worker case Instruction::DIV_LONG: {
3248*795d594fSAndroid Build Coastguard Worker BuildCheckedDivRem(instruction.VRegA_23x(),
3249*795d594fSAndroid Build Coastguard Worker instruction.VRegB_23x(),
3250*795d594fSAndroid Build Coastguard Worker instruction.VRegC_23x(),
3251*795d594fSAndroid Build Coastguard Worker dex_pc,
3252*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt64,
3253*795d594fSAndroid Build Coastguard Worker /* second_is_constant= */ false,
3254*795d594fSAndroid Build Coastguard Worker /* is_div=*/ true);
3255*795d594fSAndroid Build Coastguard Worker break;
3256*795d594fSAndroid Build Coastguard Worker }
3257*795d594fSAndroid Build Coastguard Worker
3258*795d594fSAndroid Build Coastguard Worker case Instruction::DIV_FLOAT: {
3259*795d594fSAndroid Build Coastguard Worker Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
3260*795d594fSAndroid Build Coastguard Worker break;
3261*795d594fSAndroid Build Coastguard Worker }
3262*795d594fSAndroid Build Coastguard Worker
3263*795d594fSAndroid Build Coastguard Worker case Instruction::DIV_DOUBLE: {
3264*795d594fSAndroid Build Coastguard Worker Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
3265*795d594fSAndroid Build Coastguard Worker break;
3266*795d594fSAndroid Build Coastguard Worker }
3267*795d594fSAndroid Build Coastguard Worker
3268*795d594fSAndroid Build Coastguard Worker case Instruction::REM_INT: {
3269*795d594fSAndroid Build Coastguard Worker BuildCheckedDivRem(instruction.VRegA_23x(),
3270*795d594fSAndroid Build Coastguard Worker instruction.VRegB_23x(),
3271*795d594fSAndroid Build Coastguard Worker instruction.VRegC_23x(),
3272*795d594fSAndroid Build Coastguard Worker dex_pc,
3273*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt32,
3274*795d594fSAndroid Build Coastguard Worker /* second_is_constant= */ false,
3275*795d594fSAndroid Build Coastguard Worker /* is_div=*/ false);
3276*795d594fSAndroid Build Coastguard Worker break;
3277*795d594fSAndroid Build Coastguard Worker }
3278*795d594fSAndroid Build Coastguard Worker
3279*795d594fSAndroid Build Coastguard Worker case Instruction::REM_LONG: {
3280*795d594fSAndroid Build Coastguard Worker BuildCheckedDivRem(instruction.VRegA_23x(),
3281*795d594fSAndroid Build Coastguard Worker instruction.VRegB_23x(),
3282*795d594fSAndroid Build Coastguard Worker instruction.VRegC_23x(),
3283*795d594fSAndroid Build Coastguard Worker dex_pc,
3284*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt64,
3285*795d594fSAndroid Build Coastguard Worker /* second_is_constant= */ false,
3286*795d594fSAndroid Build Coastguard Worker /* is_div=*/ false);
3287*795d594fSAndroid Build Coastguard Worker break;
3288*795d594fSAndroid Build Coastguard Worker }
3289*795d594fSAndroid Build Coastguard Worker
3290*795d594fSAndroid Build Coastguard Worker case Instruction::REM_FLOAT: {
3291*795d594fSAndroid Build Coastguard Worker Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
3292*795d594fSAndroid Build Coastguard Worker break;
3293*795d594fSAndroid Build Coastguard Worker }
3294*795d594fSAndroid Build Coastguard Worker
3295*795d594fSAndroid Build Coastguard Worker case Instruction::REM_DOUBLE: {
3296*795d594fSAndroid Build Coastguard Worker Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
3297*795d594fSAndroid Build Coastguard Worker break;
3298*795d594fSAndroid Build Coastguard Worker }
3299*795d594fSAndroid Build Coastguard Worker
3300*795d594fSAndroid Build Coastguard Worker case Instruction::AND_INT: {
3301*795d594fSAndroid Build Coastguard Worker Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
3302*795d594fSAndroid Build Coastguard Worker break;
3303*795d594fSAndroid Build Coastguard Worker }
3304*795d594fSAndroid Build Coastguard Worker
3305*795d594fSAndroid Build Coastguard Worker case Instruction::AND_LONG: {
3306*795d594fSAndroid Build Coastguard Worker Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
3307*795d594fSAndroid Build Coastguard Worker break;
3308*795d594fSAndroid Build Coastguard Worker }
3309*795d594fSAndroid Build Coastguard Worker
3310*795d594fSAndroid Build Coastguard Worker case Instruction::SHL_INT: {
3311*795d594fSAndroid Build Coastguard Worker Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
3312*795d594fSAndroid Build Coastguard Worker break;
3313*795d594fSAndroid Build Coastguard Worker }
3314*795d594fSAndroid Build Coastguard Worker
3315*795d594fSAndroid Build Coastguard Worker case Instruction::SHL_LONG: {
3316*795d594fSAndroid Build Coastguard Worker Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
3317*795d594fSAndroid Build Coastguard Worker break;
3318*795d594fSAndroid Build Coastguard Worker }
3319*795d594fSAndroid Build Coastguard Worker
3320*795d594fSAndroid Build Coastguard Worker case Instruction::SHR_INT: {
3321*795d594fSAndroid Build Coastguard Worker Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
3322*795d594fSAndroid Build Coastguard Worker break;
3323*795d594fSAndroid Build Coastguard Worker }
3324*795d594fSAndroid Build Coastguard Worker
3325*795d594fSAndroid Build Coastguard Worker case Instruction::SHR_LONG: {
3326*795d594fSAndroid Build Coastguard Worker Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
3327*795d594fSAndroid Build Coastguard Worker break;
3328*795d594fSAndroid Build Coastguard Worker }
3329*795d594fSAndroid Build Coastguard Worker
3330*795d594fSAndroid Build Coastguard Worker case Instruction::USHR_INT: {
3331*795d594fSAndroid Build Coastguard Worker Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
3332*795d594fSAndroid Build Coastguard Worker break;
3333*795d594fSAndroid Build Coastguard Worker }
3334*795d594fSAndroid Build Coastguard Worker
3335*795d594fSAndroid Build Coastguard Worker case Instruction::USHR_LONG: {
3336*795d594fSAndroid Build Coastguard Worker Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
3337*795d594fSAndroid Build Coastguard Worker break;
3338*795d594fSAndroid Build Coastguard Worker }
3339*795d594fSAndroid Build Coastguard Worker
3340*795d594fSAndroid Build Coastguard Worker case Instruction::OR_INT: {
3341*795d594fSAndroid Build Coastguard Worker Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
3342*795d594fSAndroid Build Coastguard Worker break;
3343*795d594fSAndroid Build Coastguard Worker }
3344*795d594fSAndroid Build Coastguard Worker
3345*795d594fSAndroid Build Coastguard Worker case Instruction::OR_LONG: {
3346*795d594fSAndroid Build Coastguard Worker Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
3347*795d594fSAndroid Build Coastguard Worker break;
3348*795d594fSAndroid Build Coastguard Worker }
3349*795d594fSAndroid Build Coastguard Worker
3350*795d594fSAndroid Build Coastguard Worker case Instruction::XOR_INT: {
3351*795d594fSAndroid Build Coastguard Worker Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
3352*795d594fSAndroid Build Coastguard Worker break;
3353*795d594fSAndroid Build Coastguard Worker }
3354*795d594fSAndroid Build Coastguard Worker
3355*795d594fSAndroid Build Coastguard Worker case Instruction::XOR_LONG: {
3356*795d594fSAndroid Build Coastguard Worker Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
3357*795d594fSAndroid Build Coastguard Worker break;
3358*795d594fSAndroid Build Coastguard Worker }
3359*795d594fSAndroid Build Coastguard Worker
3360*795d594fSAndroid Build Coastguard Worker case Instruction::ADD_LONG_2ADDR: {
3361*795d594fSAndroid Build Coastguard Worker Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
3362*795d594fSAndroid Build Coastguard Worker break;
3363*795d594fSAndroid Build Coastguard Worker }
3364*795d594fSAndroid Build Coastguard Worker
3365*795d594fSAndroid Build Coastguard Worker case Instruction::ADD_DOUBLE_2ADDR: {
3366*795d594fSAndroid Build Coastguard Worker Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
3367*795d594fSAndroid Build Coastguard Worker break;
3368*795d594fSAndroid Build Coastguard Worker }
3369*795d594fSAndroid Build Coastguard Worker
3370*795d594fSAndroid Build Coastguard Worker case Instruction::ADD_FLOAT_2ADDR: {
3371*795d594fSAndroid Build Coastguard Worker Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
3372*795d594fSAndroid Build Coastguard Worker break;
3373*795d594fSAndroid Build Coastguard Worker }
3374*795d594fSAndroid Build Coastguard Worker
3375*795d594fSAndroid Build Coastguard Worker case Instruction::SUB_INT_2ADDR: {
3376*795d594fSAndroid Build Coastguard Worker Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
3377*795d594fSAndroid Build Coastguard Worker break;
3378*795d594fSAndroid Build Coastguard Worker }
3379*795d594fSAndroid Build Coastguard Worker
3380*795d594fSAndroid Build Coastguard Worker case Instruction::SUB_LONG_2ADDR: {
3381*795d594fSAndroid Build Coastguard Worker Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
3382*795d594fSAndroid Build Coastguard Worker break;
3383*795d594fSAndroid Build Coastguard Worker }
3384*795d594fSAndroid Build Coastguard Worker
3385*795d594fSAndroid Build Coastguard Worker case Instruction::SUB_FLOAT_2ADDR: {
3386*795d594fSAndroid Build Coastguard Worker Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
3387*795d594fSAndroid Build Coastguard Worker break;
3388*795d594fSAndroid Build Coastguard Worker }
3389*795d594fSAndroid Build Coastguard Worker
3390*795d594fSAndroid Build Coastguard Worker case Instruction::SUB_DOUBLE_2ADDR: {
3391*795d594fSAndroid Build Coastguard Worker Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
3392*795d594fSAndroid Build Coastguard Worker break;
3393*795d594fSAndroid Build Coastguard Worker }
3394*795d594fSAndroid Build Coastguard Worker
3395*795d594fSAndroid Build Coastguard Worker case Instruction::MUL_INT_2ADDR: {
3396*795d594fSAndroid Build Coastguard Worker Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
3397*795d594fSAndroid Build Coastguard Worker break;
3398*795d594fSAndroid Build Coastguard Worker }
3399*795d594fSAndroid Build Coastguard Worker
3400*795d594fSAndroid Build Coastguard Worker case Instruction::MUL_LONG_2ADDR: {
3401*795d594fSAndroid Build Coastguard Worker Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
3402*795d594fSAndroid Build Coastguard Worker break;
3403*795d594fSAndroid Build Coastguard Worker }
3404*795d594fSAndroid Build Coastguard Worker
3405*795d594fSAndroid Build Coastguard Worker case Instruction::MUL_FLOAT_2ADDR: {
3406*795d594fSAndroid Build Coastguard Worker Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
3407*795d594fSAndroid Build Coastguard Worker break;
3408*795d594fSAndroid Build Coastguard Worker }
3409*795d594fSAndroid Build Coastguard Worker
3410*795d594fSAndroid Build Coastguard Worker case Instruction::MUL_DOUBLE_2ADDR: {
3411*795d594fSAndroid Build Coastguard Worker Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
3412*795d594fSAndroid Build Coastguard Worker break;
3413*795d594fSAndroid Build Coastguard Worker }
3414*795d594fSAndroid Build Coastguard Worker
3415*795d594fSAndroid Build Coastguard Worker case Instruction::DIV_INT_2ADDR: {
3416*795d594fSAndroid Build Coastguard Worker BuildCheckedDivRem(instruction.VRegA_12x(),
3417*795d594fSAndroid Build Coastguard Worker instruction.VRegA_12x(),
3418*795d594fSAndroid Build Coastguard Worker instruction.VRegB_12x(),
3419*795d594fSAndroid Build Coastguard Worker dex_pc,
3420*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt32,
3421*795d594fSAndroid Build Coastguard Worker /* second_is_constant= */ false,
3422*795d594fSAndroid Build Coastguard Worker /* is_div=*/ true);
3423*795d594fSAndroid Build Coastguard Worker break;
3424*795d594fSAndroid Build Coastguard Worker }
3425*795d594fSAndroid Build Coastguard Worker
3426*795d594fSAndroid Build Coastguard Worker case Instruction::DIV_LONG_2ADDR: {
3427*795d594fSAndroid Build Coastguard Worker BuildCheckedDivRem(instruction.VRegA_12x(),
3428*795d594fSAndroid Build Coastguard Worker instruction.VRegA_12x(),
3429*795d594fSAndroid Build Coastguard Worker instruction.VRegB_12x(),
3430*795d594fSAndroid Build Coastguard Worker dex_pc,
3431*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt64,
3432*795d594fSAndroid Build Coastguard Worker /* second_is_constant= */ false,
3433*795d594fSAndroid Build Coastguard Worker /* is_div=*/ true);
3434*795d594fSAndroid Build Coastguard Worker break;
3435*795d594fSAndroid Build Coastguard Worker }
3436*795d594fSAndroid Build Coastguard Worker
3437*795d594fSAndroid Build Coastguard Worker case Instruction::REM_INT_2ADDR: {
3438*795d594fSAndroid Build Coastguard Worker BuildCheckedDivRem(instruction.VRegA_12x(),
3439*795d594fSAndroid Build Coastguard Worker instruction.VRegA_12x(),
3440*795d594fSAndroid Build Coastguard Worker instruction.VRegB_12x(),
3441*795d594fSAndroid Build Coastguard Worker dex_pc,
3442*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt32,
3443*795d594fSAndroid Build Coastguard Worker /* second_is_constant= */ false,
3444*795d594fSAndroid Build Coastguard Worker /* is_div=*/ false);
3445*795d594fSAndroid Build Coastguard Worker break;
3446*795d594fSAndroid Build Coastguard Worker }
3447*795d594fSAndroid Build Coastguard Worker
3448*795d594fSAndroid Build Coastguard Worker case Instruction::REM_LONG_2ADDR: {
3449*795d594fSAndroid Build Coastguard Worker BuildCheckedDivRem(instruction.VRegA_12x(),
3450*795d594fSAndroid Build Coastguard Worker instruction.VRegA_12x(),
3451*795d594fSAndroid Build Coastguard Worker instruction.VRegB_12x(),
3452*795d594fSAndroid Build Coastguard Worker dex_pc,
3453*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt64,
3454*795d594fSAndroid Build Coastguard Worker /* second_is_constant= */ false,
3455*795d594fSAndroid Build Coastguard Worker /* is_div=*/ false);
3456*795d594fSAndroid Build Coastguard Worker break;
3457*795d594fSAndroid Build Coastguard Worker }
3458*795d594fSAndroid Build Coastguard Worker
3459*795d594fSAndroid Build Coastguard Worker case Instruction::REM_FLOAT_2ADDR: {
3460*795d594fSAndroid Build Coastguard Worker Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
3461*795d594fSAndroid Build Coastguard Worker break;
3462*795d594fSAndroid Build Coastguard Worker }
3463*795d594fSAndroid Build Coastguard Worker
3464*795d594fSAndroid Build Coastguard Worker case Instruction::REM_DOUBLE_2ADDR: {
3465*795d594fSAndroid Build Coastguard Worker Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
3466*795d594fSAndroid Build Coastguard Worker break;
3467*795d594fSAndroid Build Coastguard Worker }
3468*795d594fSAndroid Build Coastguard Worker
3469*795d594fSAndroid Build Coastguard Worker case Instruction::SHL_INT_2ADDR: {
3470*795d594fSAndroid Build Coastguard Worker Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
3471*795d594fSAndroid Build Coastguard Worker break;
3472*795d594fSAndroid Build Coastguard Worker }
3473*795d594fSAndroid Build Coastguard Worker
3474*795d594fSAndroid Build Coastguard Worker case Instruction::SHL_LONG_2ADDR: {
3475*795d594fSAndroid Build Coastguard Worker Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
3476*795d594fSAndroid Build Coastguard Worker break;
3477*795d594fSAndroid Build Coastguard Worker }
3478*795d594fSAndroid Build Coastguard Worker
3479*795d594fSAndroid Build Coastguard Worker case Instruction::SHR_INT_2ADDR: {
3480*795d594fSAndroid Build Coastguard Worker Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
3481*795d594fSAndroid Build Coastguard Worker break;
3482*795d594fSAndroid Build Coastguard Worker }
3483*795d594fSAndroid Build Coastguard Worker
3484*795d594fSAndroid Build Coastguard Worker case Instruction::SHR_LONG_2ADDR: {
3485*795d594fSAndroid Build Coastguard Worker Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
3486*795d594fSAndroid Build Coastguard Worker break;
3487*795d594fSAndroid Build Coastguard Worker }
3488*795d594fSAndroid Build Coastguard Worker
3489*795d594fSAndroid Build Coastguard Worker case Instruction::USHR_INT_2ADDR: {
3490*795d594fSAndroid Build Coastguard Worker Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
3491*795d594fSAndroid Build Coastguard Worker break;
3492*795d594fSAndroid Build Coastguard Worker }
3493*795d594fSAndroid Build Coastguard Worker
3494*795d594fSAndroid Build Coastguard Worker case Instruction::USHR_LONG_2ADDR: {
3495*795d594fSAndroid Build Coastguard Worker Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
3496*795d594fSAndroid Build Coastguard Worker break;
3497*795d594fSAndroid Build Coastguard Worker }
3498*795d594fSAndroid Build Coastguard Worker
3499*795d594fSAndroid Build Coastguard Worker case Instruction::DIV_FLOAT_2ADDR: {
3500*795d594fSAndroid Build Coastguard Worker Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
3501*795d594fSAndroid Build Coastguard Worker break;
3502*795d594fSAndroid Build Coastguard Worker }
3503*795d594fSAndroid Build Coastguard Worker
3504*795d594fSAndroid Build Coastguard Worker case Instruction::DIV_DOUBLE_2ADDR: {
3505*795d594fSAndroid Build Coastguard Worker Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
3506*795d594fSAndroid Build Coastguard Worker break;
3507*795d594fSAndroid Build Coastguard Worker }
3508*795d594fSAndroid Build Coastguard Worker
3509*795d594fSAndroid Build Coastguard Worker case Instruction::AND_INT_2ADDR: {
3510*795d594fSAndroid Build Coastguard Worker Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
3511*795d594fSAndroid Build Coastguard Worker break;
3512*795d594fSAndroid Build Coastguard Worker }
3513*795d594fSAndroid Build Coastguard Worker
3514*795d594fSAndroid Build Coastguard Worker case Instruction::AND_LONG_2ADDR: {
3515*795d594fSAndroid Build Coastguard Worker Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
3516*795d594fSAndroid Build Coastguard Worker break;
3517*795d594fSAndroid Build Coastguard Worker }
3518*795d594fSAndroid Build Coastguard Worker
3519*795d594fSAndroid Build Coastguard Worker case Instruction::OR_INT_2ADDR: {
3520*795d594fSAndroid Build Coastguard Worker Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
3521*795d594fSAndroid Build Coastguard Worker break;
3522*795d594fSAndroid Build Coastguard Worker }
3523*795d594fSAndroid Build Coastguard Worker
3524*795d594fSAndroid Build Coastguard Worker case Instruction::OR_LONG_2ADDR: {
3525*795d594fSAndroid Build Coastguard Worker Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
3526*795d594fSAndroid Build Coastguard Worker break;
3527*795d594fSAndroid Build Coastguard Worker }
3528*795d594fSAndroid Build Coastguard Worker
3529*795d594fSAndroid Build Coastguard Worker case Instruction::XOR_INT_2ADDR: {
3530*795d594fSAndroid Build Coastguard Worker Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
3531*795d594fSAndroid Build Coastguard Worker break;
3532*795d594fSAndroid Build Coastguard Worker }
3533*795d594fSAndroid Build Coastguard Worker
3534*795d594fSAndroid Build Coastguard Worker case Instruction::XOR_LONG_2ADDR: {
3535*795d594fSAndroid Build Coastguard Worker Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
3536*795d594fSAndroid Build Coastguard Worker break;
3537*795d594fSAndroid Build Coastguard Worker }
3538*795d594fSAndroid Build Coastguard Worker
3539*795d594fSAndroid Build Coastguard Worker case Instruction::ADD_INT_LIT16: {
3540*795d594fSAndroid Build Coastguard Worker Binop_22s<HAdd>(instruction, false, dex_pc);
3541*795d594fSAndroid Build Coastguard Worker break;
3542*795d594fSAndroid Build Coastguard Worker }
3543*795d594fSAndroid Build Coastguard Worker
3544*795d594fSAndroid Build Coastguard Worker case Instruction::AND_INT_LIT16: {
3545*795d594fSAndroid Build Coastguard Worker Binop_22s<HAnd>(instruction, false, dex_pc);
3546*795d594fSAndroid Build Coastguard Worker break;
3547*795d594fSAndroid Build Coastguard Worker }
3548*795d594fSAndroid Build Coastguard Worker
3549*795d594fSAndroid Build Coastguard Worker case Instruction::OR_INT_LIT16: {
3550*795d594fSAndroid Build Coastguard Worker Binop_22s<HOr>(instruction, false, dex_pc);
3551*795d594fSAndroid Build Coastguard Worker break;
3552*795d594fSAndroid Build Coastguard Worker }
3553*795d594fSAndroid Build Coastguard Worker
3554*795d594fSAndroid Build Coastguard Worker case Instruction::XOR_INT_LIT16: {
3555*795d594fSAndroid Build Coastguard Worker Binop_22s<HXor>(instruction, false, dex_pc);
3556*795d594fSAndroid Build Coastguard Worker break;
3557*795d594fSAndroid Build Coastguard Worker }
3558*795d594fSAndroid Build Coastguard Worker
3559*795d594fSAndroid Build Coastguard Worker case Instruction::RSUB_INT: {
3560*795d594fSAndroid Build Coastguard Worker Binop_22s<HSub>(instruction, true, dex_pc);
3561*795d594fSAndroid Build Coastguard Worker break;
3562*795d594fSAndroid Build Coastguard Worker }
3563*795d594fSAndroid Build Coastguard Worker
3564*795d594fSAndroid Build Coastguard Worker case Instruction::MUL_INT_LIT16: {
3565*795d594fSAndroid Build Coastguard Worker Binop_22s<HMul>(instruction, false, dex_pc);
3566*795d594fSAndroid Build Coastguard Worker break;
3567*795d594fSAndroid Build Coastguard Worker }
3568*795d594fSAndroid Build Coastguard Worker
3569*795d594fSAndroid Build Coastguard Worker case Instruction::ADD_INT_LIT8: {
3570*795d594fSAndroid Build Coastguard Worker Binop_22b<HAdd>(instruction, false, dex_pc);
3571*795d594fSAndroid Build Coastguard Worker break;
3572*795d594fSAndroid Build Coastguard Worker }
3573*795d594fSAndroid Build Coastguard Worker
3574*795d594fSAndroid Build Coastguard Worker case Instruction::AND_INT_LIT8: {
3575*795d594fSAndroid Build Coastguard Worker Binop_22b<HAnd>(instruction, false, dex_pc);
3576*795d594fSAndroid Build Coastguard Worker break;
3577*795d594fSAndroid Build Coastguard Worker }
3578*795d594fSAndroid Build Coastguard Worker
3579*795d594fSAndroid Build Coastguard Worker case Instruction::OR_INT_LIT8: {
3580*795d594fSAndroid Build Coastguard Worker Binop_22b<HOr>(instruction, false, dex_pc);
3581*795d594fSAndroid Build Coastguard Worker break;
3582*795d594fSAndroid Build Coastguard Worker }
3583*795d594fSAndroid Build Coastguard Worker
3584*795d594fSAndroid Build Coastguard Worker case Instruction::XOR_INT_LIT8: {
3585*795d594fSAndroid Build Coastguard Worker Binop_22b<HXor>(instruction, false, dex_pc);
3586*795d594fSAndroid Build Coastguard Worker break;
3587*795d594fSAndroid Build Coastguard Worker }
3588*795d594fSAndroid Build Coastguard Worker
3589*795d594fSAndroid Build Coastguard Worker case Instruction::RSUB_INT_LIT8: {
3590*795d594fSAndroid Build Coastguard Worker Binop_22b<HSub>(instruction, true, dex_pc);
3591*795d594fSAndroid Build Coastguard Worker break;
3592*795d594fSAndroid Build Coastguard Worker }
3593*795d594fSAndroid Build Coastguard Worker
3594*795d594fSAndroid Build Coastguard Worker case Instruction::MUL_INT_LIT8: {
3595*795d594fSAndroid Build Coastguard Worker Binop_22b<HMul>(instruction, false, dex_pc);
3596*795d594fSAndroid Build Coastguard Worker break;
3597*795d594fSAndroid Build Coastguard Worker }
3598*795d594fSAndroid Build Coastguard Worker
3599*795d594fSAndroid Build Coastguard Worker case Instruction::DIV_INT_LIT16: {
3600*795d594fSAndroid Build Coastguard Worker BuildCheckedDivRem(instruction.VRegA_22s(),
3601*795d594fSAndroid Build Coastguard Worker instruction.VRegB_22s(),
3602*795d594fSAndroid Build Coastguard Worker instruction.VRegC_22s(),
3603*795d594fSAndroid Build Coastguard Worker dex_pc,
3604*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt32,
3605*795d594fSAndroid Build Coastguard Worker /* second_is_constant= */ true,
3606*795d594fSAndroid Build Coastguard Worker /* is_div=*/ true);
3607*795d594fSAndroid Build Coastguard Worker break;
3608*795d594fSAndroid Build Coastguard Worker }
3609*795d594fSAndroid Build Coastguard Worker
3610*795d594fSAndroid Build Coastguard Worker case Instruction::DIV_INT_LIT8: {
3611*795d594fSAndroid Build Coastguard Worker BuildCheckedDivRem(instruction.VRegA_22b(),
3612*795d594fSAndroid Build Coastguard Worker instruction.VRegB_22b(),
3613*795d594fSAndroid Build Coastguard Worker instruction.VRegC_22b(),
3614*795d594fSAndroid Build Coastguard Worker dex_pc,
3615*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt32,
3616*795d594fSAndroid Build Coastguard Worker /* second_is_constant= */ true,
3617*795d594fSAndroid Build Coastguard Worker /* is_div=*/ true);
3618*795d594fSAndroid Build Coastguard Worker break;
3619*795d594fSAndroid Build Coastguard Worker }
3620*795d594fSAndroid Build Coastguard Worker
3621*795d594fSAndroid Build Coastguard Worker case Instruction::REM_INT_LIT16: {
3622*795d594fSAndroid Build Coastguard Worker BuildCheckedDivRem(instruction.VRegA_22s(),
3623*795d594fSAndroid Build Coastguard Worker instruction.VRegB_22s(),
3624*795d594fSAndroid Build Coastguard Worker instruction.VRegC_22s(),
3625*795d594fSAndroid Build Coastguard Worker dex_pc,
3626*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt32,
3627*795d594fSAndroid Build Coastguard Worker /* second_is_constant= */ true,
3628*795d594fSAndroid Build Coastguard Worker /* is_div=*/ false);
3629*795d594fSAndroid Build Coastguard Worker break;
3630*795d594fSAndroid Build Coastguard Worker }
3631*795d594fSAndroid Build Coastguard Worker
3632*795d594fSAndroid Build Coastguard Worker case Instruction::REM_INT_LIT8: {
3633*795d594fSAndroid Build Coastguard Worker BuildCheckedDivRem(instruction.VRegA_22b(),
3634*795d594fSAndroid Build Coastguard Worker instruction.VRegB_22b(),
3635*795d594fSAndroid Build Coastguard Worker instruction.VRegC_22b(),
3636*795d594fSAndroid Build Coastguard Worker dex_pc,
3637*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt32,
3638*795d594fSAndroid Build Coastguard Worker /* second_is_constant= */ true,
3639*795d594fSAndroid Build Coastguard Worker /* is_div=*/ false);
3640*795d594fSAndroid Build Coastguard Worker break;
3641*795d594fSAndroid Build Coastguard Worker }
3642*795d594fSAndroid Build Coastguard Worker
3643*795d594fSAndroid Build Coastguard Worker case Instruction::SHL_INT_LIT8: {
3644*795d594fSAndroid Build Coastguard Worker Binop_22b<HShl>(instruction, false, dex_pc);
3645*795d594fSAndroid Build Coastguard Worker break;
3646*795d594fSAndroid Build Coastguard Worker }
3647*795d594fSAndroid Build Coastguard Worker
3648*795d594fSAndroid Build Coastguard Worker case Instruction::SHR_INT_LIT8: {
3649*795d594fSAndroid Build Coastguard Worker Binop_22b<HShr>(instruction, false, dex_pc);
3650*795d594fSAndroid Build Coastguard Worker break;
3651*795d594fSAndroid Build Coastguard Worker }
3652*795d594fSAndroid Build Coastguard Worker
3653*795d594fSAndroid Build Coastguard Worker case Instruction::USHR_INT_LIT8: {
3654*795d594fSAndroid Build Coastguard Worker Binop_22b<HUShr>(instruction, false, dex_pc);
3655*795d594fSAndroid Build Coastguard Worker break;
3656*795d594fSAndroid Build Coastguard Worker }
3657*795d594fSAndroid Build Coastguard Worker
3658*795d594fSAndroid Build Coastguard Worker case Instruction::NEW_INSTANCE: {
3659*795d594fSAndroid Build Coastguard Worker HNewInstance* new_instance =
3660*795d594fSAndroid Build Coastguard Worker BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
3661*795d594fSAndroid Build Coastguard Worker DCHECK(new_instance != nullptr);
3662*795d594fSAndroid Build Coastguard Worker
3663*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3664*795d594fSAndroid Build Coastguard Worker BuildConstructorFenceForAllocation(new_instance);
3665*795d594fSAndroid Build Coastguard Worker break;
3666*795d594fSAndroid Build Coastguard Worker }
3667*795d594fSAndroid Build Coastguard Worker
3668*795d594fSAndroid Build Coastguard Worker case Instruction::NEW_ARRAY: {
3669*795d594fSAndroid Build Coastguard Worker dex::TypeIndex type_index(instruction.VRegC_22c());
3670*795d594fSAndroid Build Coastguard Worker HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
3671*795d594fSAndroid Build Coastguard Worker HNewArray* new_array = BuildNewArray(dex_pc, type_index, length);
3672*795d594fSAndroid Build Coastguard Worker
3673*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
3674*795d594fSAndroid Build Coastguard Worker BuildConstructorFenceForAllocation(new_array);
3675*795d594fSAndroid Build Coastguard Worker break;
3676*795d594fSAndroid Build Coastguard Worker }
3677*795d594fSAndroid Build Coastguard Worker
3678*795d594fSAndroid Build Coastguard Worker case Instruction::FILLED_NEW_ARRAY: {
3679*795d594fSAndroid Build Coastguard Worker dex::TypeIndex type_index(instruction.VRegB_35c());
3680*795d594fSAndroid Build Coastguard Worker uint32_t args[5];
3681*795d594fSAndroid Build Coastguard Worker uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
3682*795d594fSAndroid Build Coastguard Worker VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
3683*795d594fSAndroid Build Coastguard Worker HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
3684*795d594fSAndroid Build Coastguard Worker BuildConstructorFenceForAllocation(new_array);
3685*795d594fSAndroid Build Coastguard Worker break;
3686*795d594fSAndroid Build Coastguard Worker }
3687*795d594fSAndroid Build Coastguard Worker
3688*795d594fSAndroid Build Coastguard Worker case Instruction::FILLED_NEW_ARRAY_RANGE: {
3689*795d594fSAndroid Build Coastguard Worker dex::TypeIndex type_index(instruction.VRegB_3rc());
3690*795d594fSAndroid Build Coastguard Worker RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
3691*795d594fSAndroid Build Coastguard Worker HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
3692*795d594fSAndroid Build Coastguard Worker BuildConstructorFenceForAllocation(new_array);
3693*795d594fSAndroid Build Coastguard Worker break;
3694*795d594fSAndroid Build Coastguard Worker }
3695*795d594fSAndroid Build Coastguard Worker
3696*795d594fSAndroid Build Coastguard Worker case Instruction::FILL_ARRAY_DATA: {
3697*795d594fSAndroid Build Coastguard Worker BuildFillArrayData(instruction, dex_pc);
3698*795d594fSAndroid Build Coastguard Worker break;
3699*795d594fSAndroid Build Coastguard Worker }
3700*795d594fSAndroid Build Coastguard Worker
3701*795d594fSAndroid Build Coastguard Worker case Instruction::MOVE_RESULT:
3702*795d594fSAndroid Build Coastguard Worker case Instruction::MOVE_RESULT_WIDE:
3703*795d594fSAndroid Build Coastguard Worker case Instruction::MOVE_RESULT_OBJECT: {
3704*795d594fSAndroid Build Coastguard Worker DCHECK(latest_result_ != nullptr);
3705*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_11x(), latest_result_);
3706*795d594fSAndroid Build Coastguard Worker latest_result_ = nullptr;
3707*795d594fSAndroid Build Coastguard Worker break;
3708*795d594fSAndroid Build Coastguard Worker }
3709*795d594fSAndroid Build Coastguard Worker
3710*795d594fSAndroid Build Coastguard Worker case Instruction::CMP_LONG: {
3711*795d594fSAndroid Build Coastguard Worker Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
3712*795d594fSAndroid Build Coastguard Worker break;
3713*795d594fSAndroid Build Coastguard Worker }
3714*795d594fSAndroid Build Coastguard Worker
3715*795d594fSAndroid Build Coastguard Worker case Instruction::CMPG_FLOAT: {
3716*795d594fSAndroid Build Coastguard Worker Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
3717*795d594fSAndroid Build Coastguard Worker break;
3718*795d594fSAndroid Build Coastguard Worker }
3719*795d594fSAndroid Build Coastguard Worker
3720*795d594fSAndroid Build Coastguard Worker case Instruction::CMPG_DOUBLE: {
3721*795d594fSAndroid Build Coastguard Worker Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
3722*795d594fSAndroid Build Coastguard Worker break;
3723*795d594fSAndroid Build Coastguard Worker }
3724*795d594fSAndroid Build Coastguard Worker
3725*795d594fSAndroid Build Coastguard Worker case Instruction::CMPL_FLOAT: {
3726*795d594fSAndroid Build Coastguard Worker Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
3727*795d594fSAndroid Build Coastguard Worker break;
3728*795d594fSAndroid Build Coastguard Worker }
3729*795d594fSAndroid Build Coastguard Worker
3730*795d594fSAndroid Build Coastguard Worker case Instruction::CMPL_DOUBLE: {
3731*795d594fSAndroid Build Coastguard Worker Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
3732*795d594fSAndroid Build Coastguard Worker break;
3733*795d594fSAndroid Build Coastguard Worker }
3734*795d594fSAndroid Build Coastguard Worker
3735*795d594fSAndroid Build Coastguard Worker case Instruction::NOP:
3736*795d594fSAndroid Build Coastguard Worker break;
3737*795d594fSAndroid Build Coastguard Worker
3738*795d594fSAndroid Build Coastguard Worker case Instruction::IGET:
3739*795d594fSAndroid Build Coastguard Worker case Instruction::IGET_WIDE:
3740*795d594fSAndroid Build Coastguard Worker case Instruction::IGET_OBJECT:
3741*795d594fSAndroid Build Coastguard Worker case Instruction::IGET_BOOLEAN:
3742*795d594fSAndroid Build Coastguard Worker case Instruction::IGET_BYTE:
3743*795d594fSAndroid Build Coastguard Worker case Instruction::IGET_CHAR:
3744*795d594fSAndroid Build Coastguard Worker case Instruction::IGET_SHORT: {
3745*795d594fSAndroid Build Coastguard Worker if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put= */ false)) {
3746*795d594fSAndroid Build Coastguard Worker return false;
3747*795d594fSAndroid Build Coastguard Worker }
3748*795d594fSAndroid Build Coastguard Worker break;
3749*795d594fSAndroid Build Coastguard Worker }
3750*795d594fSAndroid Build Coastguard Worker
3751*795d594fSAndroid Build Coastguard Worker case Instruction::IPUT:
3752*795d594fSAndroid Build Coastguard Worker case Instruction::IPUT_WIDE:
3753*795d594fSAndroid Build Coastguard Worker case Instruction::IPUT_OBJECT:
3754*795d594fSAndroid Build Coastguard Worker case Instruction::IPUT_BOOLEAN:
3755*795d594fSAndroid Build Coastguard Worker case Instruction::IPUT_BYTE:
3756*795d594fSAndroid Build Coastguard Worker case Instruction::IPUT_CHAR:
3757*795d594fSAndroid Build Coastguard Worker case Instruction::IPUT_SHORT: {
3758*795d594fSAndroid Build Coastguard Worker if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put= */ true)) {
3759*795d594fSAndroid Build Coastguard Worker return false;
3760*795d594fSAndroid Build Coastguard Worker }
3761*795d594fSAndroid Build Coastguard Worker break;
3762*795d594fSAndroid Build Coastguard Worker }
3763*795d594fSAndroid Build Coastguard Worker
3764*795d594fSAndroid Build Coastguard Worker case Instruction::SGET:
3765*795d594fSAndroid Build Coastguard Worker case Instruction::SGET_WIDE:
3766*795d594fSAndroid Build Coastguard Worker case Instruction::SGET_OBJECT:
3767*795d594fSAndroid Build Coastguard Worker case Instruction::SGET_BOOLEAN:
3768*795d594fSAndroid Build Coastguard Worker case Instruction::SGET_BYTE:
3769*795d594fSAndroid Build Coastguard Worker case Instruction::SGET_CHAR:
3770*795d594fSAndroid Build Coastguard Worker case Instruction::SGET_SHORT: {
3771*795d594fSAndroid Build Coastguard Worker BuildStaticFieldAccess(instruction, dex_pc, /* is_put= */ false);
3772*795d594fSAndroid Build Coastguard Worker break;
3773*795d594fSAndroid Build Coastguard Worker }
3774*795d594fSAndroid Build Coastguard Worker
3775*795d594fSAndroid Build Coastguard Worker case Instruction::SPUT:
3776*795d594fSAndroid Build Coastguard Worker case Instruction::SPUT_WIDE:
3777*795d594fSAndroid Build Coastguard Worker case Instruction::SPUT_OBJECT:
3778*795d594fSAndroid Build Coastguard Worker case Instruction::SPUT_BOOLEAN:
3779*795d594fSAndroid Build Coastguard Worker case Instruction::SPUT_BYTE:
3780*795d594fSAndroid Build Coastguard Worker case Instruction::SPUT_CHAR:
3781*795d594fSAndroid Build Coastguard Worker case Instruction::SPUT_SHORT: {
3782*795d594fSAndroid Build Coastguard Worker BuildStaticFieldAccess(instruction, dex_pc, /* is_put= */ true);
3783*795d594fSAndroid Build Coastguard Worker break;
3784*795d594fSAndroid Build Coastguard Worker }
3785*795d594fSAndroid Build Coastguard Worker
3786*795d594fSAndroid Build Coastguard Worker #define ARRAY_XX(kind, anticipated_type) \
3787*795d594fSAndroid Build Coastguard Worker case Instruction::AGET##kind: { \
3788*795d594fSAndroid Build Coastguard Worker BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
3789*795d594fSAndroid Build Coastguard Worker break; \
3790*795d594fSAndroid Build Coastguard Worker } \
3791*795d594fSAndroid Build Coastguard Worker case Instruction::APUT##kind: { \
3792*795d594fSAndroid Build Coastguard Worker BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
3793*795d594fSAndroid Build Coastguard Worker break; \
3794*795d594fSAndroid Build Coastguard Worker }
3795*795d594fSAndroid Build Coastguard Worker
3796*795d594fSAndroid Build Coastguard Worker ARRAY_XX(, DataType::Type::kInt32);
3797*795d594fSAndroid Build Coastguard Worker ARRAY_XX(_WIDE, DataType::Type::kInt64);
3798*795d594fSAndroid Build Coastguard Worker ARRAY_XX(_OBJECT, DataType::Type::kReference);
3799*795d594fSAndroid Build Coastguard Worker ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
3800*795d594fSAndroid Build Coastguard Worker ARRAY_XX(_BYTE, DataType::Type::kInt8);
3801*795d594fSAndroid Build Coastguard Worker ARRAY_XX(_CHAR, DataType::Type::kUint16);
3802*795d594fSAndroid Build Coastguard Worker ARRAY_XX(_SHORT, DataType::Type::kInt16);
3803*795d594fSAndroid Build Coastguard Worker
3804*795d594fSAndroid Build Coastguard Worker case Instruction::ARRAY_LENGTH: {
3805*795d594fSAndroid Build Coastguard Worker HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
3806*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
3807*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
3808*795d594fSAndroid Build Coastguard Worker break;
3809*795d594fSAndroid Build Coastguard Worker }
3810*795d594fSAndroid Build Coastguard Worker
3811*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_STRING: {
3812*795d594fSAndroid Build Coastguard Worker dex::StringIndex string_index(instruction.VRegB_21c());
3813*795d594fSAndroid Build Coastguard Worker BuildLoadString(string_index, dex_pc);
3814*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3815*795d594fSAndroid Build Coastguard Worker break;
3816*795d594fSAndroid Build Coastguard Worker }
3817*795d594fSAndroid Build Coastguard Worker
3818*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_STRING_JUMBO: {
3819*795d594fSAndroid Build Coastguard Worker dex::StringIndex string_index(instruction.VRegB_31c());
3820*795d594fSAndroid Build Coastguard Worker BuildLoadString(string_index, dex_pc);
3821*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
3822*795d594fSAndroid Build Coastguard Worker break;
3823*795d594fSAndroid Build Coastguard Worker }
3824*795d594fSAndroid Build Coastguard Worker
3825*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_CLASS: {
3826*795d594fSAndroid Build Coastguard Worker dex::TypeIndex type_index(instruction.VRegB_21c());
3827*795d594fSAndroid Build Coastguard Worker BuildLoadClass(type_index, dex_pc);
3828*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3829*795d594fSAndroid Build Coastguard Worker break;
3830*795d594fSAndroid Build Coastguard Worker }
3831*795d594fSAndroid Build Coastguard Worker
3832*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_METHOD_HANDLE: {
3833*795d594fSAndroid Build Coastguard Worker uint16_t method_handle_idx = instruction.VRegB_21c();
3834*795d594fSAndroid Build Coastguard Worker BuildLoadMethodHandle(method_handle_idx, dex_pc);
3835*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3836*795d594fSAndroid Build Coastguard Worker break;
3837*795d594fSAndroid Build Coastguard Worker }
3838*795d594fSAndroid Build Coastguard Worker
3839*795d594fSAndroid Build Coastguard Worker case Instruction::CONST_METHOD_TYPE: {
3840*795d594fSAndroid Build Coastguard Worker dex::ProtoIndex proto_idx(instruction.VRegB_21c());
3841*795d594fSAndroid Build Coastguard Worker BuildLoadMethodType(proto_idx, dex_pc);
3842*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3843*795d594fSAndroid Build Coastguard Worker break;
3844*795d594fSAndroid Build Coastguard Worker }
3845*795d594fSAndroid Build Coastguard Worker
3846*795d594fSAndroid Build Coastguard Worker case Instruction::MOVE_EXCEPTION: {
3847*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HLoadException(dex_pc));
3848*795d594fSAndroid Build Coastguard Worker UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
3849*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HClearException(dex_pc));
3850*795d594fSAndroid Build Coastguard Worker break;
3851*795d594fSAndroid Build Coastguard Worker }
3852*795d594fSAndroid Build Coastguard Worker
3853*795d594fSAndroid Build Coastguard Worker case Instruction::THROW: {
3854*795d594fSAndroid Build Coastguard Worker HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
3855*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
3856*795d594fSAndroid Build Coastguard Worker // We finished building this block. Set the current block to null to avoid
3857*795d594fSAndroid Build Coastguard Worker // adding dead instructions to it.
3858*795d594fSAndroid Build Coastguard Worker current_block_ = nullptr;
3859*795d594fSAndroid Build Coastguard Worker break;
3860*795d594fSAndroid Build Coastguard Worker }
3861*795d594fSAndroid Build Coastguard Worker
3862*795d594fSAndroid Build Coastguard Worker case Instruction::INSTANCE_OF: {
3863*795d594fSAndroid Build Coastguard Worker uint8_t destination = instruction.VRegA_22c();
3864*795d594fSAndroid Build Coastguard Worker uint8_t reference = instruction.VRegB_22c();
3865*795d594fSAndroid Build Coastguard Worker dex::TypeIndex type_index(instruction.VRegC_22c());
3866*795d594fSAndroid Build Coastguard Worker BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
3867*795d594fSAndroid Build Coastguard Worker break;
3868*795d594fSAndroid Build Coastguard Worker }
3869*795d594fSAndroid Build Coastguard Worker
3870*795d594fSAndroid Build Coastguard Worker case Instruction::CHECK_CAST: {
3871*795d594fSAndroid Build Coastguard Worker uint8_t reference = instruction.VRegA_21c();
3872*795d594fSAndroid Build Coastguard Worker dex::TypeIndex type_index(instruction.VRegB_21c());
3873*795d594fSAndroid Build Coastguard Worker BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
3874*795d594fSAndroid Build Coastguard Worker break;
3875*795d594fSAndroid Build Coastguard Worker }
3876*795d594fSAndroid Build Coastguard Worker
3877*795d594fSAndroid Build Coastguard Worker case Instruction::MONITOR_ENTER: {
3878*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HMonitorOperation(
3879*795d594fSAndroid Build Coastguard Worker LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
3880*795d594fSAndroid Build Coastguard Worker HMonitorOperation::OperationKind::kEnter,
3881*795d594fSAndroid Build Coastguard Worker dex_pc));
3882*795d594fSAndroid Build Coastguard Worker graph_->SetHasMonitorOperations(true);
3883*795d594fSAndroid Build Coastguard Worker break;
3884*795d594fSAndroid Build Coastguard Worker }
3885*795d594fSAndroid Build Coastguard Worker
3886*795d594fSAndroid Build Coastguard Worker case Instruction::MONITOR_EXIT: {
3887*795d594fSAndroid Build Coastguard Worker AppendInstruction(new (allocator_) HMonitorOperation(
3888*795d594fSAndroid Build Coastguard Worker LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
3889*795d594fSAndroid Build Coastguard Worker HMonitorOperation::OperationKind::kExit,
3890*795d594fSAndroid Build Coastguard Worker dex_pc));
3891*795d594fSAndroid Build Coastguard Worker graph_->SetHasMonitorOperations(true);
3892*795d594fSAndroid Build Coastguard Worker break;
3893*795d594fSAndroid Build Coastguard Worker }
3894*795d594fSAndroid Build Coastguard Worker
3895*795d594fSAndroid Build Coastguard Worker case Instruction::SPARSE_SWITCH:
3896*795d594fSAndroid Build Coastguard Worker case Instruction::PACKED_SWITCH: {
3897*795d594fSAndroid Build Coastguard Worker BuildSwitch(instruction, dex_pc);
3898*795d594fSAndroid Build Coastguard Worker break;
3899*795d594fSAndroid Build Coastguard Worker }
3900*795d594fSAndroid Build Coastguard Worker
3901*795d594fSAndroid Build Coastguard Worker case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
3902*795d594fSAndroid Build Coastguard Worker case Instruction::UNUSED_73:
3903*795d594fSAndroid Build Coastguard Worker case Instruction::UNUSED_79:
3904*795d594fSAndroid Build Coastguard Worker case Instruction::UNUSED_7A:
3905*795d594fSAndroid Build Coastguard Worker case Instruction::UNUSED_E3 ... Instruction::UNUSED_F9: {
3906*795d594fSAndroid Build Coastguard Worker VLOG(compiler) << "Did not compile "
3907*795d594fSAndroid Build Coastguard Worker << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
3908*795d594fSAndroid Build Coastguard Worker << " because of unhandled instruction "
3909*795d594fSAndroid Build Coastguard Worker << instruction.Name();
3910*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(compilation_stats_,
3911*795d594fSAndroid Build Coastguard Worker MethodCompilationStat::kNotCompiledUnhandledInstruction);
3912*795d594fSAndroid Build Coastguard Worker return false;
3913*795d594fSAndroid Build Coastguard Worker }
3914*795d594fSAndroid Build Coastguard Worker }
3915*795d594fSAndroid Build Coastguard Worker return true;
3916*795d594fSAndroid Build Coastguard Worker } // NOLINT(readability/fn_size)
3917*795d594fSAndroid Build Coastguard Worker
LookupResolvedType(dex::TypeIndex type_index,const DexCompilationUnit & compilation_unit) const3918*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
3919*795d594fSAndroid Build Coastguard Worker dex::TypeIndex type_index,
3920*795d594fSAndroid Build Coastguard Worker const DexCompilationUnit& compilation_unit) const {
3921*795d594fSAndroid Build Coastguard Worker return compilation_unit.GetClassLinker()->LookupResolvedType(
3922*795d594fSAndroid Build Coastguard Worker type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
3923*795d594fSAndroid Build Coastguard Worker }
3924*795d594fSAndroid Build Coastguard Worker
LookupReferrerClass() const3925*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
3926*795d594fSAndroid Build Coastguard Worker // TODO: Cache the result in a Handle<mirror::Class>.
3927*795d594fSAndroid Build Coastguard Worker const dex::MethodId& method_id =
3928*795d594fSAndroid Build Coastguard Worker dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
3929*795d594fSAndroid Build Coastguard Worker return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
3930*795d594fSAndroid Build Coastguard Worker }
3931*795d594fSAndroid Build Coastguard Worker
3932*795d594fSAndroid Build Coastguard Worker } // namespace art
3933