builder.h revision 01bc96d007b67fdb7fe349232a83e4b354ce3d08
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"
2201bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray#include "primitive.h"
23818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray#include "utils/allocation.h"
24be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray#include "utils/growable_array.h"
25818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
26818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffraynamespace art {
27818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
28818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass ArenaAllocator;
29818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass Instruction;
30818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass HBasicBlock;
31818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass HGraph;
323ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffrayclass HIntConstant;
3301bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffrayclass HLongConstant;
343ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffrayclass HInstruction;
353ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffrayclass HLocal;
36818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
37818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffrayclass HGraphBuilder : public ValueObject {
38818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray public:
398ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  HGraphBuilder(ArenaAllocator* arena,
4001bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray                DexCompilationUnit* dex_compilation_unit = nullptr,
418ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray                const DexFile* dex_file = nullptr)
42818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray      : arena_(arena),
43be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray        branch_targets_(arena, 0),
443ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray        locals_(arena, 0),
45818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray        entry_block_(nullptr),
46818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray        exit_block_(nullptr),
47818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray        current_block_(nullptr),
483ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray        graph_(nullptr),
49bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray        constant0_(nullptr),
508ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray        constant1_(nullptr),
518ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray        dex_file_(dex_file),
528ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray        dex_compilation_unit_(dex_compilation_unit) { }
53818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
543ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HGraph* BuildGraph(const DexFile::CodeItem& code);
55818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
56818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray private:
57818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  // Analyzes the dex instruction and adds HInstruction to the graph
58818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  // to execute that instruction. Returns whether the instruction can
59818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  // be handled.
60be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  bool AnalyzeDexInstruction(const Instruction& instruction, int32_t dex_offset);
61be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray
62be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // Finds all instructions that start a new block, and populates branch_targets_ with
63be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // the newly created blocks.
64be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  void ComputeBranchTargets(const uint16_t* start, const uint16_t* end);
65be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  void MaybeUpdateCurrentBlock(size_t index);
66be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  HBasicBlock* FindBlockStartingAt(int32_t index) const;
67818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
6801bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  HIntConstant* GetIntConstant0();
6901bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  HIntConstant* GetIntConstant1();
7001bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  HIntConstant* GetIntConstant(int32_t constant);
7101bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  HLongConstant* GetLongConstant(int64_t constant);
72f583e5976e1de9aa206fb8de4f91000180685066Nicolas Geoffray  void InitializeLocals(uint16_t count);
733ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HLocal* GetLocalAt(int register_index) const;
743ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  void UpdateLocal(int register_index, HInstruction* instruction) const;
7501bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  HInstruction* LoadLocal(int register_index, Primitive::Type type) const;
763ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray
77f583e5976e1de9aa206fb8de4f91000180685066Nicolas Geoffray  // Temporarily returns whether the compiler supports the parameters
78f583e5976e1de9aa206fb8de4f91000180685066Nicolas Geoffray  // of the method.
79f583e5976e1de9aa206fb8de4f91000180685066Nicolas Geoffray  bool InitializeParameters(uint16_t number_of_parameters);
80f583e5976e1de9aa206fb8de4f91000180685066Nicolas Geoffray
8101bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  template<typename T>
8201bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  void Binop_32x(const Instruction& instruction, Primitive::Type type);
8301bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray
8401bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  template<typename T>
8501bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  void Binop_12x(const Instruction& instruction, Primitive::Type type);
8601bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray
8701bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  template<typename T>
8801bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  void Binop_22b(const Instruction& instruction, bool reverse);
8901bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray
9001bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  template<typename T>
9101bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  void Binop_22s(const Instruction& instruction, bool reverse);
9201bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray
93b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray  template<typename T> void If_22t(const Instruction& instruction, int32_t dex_offset, bool is_not);
94f583e5976e1de9aa206fb8de4f91000180685066Nicolas Geoffray
9501bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  void BuildReturn(const Instruction& instruction, Primitive::Type type);
9601bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray
9701bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  // Builds an invocation node and returns whether the instruction is supported.
9801bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  bool BuildInvoke(const Instruction& instruction,
9901bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray                   uint32_t dex_offset,
10001bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray                   uint32_t method_idx,
10101bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray                   uint32_t number_of_vreg_arguments,
10201bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray                   bool is_range,
10301bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray                   uint32_t* args,
10401bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray                   uint32_t register_index);
10501bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray
106818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  ArenaAllocator* const arena_;
107be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray
108be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // A list of the size of the dex code holding block information for
109be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // the method. If an entry contains a block, then the dex instruction
110be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  // starting at that entry is the first instruction of a new block.
111be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray  GrowableArray<HBasicBlock*> branch_targets_;
112be9a92aa804c0d210f80966b74ef8ed3987f335aNicolas Geoffray
1133ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  GrowableArray<HLocal*> locals_;
1143ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray
115818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HBasicBlock* entry_block_;
116818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HBasicBlock* exit_block_;
117818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HBasicBlock* current_block_;
118818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  HGraph* graph_;
119818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
1203ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray  HIntConstant* constant0_;
121bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray  HIntConstant* constant1_;
1223ff386aafefd5282bb76c8a50506a70a4321e698Nicolas Geoffray
1238ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  const DexFile* const dex_file_;
12401bc96d007b67fdb7fe349232a83e4b354ce3d08Nicolas Geoffray  DexCompilationUnit* const dex_compilation_unit_;
1258ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray
126818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
127818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray};
128818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
129818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray}  // namespace art
130818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray
131818f2107e6d2d9e80faac8ae8c92faffa83cbd11Nicolas Geoffray#endif  // ART_COMPILER_OPTIMIZING_BUILDER_H_
132