blob: 0bb3a261b3535c33e218813c4c43b90a9ac0a07b [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#include <llvm/Support/raw_ostream.h>
18#include "sea.h"
19#include "code_gen.h"
20
21namespace sea_ir {
22
23void CodeGenPrepassVisitor::Visit(PhiInstructionNode* phi) {
24 Region* r = phi->GetRegion();
25 const std::vector<Region*>* predecessors = r->GetPredecessors();
26 DCHECK(NULL != predecessors);
27 DCHECK_GT(predecessors->size(), 0u);
28 llvm::PHINode *llvm_phi = llvm_data_->builder_.CreatePHI(
29 llvm::Type::getInt32Ty(*llvm_data_->context_), predecessors->size(), phi->StringId());
30 llvm_data_->AddValue(phi, llvm_phi);
31}
32
33void CodeGenPassVisitor::Initialize(SeaGraph* graph) {
34 Region* root_region;
35 ordered_regions_.clear();
36 for (std::vector<Region*>::const_iterator cit = graph->GetRegions()->begin();
37 cit != graph->GetRegions()->end(); cit++ ) {
38 if ((*cit)->GetIDominator() == (*cit)) {
39 root_region = *cit;
40 }
41 }
42 ordered_regions_.push_back(root_region);
43 for (unsigned int id = 0; id < ordered_regions_.size(); id++) {
44 Region* current_region = ordered_regions_.at(id);
45 const std::set<Region*>* dominated_regions = current_region->GetIDominatedSet();
46 for (std::set<Region*>::const_iterator cit = dominated_regions->begin();
47 cit != dominated_regions->end(); cit++ ) {
48 ordered_regions_.push_back(*cit);
49 }
50 }
51}
52
53void CodeGenPostpassVisitor::Visit(SeaGraph* graph) {
54 std::vector<SignatureNode*>* parameters = graph->GetParameterNodes();
55 std::cout << "=== SeaGraph ===" << parameters->size() << std::endl;
56}
57void CodeGenVisitor::Visit(SeaGraph* graph) {
58 std::vector<SignatureNode*>* parameters = graph->GetParameterNodes();
59 std::cout << "=== SeaGraph ===" << parameters->size() << std::endl;
60}
61void CodeGenPrepassVisitor::Visit(SeaGraph* graph) {
62 std::vector<SignatureNode*>* parameters = graph->GetParameterNodes();
63 std::cout << "=== SeaGraph ===" << parameters->size() << std::endl;
64 // TODO: Extract correct types from dex for params and return value.
65 DCHECK(parameters != NULL);
66 std::vector<llvm::Type*> parameter_types(parameters->size(),
67 llvm::Type::getInt32Ty(*llvm_data_->context_));
68 // Build llvm function name.
69 std::string function_name = "class=";
70 std::stringstream ss;
71 ss << graph->class_def_idx_ << "_method=" << graph->method_idx_;
72 function_name.append(ss.str());
73
74 // Build llvm function type and parameters.
75 llvm::FunctionType *function_type = llvm::FunctionType::get(
76 llvm::Type::getInt32Ty(*llvm_data_->context_),
77 parameter_types, false);
78 llvm_data_->function_ = llvm::Function::Create(function_type,
79 llvm::Function::ExternalLinkage, function_name, &llvm_data_->module_);
80 unsigned param_id = 0;
81 for (llvm::Function::arg_iterator arg_it = llvm_data_->function_->arg_begin();
82 param_id != llvm_data_->function_->arg_size(); ++arg_it, ++param_id) {
83 DCHECK(parameters->size() > param_id) << "Insufficient parameters for function signature";
84 // Build parameter register name for LLVM IR clarity.
85 std::stringstream ss;
86 ss << "r" << parameters->at(param_id);
87 std::string arg_name;
88 arg_name.append(ss.str());
89 arg_it->setName(arg_name);
90 SignatureNode* parameter = parameters->at(param_id);
91 llvm_data_->AddValue(parameter, arg_it);
92 }
93
94 std::vector<Region*>* regions = &ordered_regions_;
95 DCHECK_GT(regions->size(), 0u);
96 // Then create all other basic blocks.
97 for (std::vector<Region*>::const_iterator cit = regions->begin(); cit != regions->end(); cit++) {
98 llvm::BasicBlock* new_basic_block = llvm::BasicBlock::Create(*llvm_data_->context_,
99 (*cit)->StringId(), llvm_data_->function_);
100 llvm_data_->AddBlock((*cit), new_basic_block);
101 }
102}
103
104void CodeGenPrepassVisitor::Visit(Region* region) {
105 std::cout << " == Region " << region->StringId() << " ==" << std::endl;
106 llvm_data_->builder_.SetInsertPoint(llvm_data_->GetBlock(region));
107}
108void CodeGenPostpassVisitor::Visit(Region* region) {
109 std::cout << " == Region " << region->StringId() << " ==" << std::endl;
110 llvm_data_->builder_.SetInsertPoint(llvm_data_->GetBlock(region));
111}
112void CodeGenVisitor::Visit(Region* region) {
113 std::cout << " == Region " << region->StringId() << " ==" << std::endl;
114 llvm_data_->builder_.SetInsertPoint(llvm_data_->GetBlock(region));
115}
116
117
118void CodeGenVisitor::Visit(InstructionNode* instruction) {
119 std::string instr = instruction->GetInstruction()->DumpString(NULL);
120 DCHECK(0); // This whole function is useful only during development.
121}
122void CodeGenVisitor::Visit(ConstInstructionNode* instruction) {
123 std::string instr = instruction->GetInstruction()->DumpString(NULL);
124 std::cout << "1.Instruction: " << instr << std::endl;
125 llvm_data_->AddValue(instruction,
126 llvm::ConstantInt::get(*llvm_data_->context_, llvm::APInt(32, instruction->GetConstValue())));
127}
128void CodeGenVisitor::Visit(ReturnInstructionNode* instruction) {
129 std::string instr = instruction->GetInstruction()->DumpString(NULL);
130 std::cout << "2.Instruction: " << instr << std::endl;
131 DCHECK_GT(instruction->GetSSAUses().size(), 0u);
132 llvm::Value* return_value = llvm_data_->GetValue(instruction->GetSSAUses().at(0));
133 llvm_data_->builder_.CreateRet(return_value);
134}
135void CodeGenVisitor::Visit(IfNeInstructionNode* instruction) {
136 std::string instr = instruction->GetInstruction()->DumpString(NULL);
137 std::cout << "3.Instruction: " << instr << std::endl;
138 std::vector<InstructionNode*> ssa_uses = instruction->GetSSAUses();
139 DCHECK_GT(ssa_uses.size(), 1u);
140 InstructionNode* use_l = ssa_uses.at(0);
141 llvm::Value* left = llvm_data_->GetValue(use_l);
142
143 InstructionNode* use_r = ssa_uses.at(1);
144 llvm::Value* right = llvm_data_->GetValue(use_r);
145 llvm::Value* ifne = llvm_data_->builder_.CreateICmpNE(left, right, instruction->StringId());
146 DCHECK(instruction->GetRegion() != NULL);
147 std::vector<Region*>* successors = instruction->GetRegion()->GetSuccessors();
148 DCHECK_GT(successors->size(), 0u);
149 llvm::BasicBlock* then_block = llvm_data_->GetBlock(successors->at(0));
150 llvm::BasicBlock* else_block = llvm_data_->GetBlock(successors->at(1));
151
152 llvm_data_->builder_.CreateCondBr(ifne, then_block, else_block);
153}
154
155/*
156void CodeGenVisitor::Visit(AddIntLitInstructionNode* instruction) {
157 std::string instr = instruction->GetInstruction()->DumpString(NULL);
158 std::cout << "4.Instruction: " << instr << std::endl;
159 std::vector<InstructionNode*> ssa_uses = instruction->GetSSAUses();
160 InstructionNode* use_l = ssa_uses.at(0);
161 llvm::Value* left = llvm_data->GetValue(use_l);
162 llvm::Value* right = llvm::ConstantInt::get(*llvm_data->context_,
163 llvm::APInt(32, instruction->GetConstValue()));
164 llvm::Value* result = llvm_data->builder_.CreateAdd(left, right);
165 llvm_data->AddValue(instruction, result);
166}
167*/
168void CodeGenVisitor::Visit(MoveResultInstructionNode* instruction) {
169 std::string instr = instruction->GetInstruction()->DumpString(NULL);
170 std::cout << "5.Instruction: " << instr << std::endl;
171 // TODO: Currently, this "mov" instruction is simulated by "res = return_register + 0".
172 // This is inefficient, but should be optimized out by the coalescing phase of the reg alloc.
173 // The TODO is to either ensure that this happens, or to
174 // remove the move-result instructions completely from the IR
175 // by merging them with the invoke-* instructions,
176 // since their purpose of minimizing the number of opcodes in dex is
177 // not relevant for the IR. (Will need to have different
178 // instruction subclasses for functions and procedures.)
179 std::vector<InstructionNode*> ssa_uses = instruction->GetSSAUses();
180 InstructionNode* use_l = ssa_uses.at(0);
181 llvm::Value* left = llvm_data_->GetValue(use_l);
182 llvm::Value* right = llvm::ConstantInt::get(*llvm_data_->context_, llvm::APInt(32, 0));
183 llvm::Value* result = llvm_data_->builder_.CreateAdd(left, right);
184 llvm_data_->AddValue(instruction, result);
185}
186void CodeGenVisitor::Visit(InvokeStaticInstructionNode* invoke) {
187 std::string instr = invoke->GetInstruction()->DumpString(NULL);
188 std::cout << "6.Instruction: " << instr << std::endl;
189 // TODO: Build callee llvm function name.
190 std::string function_name = "class=";
191 std::stringstream ss;
192 ss << 0 << "_method=" << 1;
193 function_name.append(ss.str());
194 llvm::Function *callee = llvm_data_->module_.getFunction(function_name);
195 // TODO: Add proper checking of the matching between formal and actual signature.
196 DCHECK(NULL != callee);
197 std::vector<llvm::Value*> parameter_values;
198 std::vector<InstructionNode*> parameter_sources = invoke->GetSSAUses();
199 for (std::vector<InstructionNode*>::const_iterator cit = parameter_sources.begin();
200 cit != parameter_sources.end(); ++cit) {
201 llvm::Value* parameter_value = llvm_data_->GetValue((*cit));
202 DCHECK(NULL != parameter_value);
203 parameter_values.push_back(parameter_value);
204 }
205 llvm::Value* return_value = llvm_data_->builder_.CreateCall(callee,
206 parameter_values, invoke->StringId());
207 llvm_data_->AddValue(invoke, return_value);
208}
209void CodeGenVisitor::Visit(AddIntInstructionNode* instruction) {
210 std::string instr = instruction->GetInstruction()->DumpString(NULL);
211 std::cout << "7.Instruction: " << instr << std::endl;
212 std::vector<InstructionNode*> ssa_uses = instruction->GetSSAUses();
213 DCHECK_GT(ssa_uses.size(), 1u);
214 InstructionNode* use_l = ssa_uses.at(0);
215 InstructionNode* use_r = ssa_uses.at(1);
216 llvm::Value* left = llvm_data_->GetValue(use_l);
217 llvm::Value* right = llvm_data_->GetValue(use_r);
218 llvm::Value* result = llvm_data_->builder_.CreateAdd(left, right);
219 llvm_data_->AddValue(instruction, result);
220}
221void CodeGenVisitor::Visit(GotoInstructionNode* instruction) {
222 std::string instr = instruction->GetInstruction()->DumpString(NULL);
223 std::cout << "8.Instruction: " << instr << std::endl;
224 std::vector<sea_ir::Region*>* targets = instruction->GetRegion()->GetSuccessors();
225 DCHECK_EQ(targets->size(), 1u);
226 llvm::BasicBlock* target_block = llvm_data_->GetBlock(targets->at(0));
227 llvm_data_->builder_.CreateBr(target_block);
228}
229void CodeGenVisitor::Visit(IfEqzInstructionNode* instruction) {
230 std::string instr = instruction->GetInstruction()->DumpString(NULL);
231 std::cout << "9. Instruction: " << instr << "; Id: " <<instruction << std::endl;
232 std::vector<InstructionNode*> ssa_uses = instruction->GetSSAUses();
233 DCHECK_GT(ssa_uses.size(), 0u);
234 InstructionNode* use_l = ssa_uses.at(0);
235 llvm::Value* left = llvm_data_->GetValue(use_l);
236 llvm::Value* ifeqz = llvm_data_->builder_.CreateICmpEQ(left,
237 llvm::ConstantInt::get(*llvm_data_->context_, llvm::APInt::getNullValue(32)),
238 instruction->StringId());
239 DCHECK(instruction->GetRegion() != NULL);
240 std::vector<Region*>* successors = instruction->GetRegion()->GetSuccessors();
241 DCHECK_GT(successors->size(), 0u);
242 llvm::BasicBlock* then_block = llvm_data_->GetBlock(successors->at(0));
243 llvm::BasicBlock* else_block = llvm_data_->GetBlock(successors->at(1));
244 llvm_data_->builder_.CreateCondBr(ifeqz, then_block, else_block);
245}
246
247void CodeGenPostpassVisitor::Visit(PhiInstructionNode* phi) {
248 std::cout << "Phi node for: " << phi->GetRegisterNumber() << std::endl;
249 Region* r = phi->GetRegion();
250 const std::vector<Region*>* predecessors = r->GetPredecessors();
251 DCHECK(NULL != predecessors);
252 DCHECK_GT(predecessors->size(), 0u);
253 // Prepass (CodeGenPrepassVisitor) should create the phi function value.
254 llvm::PHINode* llvm_phi = (llvm::PHINode*) llvm_data_->GetValue(phi);
255 int predecessor_pos = 0;
256 for (std::vector<Region*>::const_iterator cit = predecessors->begin();
257 cit != predecessors->end(); ++cit) {
258 std::vector<InstructionNode*>* defining_instructions = phi->GetSSAUses(predecessor_pos++);
259 DCHECK_EQ(defining_instructions->size(), 1u);
260 InstructionNode* defining_instruction = defining_instructions->at(0);
261 DCHECK(NULL != defining_instruction);
262 Region* incoming_region = *cit;
263 llvm::BasicBlock* incoming_basic_block = llvm_data_->GetBlock(incoming_region);
264 llvm::Value* incoming_value = llvm_data_->GetValue(defining_instruction);
265 llvm_phi->addIncoming(incoming_value, incoming_basic_block);
266 }
267}
268
269void CodeGenVisitor::Visit(SignatureNode* signature) {
270 std::cout << "Signature: ;" << "Id:" << signature->StringId() << std::endl;
271 DCHECK_EQ(signature->GetDefinitions().size(), 1u) << "Signature nodes must correspond to a single parameter register.";
272}
273void CodeGenPrepassVisitor::Visit(SignatureNode* signature) {
274 std::cout << "Signature: ;" << "Id:" << signature->StringId() << std::endl;
275 DCHECK_EQ(signature->GetDefinitions().size(), 1u) << "Signature nodes must correspond to a single parameter register.";
276}
277void CodeGenPostpassVisitor::Visit(SignatureNode* signature) {
278 std::cout << "Signature: ;" << "Id:" << signature->StringId() << std::endl;
279 DCHECK_EQ(signature->GetDefinitions().size(), 1u) << "Signature nodes must correspond to a single parameter register.";
280}
281
282} // end namespace sea_ir