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