blob: 09e193d46b0bd607f3ae227ad428cf8da5a18311 [file] [log] [blame]
Logan Chien8b977d32012-02-21 19:14:55 +08001/*
2 * Copyright (C) 2012 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 "compilation_unit.h"
18
Logan Chien110bcba2012-04-16 19:11:28 +080019#include "compiled_method.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070020#include "instruction_set.h"
Logan Chien8b977d32012-02-21 19:14:55 +080021#include "ir_builder.h"
22#include "logging.h"
23
TDYa127d668a062012-04-13 12:36:57 -070024#include "runtime_support_builder_arm.h"
25#include "runtime_support_builder_x86.h"
26
Logan Chien8b977d32012-02-21 19:14:55 +080027#include <llvm/ADT/OwningPtr.h>
28#include <llvm/ADT/StringSet.h>
29#include <llvm/ADT/Triple.h>
30#include <llvm/Analysis/CallGraph.h>
31#include <llvm/Analysis/DebugInfo.h>
32#include <llvm/Analysis/LoopPass.h>
33#include <llvm/Analysis/RegionPass.h>
34#include <llvm/Analysis/Verifier.h>
35#include <llvm/Assembly/PrintModulePass.h>
36#include <llvm/Bitcode/ReaderWriter.h>
37#include <llvm/CallGraphSCCPass.h>
Logan Chien110bcba2012-04-16 19:11:28 +080038#include <llvm/CodeGen/MachineFrameInfo.h>
39#include <llvm/CodeGen/MachineFunction.h>
40#include <llvm/CodeGen/MachineFunctionPass.h>
Logan Chien8b977d32012-02-21 19:14:55 +080041#include <llvm/DerivedTypes.h>
42#include <llvm/LLVMContext.h>
Logan Chien8b977d32012-02-21 19:14:55 +080043#include <llvm/Module.h>
44#include <llvm/PassManager.h>
45#include <llvm/Support/Debug.h>
46#include <llvm/Support/FormattedStream.h>
47#include <llvm/Support/ManagedStatic.h>
48#include <llvm/Support/PassNameParser.h>
49#include <llvm/Support/PluginLoader.h>
50#include <llvm/Support/PrettyStackTrace.h>
51#include <llvm/Support/Signals.h>
52#include <llvm/Support/SystemUtils.h>
53#include <llvm/Support/TargetRegistry.h>
54#include <llvm/Support/TargetSelect.h>
55#include <llvm/Support/ToolOutputFile.h>
56#include <llvm/Support/raw_ostream.h>
57#include <llvm/Target/TargetData.h>
58#include <llvm/Target/TargetLibraryInfo.h>
59#include <llvm/Target/TargetMachine.h>
60#include <llvm/Transforms/IPO/PassManagerBuilder.h>
61
62#include <string>
63
Logan Chien110bcba2012-04-16 19:11:28 +080064namespace {
65
66class UpdateFrameSizePass : public llvm::MachineFunctionPass {
67 public:
68 static char ID;
69
70 UpdateFrameSizePass() : llvm::MachineFunctionPass(ID), cunit_(NULL) {
71 LOG(FATAL) << "Unexpected instantiation of UpdateFrameSizePass";
72 // NOTE: We have to declare this constructor for llvm::RegisterPass, but
73 // this constructor won't work because we have no information on
74 // CompilationUnit. Thus, we should place a LOG(FATAL) here.
75 }
76
77 UpdateFrameSizePass(art::compiler_llvm::CompilationUnit* cunit)
78 : llvm::MachineFunctionPass(ID), cunit_(cunit) {
79 }
80
81 virtual bool runOnMachineFunction(llvm::MachineFunction &MF) {
82 cunit_->UpdateFrameSizeInBytes(MF.getFunction(),
83 MF.getFrameInfo()->getStackSize());
84 return false;
85 }
86
87 private:
88 art::compiler_llvm::CompilationUnit* cunit_;
89};
90
91char UpdateFrameSizePass::ID = 0;
92
93llvm::RegisterPass<UpdateFrameSizePass> reg_update_frame_size_pass_(
94 "update-frame-size", "Update frame size pass", false, false);
95
96} // end anonymous namespace
97
Logan Chien8b977d32012-02-21 19:14:55 +080098namespace art {
99namespace compiler_llvm {
100
101llvm::Module* makeLLVMModuleContents(llvm::Module* module);
102
103
Logan Chien6546ec52012-03-17 20:08:29 +0800104CompilationUnit::CompilationUnit(InstructionSet insn_set, size_t elf_idx)
105: insn_set_(insn_set), elf_idx_(elf_idx), context_(new llvm::LLVMContext()),
Logan Chien937105a2012-04-02 02:37:37 +0800106 mem_usage_(0), num_elf_funcs_(0) {
Logan Chien8b977d32012-02-21 19:14:55 +0800107
108 // Create the module and include the runtime function declaration
109 module_ = new llvm::Module("art", *context_);
110 makeLLVMModuleContents(module_);
111
112 // Create IRBuilder
113 irb_.reset(new IRBuilder(*context_, *module_));
TDYa127d668a062012-04-13 12:36:57 -0700114
115 // We always need a switch case, so just use a normal function.
116 switch(insn_set_) {
117 default:
118 runtime_support_.reset(new RuntimeSupportBuilder(*context_, *module_, *irb_));
119 break;
120 case kArm:
121 case kThumb2:
122 runtime_support_.reset(new RuntimeSupportBuilderARM(*context_, *module_, *irb_));
123 break;
124 case kX86:
125 runtime_support_.reset(new RuntimeSupportBuilderX86(*context_, *module_, *irb_));
126 break;
127 }
128
129 runtime_support_->OptimizeRuntimeSupport();
130
131 irb_->SetRuntimeSupport(runtime_support_.get());
Logan Chien8b977d32012-02-21 19:14:55 +0800132}
133
134
135CompilationUnit::~CompilationUnit() {
136}
137
138
139bool CompilationUnit::WriteBitcodeToFile() {
140 std::string errmsg;
141
142 llvm::OwningPtr<llvm::tool_output_file> out_file(
143 new llvm::tool_output_file(bitcode_filename_.c_str(), errmsg,
144 llvm::raw_fd_ostream::F_Binary));
145
146
147 if (!errmsg.empty()) {
148 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
149 return false;
150 }
151
152 llvm::WriteBitcodeToFile(module_, out_file->os());
153 out_file->keep();
154
155 return true;
156}
157
158
159bool CompilationUnit::Materialize() {
Logan Chien8b977d32012-02-21 19:14:55 +0800160 // Lookup the LLVM target
161 char const* target_triple = NULL;
162 char const* target_attr = NULL;
163
Logan Chien8ee03b52012-03-01 13:53:02 +0800164 switch (insn_set_) {
165 case kThumb2:
Logan Chien8b977d32012-02-21 19:14:55 +0800166 target_triple = "thumb-none-linux-gnueabi";
167 target_attr = "+thumb2,+neon,+neonfp,+vfp3";
Logan Chien8ee03b52012-03-01 13:53:02 +0800168 break;
169
170 case kArm:
Logan Chien8b977d32012-02-21 19:14:55 +0800171 target_triple = "armv7-none-linux-gnueabi";
172 target_attr = "+v7,+neon,+neonfp,+vfp3";
Logan Chien8ee03b52012-03-01 13:53:02 +0800173 break;
174
175 case kX86:
Logan Chien8b977d32012-02-21 19:14:55 +0800176 target_triple = "i386-pc-linux-gnu";
177 target_attr = "";
Logan Chien8ee03b52012-03-01 13:53:02 +0800178 break;
179
Shih-wei Liao6edfde42012-03-01 15:49:12 -0800180 case kMips:
181 target_triple = "mipsel-unknown-linux";
182 target_attr = "mips32r2";
183 break;
Logan Chien8ee03b52012-03-01 13:53:02 +0800184
185 default:
Logan Chien8b977d32012-02-21 19:14:55 +0800186 LOG(FATAL) << "Unknown instruction set: " << insn_set_;
187 }
188
189 std::string errmsg;
190 llvm::Target const* target =
191 llvm::TargetRegistry::lookupTarget(target_triple, errmsg);
192
193 CHECK(target != NULL) << errmsg;
194
195 // Target options
196 llvm::TargetOptions target_options;
Logan Chien8b977d32012-02-21 19:14:55 +0800197 target_options.FloatABIType = llvm::FloatABI::Soft;
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800198 target_options.NoFramePointerElim = true;
199 target_options.NoFramePointerElimNonLeaf = true;
Logan Chien6d6d7542012-03-02 14:00:31 +0800200 target_options.UseSoftFloat = false;
Shih-wei Liaoc6c317e2012-03-30 01:41:18 -0700201 target_options.EnableFastISel = true;
Logan Chien8b977d32012-02-21 19:14:55 +0800202
203 // Create the llvm::TargetMachine
204 llvm::TargetMachine* target_machine =
205 target->createTargetMachine(target_triple, "", target_attr, target_options,
206 llvm::Reloc::Static, llvm::CodeModel::Small,
207 llvm::CodeGenOpt::Aggressive);
208
209 CHECK(target_machine != NULL) << "Failed to create target machine";
210
211
212 // Add target data
213 llvm::TargetData const* target_data = target_machine->getTargetData();
214
215 // PassManager for code generation passes
216 llvm::PassManager pm;
217 pm.add(new llvm::TargetData(*target_data));
218
219 // FunctionPassManager for optimization pass
220 llvm::FunctionPassManager fpm(module_);
221 fpm.add(new llvm::TargetData(*target_data));
222
223 // Add optimization pass
224 llvm::PassManagerBuilder pm_builder;
225 pm_builder.Inliner = NULL; // TODO: add some inline in the future
226 pm_builder.OptLevel = 3;
227 pm_builder.DisableSimplifyLibCalls = 1;
228 pm_builder.populateModulePassManager(pm);
229 pm_builder.populateFunctionPassManager(fpm);
230
Logan Chienb9eaeea2012-03-17 19:45:01 +0800231 // Add passes to emit ELF image
232 {
233 llvm::formatted_raw_ostream formatted_os(
234 *(new llvm::raw_string_ostream(elf_image_)), true);
235
236 // Ask the target to add backend passes as necessary.
237 if (target_machine->addPassesToEmitFile(pm,
238 formatted_os,
239 llvm::TargetMachine::CGFT_ObjectFile,
240 true)) {
241 LOG(FATAL) << "Unable to generate ELF for this target";
242 return false;
243 }
244
Logan Chien110bcba2012-04-16 19:11:28 +0800245 // Add pass to update the frame_size_in_bytes_
246 pm.add(new ::UpdateFrameSizePass(this));
247
Logan Chienb9eaeea2012-03-17 19:45:01 +0800248 // Run the per-function optimization
249 fpm.doInitialization();
250 for (llvm::Module::iterator F = module_->begin(), E = module_->end();
251 F != E; ++F) {
252 fpm.run(*F);
253 }
254 fpm.doFinalization();
255
256 // Run the code generation passes
257 pm.run(*module_);
258 }
259
Logan Chiende08e842012-03-21 00:34:12 +0800260 LOG(INFO) << "Compilation Unit: " << elf_idx_ << " (done)";
Logan Chien7f767612012-03-01 18:54:49 +0800261
262 // Free the resources
263 context_.reset(NULL);
264 irb_.reset(NULL);
265 module_ = NULL;
Logan Chien8b977d32012-02-21 19:14:55 +0800266
267 return true;
268}
269
270
Logan Chien110bcba2012-04-16 19:11:28 +0800271void CompilationUnit::RegisterCompiledMethod(const llvm::Function* func,
272 CompiledMethod* compiled_method) {
273 compiled_methods_map_.Put(func, compiled_method);
274}
275
276
277void CompilationUnit::UpdateFrameSizeInBytes(const llvm::Function* func,
278 size_t frame_size_in_bytes) {
279 SafeMap<const llvm::Function*, CompiledMethod*>::iterator iter =
280 compiled_methods_map_.find(func);
281
282 if (iter != compiled_methods_map_.end()) {
283 CompiledMethod* compiled_method = iter->second;
284 compiled_method->SetFrameSizeInBytes(frame_size_in_bytes);
285
286 if (frame_size_in_bytes > 1728u) {
287 LOG(WARNING) << "Huge frame size: " << frame_size_in_bytes
288 << " elf_idx=" << compiled_method->GetElfIndex()
289 << " elf_func_idx=" << compiled_method->GetElfFuncIndex();
290 }
291 }
292}
293
294
Logan Chien8b977d32012-02-21 19:14:55 +0800295} // namespace compiler_llvm
296} // namespace art