blob: 75c8634ea370bae38ba8fdfd9962aad1c234c06b [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"
Calin Juravle48c2b032014-12-09 18:11:36 +000024#include "optimizing_compiler_stats.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010025#include "primitive.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070026#include "utils/arena_object.h"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000027#include "utils/growable_array.h"
Dave Allison20dfc792014-06-16 20:44:29 -070028#include "nodes.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000029
30namespace art {
31
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032class Instruction;
Andreas Gampee4d4d322014-12-04 09:09:57 -080033class SwitchTable;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000034
35class HGraphBuilder : public ValueObject {
36 public:
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000037 HGraphBuilder(ArenaAllocator* arena,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010038 DexCompilationUnit* dex_compilation_unit,
39 const DexFile* dex_file,
Calin Juravle48c2b032014-12-09 18:11:36 +000040 CompilerDriver* driver,
41 OptimizingCompilerStats* compiler_stats)
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000043 branch_targets_(arena, 0),
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000044 locals_(arena, 0),
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045 entry_block_(nullptr),
46 exit_block_(nullptr),
47 current_block_(nullptr),
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000048 graph_(nullptr),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000049 constant0_(nullptr),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000050 constant1_(nullptr),
51 dex_file_(dex_file),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052 dex_compilation_unit_(dex_compilation_unit),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010053 compiler_driver_(driver),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +010054 return_type_(Primitive::GetType(dex_compilation_unit_->GetShorty()[0])),
55 code_start_(nullptr),
Calin Juravle48c2b032014-12-09 18:11:36 +000056 latest_result_(nullptr),
57 compilation_stats_(compiler_stats) {}
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010058
59 // Only for unit testing.
60 HGraphBuilder(ArenaAllocator* arena, Primitive::Type return_type = Primitive::kPrimInt)
61 : arena_(arena),
62 branch_targets_(arena, 0),
63 locals_(arena, 0),
64 entry_block_(nullptr),
65 exit_block_(nullptr),
66 current_block_(nullptr),
67 graph_(nullptr),
68 constant0_(nullptr),
69 constant1_(nullptr),
70 dex_file_(nullptr),
71 dex_compilation_unit_(nullptr),
72 compiler_driver_(nullptr),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +010073 return_type_(return_type),
74 code_start_(nullptr),
Calin Juravle48c2b032014-12-09 18:11:36 +000075 latest_result_(nullptr),
76 compilation_stats_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000077
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000078 HGraph* BuildGraph(const DexFile::CodeItem& code);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000079
80 private:
81 // Analyzes the dex instruction and adds HInstruction to the graph
82 // to execute that instruction. Returns whether the instruction can
83 // be handled.
Calin Juravle225ff812014-11-13 16:46:39 +000084 bool AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000085
86 // Finds all instructions that start a new block, and populates branch_targets_ with
87 // the newly created blocks.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +000088 // As a side effect, also compute the number of dex instructions, blocks, and
89 // branches.
90 void ComputeBranchTargets(const uint16_t* start,
91 const uint16_t* end,
92 size_t* number_of_dex_instructions,
93 size_t* number_of_block,
94 size_t* number_of_branches);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000095 void MaybeUpdateCurrentBlock(size_t index);
96 HBasicBlock* FindBlockStartingAt(int32_t index) const;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000097
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010098 HIntConstant* GetIntConstant0();
99 HIntConstant* GetIntConstant1();
100 HIntConstant* GetIntConstant(int32_t constant);
101 HLongConstant* GetLongConstant(int64_t constant);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100102 void InitializeLocals(uint16_t count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000103 HLocal* GetLocalAt(int register_index) const;
104 void UpdateLocal(int register_index, HInstruction* instruction) const;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100105 HInstruction* LoadLocal(int register_index, Primitive::Type type) const;
Calin Juravle225ff812014-11-13 16:46:39 +0000106 void PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_pc);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000107 void InitializeParameters(uint16_t number_of_parameters);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100108
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100109 template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100110 void Unop_12x(const Instruction& instruction, Primitive::Type type);
111
112 template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100113 void Binop_23x(const Instruction& instruction, Primitive::Type type);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100114
115 template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000116 void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
117
118 template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000119 void Binop_23x_shift(const Instruction& instruction, Primitive::Type type);
120
Calin Juravleddb7df22014-11-25 20:56:51 +0000121 void Binop_23x_cmp(const Instruction& instruction, Primitive::Type type, HCompare::Bias bias);
122
Calin Juravle9aec02f2014-11-18 23:06:35 +0000123 template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100124 void Binop_12x(const Instruction& instruction, Primitive::Type type);
125
126 template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000127 void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
128
129 template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000130 void Binop_12x_shift(const Instruction& instruction, Primitive::Type type);
131
132 template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100133 void Binop_22b(const Instruction& instruction, bool reverse);
134
135 template<typename T>
136 void Binop_22s(const Instruction& instruction, bool reverse);
137
Calin Juravle225ff812014-11-13 16:46:39 +0000138 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
139 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100140
Roland Levillaindff1f282014-11-05 14:15:05 +0000141 void Conversion_12x(const Instruction& instruction,
142 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000143 Primitive::Type result_type,
144 uint32_t dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +0000145
Calin Juravlebacfec32014-11-14 15:54:36 +0000146 void BuildCheckedDivRem(uint16_t out_reg,
147 uint16_t first_reg,
148 int64_t second_reg_or_constant,
149 uint32_t dex_pc,
150 Primitive::Type type,
151 bool second_is_lit,
152 bool is_div);
Calin Juravled0d48522014-11-04 16:40:20 +0000153
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100154 void BuildReturn(const Instruction& instruction, Primitive::Type type);
155
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100156 // Builds an instance field access node and returns whether the instruction is supported.
Calin Juravle225ff812014-11-13 16:46:39 +0000157 bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100158
159 // Builds a static field access node and returns whether the instruction is supported.
Calin Juravle225ff812014-11-13 16:46:39 +0000160 bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100161
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100162 void BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000163 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100164 bool is_get,
165 Primitive::Type anticipated_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100166
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100167 // Builds an invocation node and returns whether the instruction is supported.
168 bool BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000169 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100170 uint32_t method_idx,
171 uint32_t number_of_vreg_arguments,
172 bool is_range,
173 uint32_t* args,
174 uint32_t register_index);
175
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100176 // Builds a new array node and the instructions that fill it.
Calin Juravle225ff812014-11-13 16:46:39 +0000177 void BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100178 uint32_t type_index,
179 uint32_t number_of_vreg_arguments,
180 bool is_range,
181 uint32_t* args,
182 uint32_t register_index);
183
Calin Juravle225ff812014-11-13 16:46:39 +0000184 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000185
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100186 // Fills the given object with data as specified in the fill-array-data
187 // instruction. Currently only used for non-reference and non-floating point
188 // arrays.
189 template <typename T>
190 void BuildFillArrayData(HInstruction* object,
191 const T* data,
192 uint32_t element_count,
193 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +0000194 uint32_t dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100195
196 // Fills the given object with data as specified in the fill-array-data
197 // instruction. The data must be for long and double arrays.
198 void BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100199 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100200 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000201 uint32_t dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100202
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000203 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
204 // Returns whether we succeeded in building the instruction.
205 bool BuildTypeCheck(const Instruction& instruction,
206 uint8_t destination,
207 uint8_t reference,
208 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +0000209 uint32_t dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000210
Andreas Gampee4d4d322014-12-04 09:09:57 -0800211 // Builds an instruction sequence for a packed switch statement.
Calin Juravle48c2b032014-12-09 18:11:36 +0000212 void BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800213
Andreas Gampee4d4d322014-12-04 09:09:57 -0800214 // Builds an instruction sequence for a sparse switch statement.
Calin Juravle48c2b032014-12-09 18:11:36 +0000215 void BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -0800216
217 void BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
218 bool is_last_case, const SwitchTable& table,
219 HInstruction* value, int32_t case_value_int,
220 int32_t target_offset, uint32_t dex_pc);
221
Calin Juravle48c2b032014-12-09 18:11:36 +0000222 bool SkipCompilation(size_t number_of_dex_instructions,
223 size_t number_of_blocks,
224 size_t number_of_branches);
225
226 void MaybeRecordStat(MethodCompilationStat compilation_stat);
227
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000228 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000229
230 // A list of the size of the dex code holding block information for
231 // the method. If an entry contains a block, then the dex instruction
232 // starting at that entry is the first instruction of a new block.
233 GrowableArray<HBasicBlock*> branch_targets_;
234
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000235 GrowableArray<HLocal*> locals_;
236
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000237 HBasicBlock* entry_block_;
238 HBasicBlock* exit_block_;
239 HBasicBlock* current_block_;
240 HGraph* graph_;
241
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000242 HIntConstant* constant0_;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000243 HIntConstant* constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000244
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000245 const DexFile* const dex_file_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100246 DexCompilationUnit* const dex_compilation_unit_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100247 CompilerDriver* const compiler_driver_;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100248 const Primitive::Type return_type_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000249
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100250 // The pointer in the dex file where the instructions of the code item
251 // being currently compiled start.
252 const uint16_t* code_start_;
253
254 // The last invoke or fill-new-array being built. Only to be
255 // used by move-result instructions.
256 HInstruction* latest_result_;
257
Calin Juravle48c2b032014-12-09 18:11:36 +0000258 OptimizingCompilerStats* compilation_stats_;
259
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000260 DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
261};
262
263} // namespace art
264
265#endif // ART_COMPILER_OPTIMIZING_BUILDER_H_