blob: 0852a26c55cb1751da25c61f1e214ffff18e9c24 [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 Geoffray8ccc3f52014-03-19 10:34:11 +000021#include "driver/dex_compilation_unit.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010022#include "primitive.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "utils/allocation.h"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000024#include "utils/growable_array.h"
Dave Allison20dfc792014-06-16 20:44:29 -070025#include "nodes.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000026
27namespace art {
28
Nicolas Geoffray818f2102014-02-18 16:43:35 +000029class Instruction;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030
31class HGraphBuilder : public ValueObject {
32 public:
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000033 HGraphBuilder(ArenaAllocator* arena,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010034 DexCompilationUnit* dex_compilation_unit = nullptr,
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000035 const DexFile* dex_file = nullptr)
Nicolas Geoffray818f2102014-02-18 16:43:35 +000036 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000037 branch_targets_(arena, 0),
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000038 locals_(arena, 0),
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039 entry_block_(nullptr),
40 exit_block_(nullptr),
41 current_block_(nullptr),
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000042 graph_(nullptr),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000043 constant0_(nullptr),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000044 constant1_(nullptr),
45 dex_file_(dex_file),
46 dex_compilation_unit_(dex_compilation_unit) { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000047
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000048 HGraph* BuildGraph(const DexFile::CodeItem& code);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000049
50 private:
51 // Analyzes the dex instruction and adds HInstruction to the graph
52 // to execute that instruction. Returns whether the instruction can
53 // be handled.
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000054 bool AnalyzeDexInstruction(const Instruction& instruction, int32_t dex_offset);
55
56 // Finds all instructions that start a new block, and populates branch_targets_ with
57 // the newly created blocks.
58 void ComputeBranchTargets(const uint16_t* start, const uint16_t* end);
59 void MaybeUpdateCurrentBlock(size_t index);
60 HBasicBlock* FindBlockStartingAt(int32_t index) const;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000061
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010062 HIntConstant* GetIntConstant0();
63 HIntConstant* GetIntConstant1();
64 HIntConstant* GetIntConstant(int32_t constant);
65 HLongConstant* GetLongConstant(int64_t constant);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010066 void InitializeLocals(uint16_t count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000067 HLocal* GetLocalAt(int register_index) const;
68 void UpdateLocal(int register_index, HInstruction* instruction) const;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010069 HInstruction* LoadLocal(int register_index, Primitive::Type type) const;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000070
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010071 // Temporarily returns whether the compiler supports the parameters
72 // of the method.
73 bool InitializeParameters(uint16_t number_of_parameters);
74
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010075 template<typename T>
76 void Binop_32x(const Instruction& instruction, Primitive::Type type);
77
78 template<typename T>
79 void Binop_12x(const Instruction& instruction, Primitive::Type type);
80
81 template<typename T>
82 void Binop_22b(const Instruction& instruction, bool reverse);
83
84 template<typename T>
85 void Binop_22s(const Instruction& instruction, bool reverse);
86
Dave Allison20dfc792014-06-16 20:44:29 -070087 template<typename T>
88 void If_22t(const Instruction& instruction, int32_t dex_offset);
89
90 template<typename T>
91 void If_21t(const Instruction& instruction, int32_t dex_offset);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010092
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010093 void BuildReturn(const Instruction& instruction, Primitive::Type type);
94
95 // Builds an invocation node and returns whether the instruction is supported.
96 bool BuildInvoke(const Instruction& instruction,
97 uint32_t dex_offset,
98 uint32_t method_idx,
99 uint32_t number_of_vreg_arguments,
100 bool is_range,
101 uint32_t* args,
102 uint32_t register_index);
103
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000104 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000105
106 // A list of the size of the dex code holding block information for
107 // the method. If an entry contains a block, then the dex instruction
108 // starting at that entry is the first instruction of a new block.
109 GrowableArray<HBasicBlock*> branch_targets_;
110
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000111 GrowableArray<HLocal*> locals_;
112
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000113 HBasicBlock* entry_block_;
114 HBasicBlock* exit_block_;
115 HBasicBlock* current_block_;
116 HGraph* graph_;
117
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000118 HIntConstant* constant0_;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000119 HIntConstant* constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000120
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000121 const DexFile* const dex_file_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100122 DexCompilationUnit* const dex_compilation_unit_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000123
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000124 DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
125};
126
127} // namespace art
128
129#endif // ART_COMPILER_OPTIMIZING_BUILDER_H_