blob: 897bcece7b674fec5e4dc54d4024a818912d8574 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
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 Geoffray3ff386a2014-03-04 14:46:47 +000020#include "dex_file.h"
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010021#include "dex_file-inl.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010022#include "driver/compiler_driver.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000023#include "driver/dex_compilation_unit.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010024#include "primitive.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070025#include "utils/arena_object.h"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000026#include "utils/growable_array.h"
Dave Allison20dfc792014-06-16 20:44:29 -070027#include "nodes.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000028
29namespace art {
30
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031class Instruction;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032
33class HGraphBuilder : public ValueObject {
34 public:
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000035 HGraphBuilder(ArenaAllocator* arena,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010036 DexCompilationUnit* dex_compilation_unit,
37 const DexFile* dex_file,
38 CompilerDriver* driver)
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000040 branch_targets_(arena, 0),
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000041 locals_(arena, 0),
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042 entry_block_(nullptr),
43 exit_block_(nullptr),
44 current_block_(nullptr),
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000045 graph_(nullptr),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000046 constant0_(nullptr),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000047 constant1_(nullptr),
48 dex_file_(dex_file),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010049 dex_compilation_unit_(dex_compilation_unit),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010050 compiler_driver_(driver),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +010051 return_type_(Primitive::GetType(dex_compilation_unit_->GetShorty()[0])),
52 code_start_(nullptr),
53 latest_result_(nullptr) {}
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010054
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 Geoffraya3d05a42014-10-20 17:41:32 +010069 return_type_(return_type),
70 code_start_(nullptr),
71 latest_result_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000072
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000073 HGraph* BuildGraph(const DexFile::CodeItem& code);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074
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 Juravle225ff812014-11-13 16:46:39 +000079 bool AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000080
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 Geoffray818f2102014-02-18 16:43:35 +000086
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010087 HIntConstant* GetIntConstant0();
88 HIntConstant* GetIntConstant1();
89 HIntConstant* GetIntConstant(int32_t constant);
90 HLongConstant* GetLongConstant(int64_t constant);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010091 void InitializeLocals(uint16_t count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000092 HLocal* GetLocalAt(int register_index) const;
93 void UpdateLocal(int register_index, HInstruction* instruction) const;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010094 HInstruction* LoadLocal(int register_index, Primitive::Type type) const;
Calin Juravle225ff812014-11-13 16:46:39 +000095 void PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_pc);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +000096 void InitializeParameters(uint16_t number_of_parameters);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010097
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010098 template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +010099 void Unop_12x(const Instruction& instruction, Primitive::Type type);
100
101 template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100102 void Binop_23x(const Instruction& instruction, Primitive::Type type);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100103
104 template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000105 void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
106
107 template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100108 void Binop_12x(const Instruction& instruction, Primitive::Type type);
109
110 template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000111 void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
112
113 template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100114 void Binop_22b(const Instruction& instruction, bool reverse);
115
116 template<typename T>
117 void Binop_22s(const Instruction& instruction, bool reverse);
118
Calin Juravle225ff812014-11-13 16:46:39 +0000119 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
120 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100121
Roland Levillaindff1f282014-11-05 14:15:05 +0000122 void Conversion_12x(const Instruction& instruction,
123 Primitive::Type input_type,
124 Primitive::Type result_type);
125
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 void BuildCheckedDivRem(uint16_t out_reg,
127 uint16_t first_reg,
128 int64_t second_reg_or_constant,
129 uint32_t dex_pc,
130 Primitive::Type type,
131 bool second_is_lit,
132 bool is_div);
Calin Juravled0d48522014-11-04 16:40:20 +0000133
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100134 void BuildReturn(const Instruction& instruction, Primitive::Type type);
135
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100136 // Builds an instance field access node and returns whether the instruction is supported.
Calin Juravle225ff812014-11-13 16:46:39 +0000137 bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100138
139 // Builds a static field access node and returns whether the instruction is supported.
Calin Juravle225ff812014-11-13 16:46:39 +0000140 bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100141
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 void BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000143 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100144 bool is_get,
145 Primitive::Type anticipated_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100146
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100147 // Builds an invocation node and returns whether the instruction is supported.
148 bool BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000149 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100150 uint32_t method_idx,
151 uint32_t number_of_vreg_arguments,
152 bool is_range,
153 uint32_t* args,
154 uint32_t register_index);
155
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100156 // Builds a new array node and the instructions that fill it.
Calin Juravle225ff812014-11-13 16:46:39 +0000157 void BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100158 uint32_t type_index,
159 uint32_t number_of_vreg_arguments,
160 bool is_range,
161 uint32_t* args,
162 uint32_t register_index);
163
Calin Juravle225ff812014-11-13 16:46:39 +0000164 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000165
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100166 // Fills the given object with data as specified in the fill-array-data
167 // instruction. Currently only used for non-reference and non-floating point
168 // arrays.
169 template <typename T>
170 void BuildFillArrayData(HInstruction* object,
171 const T* data,
172 uint32_t element_count,
173 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +0000174 uint32_t dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100175
176 // Fills the given object with data as specified in the fill-array-data
177 // instruction. The data must be for long and double arrays.
178 void BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100179 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100180 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000181 uint32_t dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100182
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000183 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
184 // Returns whether we succeeded in building the instruction.
185 bool BuildTypeCheck(const Instruction& instruction,
186 uint8_t destination,
187 uint8_t reference,
188 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +0000189 uint32_t dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000190
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000191 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000192
193 // A list of the size of the dex code holding block information for
194 // the method. If an entry contains a block, then the dex instruction
195 // starting at that entry is the first instruction of a new block.
196 GrowableArray<HBasicBlock*> branch_targets_;
197
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000198 GrowableArray<HLocal*> locals_;
199
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000200 HBasicBlock* entry_block_;
201 HBasicBlock* exit_block_;
202 HBasicBlock* current_block_;
203 HGraph* graph_;
204
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000205 HIntConstant* constant0_;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000206 HIntConstant* constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000207
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000208 const DexFile* const dex_file_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100209 DexCompilationUnit* const dex_compilation_unit_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100210 CompilerDriver* const compiler_driver_;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100211 const Primitive::Type return_type_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000212
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100213 // The pointer in the dex file where the instructions of the code item
214 // being currently compiled start.
215 const uint16_t* code_start_;
216
217 // The last invoke or fill-new-array being built. Only to be
218 // used by move-result instructions.
219 HInstruction* latest_result_;
220
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000221 DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
222};
223
224} // namespace art
225
226#endif // ART_COMPILER_OPTIMIZING_BUILDER_H_