Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_COMPILER_OPTIMIZING_BUILDER_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_BUILDER_H_ |
| 19 | |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 20 | #include "base/arena_containers.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 21 | #include "base/arena_object.h" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 22 | #include "block_builder.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 23 | #include "dex_file.h" |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 24 | #include "dex_file-inl.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 25 | #include "driver/compiler_driver.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 26 | #include "driver/dex_compilation_unit.h" |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 27 | #include "optimizing_compiler_stats.h" |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 28 | #include "primitive.h" |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 29 | #include "nodes.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
David Brazdil | 6032891 | 2016-04-04 17:47:42 +0000 | [diff] [blame^] | 33 | class Instruction; |
| 34 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 35 | class HGraphBuilder : public ValueObject { |
| 36 | public: |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 37 | HGraphBuilder(HGraph* graph, |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 38 | DexCompilationUnit* dex_compilation_unit, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 39 | const DexCompilationUnit* const outer_compilation_unit, |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 40 | const DexFile* dex_file, |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 41 | const DexFile::CodeItem& code_item, |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 42 | CompilerDriver* driver, |
Nicolas Geoffray | 9523a3e | 2015-07-17 11:51:28 +0000 | [diff] [blame] | 43 | OptimizingCompilerStats* compiler_stats, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 44 | const uint8_t* interpreter_metadata, |
David Brazdil | 6032891 | 2016-04-04 17:47:42 +0000 | [diff] [blame^] | 45 | Handle<mirror::DexCache> dex_cache) |
| 46 | : arena_(graph->GetArena()), |
| 47 | locals_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)), |
| 48 | current_block_(nullptr), |
| 49 | graph_(graph), |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 50 | dex_file_(dex_file), |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 51 | code_item_(code_item), |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 52 | dex_compilation_unit_(dex_compilation_unit), |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 53 | compiler_driver_(driver), |
David Brazdil | 6032891 | 2016-04-04 17:47:42 +0000 | [diff] [blame^] | 54 | outer_compilation_unit_(outer_compilation_unit), |
| 55 | return_type_(Primitive::GetType(dex_compilation_unit_->GetShorty()[0])), |
| 56 | code_start_(code_item.insns_), |
David Brazdil | e3ff7b2 | 2016-03-02 16:48:20 +0000 | [diff] [blame] | 57 | block_builder_(graph, dex_file, code_item), |
David Brazdil | 6032891 | 2016-04-04 17:47:42 +0000 | [diff] [blame^] | 58 | latest_result_(nullptr), |
| 59 | compilation_stats_(compiler_stats), |
| 60 | interpreter_metadata_(interpreter_metadata), |
| 61 | dex_cache_(dex_cache) {} |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 62 | |
| 63 | // Only for unit testing. |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 64 | HGraphBuilder(HGraph* graph, |
| 65 | const DexFile::CodeItem& code_item, |
| 66 | Primitive::Type return_type = Primitive::kPrimInt) |
David Brazdil | 6032891 | 2016-04-04 17:47:42 +0000 | [diff] [blame^] | 67 | : arena_(graph->GetArena()), |
| 68 | locals_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)), |
| 69 | current_block_(nullptr), |
| 70 | graph_(graph), |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 71 | dex_file_(nullptr), |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 72 | code_item_(code_item), |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 73 | dex_compilation_unit_(nullptr), |
| 74 | compiler_driver_(nullptr), |
David Brazdil | 6032891 | 2016-04-04 17:47:42 +0000 | [diff] [blame^] | 75 | outer_compilation_unit_(nullptr), |
| 76 | return_type_(return_type), |
| 77 | code_start_(code_item.insns_), |
David Brazdil | e3ff7b2 | 2016-03-02 16:48:20 +0000 | [diff] [blame] | 78 | block_builder_(graph, nullptr, code_item), |
David Brazdil | 6032891 | 2016-04-04 17:47:42 +0000 | [diff] [blame^] | 79 | latest_result_(nullptr), |
| 80 | compilation_stats_(nullptr), |
| 81 | interpreter_metadata_(nullptr), |
| 82 | null_dex_cache_(), |
| 83 | dex_cache_(null_dex_cache_) {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 84 | |
David Brazdil | 6032891 | 2016-04-04 17:47:42 +0000 | [diff] [blame^] | 85 | GraphAnalysisResult BuildGraph(StackHandleScopeCollection* handles); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 86 | |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 87 | static constexpr const char* kBuilderPassName = "builder"; |
| 88 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 89 | private: |
David Brazdil | 6032891 | 2016-04-04 17:47:42 +0000 | [diff] [blame^] | 90 | bool GenerateInstructions(); |
| 91 | bool AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc); |
| 92 | |
| 93 | void FindNativeDebugInfoLocations(ArenaBitVector* locations); |
| 94 | |
| 95 | bool CanDecodeQuickenedInfo() const; |
| 96 | uint16_t LookupQuickenedInfo(uint32_t dex_pc); |
| 97 | |
| 98 | HBasicBlock* FindBlockStartingAt(uint32_t dex_pc) const { |
| 99 | return block_builder_.GetBlockAt(dex_pc); |
| 100 | } |
| 101 | |
| 102 | void InitializeLocals(uint16_t count); |
| 103 | HLocal* GetLocalAt(uint32_t register_index) const; |
| 104 | void UpdateLocal(uint32_t register_index, HInstruction* instruction, uint32_t dex_pc) const; |
| 105 | HInstruction* LoadLocal(uint32_t register_index, Primitive::Type type, uint32_t dex_pc) const; |
| 106 | void InitializeParameters(uint16_t number_of_parameters); |
| 107 | |
| 108 | // Returns whether the current method needs access check for the type. |
| 109 | // Output parameter finalizable is set to whether the type is finalizable. |
| 110 | bool NeedsAccessCheck(uint32_t type_index, /*out*/bool* finalizable) const; |
| 111 | |
| 112 | template<typename T> |
| 113 | void Unop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); |
| 114 | |
| 115 | template<typename T> |
| 116 | void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); |
| 117 | |
| 118 | template<typename T> |
| 119 | void Binop_23x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); |
| 120 | |
| 121 | void Binop_23x_cmp(const Instruction& instruction, |
| 122 | Primitive::Type type, |
| 123 | ComparisonBias bias, |
| 124 | uint32_t dex_pc); |
| 125 | |
| 126 | template<typename T> |
| 127 | void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); |
| 128 | |
| 129 | template<typename T> |
| 130 | void Binop_12x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); |
| 131 | |
| 132 | template<typename T> |
| 133 | void Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc); |
| 134 | |
| 135 | template<typename T> |
| 136 | void Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc); |
| 137 | |
| 138 | template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc); |
| 139 | template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc); |
| 140 | |
| 141 | void Conversion_12x(const Instruction& instruction, |
| 142 | Primitive::Type input_type, |
| 143 | Primitive::Type result_type, |
| 144 | uint32_t dex_pc); |
| 145 | |
| 146 | void BuildCheckedDivRem(uint16_t out_reg, |
| 147 | uint16_t first_reg, |
| 148 | int64_t second_reg_or_constant, |
| 149 | uint32_t dex_pc, |
| 150 | Primitive::Type type, |
| 151 | bool second_is_lit, |
| 152 | bool is_div); |
| 153 | |
| 154 | void BuildReturn(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); |
| 155 | |
| 156 | // Builds an instance field access node and returns whether the instruction is supported. |
| 157 | bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put); |
| 158 | |
| 159 | void BuildUnresolvedStaticFieldAccess(const Instruction& instruction, |
| 160 | uint32_t dex_pc, |
| 161 | bool is_put, |
| 162 | Primitive::Type field_type); |
| 163 | // Builds a static field access node and returns whether the instruction is supported. |
| 164 | bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put); |
| 165 | |
| 166 | void BuildArrayAccess(const Instruction& instruction, |
| 167 | uint32_t dex_pc, |
| 168 | bool is_get, |
| 169 | Primitive::Type anticipated_type); |
| 170 | |
| 171 | // Builds an invocation node and returns whether the instruction is supported. |
| 172 | bool BuildInvoke(const Instruction& instruction, |
| 173 | uint32_t dex_pc, |
| 174 | uint32_t method_idx, |
| 175 | uint32_t number_of_vreg_arguments, |
| 176 | bool is_range, |
| 177 | uint32_t* args, |
| 178 | uint32_t register_index); |
| 179 | |
| 180 | // Builds a new array node and the instructions that fill it. |
| 181 | void BuildFilledNewArray(uint32_t dex_pc, |
| 182 | uint32_t type_index, |
| 183 | uint32_t number_of_vreg_arguments, |
| 184 | bool is_range, |
| 185 | uint32_t* args, |
| 186 | uint32_t register_index); |
| 187 | |
| 188 | void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc); |
| 189 | |
| 190 | // Fills the given object with data as specified in the fill-array-data |
| 191 | // instruction. Currently only used for non-reference and non-floating point |
| 192 | // arrays. |
| 193 | template <typename T> |
| 194 | void BuildFillArrayData(HInstruction* object, |
| 195 | const T* data, |
| 196 | uint32_t element_count, |
| 197 | Primitive::Type anticipated_type, |
| 198 | uint32_t dex_pc); |
| 199 | |
| 200 | // Fills the given object with data as specified in the fill-array-data |
| 201 | // instruction. The data must be for long and double arrays. |
| 202 | void BuildFillWideArrayData(HInstruction* object, |
| 203 | const int64_t* data, |
| 204 | uint32_t element_count, |
| 205 | uint32_t dex_pc); |
| 206 | |
| 207 | // Builds a `HInstanceOf`, or a `HCheckCast` instruction. |
| 208 | void BuildTypeCheck(const Instruction& instruction, |
| 209 | uint8_t destination, |
| 210 | uint8_t reference, |
| 211 | uint16_t type_index, |
| 212 | uint32_t dex_pc); |
| 213 | |
| 214 | // Builds an instruction sequence for a switch statement. |
| 215 | void BuildSwitch(const Instruction& instruction, uint32_t dex_pc); |
| 216 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 217 | bool SkipCompilation(size_t number_of_branches); |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 218 | |
David Brazdil | 6032891 | 2016-04-04 17:47:42 +0000 | [diff] [blame^] | 219 | void MaybeRecordStat(MethodCompilationStat compilation_stat); |
| 220 | |
| 221 | // Returns the outer-most compiling method's class. |
| 222 | mirror::Class* GetOutermostCompilingClass() const; |
| 223 | |
| 224 | // Returns the class whose method is being compiled. |
| 225 | mirror::Class* GetCompilingClass() const; |
| 226 | |
| 227 | // Returns whether `type_index` points to the outer-most compiling method's class. |
| 228 | bool IsOutermostCompilingClass(uint16_t type_index) const; |
| 229 | |
| 230 | void PotentiallySimplifyFakeString(uint16_t original_dex_register, |
| 231 | uint32_t dex_pc, |
| 232 | HInvoke* invoke); |
| 233 | |
| 234 | bool SetupInvokeArguments(HInvoke* invoke, |
| 235 | uint32_t number_of_vreg_arguments, |
| 236 | uint32_t* args, |
| 237 | uint32_t register_index, |
| 238 | bool is_range, |
| 239 | const char* descriptor, |
| 240 | size_t start_index, |
| 241 | size_t* argument_index); |
| 242 | |
| 243 | bool HandleInvoke(HInvoke* invoke, |
| 244 | uint32_t number_of_vreg_arguments, |
| 245 | uint32_t* args, |
| 246 | uint32_t register_index, |
| 247 | bool is_range, |
| 248 | const char* descriptor, |
| 249 | HClinitCheck* clinit_check); |
| 250 | |
| 251 | bool HandleStringInit(HInvoke* invoke, |
| 252 | uint32_t number_of_vreg_arguments, |
| 253 | uint32_t* args, |
| 254 | uint32_t register_index, |
| 255 | bool is_range, |
| 256 | const char* descriptor); |
| 257 | |
| 258 | HClinitCheck* ProcessClinitCheckForInvoke( |
| 259 | uint32_t dex_pc, |
| 260 | ArtMethod* method, |
| 261 | uint32_t method_idx, |
| 262 | HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) |
| 263 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 264 | |
| 265 | // Build a HNewInstance instruction. |
| 266 | bool BuildNewInstance(uint16_t type_index, uint32_t dex_pc); |
| 267 | |
| 268 | // Return whether the compiler can assume `cls` is initialized. |
| 269 | bool IsInitialized(Handle<mirror::Class> cls) const |
| 270 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 271 | |
| 272 | // Try to resolve a method using the class linker. Return null if a method could |
| 273 | // not be resolved. |
| 274 | ArtMethod* ResolveMethod(uint16_t method_idx, InvokeType invoke_type); |
| 275 | |
| 276 | ArenaAllocator* const arena_; |
| 277 | |
| 278 | ArenaVector<HLocal*> locals_; |
| 279 | |
| 280 | HBasicBlock* current_block_; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 281 | HGraph* const graph_; |
David Brazdil | 6032891 | 2016-04-04 17:47:42 +0000 | [diff] [blame^] | 282 | |
| 283 | // The dex file where the method being compiled is, and the bytecode data. |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 284 | const DexFile* const dex_file_; |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 285 | const DexFile::CodeItem& code_item_; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 286 | |
| 287 | // The compilation unit of the current method being compiled. Note that |
| 288 | // it can be an inlined method. |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 289 | DexCompilationUnit* const dex_compilation_unit_; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 290 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 291 | CompilerDriver* const compiler_driver_; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 292 | |
David Brazdil | 6032891 | 2016-04-04 17:47:42 +0000 | [diff] [blame^] | 293 | // The compilation unit of the outermost method being compiled. That is the |
| 294 | // method being compiled (and not inlined), and potentially inlining other |
| 295 | // methods. |
| 296 | const DexCompilationUnit* const outer_compilation_unit_; |
| 297 | |
| 298 | // The return type of the method being compiled. |
| 299 | const Primitive::Type return_type_; |
| 300 | |
| 301 | // The pointer in the dex file where the instructions of the code item |
| 302 | // being currently compiled start. |
| 303 | const uint16_t* code_start_; |
| 304 | |
| 305 | HBasicBlockBuilder block_builder_; |
| 306 | |
| 307 | // The last invoke or fill-new-array being built. Only to be |
| 308 | // used by move-result instructions. |
| 309 | HInstruction* latest_result_; |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 310 | |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 311 | OptimizingCompilerStats* compilation_stats_; |
| 312 | |
David Brazdil | 6032891 | 2016-04-04 17:47:42 +0000 | [diff] [blame^] | 313 | const uint8_t* interpreter_metadata_; |
| 314 | |
| 315 | // Dex cache for dex_file_. |
| 316 | ScopedNullHandle<mirror::DexCache> null_dex_cache_; |
| 317 | Handle<mirror::DexCache> dex_cache_; |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 318 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 319 | DISALLOW_COPY_AND_ASSIGN(HGraphBuilder); |
| 320 | }; |
| 321 | |
| 322 | } // namespace art |
| 323 | |
| 324 | #endif // ART_COMPILER_OPTIMIZING_BUILDER_H_ |