builder.h revision 8ccc3f5d06fd217cdaabd37e743adab2031d3720
1818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray/*
2818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray * Copyright (C) 2014 The Android Open Source Project
3818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray *
4818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License");
5818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray * you may not use this file except in compliance with the License.
6818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray * You may obtain a copy of the License at
7818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray *
8818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray *      http://www.apache.org/licenses/LICENSE-2.0
9818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray *
10818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray * Unless required by applicable law or agreed to in writing, software
11818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS,
12818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray * See the License for the specific language governing permissions and
14818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray * limitations under the License.
15818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray */
16818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
17818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray#ifndef ART_COMPILER_OPTIMIZING_BUILDER_H_
18818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray#define ART_COMPILER_OPTIMIZING_BUILDER_H_
19818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
203ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray#include "dex_file.h"
218ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray#include "driver/dex_compilation_unit.h"
22818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray#include "utils/allocation.h"
23be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray#include "utils/growable_array.h"
24818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
25818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffraynamespace art {
26818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
27818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass ArenaAllocator;
28818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass Instruction;
29818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass HBasicBlock;
30818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass HGraph;
313ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffrayclass HIntConstant;
323ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffrayclass HInstruction;
333ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffrayclass HLocal;
34818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
35818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass HGraphBuilder : public ValueObject {
36818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray public:
378ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  HGraphBuilder(ArenaAllocator* arena,
388ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray                const DexCompilationUnit* dex_compilation_unit = nullptr,
398ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray                const DexFile* dex_file = nullptr)
40818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray      : arena_(arena),
41be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray        branch_targets_(arena, 0),
423ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray        locals_(arena, 0),
43818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray        entry_block_(nullptr),
44818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray        exit_block_(nullptr),
45818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray        current_block_(nullptr),
463ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray        graph_(nullptr),
47bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray        constant0_(nullptr),
488ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray        constant1_(nullptr),
498ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray        dex_file_(dex_file),
508ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray        dex_compilation_unit_(dex_compilation_unit) { }
51818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
523ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HGraph* BuildGraph(const DexFile::CodeItem& code);
53818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
54818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray private:
55818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  // Analyzes the dex instruction and adds HInstruction to the graph
56818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  // to execute that instruction. Returns whether the instruction can
57818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  // be handled.
58be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  bool AnalyzeDexInstruction(const Instruction& instruction, int32_t dex_offset);
59be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray
60be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // Finds all instructions that start a new block, and populates branch_targets_ with
61be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // the newly created blocks.
62be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  void ComputeBranchTargets(const uint16_t* start, const uint16_t* end);
63be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  void MaybeUpdateCurrentBlock(size_t index);
64be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  HBasicBlock* FindBlockStartingAt(int32_t index) const;
65818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
663ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HIntConstant* GetConstant0();
67bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray  HIntConstant* GetConstant1();
683ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HIntConstant* GetConstant(int constant);
693ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  void InitializeLocals(int count);
703ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HLocal* GetLocalAt(int register_index) const;
713ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  void UpdateLocal(int register_index, HInstruction* instruction) const;
723ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HInstruction* LoadLocal(int register_index) const;
733ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray
74818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  ArenaAllocator* const arena_;
75be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray
76be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // A list of the size of the dex code holding block information for
77be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // the method. If an entry contains a block, then the dex instruction
78be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // starting at that entry is the first instruction of a new block.
79be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  GrowableArray<HBasicBlock*> branch_targets_;
80be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray
813ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  GrowableArray<HLocal*> locals_;
823ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray
83818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HBasicBlock* entry_block_;
84818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HBasicBlock* exit_block_;
85818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HBasicBlock* current_block_;
86818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HGraph* graph_;
87818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
883ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HIntConstant* constant0_;
89bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray  HIntConstant* constant1_;
903ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray
918ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  const DexFile* const dex_file_;
928ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  const DexCompilationUnit* const dex_compilation_unit_;
938ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray
94818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
95818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray};
96818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
97818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray}  // namespace art
98818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
99818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray#endif  // ART_COMPILER_OPTIMIZING_BUILDER_H_
100