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 | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 22 | #include "utils/allocation.h" |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 23 | #include "utils/growable_array.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | class ArenaAllocator; |
| 28 | class Instruction; |
| 29 | class HBasicBlock; |
| 30 | class HGraph; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 31 | class HIntConstant; |
| 32 | class HInstruction; |
| 33 | class HLocal; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 34 | |
| 35 | class HGraphBuilder : public ValueObject { |
| 36 | public: |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 37 | HGraphBuilder(ArenaAllocator* arena, |
| 38 | const DexCompilationUnit* dex_compilation_unit = nullptr, |
| 39 | const DexFile* dex_file = nullptr) |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 40 | : arena_(arena), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 41 | branch_targets_(arena, 0), |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 42 | locals_(arena, 0), |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 43 | entry_block_(nullptr), |
| 44 | exit_block_(nullptr), |
| 45 | current_block_(nullptr), |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 46 | graph_(nullptr), |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 47 | constant0_(nullptr), |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 48 | constant1_(nullptr), |
| 49 | dex_file_(dex_file), |
| 50 | dex_compilation_unit_(dex_compilation_unit) { } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 51 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 52 | HGraph* BuildGraph(const DexFile::CodeItem& code); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 53 | |
| 54 | private: |
| 55 | // Analyzes the dex instruction and adds HInstruction to the graph |
| 56 | // to execute that instruction. Returns whether the instruction can |
| 57 | // be handled. |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 58 | bool AnalyzeDexInstruction(const Instruction& instruction, int32_t dex_offset); |
| 59 | |
| 60 | // Finds all instructions that start a new block, and populates branch_targets_ with |
| 61 | // the newly created blocks. |
| 62 | void ComputeBranchTargets(const uint16_t* start, const uint16_t* end); |
| 63 | void MaybeUpdateCurrentBlock(size_t index); |
| 64 | HBasicBlock* FindBlockStartingAt(int32_t index) const; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 65 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 66 | HIntConstant* GetConstant0(); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 67 | HIntConstant* GetConstant1(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 68 | HIntConstant* GetConstant(int constant); |
| 69 | void InitializeLocals(int count); |
| 70 | HLocal* GetLocalAt(int register_index) const; |
| 71 | void UpdateLocal(int register_index, HInstruction* instruction) const; |
| 72 | HInstruction* LoadLocal(int register_index) const; |
| 73 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 74 | ArenaAllocator* const arena_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 75 | |
| 76 | // A list of the size of the dex code holding block information for |
| 77 | // the method. If an entry contains a block, then the dex instruction |
| 78 | // starting at that entry is the first instruction of a new block. |
| 79 | GrowableArray<HBasicBlock*> branch_targets_; |
| 80 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 81 | GrowableArray<HLocal*> locals_; |
| 82 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 83 | HBasicBlock* entry_block_; |
| 84 | HBasicBlock* exit_block_; |
| 85 | HBasicBlock* current_block_; |
| 86 | HGraph* graph_; |
| 87 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 88 | HIntConstant* constant0_; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 89 | HIntConstant* constant1_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 90 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 91 | const DexFile* const dex_file_; |
| 92 | const DexCompilationUnit* const dex_compilation_unit_; |
| 93 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 94 | DISALLOW_COPY_AND_ASSIGN(HGraphBuilder); |
| 95 | }; |
| 96 | |
| 97 | } // namespace art |
| 98 | |
| 99 | #endif // ART_COMPILER_OPTIMIZING_BUILDER_H_ |