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), |
| 51 | return_type_(Primitive::GetType(dex_compilation_unit_->GetShorty()[0])) {} |
| 52 | |
| 53 | // Only for unit testing. |
| 54 | HGraphBuilder(ArenaAllocator* arena, Primitive::Type return_type = Primitive::kPrimInt) |
| 55 | : arena_(arena), |
| 56 | branch_targets_(arena, 0), |
| 57 | locals_(arena, 0), |
| 58 | entry_block_(nullptr), |
| 59 | exit_block_(nullptr), |
| 60 | current_block_(nullptr), |
| 61 | graph_(nullptr), |
| 62 | constant0_(nullptr), |
| 63 | constant1_(nullptr), |
| 64 | dex_file_(nullptr), |
| 65 | dex_compilation_unit_(nullptr), |
| 66 | compiler_driver_(nullptr), |
| 67 | return_type_(return_type) {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 68 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 69 | HGraph* BuildGraph(const DexFile::CodeItem& code); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 70 | |
| 71 | private: |
| 72 | // Analyzes the dex instruction and adds HInstruction to the graph |
| 73 | // to execute that instruction. Returns whether the instruction can |
| 74 | // be handled. |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 75 | bool AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 76 | |
| 77 | // Finds all instructions that start a new block, and populates branch_targets_ with |
| 78 | // the newly created blocks. |
| 79 | void ComputeBranchTargets(const uint16_t* start, const uint16_t* end); |
| 80 | void MaybeUpdateCurrentBlock(size_t index); |
| 81 | HBasicBlock* FindBlockStartingAt(int32_t index) const; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 82 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 83 | HIntConstant* GetIntConstant0(); |
| 84 | HIntConstant* GetIntConstant1(); |
| 85 | HIntConstant* GetIntConstant(int32_t constant); |
| 86 | HLongConstant* GetLongConstant(int64_t constant); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 87 | void InitializeLocals(uint16_t count); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 88 | HLocal* GetLocalAt(int register_index) const; |
| 89 | void UpdateLocal(int register_index, HInstruction* instruction) const; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 90 | HInstruction* LoadLocal(int register_index, Primitive::Type type) const; |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 91 | void PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 92 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 93 | // Temporarily returns whether the compiler supports the parameters |
| 94 | // of the method. |
| 95 | bool InitializeParameters(uint16_t number_of_parameters); |
| 96 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 97 | template<typename T> |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 98 | void Binop_23x(const Instruction& instruction, Primitive::Type type); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 99 | |
| 100 | template<typename T> |
| 101 | void Binop_12x(const Instruction& instruction, Primitive::Type type); |
| 102 | |
| 103 | template<typename T> |
| 104 | void Binop_22b(const Instruction& instruction, bool reverse); |
| 105 | |
| 106 | template<typename T> |
| 107 | void Binop_22s(const Instruction& instruction, bool reverse); |
| 108 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 109 | template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_offset); |
| 110 | template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_offset); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 111 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 112 | void BuildReturn(const Instruction& instruction, Primitive::Type type); |
| 113 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 114 | bool BuildFieldAccess(const Instruction& instruction, uint32_t dex_offset, bool is_get); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 115 | void BuildArrayAccess(const Instruction& instruction, |
| 116 | uint32_t dex_offset, |
| 117 | bool is_get, |
| 118 | Primitive::Type anticipated_type); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 119 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 120 | // Builds an invocation node and returns whether the instruction is supported. |
| 121 | bool BuildInvoke(const Instruction& instruction, |
| 122 | uint32_t dex_offset, |
| 123 | uint32_t method_idx, |
| 124 | uint32_t number_of_vreg_arguments, |
| 125 | bool is_range, |
| 126 | uint32_t* args, |
| 127 | uint32_t register_index); |
| 128 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 129 | ArenaAllocator* const arena_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 130 | |
| 131 | // A list of the size of the dex code holding block information for |
| 132 | // the method. If an entry contains a block, then the dex instruction |
| 133 | // starting at that entry is the first instruction of a new block. |
| 134 | GrowableArray<HBasicBlock*> branch_targets_; |
| 135 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 136 | GrowableArray<HLocal*> locals_; |
| 137 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 138 | HBasicBlock* entry_block_; |
| 139 | HBasicBlock* exit_block_; |
| 140 | HBasicBlock* current_block_; |
| 141 | HGraph* graph_; |
| 142 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 143 | HIntConstant* constant0_; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 144 | HIntConstant* constant1_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 145 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 146 | const DexFile* const dex_file_; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 147 | DexCompilationUnit* const dex_compilation_unit_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 148 | CompilerDriver* const compiler_driver_; |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 149 | const Primitive::Type return_type_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 150 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 151 | DISALLOW_COPY_AND_ASSIGN(HGraphBuilder); |
| 152 | }; |
| 153 | |
| 154 | } // namespace art |
| 155 | |
| 156 | #endif // ART_COMPILER_OPTIMIZING_BUILDER_H_ |