blob: 73c2f509587a155a4674fa04899ffb90c26b8f62 [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;
Andreas Gampee4d4d322014-12-04 09:09:57 -080032class SwitchTable;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033
34class HGraphBuilder : public ValueObject {
35 public:
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000036 HGraphBuilder(ArenaAllocator* arena,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010037 DexCompilationUnit* dex_compilation_unit,
38 const DexFile* dex_file,
39 CompilerDriver* driver)
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000041 branch_targets_(arena, 0),
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000042 locals_(arena, 0),
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043 entry_block_(nullptr),
44 exit_block_(nullptr),
45 current_block_(nullptr),
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000046 graph_(nullptr),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000047 constant0_(nullptr),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000048 constant1_(nullptr),
49 dex_file_(dex_file),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010050 dex_compilation_unit_(dex_compilation_unit),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010051 compiler_driver_(driver),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +010052 return_type_(Primitive::GetType(dex_compilation_unit_->GetShorty()[0])),
53 code_start_(nullptr),
54 latest_result_(nullptr) {}
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010055
56 // Only for unit testing.
57 HGraphBuilder(ArenaAllocator* arena, Primitive::Type return_type = Primitive::kPrimInt)
58 : arena_(arena),
59 branch_targets_(arena, 0),
60 locals_(arena, 0),
61 entry_block_(nullptr),
62 exit_block_(nullptr),
63 current_block_(nullptr),
64 graph_(nullptr),
65 constant0_(nullptr),
66 constant1_(nullptr),
67 dex_file_(nullptr),
68 dex_compilation_unit_(nullptr),
69 compiler_driver_(nullptr),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +010070 return_type_(return_type),
71 code_start_(nullptr),
72 latest_result_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000074 HGraph* BuildGraph(const DexFile::CodeItem& code);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000075
76 private:
77 // Analyzes the dex instruction and adds HInstruction to the graph
78 // to execute that instruction. Returns whether the instruction can
79 // be handled.
Calin Juravle225ff812014-11-13 16:46:39 +000080 bool AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081
82 // Finds all instructions that start a new block, and populates branch_targets_ with
83 // the newly created blocks.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +000084 // As a side effect, also compute the number of dex instructions, blocks, and
85 // branches.
86 void ComputeBranchTargets(const uint16_t* start,
87 const uint16_t* end,
88 size_t* number_of_dex_instructions,
89 size_t* number_of_block,
90 size_t* number_of_branches);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000091 void MaybeUpdateCurrentBlock(size_t index);
92 HBasicBlock* FindBlockStartingAt(int32_t index) const;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000093
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010094 HIntConstant* GetIntConstant0();
95 HIntConstant* GetIntConstant1();
96 HIntConstant* GetIntConstant(int32_t constant);
97 HLongConstant* GetLongConstant(int64_t constant);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010098 void InitializeLocals(uint16_t count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000099 HLocal* GetLocalAt(int register_index) const;
100 void UpdateLocal(int register_index, HInstruction* instruction) const;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100101 HInstruction* LoadLocal(int register_index, Primitive::Type type) const;
Calin Juravle225ff812014-11-13 16:46:39 +0000102 void PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_pc);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000103 void InitializeParameters(uint16_t number_of_parameters);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100104
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100105 template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100106 void Unop_12x(const Instruction& instruction, Primitive::Type type);
107
108 template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100109 void Binop_23x(const Instruction& instruction, Primitive::Type type);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100110
111 template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000112 void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
113
114 template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000115 void Binop_23x_shift(const Instruction& instruction, Primitive::Type type);
116
Calin Juravleddb7df22014-11-25 20:56:51 +0000117 void Binop_23x_cmp(const Instruction& instruction, Primitive::Type type, HCompare::Bias bias);
118
Calin Juravle9aec02f2014-11-18 23:06:35 +0000119 template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100120 void Binop_12x(const Instruction& instruction, Primitive::Type type);
121
122 template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000123 void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
124
125 template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000126 void Binop_12x_shift(const Instruction& instruction, Primitive::Type type);
127
128 template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100129 void Binop_22b(const Instruction& instruction, bool reverse);
130
131 template<typename T>
132 void Binop_22s(const Instruction& instruction, bool reverse);
133
Calin Juravle225ff812014-11-13 16:46:39 +0000134 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
135 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100136
Roland Levillaindff1f282014-11-05 14:15:05 +0000137 void Conversion_12x(const Instruction& instruction,
138 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000139 Primitive::Type result_type,
140 uint32_t dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +0000141
Calin Juravlebacfec32014-11-14 15:54:36 +0000142 void BuildCheckedDivRem(uint16_t out_reg,
143 uint16_t first_reg,
144 int64_t second_reg_or_constant,
145 uint32_t dex_pc,
146 Primitive::Type type,
147 bool second_is_lit,
148 bool is_div);
Calin Juravled0d48522014-11-04 16:40:20 +0000149
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100150 void BuildReturn(const Instruction& instruction, Primitive::Type type);
151
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100152 // Builds an instance field access node and returns whether the instruction is supported.
Calin Juravle225ff812014-11-13 16:46:39 +0000153 bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100154
155 // Builds a static field access node and returns whether the instruction is supported.
Calin Juravle225ff812014-11-13 16:46:39 +0000156 bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100157
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100158 void BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000159 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 bool is_get,
161 Primitive::Type anticipated_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100162
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100163 // Builds an invocation node and returns whether the instruction is supported.
164 bool BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000165 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100166 uint32_t method_idx,
167 uint32_t number_of_vreg_arguments,
168 bool is_range,
169 uint32_t* args,
170 uint32_t register_index);
171
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100172 // Builds a new array node and the instructions that fill it.
Calin Juravle225ff812014-11-13 16:46:39 +0000173 void BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100174 uint32_t type_index,
175 uint32_t number_of_vreg_arguments,
176 bool is_range,
177 uint32_t* args,
178 uint32_t register_index);
179
Calin Juravle225ff812014-11-13 16:46:39 +0000180 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000181
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100182 // Fills the given object with data as specified in the fill-array-data
183 // instruction. Currently only used for non-reference and non-floating point
184 // arrays.
185 template <typename T>
186 void BuildFillArrayData(HInstruction* object,
187 const T* data,
188 uint32_t element_count,
189 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +0000190 uint32_t dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100191
192 // Fills the given object with data as specified in the fill-array-data
193 // instruction. The data must be for long and double arrays.
194 void BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100195 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100196 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000197 uint32_t dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100198
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000199 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
200 // Returns whether we succeeded in building the instruction.
201 bool BuildTypeCheck(const Instruction& instruction,
202 uint8_t destination,
203 uint8_t reference,
204 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +0000205 uint32_t dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000206
Andreas Gampee4d4d322014-12-04 09:09:57 -0800207 // Builds an instruction sequence for a packed switch statement.
Andreas Gamped881df52014-11-24 23:28:39 -0800208 bool BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc);
209
Andreas Gampee4d4d322014-12-04 09:09:57 -0800210 // Builds an instruction sequence for a sparse switch statement.
211 bool BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc);
212
213 void BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
214 bool is_last_case, const SwitchTable& table,
215 HInstruction* value, int32_t case_value_int,
216 int32_t target_offset, uint32_t dex_pc);
217
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000218 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000219
220 // A list of the size of the dex code holding block information for
221 // the method. If an entry contains a block, then the dex instruction
222 // starting at that entry is the first instruction of a new block.
223 GrowableArray<HBasicBlock*> branch_targets_;
224
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000225 GrowableArray<HLocal*> locals_;
226
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000227 HBasicBlock* entry_block_;
228 HBasicBlock* exit_block_;
229 HBasicBlock* current_block_;
230 HGraph* graph_;
231
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000232 HIntConstant* constant0_;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000233 HIntConstant* constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000234
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000235 const DexFile* const dex_file_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100236 DexCompilationUnit* const dex_compilation_unit_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100237 CompilerDriver* const compiler_driver_;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100238 const Primitive::Type return_type_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000239
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100240 // The pointer in the dex file where the instructions of the code item
241 // being currently compiled start.
242 const uint16_t* code_start_;
243
244 // The last invoke or fill-new-array being built. Only to be
245 // used by move-result instructions.
246 HInstruction* latest_result_;
247
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000248 DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
249};
250
251} // namespace art
252
253#endif // ART_COMPILER_OPTIMIZING_BUILDER_H_