blob: 1b81e9add3f6d81012986ba79e804366ba5ccb7d [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().
Dragos Sbirleab40eddf2013-07-31 13:37:31 -070064 std::vector<InstructionNode*> GetSSAProducers() {
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070065 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 Sbirleab40eddf2013-07-31 13:37:31 -070073 std::vector<InstructionNode*> GetSSAConsumers() {
74 return used_in_;
75 }
76
Dragos Sbirleae2245322013-07-29 14:16:14 -070077 virtual void AddSSAUse(InstructionNode* use) {
78 used_in_.push_back(use);
79 }
80
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070081 void Accept(IRVisitor* v) {
82 v->Visit(this);
83 v->Traverse(this);
84 }
85 // Set the region to which this instruction belongs.
86 Region* GetRegion() {
87 DCHECK(NULL != region_);
88 return region_;
89 }
90 // Get the region to which this instruction belongs.
91 void SetRegion(Region* region) {
92 region_ = region;
93 }
94
95 protected:
96 explicit InstructionNode(const art::Instruction* in):
Dragos Sbirleae2245322013-07-29 14:16:14 -070097 SeaNode(), instruction_(in), used_in_(), de_def_(false), region_(NULL) { }
98 void ToDotSSAEdges(std::string& result) const;
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070099
100 protected:
101 const art::Instruction* const instruction_;
102 std::map<int, InstructionNode* > definition_edges_;
Dragos Sbirleae2245322013-07-29 14:16:14 -0700103 // Stores pointers to instructions that use the result of the current instruction.
104 std::vector<InstructionNode*> used_in_;
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700105 bool de_def_;
106 Region* region_;
107};
108
109class ConstInstructionNode: public InstructionNode {
110 public:
111 explicit ConstInstructionNode(const art::Instruction* inst):
112 InstructionNode(inst) { }
113
114 void Accept(IRVisitor* v) {
115 v->Visit(this);
116 v->Traverse(this);
117 }
118
119 virtual int32_t GetConstValue() const {
120 return GetInstruction()->VRegB_11n();
121 }
122};
123
124class UnnamedConstInstructionNode: public ConstInstructionNode {
125 public:
126 explicit UnnamedConstInstructionNode(const art::Instruction* inst, int32_t value):
127 ConstInstructionNode(inst), value_(value) { }
128 void Accept(IRVisitor* v) {
129 v->Visit(this);
130 v->Traverse(this);
131 }
132
133 int GetResultRegister() const {
134 return UNNAMED_CONST_REGISTER;
135 }
136
137 int32_t GetConstValue() const {
138 return value_;
139 }
140
Dragos Sbirlea6bee4452013-07-26 12:05:03 -0700141 void ToDot(std::string& result, const art::DexFile& dex_file) const {
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700142 std::ostringstream sstream;
143 sstream << GetConstValue();
144 const std::string value_as_string(sstream.str());
145 result += "// Instruction ("+StringId()+"): \n" + StringId() +
146 " [label=\"const/x v-3, #"+ value_as_string + "\"";
147 if (de_def_) {
148 result += "style=bold";
149 }
150 result += "];\n";
Dragos Sbirleae2245322013-07-29 14:16:14 -0700151 ToDotSSAEdges(result);
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700152 }
153
154 private:
155 const int32_t value_;
156};
157
158class ReturnInstructionNode: public InstructionNode {
159 public:
160 explicit ReturnInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
161 void Accept(IRVisitor* v) {
162 v->Visit(this);
163 v->Traverse(this);
164 }
165};
166
167class IfNeInstructionNode: public InstructionNode {
168 public:
169 explicit IfNeInstructionNode(const art::Instruction* inst): InstructionNode(inst) {
170 DCHECK(InstructionTools::IsDefinition(inst) == false);
171 }
172 void Accept(IRVisitor* v) {
173 v->Visit(this);
174 v->Traverse(this);
175 }
176};
177
178
179
180class MoveResultInstructionNode: public InstructionNode {
181 public:
182 explicit MoveResultInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
183 std::vector<int> GetUses() {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700184 std::vector<int> uses; // Using vector<> instead of set<> because order matters.
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700185 uses.push_back(RETURN_REGISTER);
186 return uses;
187 }
188 void Accept(IRVisitor* v) {
189 v->Visit(this);
190 v->Traverse(this);
191 }
192};
193
194class InvokeStaticInstructionNode: public InstructionNode {
195 public:
196 explicit InvokeStaticInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
197 int GetResultRegister() const {
198 return RETURN_REGISTER;
199 }
200 void Accept(IRVisitor* v) {
201 v->Visit(this);
202 v->Traverse(this);
203 }
204};
205
206class AddIntInstructionNode: public InstructionNode {
207 public:
208 explicit AddIntInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
209 void Accept(IRVisitor* v) {
210 v->Visit(this);
211 v->Traverse(this);
212 }
213};
214
215class AddIntLitInstructionNode: public AddIntInstructionNode {
216 public:
217 explicit AddIntLitInstructionNode(const art::Instruction* inst):
218 AddIntInstructionNode(inst) { }
219
220 std::vector<int> GetUses() {
221 std::vector<int> uses = AddIntInstructionNode::GetUses();
222 uses.push_back(UNNAMED_CONST_REGISTER);
223 return uses;
224 }
225
226 void Accept(IRVisitor* v) {
227 v->Visit(this);
228 v->Traverse(this);
229 }
230};
231
232class GotoInstructionNode: public InstructionNode {
233 public:
234 explicit GotoInstructionNode(const art::Instruction* inst): InstructionNode(inst) { }
235 void Accept(IRVisitor* v) {
236 v->Visit(this);
237 v->Traverse(this);
238 }
239};
240
241class IfEqzInstructionNode: public InstructionNode {
242 public:
243 explicit IfEqzInstructionNode(const art::Instruction* inst): InstructionNode(inst) {
244 DCHECK(InstructionTools::IsDefinition(inst) == false);
245 }
246 void Accept(IRVisitor* v) {
247 v->Visit(this);
248 v->Traverse(this);
249 }
250};
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700251} // namespace sea_ir
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700252#endif // ART_COMPILER_SEA_IR_INSTRUCTION_NODES_H_