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 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 20 | #include "dex_file.h" |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 21 | #include "dex_file-inl.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 22 | #include "driver/compiler_driver.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 23 | #include "driver/dex_compilation_unit.h" |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 24 | #include "primitive.h" |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 25 | #include "utils/arena_object.h" |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 26 | #include "utils/growable_array.h" |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 27 | #include "nodes.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 31 | class Instruction; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 32 | |
| 33 | class HGraphBuilder : public ValueObject { |
| 34 | public: |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 35 | HGraphBuilder(ArenaAllocator* arena, |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 36 | DexCompilationUnit* dex_compilation_unit, |
| 37 | const DexFile* dex_file, |
| 38 | CompilerDriver* driver) |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 39 | : arena_(arena), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 40 | branch_targets_(arena, 0), |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 41 | locals_(arena, 0), |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 42 | entry_block_(nullptr), |
| 43 | exit_block_(nullptr), |
| 44 | current_block_(nullptr), |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 45 | graph_(nullptr), |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 46 | constant0_(nullptr), |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 47 | constant1_(nullptr), |
| 48 | dex_file_(dex_file), |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 49 | dex_compilation_unit_(dex_compilation_unit), |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 50 | compiler_driver_(driver), |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 51 | return_type_(Primitive::GetType(dex_compilation_unit_->GetShorty()[0])), |
| 52 | code_start_(nullptr), |
| 53 | latest_result_(nullptr) {} |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 54 | |
| 55 | // Only for unit testing. |
| 56 | HGraphBuilder(ArenaAllocator* arena, Primitive::Type return_type = Primitive::kPrimInt) |
| 57 | : arena_(arena), |
| 58 | branch_targets_(arena, 0), |
| 59 | locals_(arena, 0), |
| 60 | entry_block_(nullptr), |
| 61 | exit_block_(nullptr), |
| 62 | current_block_(nullptr), |
| 63 | graph_(nullptr), |
| 64 | constant0_(nullptr), |
| 65 | constant1_(nullptr), |
| 66 | dex_file_(nullptr), |
| 67 | dex_compilation_unit_(nullptr), |
| 68 | compiler_driver_(nullptr), |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 69 | return_type_(return_type), |
| 70 | code_start_(nullptr), |
| 71 | latest_result_(nullptr) {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 72 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 73 | HGraph* BuildGraph(const DexFile::CodeItem& code); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 74 | |
| 75 | private: |
| 76 | // Analyzes the dex instruction and adds HInstruction to the graph |
| 77 | // to execute that instruction. Returns whether the instruction can |
| 78 | // be handled. |
Calin Juravle | 225ff81 | 2014-11-13 16:46:39 +0000 | [diff] [blame] | 79 | bool AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 80 | |
| 81 | // Finds all instructions that start a new block, and populates branch_targets_ with |
| 82 | // the newly created blocks. |
| 83 | void ComputeBranchTargets(const uint16_t* start, const uint16_t* end); |
| 84 | void MaybeUpdateCurrentBlock(size_t index); |
| 85 | HBasicBlock* FindBlockStartingAt(int32_t index) const; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 86 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 87 | HIntConstant* GetIntConstant0(); |
| 88 | HIntConstant* GetIntConstant1(); |
| 89 | HIntConstant* GetIntConstant(int32_t constant); |
| 90 | HLongConstant* GetLongConstant(int64_t constant); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 91 | void InitializeLocals(uint16_t count); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 92 | HLocal* GetLocalAt(int register_index) const; |
| 93 | void UpdateLocal(int register_index, HInstruction* instruction) const; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 94 | HInstruction* LoadLocal(int register_index, Primitive::Type type) const; |
Calin Juravle | 225ff81 | 2014-11-13 16:46:39 +0000 | [diff] [blame] | 95 | void PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_pc); |
Nicolas Geoffray | 52e832b | 2014-11-06 15:15:31 +0000 | [diff] [blame] | 96 | void InitializeParameters(uint16_t number_of_parameters); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 97 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 98 | template<typename T> |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 99 | void Unop_12x(const Instruction& instruction, Primitive::Type type); |
| 100 | |
| 101 | template<typename T> |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 102 | void Binop_23x(const Instruction& instruction, Primitive::Type type); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 103 | |
| 104 | template<typename T> |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 105 | void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); |
| 106 | |
| 107 | template<typename T> |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 108 | void Binop_23x_shift(const Instruction& instruction, Primitive::Type type); |
| 109 | |
| 110 | template<typename T> |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 111 | void Binop_12x(const Instruction& instruction, Primitive::Type type); |
| 112 | |
| 113 | template<typename T> |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 114 | void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc); |
| 115 | |
| 116 | template<typename T> |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 117 | void Binop_12x_shift(const Instruction& instruction, Primitive::Type type); |
| 118 | |
| 119 | template<typename T> |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 120 | void Binop_22b(const Instruction& instruction, bool reverse); |
| 121 | |
| 122 | template<typename T> |
| 123 | void Binop_22s(const Instruction& instruction, bool reverse); |
| 124 | |
Calin Juravle | 225ff81 | 2014-11-13 16:46:39 +0000 | [diff] [blame] | 125 | template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc); |
| 126 | template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 127 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 128 | void Conversion_12x(const Instruction& instruction, |
| 129 | Primitive::Type input_type, |
| 130 | Primitive::Type result_type); |
| 131 | |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 132 | void BuildCheckedDivRem(uint16_t out_reg, |
| 133 | uint16_t first_reg, |
| 134 | int64_t second_reg_or_constant, |
| 135 | uint32_t dex_pc, |
| 136 | Primitive::Type type, |
| 137 | bool second_is_lit, |
| 138 | bool is_div); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 139 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 140 | void BuildReturn(const Instruction& instruction, Primitive::Type type); |
| 141 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 142 | // Builds an instance field access node and returns whether the instruction is supported. |
Calin Juravle | 225ff81 | 2014-11-13 16:46:39 +0000 | [diff] [blame] | 143 | bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 144 | |
| 145 | // Builds a static field access node and returns whether the instruction is supported. |
Calin Juravle | 225ff81 | 2014-11-13 16:46:39 +0000 | [diff] [blame] | 146 | bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 147 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 148 | void BuildArrayAccess(const Instruction& instruction, |
Calin Juravle | 225ff81 | 2014-11-13 16:46:39 +0000 | [diff] [blame] | 149 | uint32_t dex_pc, |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 150 | bool is_get, |
| 151 | Primitive::Type anticipated_type); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 152 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 153 | // Builds an invocation node and returns whether the instruction is supported. |
| 154 | bool BuildInvoke(const Instruction& instruction, |
Calin Juravle | 225ff81 | 2014-11-13 16:46:39 +0000 | [diff] [blame] | 155 | uint32_t dex_pc, |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 156 | uint32_t method_idx, |
| 157 | uint32_t number_of_vreg_arguments, |
| 158 | bool is_range, |
| 159 | uint32_t* args, |
| 160 | uint32_t register_index); |
| 161 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 162 | // Builds a new array node and the instructions that fill it. |
Calin Juravle | 225ff81 | 2014-11-13 16:46:39 +0000 | [diff] [blame] | 163 | void BuildFilledNewArray(uint32_t dex_pc, |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 164 | uint32_t type_index, |
| 165 | uint32_t number_of_vreg_arguments, |
| 166 | bool is_range, |
| 167 | uint32_t* args, |
| 168 | uint32_t register_index); |
| 169 | |
Calin Juravle | 225ff81 | 2014-11-13 16:46:39 +0000 | [diff] [blame] | 170 | void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 171 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 172 | // Fills the given object with data as specified in the fill-array-data |
| 173 | // instruction. Currently only used for non-reference and non-floating point |
| 174 | // arrays. |
| 175 | template <typename T> |
| 176 | void BuildFillArrayData(HInstruction* object, |
| 177 | const T* data, |
| 178 | uint32_t element_count, |
| 179 | Primitive::Type anticipated_type, |
Calin Juravle | 225ff81 | 2014-11-13 16:46:39 +0000 | [diff] [blame] | 180 | uint32_t dex_pc); |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 181 | |
| 182 | // Fills the given object with data as specified in the fill-array-data |
| 183 | // instruction. The data must be for long and double arrays. |
| 184 | void BuildFillWideArrayData(HInstruction* object, |
Nicolas Geoffray | 8d6ae52 | 2014-10-23 18:32:13 +0100 | [diff] [blame] | 185 | const int64_t* data, |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 186 | uint32_t element_count, |
Calin Juravle | 225ff81 | 2014-11-13 16:46:39 +0000 | [diff] [blame] | 187 | uint32_t dex_pc); |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 188 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 189 | // Builds a `HInstanceOf`, or a `HCheckCast` instruction. |
| 190 | // Returns whether we succeeded in building the instruction. |
| 191 | bool BuildTypeCheck(const Instruction& instruction, |
| 192 | uint8_t destination, |
| 193 | uint8_t reference, |
| 194 | uint16_t type_index, |
Calin Juravle | 225ff81 | 2014-11-13 16:46:39 +0000 | [diff] [blame] | 195 | uint32_t dex_pc); |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 196 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 197 | ArenaAllocator* const arena_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 198 | |
| 199 | // A list of the size of the dex code holding block information for |
| 200 | // the method. If an entry contains a block, then the dex instruction |
| 201 | // starting at that entry is the first instruction of a new block. |
| 202 | GrowableArray<HBasicBlock*> branch_targets_; |
| 203 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 204 | GrowableArray<HLocal*> locals_; |
| 205 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 206 | HBasicBlock* entry_block_; |
| 207 | HBasicBlock* exit_block_; |
| 208 | HBasicBlock* current_block_; |
| 209 | HGraph* graph_; |
| 210 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 211 | HIntConstant* constant0_; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 212 | HIntConstant* constant1_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 213 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 214 | const DexFile* const dex_file_; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 215 | DexCompilationUnit* const dex_compilation_unit_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 216 | CompilerDriver* const compiler_driver_; |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 217 | const Primitive::Type return_type_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 218 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 219 | // The pointer in the dex file where the instructions of the code item |
| 220 | // being currently compiled start. |
| 221 | const uint16_t* code_start_; |
| 222 | |
| 223 | // The last invoke or fill-new-array being built. Only to be |
| 224 | // used by move-result instructions. |
| 225 | HInstruction* latest_result_; |
| 226 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 227 | DISALLOW_COPY_AND_ASSIGN(HGraphBuilder); |
| 228 | }; |
| 229 | |
| 230 | } // namespace art |
| 231 | |
| 232 | #endif // ART_COMPILER_OPTIMIZING_BUILDER_H_ |