blob: 6f9bdddf77ffea3aab646cd8b58e70ae2edb7619 [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));
Dragos Sbirleae2245322013-07-29 14:16:14 -070060 definition->AddSSAUse(this);
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070061 }
62 // Returns the ordered set of Instructions that define the input operands of this instruction.
63 // Precondition: SeaGraph.ConvertToSSA().
64 std::vector<InstructionNode*> GetSSAUses() {
65 std::vector<int> uses = GetUses();
66 std::vector<InstructionNode*> ssa_uses;
67 for (std::vector<int>::const_iterator cit = uses.begin(); cit != uses.end(); cit++) {
68 ssa_uses.push_back((*definition_edges_.find(*cit)).second);
69 }
70 return ssa_uses;
71 }
72
Dragos Sbirleae2245322013-07-29 14:16:14 -070073 virtual void AddSSAUse(InstructionNode* use) {
74 used_in_.push_back(use);
75 }
76
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070077 void Accept(IRVisitor* v) {
78 v->Visit(this);
79 v->Traverse(this);
80 }
81 // Set the region to which this instruction belongs.
82 Region* GetRegion() {
83 DCHECK(NULL != region_);
84 return region_;
85 }
86 // Get the region to which this instruction belongs.
87 void SetRegion(Region* region) {
88 region_ = region;
89 }
90
91 protected:
92 explicit InstructionNode(const art::Instruction* in):
Dragos Sbirleae2245322013-07-29 14:16:14 -070093 SeaNode(), instruction_(in), used_in_(), de_def_(false), region_(NULL) { }
94 void ToDotSSAEdges(std::string& result) const;
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070095
96 protected:
97 const art::Instruction* const instruction_;
98 std::map<int, InstructionNode* > definition_edges_;
Dragos Sbirleae2245322013-07-29 14:16:14 -070099 // Stores pointers to instructions that use the result of the current instruction.
100 std::vector<InstructionNode*> used_in_;
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700101 bool de_def_;
102 Region* region_;
103};
104
105class ConstInstructionNode: public InstructionNode {
106 public:
107 explicit ConstInstructionNode(const art::Instruction* inst):
108 InstructionNode(inst) { }
109
110 void Accept(IRVisitor* v) {
111 v->Visit(this);
112 v->Traverse(this);
113 }
114
115 virtual int32_t GetConstValue() const {
116 return GetInstruction()->VRegB_11n();
117 }
118};
119
120class UnnamedConstInstructionNode: public ConstInstructionNode {
121 public:
122 explicit UnnamedConstInstructionNode(const art::Instruction* inst, int32_t value):
123 ConstInstructionNode(inst), value_(value) { }
124 void Accept(IRVisitor* v) {
125 v->Visit(this);
126 v->Traverse(this);
127 }
128
129 int GetResultRegister() const {
130 return UNNAMED_CONST_REGISTER;
131 }
132
133 int32_t GetConstValue() const {
134 return value_;
135 }
136
Dragos Sbirlea6bee4452013-07-26 12:05:03 -0700137 void ToDot(std::string& result, const art::DexFile& dex_file) const {
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700138 std::ostringstream sstream;
139 sstream << GetConstValue();
140 const std::string value_as_string(sstream.str());
141 result += "// Instruction ("+StringId()+"): \n" + StringId() +
142 " [label=\"const/x v-3, #"+ value_as_string + "\"";
143 if (de_def_) {
144 result += "style=bold";
145 }
146 result += "];\n";
Dragos Sbirleae2245322013-07-29 14:16:14 -0700147 ToDotSSAEdges(result);
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700148 }
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_