blob: 60d998224ce63e1bf9f26046e90cc09327833e1d [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 Geoffray818f2102014-02-18 16:43:35 +000022#include "utils/allocation.h"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000023#include "utils/growable_array.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000024
25namespace art {
26
27class ArenaAllocator;
28class Instruction;
29class HBasicBlock;
30class HGraph;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000031class HIntConstant;
32class HInstruction;
33class HLocal;
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,
38 const DexCompilationUnit* dex_compilation_unit = nullptr,
39 const DexFile* dex_file = nullptr)
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),
50 dex_compilation_unit_(dex_compilation_unit) { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052 HGraph* BuildGraph(const DexFile::CodeItem& code);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000053
54 private:
55 // Analyzes the dex instruction and adds HInstruction to the graph
56 // to execute that instruction. Returns whether the instruction can
57 // be handled.
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000058 bool AnalyzeDexInstruction(const Instruction& instruction, int32_t dex_offset);
59
60 // Finds all instructions that start a new block, and populates branch_targets_ with
61 // the newly created blocks.
62 void ComputeBranchTargets(const uint16_t* start, const uint16_t* end);
63 void MaybeUpdateCurrentBlock(size_t index);
64 HBasicBlock* FindBlockStartingAt(int32_t index) const;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000065
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000066 HIntConstant* GetConstant0();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000067 HIntConstant* GetConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000068 HIntConstant* GetConstant(int 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;
72 HInstruction* LoadLocal(int register_index) const;
73
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010074 // Temporarily returns whether the compiler supports the parameters
75 // of the method.
76 bool InitializeParameters(uint16_t number_of_parameters);
77
78 template<typename T> void Binop_32x(const Instruction& instruction);
79 template<typename T> void Binop_12x(const Instruction& instruction);
80 template<typename T> void Binop_22b(const Instruction& instruction, bool reverse);
81 template<typename T> void Binop_22s(const Instruction& instruction, bool reverse);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010082 template<typename T> void If_22t(const Instruction& instruction, int32_t dex_offset, bool is_not);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010083
Nicolas Geoffray818f2102014-02-18 16:43:35 +000084 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000085
86 // A list of the size of the dex code holding block information for
87 // the method. If an entry contains a block, then the dex instruction
88 // starting at that entry is the first instruction of a new block.
89 GrowableArray<HBasicBlock*> branch_targets_;
90
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000091 GrowableArray<HLocal*> locals_;
92
Nicolas Geoffray818f2102014-02-18 16:43:35 +000093 HBasicBlock* entry_block_;
94 HBasicBlock* exit_block_;
95 HBasicBlock* current_block_;
96 HGraph* graph_;
97
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000098 HIntConstant* constant0_;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000099 HIntConstant* constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000100
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000101 const DexFile* const dex_file_;
102 const DexCompilationUnit* const dex_compilation_unit_;
103
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000104 DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
105};
106
107} // namespace art
108
109#endif // ART_COMPILER_OPTIMIZING_BUILDER_H_