blob: c5101363eee404f55b64eefbd885addd144afa61 [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:
David Brazdil5e8b1372015-01-23 14:39:08 +000037 HGraphBuilder(HGraph* graph,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010038 DexCompilationUnit* dex_compilation_unit,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000039 const DexCompilationUnit* const outer_compilation_unit,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010040 const DexFile* dex_file,
Calin Juravle48c2b032014-12-09 18:11:36 +000041 CompilerDriver* driver,
42 OptimizingCompilerStats* compiler_stats)
David Brazdil5e8b1372015-01-23 14:39:08 +000043 : arena_(graph->GetArena()),
44 branch_targets_(graph->GetArena(), 0),
45 locals_(graph->GetArena(), 0),
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046 entry_block_(nullptr),
47 exit_block_(nullptr),
48 current_block_(nullptr),
David Brazdil5e8b1372015-01-23 14:39:08 +000049 graph_(graph),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000050 constant0_(nullptr),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000051 constant1_(nullptr),
52 dex_file_(dex_file),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053 dex_compilation_unit_(dex_compilation_unit),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010054 compiler_driver_(driver),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000055 outer_compilation_unit_(outer_compilation_unit),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +010056 return_type_(Primitive::GetType(dex_compilation_unit_->GetShorty()[0])),
57 code_start_(nullptr),
Calin Juravle48c2b032014-12-09 18:11:36 +000058 latest_result_(nullptr),
59 compilation_stats_(compiler_stats) {}
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010060
61 // Only for unit testing.
David Brazdil5e8b1372015-01-23 14:39:08 +000062 HGraphBuilder(HGraph* graph, Primitive::Type return_type = Primitive::kPrimInt)
63 : arena_(graph->GetArena()),
64 branch_targets_(graph->GetArena(), 0),
65 locals_(graph->GetArena(), 0),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010066 entry_block_(nullptr),
67 exit_block_(nullptr),
68 current_block_(nullptr),
David Brazdil5e8b1372015-01-23 14:39:08 +000069 graph_(graph),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010070 constant0_(nullptr),
71 constant1_(nullptr),
72 dex_file_(nullptr),
73 dex_compilation_unit_(nullptr),
74 compiler_driver_(nullptr),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000075 outer_compilation_unit_(nullptr),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +010076 return_type_(return_type),
77 code_start_(nullptr),
Calin Juravle48c2b032014-12-09 18:11:36 +000078 latest_result_(nullptr),
79 compilation_stats_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000080
David Brazdil5e8b1372015-01-23 14:39:08 +000081 bool BuildGraph(const DexFile::CodeItem& code);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000082
83 private:
84 // Analyzes the dex instruction and adds HInstruction to the graph
85 // to execute that instruction. Returns whether the instruction can
86 // be handled.
Calin Juravle225ff812014-11-13 16:46:39 +000087 bool AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000088
89 // Finds all instructions that start a new block, and populates branch_targets_ with
90 // the newly created blocks.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +000091 // As a side effect, also compute the number of dex instructions, blocks, and
92 // branches.
93 void ComputeBranchTargets(const uint16_t* start,
94 const uint16_t* end,
95 size_t* number_of_dex_instructions,
96 size_t* number_of_block,
97 size_t* number_of_branches);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000098 void MaybeUpdateCurrentBlock(size_t index);
99 HBasicBlock* FindBlockStartingAt(int32_t index) const;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000100
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100101 HIntConstant* GetIntConstant0();
102 HIntConstant* GetIntConstant1();
103 HIntConstant* GetIntConstant(int32_t constant);
104 HLongConstant* GetLongConstant(int64_t constant);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100105 void InitializeLocals(uint16_t count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000106 HLocal* GetLocalAt(int register_index) const;
107 void UpdateLocal(int register_index, HInstruction* instruction) const;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100108 HInstruction* LoadLocal(int register_index, Primitive::Type type) const;
David Brazdil852eaff2015-02-02 15:23:05 +0000109 void PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000110 void InitializeParameters(uint16_t number_of_parameters);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +0000111 bool NeedsAccessCheck(uint32_t type_index) const;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100112
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100113 template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100114 void Unop_12x(const Instruction& instruction, Primitive::Type type);
115
116 template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100117 void Binop_23x(const Instruction& instruction, Primitive::Type type);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100118
119 template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000120 void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
121
122 template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000123 void Binop_23x_shift(const Instruction& instruction, Primitive::Type type);
124
Calin Juravleddb7df22014-11-25 20:56:51 +0000125 void Binop_23x_cmp(const Instruction& instruction, Primitive::Type type, HCompare::Bias bias);
126
Calin Juravle9aec02f2014-11-18 23:06:35 +0000127 template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100128 void Binop_12x(const Instruction& instruction, Primitive::Type type);
129
130 template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000131 void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
132
133 template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000134 void Binop_12x_shift(const Instruction& instruction, Primitive::Type type);
135
136 template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100137 void Binop_22b(const Instruction& instruction, bool reverse);
138
139 template<typename T>
140 void Binop_22s(const Instruction& instruction, bool reverse);
141
Calin Juravle225ff812014-11-13 16:46:39 +0000142 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
143 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100144
Roland Levillaindff1f282014-11-05 14:15:05 +0000145 void Conversion_12x(const Instruction& instruction,
146 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000147 Primitive::Type result_type,
148 uint32_t dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +0000149
Calin Juravlebacfec32014-11-14 15:54:36 +0000150 void BuildCheckedDivRem(uint16_t out_reg,
151 uint16_t first_reg,
152 int64_t second_reg_or_constant,
153 uint32_t dex_pc,
154 Primitive::Type type,
155 bool second_is_lit,
156 bool is_div);
Calin Juravled0d48522014-11-04 16:40:20 +0000157
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100158 void BuildReturn(const Instruction& instruction, Primitive::Type type);
159
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100160 // Builds an instance field access node and returns whether the instruction is supported.
Calin Juravle225ff812014-11-13 16:46:39 +0000161 bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100162
163 // Builds a static field access node and returns whether the instruction is supported.
Calin Juravle225ff812014-11-13 16:46:39 +0000164 bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100165
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100166 void BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000167 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100168 bool is_get,
169 Primitive::Type anticipated_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100170
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100171 // Builds an invocation node and returns whether the instruction is supported.
172 bool BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000173 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100174 uint32_t method_idx,
175 uint32_t number_of_vreg_arguments,
176 bool is_range,
177 uint32_t* args,
178 uint32_t register_index);
179
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100180 // Builds a new array node and the instructions that fill it.
Calin Juravle225ff812014-11-13 16:46:39 +0000181 void BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100182 uint32_t type_index,
183 uint32_t number_of_vreg_arguments,
184 bool is_range,
185 uint32_t* args,
186 uint32_t register_index);
187
Calin Juravle225ff812014-11-13 16:46:39 +0000188 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000189
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100190 // Fills the given object with data as specified in the fill-array-data
191 // instruction. Currently only used for non-reference and non-floating point
192 // arrays.
193 template <typename T>
194 void BuildFillArrayData(HInstruction* object,
195 const T* data,
196 uint32_t element_count,
197 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +0000198 uint32_t dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100199
200 // Fills the given object with data as specified in the fill-array-data
201 // instruction. The data must be for long and double arrays.
202 void BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100203 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100204 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000205 uint32_t dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100206
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000207 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
208 // Returns whether we succeeded in building the instruction.
209 bool BuildTypeCheck(const Instruction& instruction,
210 uint8_t destination,
211 uint8_t reference,
212 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +0000213 uint32_t dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000214
Andreas Gampee4d4d322014-12-04 09:09:57 -0800215 // Builds an instruction sequence for a packed switch statement.
Calin Juravle48c2b032014-12-09 18:11:36 +0000216 void BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800217
Andreas Gampee4d4d322014-12-04 09:09:57 -0800218 // Builds an instruction sequence for a sparse switch statement.
Calin Juravle48c2b032014-12-09 18:11:36 +0000219 void BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -0800220
221 void BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
222 bool is_last_case, const SwitchTable& table,
223 HInstruction* value, int32_t case_value_int,
224 int32_t target_offset, uint32_t dex_pc);
225
Calin Juravle48c2b032014-12-09 18:11:36 +0000226 bool SkipCompilation(size_t number_of_dex_instructions,
227 size_t number_of_blocks,
228 size_t number_of_branches);
229
230 void MaybeRecordStat(MethodCompilationStat compilation_stat);
231
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000232 // Returns whether `type_index` points to the outer-most compiling method's class.
233 bool IsCompilingClass(uint16_t type_index) const {
234 uint32_t referrer_index = outer_compilation_unit_->GetDexMethodIndex();
235 const DexFile::MethodId& method_id =
236 outer_compilation_unit_->GetDexFile()->GetMethodId(referrer_index);
237 return method_id.class_idx_ == type_index;
238 }
239
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000240 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000241
242 // A list of the size of the dex code holding block information for
243 // the method. If an entry contains a block, then the dex instruction
244 // starting at that entry is the first instruction of a new block.
245 GrowableArray<HBasicBlock*> branch_targets_;
246
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000247 GrowableArray<HLocal*> locals_;
248
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000249 HBasicBlock* entry_block_;
250 HBasicBlock* exit_block_;
251 HBasicBlock* current_block_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000252 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000253
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000254 HIntConstant* constant0_;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000255 HIntConstant* constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000256
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000257 // The dex file where the method being compiled is.
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000258 const DexFile* const dex_file_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000259
260 // The compilation unit of the current method being compiled. Note that
261 // it can be an inlined method.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100262 DexCompilationUnit* const dex_compilation_unit_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000263
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100264 CompilerDriver* const compiler_driver_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000265
266 // The compilation unit of the outermost method being compiled. That is the
267 // method being compiled (and not inlined), and potentially inlining other
268 // methods.
269 const DexCompilationUnit* const outer_compilation_unit_;
270
271 // The return type of the method being compiled.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100272 const Primitive::Type return_type_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000273
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100274 // The pointer in the dex file where the instructions of the code item
275 // being currently compiled start.
276 const uint16_t* code_start_;
277
278 // The last invoke or fill-new-array being built. Only to be
279 // used by move-result instructions.
280 HInstruction* latest_result_;
281
Calin Juravle48c2b032014-12-09 18:11:36 +0000282 OptimizingCompilerStats* compilation_stats_;
283
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000284 DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
285};
286
287} // namespace art
288
289#endif // ART_COMPILER_OPTIMIZING_BUILDER_H_