blob: 103c16f8ac19774ea65ec0ca58222f81523099f4 [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.
Dragos Sbirlea6bee4452013-07-26 12:05:03 -070053 virtual void ToDot(std::string& result, const art::DexFile& dex_file) const;
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070054 // 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
Dragos Sbirlea6bee4452013-07-26 12:05:03 -0700129 void ToDot(std::string& result, const art::DexFile& dex_file) const {
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700130 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) {
Dragos Sbirlea6bee4452013-07-26 12:05:03 -0700143 result += def_it->second->StringId() + " -> " + StringId() +"[color=gray,label=\"";
144 result += art::StringPrintf("vR = %d", def_it->first);
145 result += "\"] ; // ssa edge\n";
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700146 }
147 }
148 }
149
150 private:
151 const int32_t value_;
152};
153
154class ReturnInstructionNode: public InstructionNode {
155 public:
156 explicit ReturnInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
157 void Accept(IRVisitor* v) {
158 v->Visit(this);
159 v->Traverse(this);
160 }
161};
162
163class IfNeInstructionNode: public InstructionNode {
164 public:
165 explicit IfNeInstructionNode(const art::Instruction* inst): InstructionNode(inst) {
166 DCHECK(InstructionTools::IsDefinition(inst) == false);
167 }
168 void Accept(IRVisitor* v) {
169 v->Visit(this);
170 v->Traverse(this);
171 }
172};
173
174
175
176class MoveResultInstructionNode: public InstructionNode {
177 public:
178 explicit MoveResultInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
179 std::vector<int> GetUses() {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700180 std::vector<int> uses; // Using vector<> instead of set<> because order matters.
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700181 uses.push_back(RETURN_REGISTER);
182 return uses;
183 }
184 void Accept(IRVisitor* v) {
185 v->Visit(this);
186 v->Traverse(this);
187 }
188};
189
190class InvokeStaticInstructionNode: public InstructionNode {
191 public:
192 explicit InvokeStaticInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
193 int GetResultRegister() const {
194 return RETURN_REGISTER;
195 }
196 void Accept(IRVisitor* v) {
197 v->Visit(this);
198 v->Traverse(this);
199 }
200};
201
202class AddIntInstructionNode: public InstructionNode {
203 public:
204 explicit AddIntInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
205 void Accept(IRVisitor* v) {
206 v->Visit(this);
207 v->Traverse(this);
208 }
209};
210
211class AddIntLitInstructionNode: public AddIntInstructionNode {
212 public:
213 explicit AddIntLitInstructionNode(const art::Instruction* inst):
214 AddIntInstructionNode(inst) { }
215
216 std::vector<int> GetUses() {
217 std::vector<int> uses = AddIntInstructionNode::GetUses();
218 uses.push_back(UNNAMED_CONST_REGISTER);
219 return uses;
220 }
221
222 void Accept(IRVisitor* v) {
223 v->Visit(this);
224 v->Traverse(this);
225 }
226};
227
228class GotoInstructionNode: public InstructionNode {
229 public:
230 explicit GotoInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
231 void Accept(IRVisitor* v) {
232 v->Visit(this);
233 v->Traverse(this);
234 }
235};
236
237class IfEqzInstructionNode: public InstructionNode {
238 public:
239 explicit IfEqzInstructionNode(const art::Instruction* inst): InstructionNode(inst) {
240 DCHECK(InstructionTools::IsDefinition(inst) == false);
241 }
242 void Accept(IRVisitor* v) {
243 v->Visit(this);
244 v->Traverse(this);
245 }
246};
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700247} // namespace sea_ir
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700248#endif // ART_COMPILER_SEA_IR_INSTRUCTION_NODES_H_