blob: 5c9cfe19dc381563050027b518162004c9ce227b [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_
19#include "sea_node.h"
20#include "visitor.h"
21#include "dex_instruction-inl.h"
22
23namespace sea_ir {
24
25enum SpecialRegisters {
26 NO_REGISTER = -1, // Usually signifies that there is no register
27 // that respects the condition you asked for.
28 RETURN_REGISTER = -2, // Written by the invoke* instructions, read by move-results.
29 UNNAMED_CONST_REGISTER = -3 // Written by UnnamedConst* instructions, read by *Lit* instruction.
30};
31
32class IRVisitor;
33
34// This class represents an instruction in SEA IR.
35// As we add support for specific classes of instructions,
36// the number of InstructionNode objects should dwindle, while the
37// number of subclasses and instances of subclasses will go up.
38class InstructionNode: public SeaNode {
39 public:
40 static std::vector<sea_ir::InstructionNode*> Create(const art::Instruction* in);
41 // Returns the Dalvik instruction around which this InstructionNode is wrapped.
42 const art::Instruction* GetInstruction() const {
43 DCHECK(NULL != instruction_) << "Tried to access NULL instruction in an InstructionNode.";
44 return instruction_;
45 }
46 // Returns the register that is defined by the current instruction, or NO_REGISTER otherwise.
47 virtual int GetResultRegister() const;
48 // Returns the set of registers defined by the current instruction.
49 virtual std::vector<int> GetDefinitions() const;
50 // Returns the set of register numbers that are used by the instruction.
51 virtual std::vector<int> GetUses();
52 // Appends to @result the .dot string representation of the instruction.
53 virtual void ToDot(std::string& result) const;
54 // Mark the current instruction as a downward exposed definition.
55 void MarkAsDEDef();
56 // Rename the use of @reg_no to refer to the instruction @definition,
57 // essentially creating SSA form.
58 void RenameToSSA(int reg_no, InstructionNode* definition) {
59 definition_edges_.insert(std::pair<int, InstructionNode*>(reg_no, definition));
60 }
61 // Returns the ordered set of Instructions that define the input operands of this instruction.
62 // Precondition: SeaGraph.ConvertToSSA().
63 std::vector<InstructionNode*> GetSSAUses() {
64 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 }
71
72 void Accept(IRVisitor* v) {
73 v->Visit(this);
74 v->Traverse(this);
75 }
76 // Set the region to which this instruction belongs.
77 Region* GetRegion() {
78 DCHECK(NULL != region_);
79 return region_;
80 }
81 // Get the region to which this instruction belongs.
82 void SetRegion(Region* region) {
83 region_ = region;
84 }
85
86 protected:
87 explicit InstructionNode(const art::Instruction* in):
88 SeaNode(), instruction_(in), de_def_(false), region_(NULL) { }
89
90 protected:
91 const art::Instruction* const instruction_;
92 std::map<int, InstructionNode* > definition_edges_;
93 bool de_def_;
94 Region* region_;
95};
96
97class ConstInstructionNode: public InstructionNode {
98 public:
99 explicit ConstInstructionNode(const art::Instruction* inst):
100 InstructionNode(inst) { }
101
102 void Accept(IRVisitor* v) {
103 v->Visit(this);
104 v->Traverse(this);
105 }
106
107 virtual int32_t GetConstValue() const {
108 return GetInstruction()->VRegB_11n();
109 }
110};
111
112class UnnamedConstInstructionNode: public ConstInstructionNode {
113 public:
114 explicit UnnamedConstInstructionNode(const art::Instruction* inst, int32_t value):
115 ConstInstructionNode(inst), value_(value) { }
116 void Accept(IRVisitor* v) {
117 v->Visit(this);
118 v->Traverse(this);
119 }
120
121 int GetResultRegister() const {
122 return UNNAMED_CONST_REGISTER;
123 }
124
125 int32_t GetConstValue() const {
126 return value_;
127 }
128
129 void ToDot(std::string& result) const {
130 std::ostringstream sstream;
131 sstream << GetConstValue();
132 const std::string value_as_string(sstream.str());
133 result += "// Instruction ("+StringId()+"): \n" + StringId() +
134 " [label=\"const/x v-3, #"+ value_as_string + "\"";
135 if (de_def_) {
136 result += "style=bold";
137 }
138 result += "];\n";
139 // SSA definitions:
140 for (std::map<int, InstructionNode* >::const_iterator def_it = definition_edges_.begin();
141 def_it != definition_edges_.end(); def_it++) {
142 if (NULL != def_it->second) {
143 result += def_it->second->StringId() + " -> " + StringId() +"[color=red,label=\"";
144 std::stringstream ss;
145 ss << def_it->first;
146 result.append(ss.str());
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700147 result += "\"] ; // ssa edge\n";
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700148 }
149 }
150 }
151
152 private:
153 const int32_t value_;
154};
155
156class ReturnInstructionNode: public InstructionNode {
157 public:
158 explicit ReturnInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
159 void Accept(IRVisitor* v) {
160 v->Visit(this);
161 v->Traverse(this);
162 }
163};
164
165class IfNeInstructionNode: public InstructionNode {
166 public:
167 explicit IfNeInstructionNode(const art::Instruction* inst): InstructionNode(inst) {
168 DCHECK(InstructionTools::IsDefinition(inst) == false);
169 }
170 void Accept(IRVisitor* v) {
171 v->Visit(this);
172 v->Traverse(this);
173 }
174};
175
176
177
178class MoveResultInstructionNode: public InstructionNode {
179 public:
180 explicit MoveResultInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
181 std::vector<int> GetUses() {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700182 std::vector<int> uses; // Using vector<> instead of set<> because order matters.
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700183 uses.push_back(RETURN_REGISTER);
184 return uses;
185 }
186 void Accept(IRVisitor* v) {
187 v->Visit(this);
188 v->Traverse(this);
189 }
190};
191
192class InvokeStaticInstructionNode: public InstructionNode {
193 public:
194 explicit InvokeStaticInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
195 int GetResultRegister() const {
196 return RETURN_REGISTER;
197 }
198 void Accept(IRVisitor* v) {
199 v->Visit(this);
200 v->Traverse(this);
201 }
202};
203
204class AddIntInstructionNode: public InstructionNode {
205 public:
206 explicit AddIntInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
207 void Accept(IRVisitor* v) {
208 v->Visit(this);
209 v->Traverse(this);
210 }
211};
212
213class AddIntLitInstructionNode: public AddIntInstructionNode {
214 public:
215 explicit AddIntLitInstructionNode(const art::Instruction* inst):
216 AddIntInstructionNode(inst) { }
217
218 std::vector<int> GetUses() {
219 std::vector<int> uses = AddIntInstructionNode::GetUses();
220 uses.push_back(UNNAMED_CONST_REGISTER);
221 return uses;
222 }
223
224 void Accept(IRVisitor* v) {
225 v->Visit(this);
226 v->Traverse(this);
227 }
228};
229
230class GotoInstructionNode: public InstructionNode {
231 public:
232 explicit GotoInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
233 void Accept(IRVisitor* v) {
234 v->Visit(this);
235 v->Traverse(this);
236 }
237};
238
239class IfEqzInstructionNode: public InstructionNode {
240 public:
241 explicit IfEqzInstructionNode(const art::Instruction* inst): InstructionNode(inst) {
242 DCHECK(InstructionTools::IsDefinition(inst) == false);
243 }
244 void Accept(IRVisitor* v) {
245 v->Visit(this);
246 v->Traverse(this);
247 }
248};
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700249} // namespace sea_ir
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700250#endif // ART_COMPILER_SEA_IR_INSTRUCTION_NODES_H_