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"
21e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray#include "driver/compiler_driver.h"
228ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray#include "driver/dex_compilation_unit.h"
2301bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray#include "primitive.h"
24818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray#include "utils/allocation.h"
25be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray#include "utils/growable_array.h"
2620dfc797dc631bf8d655dcf123f46f13332d3074Dave Allison#include "nodes.h"
27818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
28818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffraynamespace art {
29818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
30818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass Instruction;
31818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
32818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass HGraphBuilder : public ValueObject {
33818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray public:
348ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  HGraphBuilder(ArenaAllocator* arena,
3501bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray                DexCompilationUnit* dex_compilation_unit = nullptr,
36e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray                const DexFile* dex_file = nullptr,
37e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray                CompilerDriver* driver = nullptr)
38818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray      : arena_(arena),
39be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray        branch_targets_(arena, 0),
403ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray        locals_(arena, 0),
41818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray        entry_block_(nullptr),
42818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray        exit_block_(nullptr),
43818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray        current_block_(nullptr),
443ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray        graph_(nullptr),
45bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray        constant0_(nullptr),
468ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray        constant1_(nullptr),
478ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray        dex_file_(dex_file),
48e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray        dex_compilation_unit_(dex_compilation_unit),
49e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray        compiler_driver_(driver) {}
50818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
513ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HGraph* BuildGraph(const DexFile::CodeItem& code);
52818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
53818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray private:
54818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  // Analyzes the dex instruction and adds HInstruction to the graph
55818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  // to execute that instruction. Returns whether the instruction can
56818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  // be handled.
57be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  bool AnalyzeDexInstruction(const Instruction& instruction, int32_t dex_offset);
58be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray
59be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // Finds all instructions that start a new block, and populates branch_targets_ with
60be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // the newly created blocks.
61be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  void ComputeBranchTargets(const uint16_t* start, const uint16_t* end);
62be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  void MaybeUpdateCurrentBlock(size_t index);
63be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  HBasicBlock* FindBlockStartingAt(int32_t index) const;
64818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
6501bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  HIntConstant* GetIntConstant0();
6601bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  HIntConstant* GetIntConstant1();
6701bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  HIntConstant* GetIntConstant(int32_t constant);
6801bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  HLongConstant* GetLongConstant(int64_t constant);
69f583e5976e1de9aa206fb8de4f91000180685066Nicolas Geoffray  void InitializeLocals(uint16_t count);
703ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HLocal* GetLocalAt(int register_index) const;
713ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  void UpdateLocal(int register_index, HInstruction* instruction) const;
7201bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  HInstruction* LoadLocal(int register_index, Primitive::Type type) const;
733ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray
74f583e5976e1de9aa206fb8de4f91000180685066Nicolas Geoffray  // Temporarily returns whether the compiler supports the parameters
75f583e5976e1de9aa206fb8de4f91000180685066Nicolas Geoffray  // of the method.
76f583e5976e1de9aa206fb8de4f91000180685066Nicolas Geoffray  bool InitializeParameters(uint16_t number_of_parameters);
77f583e5976e1de9aa206fb8de4f91000180685066Nicolas Geoffray
7801bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  template<typename T>
79412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  void Binop_23x(const Instruction& instruction, Primitive::Type type);
8001bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray
8101bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  template<typename T>
8201bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  void Binop_12x(const Instruction& instruction, Primitive::Type type);
8301bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray
8401bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  template<typename T>
8501bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  void Binop_22b(const Instruction& instruction, bool reverse);
8601bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray
8701bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  template<typename T>
8801bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  void Binop_22s(const Instruction& instruction, bool reverse);
8901bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray
90e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray  template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_offset);
91e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray  template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_offset);
92f583e5976e1de9aa206fb8de4f91000180685066Nicolas Geoffray
9301bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  void BuildReturn(const Instruction& instruction, Primitive::Type type);
9401bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray
95e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray  bool BuildFieldAccess(const Instruction& instruction, uint32_t dex_offset, bool is_get);
963c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  void BuildArrayAccess(const Instruction& instruction,
973c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray                        uint32_t dex_offset,
983c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray                        bool is_get,
993c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray                        Primitive::Type anticipated_type);
100e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray
10101bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  // Builds an invocation node and returns whether the instruction is supported.
10201bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  bool BuildInvoke(const Instruction& instruction,
10301bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray                   uint32_t dex_offset,
10401bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray                   uint32_t method_idx,
10501bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray                   uint32_t number_of_vreg_arguments,
10601bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray                   bool is_range,
10701bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray                   uint32_t* args,
10801bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray                   uint32_t register_index);
10901bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray
110818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  ArenaAllocator* const arena_;
111be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray
112be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // A list of the size of the dex code holding block information for
113be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // the method. If an entry contains a block, then the dex instruction
114be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // starting at that entry is the first instruction of a new block.
115be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  GrowableArray<HBasicBlock*> branch_targets_;
116be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray
1173ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  GrowableArray<HLocal*> locals_;
1183ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray
119818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HBasicBlock* entry_block_;
120818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HBasicBlock* exit_block_;
121818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HBasicBlock* current_block_;
122818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HGraph* graph_;
123818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
1243ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HIntConstant* constant0_;
125bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray  HIntConstant* constant1_;
1263ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray
1278ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  const DexFile* const dex_file_;
12801bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  DexCompilationUnit* const dex_compilation_unit_;
129e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray  CompilerDriver* const compiler_driver_;
1308ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray
131818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
132818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray};
133818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
134818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray}  // namespace art
135818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
136818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray#endif  // ART_COMPILER_OPTIMIZING_BUILDER_H_
137