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 #ifndef ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_ 18 #define ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_ 19 20 #include "base/arena_containers.h" 21 #include "base/arena_object.h" 22 #include "block_builder.h" 23 #include "dex_file_types.h" 24 #include "driver/compiler_driver.h" 25 #include "driver/compiler_driver-inl.h" 26 #include "driver/dex_compilation_unit.h" 27 #include "mirror/dex_cache.h" 28 #include "nodes.h" 29 #include "optimizing_compiler_stats.h" 30 #include "ssa_builder.h" 31 32 namespace art { 33 34 class CodeGenerator; 35 class Instruction; 36 37 class HInstructionBuilder : public ValueObject { 38 public: HInstructionBuilder(HGraph * graph,HBasicBlockBuilder * block_builder,SsaBuilder * ssa_builder,const DexFile * dex_file,const DexFile::CodeItem & code_item,Primitive::Type return_type,DexCompilationUnit * dex_compilation_unit,const DexCompilationUnit * const outer_compilation_unit,CompilerDriver * driver,CodeGenerator * code_generator,const uint8_t * interpreter_metadata,OptimizingCompilerStats * compiler_stats,Handle<mirror::DexCache> dex_cache,VariableSizedHandleScope * handles)39 HInstructionBuilder(HGraph* graph, 40 HBasicBlockBuilder* block_builder, 41 SsaBuilder* ssa_builder, 42 const DexFile* dex_file, 43 const DexFile::CodeItem& code_item, 44 Primitive::Type return_type, 45 DexCompilationUnit* dex_compilation_unit, 46 const DexCompilationUnit* const outer_compilation_unit, 47 CompilerDriver* driver, 48 CodeGenerator* code_generator, 49 const uint8_t* interpreter_metadata, 50 OptimizingCompilerStats* compiler_stats, 51 Handle<mirror::DexCache> dex_cache, 52 VariableSizedHandleScope* handles) 53 : arena_(graph->GetArena()), 54 graph_(graph), 55 handles_(handles), 56 dex_file_(dex_file), 57 code_item_(code_item), 58 return_type_(return_type), 59 block_builder_(block_builder), 60 ssa_builder_(ssa_builder), 61 locals_for_(arena_->Adapter(kArenaAllocGraphBuilder)), 62 current_block_(nullptr), 63 current_locals_(nullptr), 64 latest_result_(nullptr), 65 compiler_driver_(driver), 66 code_generator_(code_generator), 67 dex_compilation_unit_(dex_compilation_unit), 68 outer_compilation_unit_(outer_compilation_unit), 69 interpreter_metadata_(interpreter_metadata), 70 skipped_interpreter_metadata_(std::less<uint32_t>(), 71 arena_->Adapter(kArenaAllocGraphBuilder)), 72 compilation_stats_(compiler_stats), 73 dex_cache_(dex_cache), 74 loop_headers_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)) { 75 loop_headers_.reserve(kDefaultNumberOfLoops); 76 } 77 78 bool Build(); 79 80 private: 81 void MaybeRecordStat(MethodCompilationStat compilation_stat); 82 83 void InitializeBlockLocals(); 84 void PropagateLocalsToCatchBlocks(); 85 void SetLoopHeaderPhiInputs(); 86 87 bool ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc); 88 void FindNativeDebugInfoLocations(ArenaBitVector* locations); 89 90 bool CanDecodeQuickenedInfo() const; 91 uint16_t LookupQuickenedInfo(uint32_t dex_pc); 92 93 HBasicBlock* FindBlockStartingAt(uint32_t dex_pc) const; 94 95 ArenaVector<HInstruction*>* GetLocalsFor(HBasicBlock* block); 96 // Out of line version of GetLocalsFor(), which has a fast path that is 97 // beneficial to get inlined by callers. 98 ArenaVector<HInstruction*>* GetLocalsForWithAllocation( 99 HBasicBlock* block, ArenaVector<HInstruction*>* locals, const size_t vregs); 100 HInstruction* ValueOfLocalAt(HBasicBlock* block, size_t local); 101 HInstruction* LoadLocal(uint32_t register_index, Primitive::Type type) const; 102 HInstruction* LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc); 103 void UpdateLocal(uint32_t register_index, HInstruction* instruction); 104 105 void AppendInstruction(HInstruction* instruction); 106 void InsertInstructionAtTop(HInstruction* instruction); 107 void InitializeInstruction(HInstruction* instruction); 108 109 void InitializeParameters(); 110 111 // Returns whether the current method needs access check for the type. 112 // Output parameter finalizable is set to whether the type is finalizable. 113 bool NeedsAccessCheck(dex::TypeIndex type_index, /*out*/bool* finalizable) const 114 REQUIRES_SHARED(Locks::mutator_lock_); 115 116 template<typename T> 117 void Unop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); 118 119 template<typename T> 120 void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); 121 122 template<typename T> 123 void Binop_23x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); 124 125 void Binop_23x_cmp(const Instruction& instruction, 126 Primitive::Type type, 127 ComparisonBias bias, 128 uint32_t dex_pc); 129 130 template<typename T> 131 void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); 132 133 template<typename T> 134 void Binop_12x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); 135 136 template<typename T> 137 void Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc); 138 139 template<typename T> 140 void Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc); 141 142 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc); 143 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc); 144 145 void Conversion_12x(const Instruction& instruction, 146 Primitive::Type input_type, 147 Primitive::Type result_type, 148 uint32_t dex_pc); 149 150 void BuildCheckedDivRem(uint16_t out_reg, 151 uint16_t first_reg, 152 int64_t second_reg_or_constant, 153 uint32_t dex_pc, 154 Primitive::Type type, 155 bool second_is_lit, 156 bool is_div); 157 158 void BuildReturn(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); 159 160 // Builds an instance field access node and returns whether the instruction is supported. 161 bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put); 162 163 void BuildUnresolvedStaticFieldAccess(const Instruction& instruction, 164 uint32_t dex_pc, 165 bool is_put, 166 Primitive::Type field_type); 167 // Builds a static field access node and returns whether the instruction is supported. 168 bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put); 169 170 void BuildArrayAccess(const Instruction& instruction, 171 uint32_t dex_pc, 172 bool is_get, 173 Primitive::Type anticipated_type); 174 175 // Builds an invocation node and returns whether the instruction is supported. 176 bool BuildInvoke(const Instruction& instruction, 177 uint32_t dex_pc, 178 uint32_t method_idx, 179 uint32_t number_of_vreg_arguments, 180 bool is_range, 181 uint32_t* args, 182 uint32_t register_index); 183 184 // Builds an invocation node for invoke-polymorphic and returns whether the 185 // instruction is supported. 186 bool BuildInvokePolymorphic(const Instruction& instruction, 187 uint32_t dex_pc, 188 uint32_t method_idx, 189 uint32_t proto_idx, 190 uint32_t number_of_vreg_arguments, 191 bool is_range, 192 uint32_t* args, 193 uint32_t register_index); 194 195 // Builds a new array node and the instructions that fill it. 196 void BuildFilledNewArray(uint32_t dex_pc, 197 dex::TypeIndex type_index, 198 uint32_t number_of_vreg_arguments, 199 bool is_range, 200 uint32_t* args, 201 uint32_t register_index); 202 203 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc); 204 205 // Fills the given object with data as specified in the fill-array-data 206 // instruction. Currently only used for non-reference and non-floating point 207 // arrays. 208 template <typename T> 209 void BuildFillArrayData(HInstruction* object, 210 const T* data, 211 uint32_t element_count, 212 Primitive::Type anticipated_type, 213 uint32_t dex_pc); 214 215 // Fills the given object with data as specified in the fill-array-data 216 // instruction. The data must be for long and double arrays. 217 void BuildFillWideArrayData(HInstruction* object, 218 const int64_t* data, 219 uint32_t element_count, 220 uint32_t dex_pc); 221 222 // Builds a `HInstanceOf`, or a `HCheckCast` instruction. 223 void BuildTypeCheck(const Instruction& instruction, 224 uint8_t destination, 225 uint8_t reference, 226 dex::TypeIndex type_index, 227 uint32_t dex_pc); 228 229 // Builds an instruction sequence for a switch statement. 230 void BuildSwitch(const Instruction& instruction, uint32_t dex_pc); 231 232 // Builds a `HLoadClass` loading the given `type_index`. If `outer` is true, 233 // this method will use the outer class's dex file to lookup the type at 234 // `type_index`. 235 HLoadClass* BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc); 236 237 HLoadClass* BuildLoadClass(dex::TypeIndex type_index, 238 const DexFile& dex_file, 239 Handle<mirror::Class> klass, 240 uint32_t dex_pc, 241 bool needs_access_check) 242 REQUIRES_SHARED(Locks::mutator_lock_); 243 244 // Returns the outer-most compiling method's class. 245 mirror::Class* GetOutermostCompilingClass() const; 246 247 // Returns the class whose method is being compiled. 248 mirror::Class* GetCompilingClass() const; 249 250 // Returns whether `type_index` points to the outer-most compiling method's class. 251 bool IsOutermostCompilingClass(dex::TypeIndex type_index) const; 252 253 void PotentiallySimplifyFakeString(uint16_t original_dex_register, 254 uint32_t dex_pc, 255 HInvoke* invoke); 256 257 bool SetupInvokeArguments(HInvoke* invoke, 258 uint32_t number_of_vreg_arguments, 259 uint32_t* args, 260 uint32_t register_index, 261 bool is_range, 262 const char* descriptor, 263 size_t start_index, 264 size_t* argument_index); 265 266 bool HandleInvoke(HInvoke* invoke, 267 uint32_t number_of_vreg_arguments, 268 uint32_t* args, 269 uint32_t register_index, 270 bool is_range, 271 const char* descriptor, 272 HClinitCheck* clinit_check, 273 bool is_unresolved); 274 275 bool HandleStringInit(HInvoke* invoke, 276 uint32_t number_of_vreg_arguments, 277 uint32_t* args, 278 uint32_t register_index, 279 bool is_range, 280 const char* descriptor); 281 void HandleStringInitResult(HInvokeStaticOrDirect* invoke); 282 283 HClinitCheck* ProcessClinitCheckForInvoke( 284 uint32_t dex_pc, 285 ArtMethod* method, 286 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) 287 REQUIRES_SHARED(Locks::mutator_lock_); 288 289 // Build a HNewInstance instruction. 290 bool BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc); 291 292 // Return whether the compiler can assume `cls` is initialized. 293 bool IsInitialized(Handle<mirror::Class> cls) const 294 REQUIRES_SHARED(Locks::mutator_lock_); 295 296 // Try to resolve a method using the class linker. Return null if a method could 297 // not be resolved. 298 ArtMethod* ResolveMethod(uint16_t method_idx, InvokeType invoke_type); 299 300 // Try to resolve a field using the class linker. Return null if it could not 301 // be found. 302 ArtField* ResolveField(uint16_t field_idx, bool is_static, bool is_put); 303 304 ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_index, 305 const DexCompilationUnit& compilation_unit) const 306 REQUIRES_SHARED(Locks::mutator_lock_); 307 308 ObjPtr<mirror::Class> LookupReferrerClass() const REQUIRES_SHARED(Locks::mutator_lock_); 309 310 ArenaAllocator* const arena_; 311 HGraph* const graph_; 312 VariableSizedHandleScope* handles_; 313 314 // The dex file where the method being compiled is, and the bytecode data. 315 const DexFile* const dex_file_; 316 const DexFile::CodeItem& code_item_; 317 318 // The return type of the method being compiled. 319 const Primitive::Type return_type_; 320 321 HBasicBlockBuilder* block_builder_; 322 SsaBuilder* ssa_builder_; 323 324 ArenaVector<ArenaVector<HInstruction*>> locals_for_; 325 HBasicBlock* current_block_; 326 ArenaVector<HInstruction*>* current_locals_; 327 HInstruction* latest_result_; 328 329 CompilerDriver* const compiler_driver_; 330 331 CodeGenerator* const code_generator_; 332 333 // The compilation unit of the current method being compiled. Note that 334 // it can be an inlined method. 335 DexCompilationUnit* const dex_compilation_unit_; 336 337 // The compilation unit of the outermost method being compiled. That is the 338 // method being compiled (and not inlined), and potentially inlining other 339 // methods. 340 const DexCompilationUnit* const outer_compilation_unit_; 341 342 // Original values kept after instruction quickening. This is a data buffer 343 // of Leb128-encoded (dex_pc, value) pairs sorted by dex_pc. 344 const uint8_t* interpreter_metadata_; 345 346 // InstructionBuilder does not parse instructions in dex_pc order. Quickening 347 // info for out-of-order dex_pcs is stored in a map until the positions 348 // are eventually visited. 349 ArenaSafeMap<uint32_t, uint16_t> skipped_interpreter_metadata_; 350 351 OptimizingCompilerStats* compilation_stats_; 352 Handle<mirror::DexCache> dex_cache_; 353 354 ArenaVector<HBasicBlock*> loop_headers_; 355 356 static constexpr int kDefaultNumberOfLoops = 2; 357 358 DISALLOW_COPY_AND_ASSIGN(HInstructionBuilder); 359 }; 360 361 } // namespace art 362 363 #endif // ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_ 364