blob: fff83a120531d1b3de44dfbde8045740a6986a8b [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 Geoffray818f2102014-02-18 16:43:35 +000021#include "utils/allocation.h"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000022#include "utils/growable_array.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023
24namespace art {
25
26class ArenaAllocator;
27class Instruction;
28class HBasicBlock;
29class HGraph;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000030class HIntConstant;
31class HInstruction;
32class HLocal;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033
34class HGraphBuilder : public ValueObject {
35 public:
36 explicit HGraphBuilder(ArenaAllocator* arena)
37 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000038 branch_targets_(arena, 0),
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000039 locals_(arena, 0),
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040 entry_block_(nullptr),
41 exit_block_(nullptr),
42 current_block_(nullptr),
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000043 graph_(nullptr),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000044 constant0_(nullptr),
45 constant1_(nullptr) { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000047 HGraph* BuildGraph(const DexFile::CodeItem& code);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000048
49 private:
50 // Analyzes the dex instruction and adds HInstruction to the graph
51 // to execute that instruction. Returns whether the instruction can
52 // be handled.
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000053 bool AnalyzeDexInstruction(const Instruction& instruction, int32_t dex_offset);
54
55 // Finds all instructions that start a new block, and populates branch_targets_ with
56 // the newly created blocks.
57 void ComputeBranchTargets(const uint16_t* start, const uint16_t* end);
58 void MaybeUpdateCurrentBlock(size_t index);
59 HBasicBlock* FindBlockStartingAt(int32_t index) const;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000060
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000061 HIntConstant* GetConstant0();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000062 HIntConstant* GetConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000063 HIntConstant* GetConstant(int constant);
64 void InitializeLocals(int count);
65 HLocal* GetLocalAt(int register_index) const;
66 void UpdateLocal(int register_index, HInstruction* instruction) const;
67 HInstruction* LoadLocal(int register_index) const;
68
Nicolas Geoffray818f2102014-02-18 16:43:35 +000069 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000070
71 // A list of the size of the dex code holding block information for
72 // the method. If an entry contains a block, then the dex instruction
73 // starting at that entry is the first instruction of a new block.
74 GrowableArray<HBasicBlock*> branch_targets_;
75
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000076 GrowableArray<HLocal*> locals_;
77
Nicolas Geoffray818f2102014-02-18 16:43:35 +000078 HBasicBlock* entry_block_;
79 HBasicBlock* exit_block_;
80 HBasicBlock* current_block_;
81 HGraph* graph_;
82
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000083 HIntConstant* constant0_;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000084 HIntConstant* constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000085
Nicolas Geoffray818f2102014-02-18 16:43:35 +000086 DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
87};
88
89} // namespace art
90
91#endif // ART_COMPILER_OPTIMIZING_BUILDER_H_