David Brazdil | e3ff7b2 | 2016-03-02 16:48:20 +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 | #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 "driver/compiler_driver.h" |
| 24 | #include "driver/compiler_driver-inl.h" |
| 25 | #include "driver/dex_compilation_unit.h" |
| 26 | #include "mirror/dex_cache.h" |
| 27 | #include "nodes.h" |
| 28 | #include "optimizing_compiler_stats.h" |
| 29 | #include "ssa_builder.h" |
| 30 | |
| 31 | namespace art { |
| 32 | |
| 33 | class HInstructionBuilder : public ValueObject { |
| 34 | public: |
| 35 | HInstructionBuilder(HGraph* graph, |
| 36 | HBasicBlockBuilder* block_builder, |
| 37 | SsaBuilder* ssa_builder, |
| 38 | const DexFile* dex_file, |
| 39 | const DexFile::CodeItem& code_item, |
| 40 | Primitive::Type return_type, |
| 41 | DexCompilationUnit* dex_compilation_unit, |
| 42 | const DexCompilationUnit* const outer_compilation_unit, |
| 43 | CompilerDriver* driver, |
| 44 | const uint8_t* interpreter_metadata, |
| 45 | OptimizingCompilerStats* compiler_stats, |
| 46 | Handle<mirror::DexCache> dex_cache) |
| 47 | : arena_(graph->GetArena()), |
| 48 | graph_(graph), |
| 49 | dex_file_(dex_file), |
| 50 | code_item_(code_item), |
| 51 | return_type_(return_type), |
| 52 | block_builder_(block_builder), |
| 53 | ssa_builder_(ssa_builder), |
| 54 | locals_for_(arena_->Adapter(kArenaAllocGraphBuilder)), |
| 55 | current_block_(nullptr), |
| 56 | current_locals_(nullptr), |
| 57 | latest_result_(nullptr), |
| 58 | compiler_driver_(driver), |
| 59 | dex_compilation_unit_(dex_compilation_unit), |
| 60 | outer_compilation_unit_(outer_compilation_unit), |
| 61 | interpreter_metadata_(interpreter_metadata), |
| 62 | compilation_stats_(compiler_stats), |
| 63 | dex_cache_(dex_cache), |
| 64 | loop_headers_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)) { |
| 65 | loop_headers_.reserve(kDefaultNumberOfLoops); |
| 66 | } |
| 67 | |
| 68 | bool Build(); |
| 69 | |
| 70 | private: |
| 71 | void MaybeRecordStat(MethodCompilationStat compilation_stat); |
| 72 | |
| 73 | void InitializeBlockLocals(); |
| 74 | void PropagateLocalsToCatchBlocks(); |
| 75 | void SetLoopHeaderPhiInputs(); |
| 76 | |
| 77 | bool ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc); |
| 78 | void FindNativeDebugInfoLocations(ArenaBitVector* locations); |
| 79 | |
| 80 | bool CanDecodeQuickenedInfo() const; |
| 81 | uint16_t LookupQuickenedInfo(uint32_t dex_pc); |
| 82 | |
| 83 | HBasicBlock* FindBlockStartingAt(uint32_t dex_pc) const; |
| 84 | |
| 85 | ArenaVector<HInstruction*>* GetLocalsFor(HBasicBlock* block); |
| 86 | HInstruction* ValueOfLocalAt(HBasicBlock* block, size_t local); |
| 87 | HInstruction* LoadLocal(uint32_t register_index, Primitive::Type type) const; |
| 88 | void UpdateLocal(uint32_t register_index, HInstruction* instruction); |
| 89 | |
| 90 | void AppendInstruction(HInstruction* instruction); |
| 91 | void InsertInstructionAtTop(HInstruction* instruction); |
| 92 | void InitializeInstruction(HInstruction* instruction); |
| 93 | |
| 94 | void InitializeParameters(); |
| 95 | |
| 96 | // Returns whether the current method needs access check for the type. |
| 97 | // Output parameter finalizable is set to whether the type is finalizable. |
| 98 | bool NeedsAccessCheck(uint32_t type_index, /*out*/bool* finalizable) const; |
| 99 | |
| 100 | template<typename T> |
| 101 | void Unop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); |
| 102 | |
| 103 | template<typename T> |
| 104 | void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); |
| 105 | |
| 106 | template<typename T> |
| 107 | void Binop_23x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); |
| 108 | |
| 109 | void Binop_23x_cmp(const Instruction& instruction, |
| 110 | Primitive::Type type, |
| 111 | ComparisonBias bias, |
| 112 | uint32_t dex_pc); |
| 113 | |
| 114 | template<typename T> |
| 115 | void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); |
| 116 | |
| 117 | template<typename T> |
| 118 | void Binop_12x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); |
| 119 | |
| 120 | template<typename T> |
| 121 | void Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc); |
| 122 | |
| 123 | template<typename T> |
| 124 | void Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc); |
| 125 | |
| 126 | template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc); |
| 127 | template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc); |
| 128 | |
| 129 | void Conversion_12x(const Instruction& instruction, |
| 130 | Primitive::Type input_type, |
| 131 | Primitive::Type result_type, |
| 132 | uint32_t dex_pc); |
| 133 | |
| 134 | void BuildCheckedDivRem(uint16_t out_reg, |
| 135 | uint16_t first_reg, |
| 136 | int64_t second_reg_or_constant, |
| 137 | uint32_t dex_pc, |
| 138 | Primitive::Type type, |
| 139 | bool second_is_lit, |
| 140 | bool is_div); |
| 141 | |
| 142 | void BuildReturn(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); |
| 143 | |
| 144 | // Builds an instance field access node and returns whether the instruction is supported. |
| 145 | bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put); |
| 146 | |
| 147 | void BuildUnresolvedStaticFieldAccess(const Instruction& instruction, |
| 148 | uint32_t dex_pc, |
| 149 | bool is_put, |
| 150 | Primitive::Type field_type); |
| 151 | // Builds a static field access node and returns whether the instruction is supported. |
| 152 | bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put); |
| 153 | |
| 154 | void BuildArrayAccess(const Instruction& instruction, |
| 155 | uint32_t dex_pc, |
| 156 | bool is_get, |
| 157 | Primitive::Type anticipated_type); |
| 158 | |
| 159 | // Builds an invocation node and returns whether the instruction is supported. |
| 160 | bool BuildInvoke(const Instruction& instruction, |
| 161 | uint32_t dex_pc, |
| 162 | uint32_t method_idx, |
| 163 | uint32_t number_of_vreg_arguments, |
| 164 | bool is_range, |
| 165 | uint32_t* args, |
| 166 | uint32_t register_index); |
| 167 | |
| 168 | // Builds a new array node and the instructions that fill it. |
| 169 | void BuildFilledNewArray(uint32_t dex_pc, |
| 170 | uint32_t type_index, |
| 171 | uint32_t number_of_vreg_arguments, |
| 172 | bool is_range, |
| 173 | uint32_t* args, |
| 174 | uint32_t register_index); |
| 175 | |
| 176 | void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc); |
| 177 | |
| 178 | // Fills the given object with data as specified in the fill-array-data |
| 179 | // instruction. Currently only used for non-reference and non-floating point |
| 180 | // arrays. |
| 181 | template <typename T> |
| 182 | void BuildFillArrayData(HInstruction* object, |
| 183 | const T* data, |
| 184 | uint32_t element_count, |
| 185 | Primitive::Type anticipated_type, |
| 186 | uint32_t dex_pc); |
| 187 | |
| 188 | // Fills the given object with data as specified in the fill-array-data |
| 189 | // instruction. The data must be for long and double arrays. |
| 190 | void BuildFillWideArrayData(HInstruction* object, |
| 191 | const int64_t* data, |
| 192 | uint32_t element_count, |
| 193 | uint32_t dex_pc); |
| 194 | |
| 195 | // Builds a `HInstanceOf`, or a `HCheckCast` instruction. |
| 196 | void BuildTypeCheck(const Instruction& instruction, |
| 197 | uint8_t destination, |
| 198 | uint8_t reference, |
| 199 | uint16_t type_index, |
| 200 | uint32_t dex_pc); |
| 201 | |
| 202 | // Builds an instruction sequence for a switch statement. |
| 203 | void BuildSwitch(const Instruction& instruction, uint32_t dex_pc); |
| 204 | |
| 205 | // Returns the outer-most compiling method's class. |
| 206 | mirror::Class* GetOutermostCompilingClass() const; |
| 207 | |
| 208 | // Returns the class whose method is being compiled. |
| 209 | mirror::Class* GetCompilingClass() const; |
| 210 | |
| 211 | // Returns whether `type_index` points to the outer-most compiling method's class. |
| 212 | bool IsOutermostCompilingClass(uint16_t type_index) const; |
| 213 | |
| 214 | void PotentiallySimplifyFakeString(uint16_t original_dex_register, |
| 215 | uint32_t dex_pc, |
| 216 | HInvoke* invoke); |
| 217 | |
| 218 | bool SetupInvokeArguments(HInvoke* invoke, |
| 219 | uint32_t number_of_vreg_arguments, |
| 220 | uint32_t* args, |
| 221 | uint32_t register_index, |
| 222 | bool is_range, |
| 223 | const char* descriptor, |
| 224 | size_t start_index, |
| 225 | size_t* argument_index); |
| 226 | |
| 227 | bool HandleInvoke(HInvoke* invoke, |
| 228 | uint32_t number_of_vreg_arguments, |
| 229 | uint32_t* args, |
| 230 | uint32_t register_index, |
| 231 | bool is_range, |
| 232 | const char* descriptor, |
| 233 | HClinitCheck* clinit_check); |
| 234 | |
| 235 | bool HandleStringInit(HInvoke* invoke, |
| 236 | uint32_t number_of_vreg_arguments, |
| 237 | uint32_t* args, |
| 238 | uint32_t register_index, |
| 239 | bool is_range, |
| 240 | const char* descriptor); |
| 241 | void HandleStringInitResult(HInvokeStaticOrDirect* invoke); |
| 242 | |
| 243 | HClinitCheck* ProcessClinitCheckForInvoke( |
| 244 | uint32_t dex_pc, |
| 245 | ArtMethod* method, |
| 246 | uint32_t method_idx, |
| 247 | HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) |
| 248 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 249 | |
| 250 | // Build a HNewInstance instruction. |
| 251 | bool BuildNewInstance(uint16_t type_index, uint32_t dex_pc); |
| 252 | |
| 253 | // Return whether the compiler can assume `cls` is initialized. |
| 254 | bool IsInitialized(Handle<mirror::Class> cls) const |
| 255 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 256 | |
| 257 | // Try to resolve a method using the class linker. Return null if a method could |
| 258 | // not be resolved. |
| 259 | ArtMethod* ResolveMethod(uint16_t method_idx, InvokeType invoke_type); |
| 260 | |
| 261 | ArenaAllocator* const arena_; |
| 262 | HGraph* const graph_; |
| 263 | |
| 264 | // The dex file where the method being compiled is, and the bytecode data. |
| 265 | const DexFile* const dex_file_; |
| 266 | const DexFile::CodeItem& code_item_; |
| 267 | |
| 268 | // The return type of the method being compiled. |
| 269 | const Primitive::Type return_type_; |
| 270 | |
| 271 | HBasicBlockBuilder* block_builder_; |
| 272 | SsaBuilder* ssa_builder_; |
| 273 | |
| 274 | ArenaVector<ArenaVector<HInstruction*>> locals_for_; |
| 275 | HBasicBlock* current_block_; |
| 276 | ArenaVector<HInstruction*>* current_locals_; |
| 277 | HInstruction* latest_result_; |
| 278 | |
| 279 | CompilerDriver* const compiler_driver_; |
| 280 | |
| 281 | // The compilation unit of the current method being compiled. Note that |
| 282 | // it can be an inlined method. |
| 283 | DexCompilationUnit* const dex_compilation_unit_; |
| 284 | |
| 285 | // The compilation unit of the outermost method being compiled. That is the |
| 286 | // method being compiled (and not inlined), and potentially inlining other |
| 287 | // methods. |
| 288 | const DexCompilationUnit* const outer_compilation_unit_; |
| 289 | |
| 290 | const uint8_t* interpreter_metadata_; |
| 291 | OptimizingCompilerStats* compilation_stats_; |
| 292 | Handle<mirror::DexCache> dex_cache_; |
| 293 | |
| 294 | ArenaVector<HBasicBlock*> loop_headers_; |
| 295 | |
| 296 | static constexpr int kDefaultNumberOfLoops = 2; |
| 297 | |
| 298 | DISALLOW_COPY_AND_ASSIGN(HInstructionBuilder); |
| 299 | }; |
| 300 | |
| 301 | } // namespace art |
| 302 | |
| 303 | #endif // ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_ |