builder.h revision be9a92aa804c0d210f80966b74ef8ed3987f335a
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
20818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray#include "utils/allocation.h"
21be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray#include "utils/growable_array.h"
22818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
23818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffraynamespace art {
24818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
25818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass ArenaAllocator;
26818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass Instruction;
27818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass HBasicBlock;
28818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass HGraph;
29818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
30818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass HGraphBuilder : public ValueObject {
31818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray public:
32818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  explicit HGraphBuilder(ArenaAllocator* arena)
33818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray      : arena_(arena),
34be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray        branch_targets_(arena, 0),
35818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray        entry_block_(nullptr),
36818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray        exit_block_(nullptr),
37818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray        current_block_(nullptr),
38818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray        graph_(nullptr) { }
39818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
40818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HGraph* BuildGraph(const uint16_t* start, const uint16_t* end);
41818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
42818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray private:
43818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  // Analyzes the dex instruction and adds HInstruction to the graph
44818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  // to execute that instruction. Returns whether the instruction can
45818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  // be handled.
46be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  bool AnalyzeDexInstruction(const Instruction& instruction, int32_t dex_offset);
47be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray
48be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // Finds all instructions that start a new block, and populates branch_targets_ with
49be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // the newly created blocks.
50be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  void ComputeBranchTargets(const uint16_t* start, const uint16_t* end);
51be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  void MaybeUpdateCurrentBlock(size_t index);
52be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  HBasicBlock* FindBlockStartingAt(int32_t index) const;
53818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
54818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  ArenaAllocator* const arena_;
55be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray
56be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // A list of the size of the dex code holding block information for
57be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // the method. If an entry contains a block, then the dex instruction
58be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // starting at that entry is the first instruction of a new block.
59be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  GrowableArray<HBasicBlock*> branch_targets_;
60be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray
61818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HBasicBlock* entry_block_;
62818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HBasicBlock* exit_block_;
63818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HBasicBlock* current_block_;
64818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HGraph* graph_;
65818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
66818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
67818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray};
68818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
69818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray}  // namespace art
70818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
71818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray#endif  // ART_COMPILER_OPTIMIZING_BUILDER_H_
72