1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_COMPILER_OPTIMIZING_BUILDER_H_
18 #define ART_COMPILER_OPTIMIZING_BUILDER_H_
19 
20 #include "dex_file.h"
21 #include "driver/compiler_driver.h"
22 #include "driver/dex_compilation_unit.h"
23 #include "primitive.h"
24 #include "utils/allocation.h"
25 #include "utils/growable_array.h"
26 #include "nodes.h"
27 
28 namespace art {
29 
30 class Instruction;
31 
32 class HGraphBuilder : public ValueObject {
33  public:
34   HGraphBuilder(ArenaAllocator* arena,
35                 DexCompilationUnit* dex_compilation_unit = nullptr,
36                 const DexFile* dex_file = nullptr,
37                 CompilerDriver* driver = nullptr)
arena_(arena)38       : arena_(arena),
39         branch_targets_(arena, 0),
40         locals_(arena, 0),
41         entry_block_(nullptr),
42         exit_block_(nullptr),
43         current_block_(nullptr),
44         graph_(nullptr),
45         constant0_(nullptr),
46         constant1_(nullptr),
47         dex_file_(dex_file),
48         dex_compilation_unit_(dex_compilation_unit),
49         compiler_driver_(driver) {}
50 
51   HGraph* BuildGraph(const DexFile::CodeItem& code);
52 
53  private:
54   // Analyzes the dex instruction and adds HInstruction to the graph
55   // to execute that instruction. Returns whether the instruction can
56   // be handled.
57   bool AnalyzeDexInstruction(const Instruction& instruction, int32_t dex_offset);
58 
59   // Finds all instructions that start a new block, and populates branch_targets_ with
60   // the newly created blocks.
61   void ComputeBranchTargets(const uint16_t* start, const uint16_t* end);
62   void MaybeUpdateCurrentBlock(size_t index);
63   HBasicBlock* FindBlockStartingAt(int32_t index) const;
64 
65   HIntConstant* GetIntConstant0();
66   HIntConstant* GetIntConstant1();
67   HIntConstant* GetIntConstant(int32_t constant);
68   HLongConstant* GetLongConstant(int64_t constant);
69   void InitializeLocals(uint16_t count);
70   HLocal* GetLocalAt(int register_index) const;
71   void UpdateLocal(int register_index, HInstruction* instruction) const;
72   HInstruction* LoadLocal(int register_index, Primitive::Type type) const;
73 
74   // Temporarily returns whether the compiler supports the parameters
75   // of the method.
76   bool InitializeParameters(uint16_t number_of_parameters);
77 
78   template<typename T>
79   void Binop_23x(const Instruction& instruction, Primitive::Type type);
80 
81   template<typename T>
82   void Binop_12x(const Instruction& instruction, Primitive::Type type);
83 
84   template<typename T>
85   void Binop_22b(const Instruction& instruction, bool reverse);
86 
87   template<typename T>
88   void Binop_22s(const Instruction& instruction, bool reverse);
89 
90   template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_offset);
91   template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_offset);
92 
93   void BuildReturn(const Instruction& instruction, Primitive::Type type);
94 
95   bool BuildFieldAccess(const Instruction& instruction, uint32_t dex_offset, bool is_get);
96   void BuildArrayAccess(const Instruction& instruction,
97                         uint32_t dex_offset,
98                         bool is_get,
99                         Primitive::Type anticipated_type);
100 
101   // Builds an invocation node and returns whether the instruction is supported.
102   bool BuildInvoke(const Instruction& instruction,
103                    uint32_t dex_offset,
104                    uint32_t method_idx,
105                    uint32_t number_of_vreg_arguments,
106                    bool is_range,
107                    uint32_t* args,
108                    uint32_t register_index);
109 
110   ArenaAllocator* const arena_;
111 
112   // A list of the size of the dex code holding block information for
113   // the method. If an entry contains a block, then the dex instruction
114   // starting at that entry is the first instruction of a new block.
115   GrowableArray<HBasicBlock*> branch_targets_;
116 
117   GrowableArray<HLocal*> locals_;
118 
119   HBasicBlock* entry_block_;
120   HBasicBlock* exit_block_;
121   HBasicBlock* current_block_;
122   HGraph* graph_;
123 
124   HIntConstant* constant0_;
125   HIntConstant* constant1_;
126 
127   const DexFile* const dex_file_;
128   DexCompilationUnit* const dex_compilation_unit_;
129   CompilerDriver* const compiler_driver_;
130 
131   DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
132 };
133 
134 }  // namespace art
135 
136 #endif  // ART_COMPILER_OPTIMIZING_BUILDER_H_
137