blob: e143786be7db953e314e858c01bb2220c83013a6 [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 Geoffraye5038322014-07-04 09:41:32 +010021#include "driver/compiler_driver.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000022#include "driver/dex_compilation_unit.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010023#include "primitive.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000024#include "utils/allocation.h"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000025#include "utils/growable_array.h"
Dave Allison20dfc792014-06-16 20:44:29 -070026#include "nodes.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000027
28namespace art {
29
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030class Instruction;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031
32class HGraphBuilder : public ValueObject {
33 public:
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034 HGraphBuilder(ArenaAllocator* arena,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035 DexCompilationUnit* dex_compilation_unit = nullptr,
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036 const DexFile* dex_file = nullptr,
37 CompilerDriver* driver = nullptr)
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000039 branch_targets_(arena, 0),
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000040 locals_(arena, 0),
Nicolas Geoffray818f2102014-02-18 16:43:35 +000041 entry_block_(nullptr),
42 exit_block_(nullptr),
43 current_block_(nullptr),
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000044 graph_(nullptr),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000045 constant0_(nullptr),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000046 constant1_(nullptr),
47 dex_file_(dex_file),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010048 dex_compilation_unit_(dex_compilation_unit),
49 compiler_driver_(driver) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051 HGraph* BuildGraph(const DexFile::CodeItem& code);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
53 private:
54 // Analyzes the dex instruction and adds HInstruction to the graph
55 // to execute that instruction. Returns whether the instruction can
56 // be handled.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000057 bool AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000058
59 // Finds all instructions that start a new block, and populates branch_targets_ with
60 // the newly created blocks.
61 void ComputeBranchTargets(const uint16_t* start, const uint16_t* end);
62 void MaybeUpdateCurrentBlock(size_t index);
63 HBasicBlock* FindBlockStartingAt(int32_t index) const;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000064
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010065 HIntConstant* GetIntConstant0();
66 HIntConstant* GetIntConstant1();
67 HIntConstant* GetIntConstant(int32_t constant);
68 HLongConstant* GetLongConstant(int64_t constant);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010069 void InitializeLocals(uint16_t count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000070 HLocal* GetLocalAt(int register_index) const;
71 void UpdateLocal(int register_index, HInstruction* instruction) const;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010072 HInstruction* LoadLocal(int register_index, Primitive::Type type) const;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000073 void PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000074
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010075 // Temporarily returns whether the compiler supports the parameters
76 // of the method.
77 bool InitializeParameters(uint16_t number_of_parameters);
78
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010079 template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +010080 void Binop_23x(const Instruction& instruction, Primitive::Type type);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010081
82 template<typename T>
83 void Binop_12x(const Instruction& instruction, Primitive::Type type);
84
85 template<typename T>
86 void Binop_22b(const Instruction& instruction, bool reverse);
87
88 template<typename T>
89 void Binop_22s(const Instruction& instruction, bool reverse);
90
Nicolas Geoffraye5038322014-07-04 09:41:32 +010091 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_offset);
92 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_offset);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010093
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010094 void BuildReturn(const Instruction& instruction, Primitive::Type type);
95
Nicolas Geoffraye5038322014-07-04 09:41:32 +010096 bool BuildFieldAccess(const Instruction& instruction, uint32_t dex_offset, bool is_get);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010097 void BuildArrayAccess(const Instruction& instruction,
98 uint32_t dex_offset,
99 bool is_get,
100 Primitive::Type anticipated_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100101
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100102 // Builds an invocation node and returns whether the instruction is supported.
103 bool BuildInvoke(const Instruction& instruction,
104 uint32_t dex_offset,
105 uint32_t method_idx,
106 uint32_t number_of_vreg_arguments,
107 bool is_range,
108 uint32_t* args,
109 uint32_t register_index);
110
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000111 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000112
113 // A list of the size of the dex code holding block information for
114 // the method. If an entry contains a block, then the dex instruction
115 // starting at that entry is the first instruction of a new block.
116 GrowableArray<HBasicBlock*> branch_targets_;
117
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000118 GrowableArray<HLocal*> locals_;
119
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000120 HBasicBlock* entry_block_;
121 HBasicBlock* exit_block_;
122 HBasicBlock* current_block_;
123 HGraph* graph_;
124
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000125 HIntConstant* constant0_;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000126 HIntConstant* constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000127
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000128 const DexFile* const dex_file_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100129 DexCompilationUnit* const dex_compilation_unit_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100130 CompilerDriver* const compiler_driver_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000131
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000132 DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
133};
134
135} // namespace art
136
137#endif // ART_COMPILER_OPTIMIZING_BUILDER_H_