builder.h revision bab4ed7057799a4fadc6283108ab56f389d117d4
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"
21818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray#include "utils/allocation.h"
22be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray#include "utils/growable_array.h"
23818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
24818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffraynamespace art {
25818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
26818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass ArenaAllocator;
27818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass Instruction;
28818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass HBasicBlock;
29818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass HGraph;
303ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffrayclass HIntConstant;
313ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffrayclass HInstruction;
323ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffrayclass HLocal;
33818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
34818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass HGraphBuilder : public ValueObject {
35818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray public:
36818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  explicit HGraphBuilder(ArenaAllocator* arena)
37818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray      : arena_(arena),
38be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray        branch_targets_(arena, 0),
393ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray        locals_(arena, 0),
40818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray        entry_block_(nullptr),
41818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray        exit_block_(nullptr),
42818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray        current_block_(nullptr),
433ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray        graph_(nullptr),
44bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray        constant0_(nullptr),
45bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray        constant1_(nullptr) { }
46818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
473ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HGraph* BuildGraph(const DexFile::CodeItem& code);
48818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
49818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray private:
50818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  // Analyzes the dex instruction and adds HInstruction to the graph
51818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  // to execute that instruction. Returns whether the instruction can
52818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  // be handled.
53be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  bool AnalyzeDexInstruction(const Instruction& instruction, int32_t dex_offset);
54be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray
55be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // Finds all instructions that start a new block, and populates branch_targets_ with
56be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // the newly created blocks.
57be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  void ComputeBranchTargets(const uint16_t* start, const uint16_t* end);
58be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  void MaybeUpdateCurrentBlock(size_t index);
59be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  HBasicBlock* FindBlockStartingAt(int32_t index) const;
60818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
613ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HIntConstant* GetConstant0();
62bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray  HIntConstant* GetConstant1();
633ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HIntConstant* GetConstant(int constant);
643ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  void InitializeLocals(int count);
653ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HLocal* GetLocalAt(int register_index) const;
663ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  void UpdateLocal(int register_index, HInstruction* instruction) const;
673ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HInstruction* LoadLocal(int register_index) const;
683ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray
69818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  ArenaAllocator* const arena_;
70be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray
71be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // A list of the size of the dex code holding block information for
72be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // the method. If an entry contains a block, then the dex instruction
73be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // starting at that entry is the first instruction of a new block.
74be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  GrowableArray<HBasicBlock*> branch_targets_;
75be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray
763ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  GrowableArray<HLocal*> locals_;
773ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray
78818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HBasicBlock* entry_block_;
79818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HBasicBlock* exit_block_;
80818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HBasicBlock* current_block_;
81818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HGraph* graph_;
82818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
833ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HIntConstant* constant0_;
84bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray  HIntConstant* constant1_;
853ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray
86818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
87818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray};
88818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
89818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray}  // namespace art
90818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
91818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray#endif  // ART_COMPILER_OPTIMIZING_BUILDER_H_
92