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 | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 21 | #include "driver/dex_compilation_unit.h" |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 22 | #include "primitive.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 23 | #include "utils/allocation.h" |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 24 | #include "utils/growable_array.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | class ArenaAllocator; |
| 29 | class Instruction; |
| 30 | class HBasicBlock; |
| 31 | class HGraph; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 32 | class HIntConstant; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 33 | class HLongConstant; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 34 | class HInstruction; |
| 35 | class HLocal; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 36 | |
| 37 | class HGraphBuilder : public ValueObject { |
| 38 | public: |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 39 | HGraphBuilder(ArenaAllocator* arena, |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 40 | DexCompilationUnit* dex_compilation_unit = nullptr, |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 41 | const DexFile* dex_file = nullptr) |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 42 | : arena_(arena), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 43 | branch_targets_(arena, 0), |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 44 | locals_(arena, 0), |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 45 | entry_block_(nullptr), |
| 46 | exit_block_(nullptr), |
| 47 | current_block_(nullptr), |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 48 | graph_(nullptr), |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 49 | constant0_(nullptr), |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 50 | constant1_(nullptr), |
| 51 | dex_file_(dex_file), |
| 52 | dex_compilation_unit_(dex_compilation_unit) { } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 53 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 54 | HGraph* BuildGraph(const DexFile::CodeItem& code); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 55 | |
| 56 | private: |
| 57 | // Analyzes the dex instruction and adds HInstruction to the graph |
| 58 | // to execute that instruction. Returns whether the instruction can |
| 59 | // be handled. |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 60 | bool AnalyzeDexInstruction(const Instruction& instruction, int32_t dex_offset); |
| 61 | |
| 62 | // Finds all instructions that start a new block, and populates branch_targets_ with |
| 63 | // the newly created blocks. |
| 64 | void ComputeBranchTargets(const uint16_t* start, const uint16_t* end); |
| 65 | void MaybeUpdateCurrentBlock(size_t index); |
| 66 | HBasicBlock* FindBlockStartingAt(int32_t index) const; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 67 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 68 | HIntConstant* GetIntConstant0(); |
| 69 | HIntConstant* GetIntConstant1(); |
| 70 | HIntConstant* GetIntConstant(int32_t constant); |
| 71 | HLongConstant* GetLongConstant(int64_t constant); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 72 | void InitializeLocals(uint16_t count); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 73 | HLocal* GetLocalAt(int register_index) const; |
| 74 | void UpdateLocal(int register_index, HInstruction* instruction) const; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 75 | HInstruction* LoadLocal(int register_index, Primitive::Type type) const; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 76 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 77 | // Temporarily returns whether the compiler supports the parameters |
| 78 | // of the method. |
| 79 | bool InitializeParameters(uint16_t number_of_parameters); |
| 80 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 81 | template<typename T> |
| 82 | void Binop_32x(const Instruction& instruction, Primitive::Type type); |
| 83 | |
| 84 | template<typename T> |
| 85 | void Binop_12x(const Instruction& instruction, Primitive::Type type); |
| 86 | |
| 87 | template<typename T> |
| 88 | void Binop_22b(const Instruction& instruction, bool reverse); |
| 89 | |
| 90 | template<typename T> |
| 91 | void Binop_22s(const Instruction& instruction, bool reverse); |
| 92 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 93 | template<typename T> void If_22t(const Instruction& instruction, int32_t dex_offset, bool is_not); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 94 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 95 | void BuildReturn(const Instruction& instruction, Primitive::Type type); |
| 96 | |
| 97 | // Builds an invocation node and returns whether the instruction is supported. |
| 98 | bool BuildInvoke(const Instruction& instruction, |
| 99 | uint32_t dex_offset, |
| 100 | uint32_t method_idx, |
| 101 | uint32_t number_of_vreg_arguments, |
| 102 | bool is_range, |
| 103 | uint32_t* args, |
| 104 | uint32_t register_index); |
| 105 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 106 | ArenaAllocator* const arena_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 107 | |
| 108 | // A list of the size of the dex code holding block information for |
| 109 | // the method. If an entry contains a block, then the dex instruction |
| 110 | // starting at that entry is the first instruction of a new block. |
| 111 | GrowableArray<HBasicBlock*> branch_targets_; |
| 112 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 113 | GrowableArray<HLocal*> locals_; |
| 114 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 115 | HBasicBlock* entry_block_; |
| 116 | HBasicBlock* exit_block_; |
| 117 | HBasicBlock* current_block_; |
| 118 | HGraph* graph_; |
| 119 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 120 | HIntConstant* constant0_; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 121 | HIntConstant* constant1_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 122 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 123 | const DexFile* const dex_file_; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 124 | DexCompilationUnit* const dex_compilation_unit_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 125 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 126 | DISALLOW_COPY_AND_ASSIGN(HGraphBuilder); |
| 127 | }; |
| 128 | |
| 129 | } // namespace art |
| 130 | |
| 131 | #endif // ART_COMPILER_OPTIMIZING_BUILDER_H_ |