blob: fb1f83f0765dc9513d50237a9bf6f6cf8c36d7fa [file] [log] [blame]
Dragos Sbirlea0e260a32013-06-21 09:20:34 -07001/*
2 * Copyright (C) 2013 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_SEA_IR_INSTRUCTION_NODES_H_
18#define ART_COMPILER_SEA_IR_INSTRUCTION_NODES_H_
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070019#include "dex_instruction-inl.h"
Dragos Sbirlea64479192013-08-01 15:38:43 -070020#include "sea_ir/sea_node.h"
21#include "sea_ir/visitor.h"
22
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070023
24namespace sea_ir {
25
26enum SpecialRegisters {
27 NO_REGISTER = -1, // Usually signifies that there is no register
28 // that respects the condition you asked for.
29 RETURN_REGISTER = -2, // Written by the invoke* instructions, read by move-results.
30 UNNAMED_CONST_REGISTER = -3 // Written by UnnamedConst* instructions, read by *Lit* instruction.
31};
32
33class IRVisitor;
34
35// This class represents an instruction in SEA IR.
36// As we add support for specific classes of instructions,
37// the number of InstructionNode objects should dwindle, while the
38// number of subclasses and instances of subclasses will go up.
39class InstructionNode: public SeaNode {
40 public:
41 static std::vector<sea_ir::InstructionNode*> Create(const art::Instruction* in);
42 // Returns the Dalvik instruction around which this InstructionNode is wrapped.
43 const art::Instruction* GetInstruction() const {
44 DCHECK(NULL != instruction_) << "Tried to access NULL instruction in an InstructionNode.";
45 return instruction_;
46 }
47 // Returns the register that is defined by the current instruction, or NO_REGISTER otherwise.
48 virtual int GetResultRegister() const;
49 // Returns the set of registers defined by the current instruction.
50 virtual std::vector<int> GetDefinitions() const;
51 // Returns the set of register numbers that are used by the instruction.
Dragos Sbirlea64479192013-08-01 15:38:43 -070052 virtual std::vector<int> GetUses() const;
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070053 // Mark the current instruction as a downward exposed definition.
54 void MarkAsDEDef();
55 // Rename the use of @reg_no to refer to the instruction @definition,
56 // essentially creating SSA form.
57 void RenameToSSA(int reg_no, InstructionNode* definition) {
58 definition_edges_.insert(std::pair<int, InstructionNode*>(reg_no, definition));
Dragos Sbirleae2245322013-07-29 14:16:14 -070059 definition->AddSSAUse(this);
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070060 }
61 // Returns the ordered set of Instructions that define the input operands of this instruction.
62 // Precondition: SeaGraph.ConvertToSSA().
Dragos Sbirleab40eddf2013-07-31 13:37:31 -070063 std::vector<InstructionNode*> GetSSAProducers() {
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070064 std::vector<int> uses = GetUses();
65 std::vector<InstructionNode*> ssa_uses;
66 for (std::vector<int>::const_iterator cit = uses.begin(); cit != uses.end(); cit++) {
67 ssa_uses.push_back((*definition_edges_.find(*cit)).second);
68 }
69 return ssa_uses;
70 }
Dragos Sbirlea64479192013-08-01 15:38:43 -070071 std::map<int, InstructionNode* >* GetSSAProducersMap() {
72 return &definition_edges_;
Dragos Sbirleab40eddf2013-07-31 13:37:31 -070073 }
Dragos Sbirlea64479192013-08-01 15:38:43 -070074 std::vector<InstructionNode*>* GetSSAConsumers() {
75 return &used_in_;
76 }
Dragos Sbirleae2245322013-07-29 14:16:14 -070077 virtual void AddSSAUse(InstructionNode* use) {
78 used_in_.push_back(use);
79 }
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070080 void Accept(IRVisitor* v) {
81 v->Visit(this);
82 v->Traverse(this);
83 }
84 // Set the region to which this instruction belongs.
85 Region* GetRegion() {
86 DCHECK(NULL != region_);
87 return region_;
88 }
89 // Get the region to which this instruction belongs.
90 void SetRegion(Region* region) {
91 region_ = region;
92 }
93
94 protected:
95 explicit InstructionNode(const art::Instruction* in):
Dragos Sbirleae2245322013-07-29 14:16:14 -070096 SeaNode(), instruction_(in), used_in_(), de_def_(false), region_(NULL) { }
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070097
98 protected:
99 const art::Instruction* const instruction_;
Dragos Sbirlea64479192013-08-01 15:38:43 -0700100 std::map<int, InstructionNode* > definition_edges_; // Maps used registers to their definitions.
Dragos Sbirleae2245322013-07-29 14:16:14 -0700101 // Stores pointers to instructions that use the result of the current instruction.
102 std::vector<InstructionNode*> used_in_;
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700103 bool de_def_;
104 Region* region_;
105};
106
107class ConstInstructionNode: public InstructionNode {
108 public:
109 explicit ConstInstructionNode(const art::Instruction* inst):
110 InstructionNode(inst) { }
111
112 void Accept(IRVisitor* v) {
113 v->Visit(this);
114 v->Traverse(this);
115 }
116
117 virtual int32_t GetConstValue() const {
118 return GetInstruction()->VRegB_11n();
119 }
120};
121
122class UnnamedConstInstructionNode: public ConstInstructionNode {
123 public:
124 explicit UnnamedConstInstructionNode(const art::Instruction* inst, int32_t value):
125 ConstInstructionNode(inst), value_(value) { }
Dragos Sbirlea64479192013-08-01 15:38:43 -0700126
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700127 void Accept(IRVisitor* v) {
128 v->Visit(this);
129 v->Traverse(this);
130 }
131
132 int GetResultRegister() const {
133 return UNNAMED_CONST_REGISTER;
134 }
135
136 int32_t GetConstValue() const {
137 return value_;
138 }
139
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700140 private:
141 const int32_t value_;
142};
143
144class ReturnInstructionNode: public InstructionNode {
145 public:
146 explicit ReturnInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
147 void Accept(IRVisitor* v) {
148 v->Visit(this);
149 v->Traverse(this);
150 }
151};
152
153class IfNeInstructionNode: public InstructionNode {
154 public:
155 explicit IfNeInstructionNode(const art::Instruction* inst): InstructionNode(inst) {
156 DCHECK(InstructionTools::IsDefinition(inst) == false);
157 }
158 void Accept(IRVisitor* v) {
159 v->Visit(this);
160 v->Traverse(this);
161 }
162};
163
164
165
166class MoveResultInstructionNode: public InstructionNode {
167 public:
168 explicit MoveResultInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
Dragos Sbirlea64479192013-08-01 15:38:43 -0700169 std::vector<int> GetUses() const {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700170 std::vector<int> uses; // Using vector<> instead of set<> because order matters.
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700171 uses.push_back(RETURN_REGISTER);
172 return uses;
173 }
174 void Accept(IRVisitor* v) {
175 v->Visit(this);
176 v->Traverse(this);
177 }
178};
179
180class InvokeStaticInstructionNode: public InstructionNode {
181 public:
182 explicit InvokeStaticInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
183 int GetResultRegister() const {
184 return RETURN_REGISTER;
185 }
186 void Accept(IRVisitor* v) {
187 v->Visit(this);
188 v->Traverse(this);
189 }
190};
191
192class AddIntInstructionNode: public InstructionNode {
193 public:
194 explicit AddIntInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
195 void Accept(IRVisitor* v) {
196 v->Visit(this);
197 v->Traverse(this);
198 }
199};
200
201class AddIntLitInstructionNode: public AddIntInstructionNode {
202 public:
203 explicit AddIntLitInstructionNode(const art::Instruction* inst):
204 AddIntInstructionNode(inst) { }
205
Dragos Sbirlea64479192013-08-01 15:38:43 -0700206 std::vector<int> GetUses() const {
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700207 std::vector<int> uses = AddIntInstructionNode::GetUses();
208 uses.push_back(UNNAMED_CONST_REGISTER);
209 return uses;
210 }
211
212 void Accept(IRVisitor* v) {
213 v->Visit(this);
214 v->Traverse(this);
215 }
216};
217
218class GotoInstructionNode: public InstructionNode {
219 public:
220 explicit GotoInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
221 void Accept(IRVisitor* v) {
222 v->Visit(this);
223 v->Traverse(this);
224 }
225};
226
227class IfEqzInstructionNode: public InstructionNode {
228 public:
229 explicit IfEqzInstructionNode(const art::Instruction* inst): InstructionNode(inst) {
230 DCHECK(InstructionTools::IsDefinition(inst) == false);
231 }
232 void Accept(IRVisitor* v) {
233 v->Visit(this);
234 v->Traverse(this);
235 }
236};
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700237} // namespace sea_ir
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700238#endif // ART_COMPILER_SEA_IR_INSTRUCTION_NODES_H_