blob: 38faf618cfa64f4112c06c913882cfbee167574d [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"
Shih-wei Liaod7726e42012-04-20 15:23:36 -070020#include "file.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070021#include "instruction_set.h"
Logan Chien8b977d32012-02-21 19:14:55 +080022#include "ir_builder.h"
23#include "logging.h"
Shih-wei Liaod7726e42012-04-20 15:23:36 -070024#include "os.h"
Logan Chien8b977d32012-02-21 19:14:55 +080025
TDYa127d668a062012-04-13 12:36:57 -070026#include "runtime_support_builder_arm.h"
TDYa127b08ed122012-06-05 23:51:19 -070027#include "runtime_support_builder_thumb2.h"
TDYa127d668a062012-04-13 12:36:57 -070028#include "runtime_support_builder_x86.h"
29
Logan Chien8b977d32012-02-21 19:14:55 +080030#include <llvm/ADT/OwningPtr.h>
31#include <llvm/ADT/StringSet.h>
32#include <llvm/ADT/Triple.h>
33#include <llvm/Analysis/CallGraph.h>
34#include <llvm/Analysis/DebugInfo.h>
TDYa127f15b0ab2012-05-11 21:01:36 -070035#include <llvm/Analysis/Dominators.h>
36#include <llvm/Analysis/LoopInfo.h>
Logan Chien8b977d32012-02-21 19:14:55 +080037#include <llvm/Analysis/LoopPass.h>
38#include <llvm/Analysis/RegionPass.h>
TDYa127f15b0ab2012-05-11 21:01:36 -070039#include <llvm/Analysis/ScalarEvolution.h>
Logan Chien8b977d32012-02-21 19:14:55 +080040#include <llvm/Analysis/Verifier.h>
41#include <llvm/Assembly/PrintModulePass.h>
42#include <llvm/Bitcode/ReaderWriter.h>
43#include <llvm/CallGraphSCCPass.h>
Logan Chien110bcba2012-04-16 19:11:28 +080044#include <llvm/CodeGen/MachineFrameInfo.h>
45#include <llvm/CodeGen/MachineFunction.h>
46#include <llvm/CodeGen/MachineFunctionPass.h>
Logan Chien8b977d32012-02-21 19:14:55 +080047#include <llvm/DerivedTypes.h>
48#include <llvm/LLVMContext.h>
Logan Chien8b977d32012-02-21 19:14:55 +080049#include <llvm/Module.h>
50#include <llvm/PassManager.h>
51#include <llvm/Support/Debug.h>
52#include <llvm/Support/FormattedStream.h>
53#include <llvm/Support/ManagedStatic.h>
Shih-wei Liaod7726e42012-04-20 15:23:36 -070054#include <llvm/Support/MemoryBuffer.h>
Logan Chien8b977d32012-02-21 19:14:55 +080055#include <llvm/Support/PassNameParser.h>
56#include <llvm/Support/PluginLoader.h>
57#include <llvm/Support/PrettyStackTrace.h>
58#include <llvm/Support/Signals.h>
59#include <llvm/Support/SystemUtils.h>
60#include <llvm/Support/TargetRegistry.h>
61#include <llvm/Support/TargetSelect.h>
62#include <llvm/Support/ToolOutputFile.h>
63#include <llvm/Support/raw_ostream.h>
Shih-wei Liaod7726e42012-04-20 15:23:36 -070064#include <llvm/Support/system_error.h>
Logan Chien8b977d32012-02-21 19:14:55 +080065#include <llvm/Target/TargetData.h>
66#include <llvm/Target/TargetLibraryInfo.h>
67#include <llvm/Target/TargetMachine.h>
Shih-wei Liaof1cb9a52012-04-20 01:49:18 -070068#include <llvm/Transforms/IPO.h>
Logan Chien8b977d32012-02-21 19:14:55 +080069#include <llvm/Transforms/IPO/PassManagerBuilder.h>
TDYa127f15b0ab2012-05-11 21:01:36 -070070#include <llvm/Transforms/Scalar.h>
Logan Chien8b977d32012-02-21 19:14:55 +080071
Shih-wei Liaod7726e42012-04-20 15:23:36 -070072#include <sys/types.h>
73#include <sys/wait.h>
74#include <unistd.h>
75
Logan Chien8b977d32012-02-21 19:14:55 +080076#include <string>
77
Logan Chien110bcba2012-04-16 19:11:28 +080078namespace {
79
80class UpdateFrameSizePass : public llvm::MachineFunctionPass {
81 public:
82 static char ID;
83
84 UpdateFrameSizePass() : llvm::MachineFunctionPass(ID), cunit_(NULL) {
85 LOG(FATAL) << "Unexpected instantiation of UpdateFrameSizePass";
86 // NOTE: We have to declare this constructor for llvm::RegisterPass, but
87 // this constructor won't work because we have no information on
88 // CompilationUnit. Thus, we should place a LOG(FATAL) here.
89 }
90
91 UpdateFrameSizePass(art::compiler_llvm::CompilationUnit* cunit)
92 : llvm::MachineFunctionPass(ID), cunit_(cunit) {
93 }
94
95 virtual bool runOnMachineFunction(llvm::MachineFunction &MF) {
96 cunit_->UpdateFrameSizeInBytes(MF.getFunction(),
97 MF.getFrameInfo()->getStackSize());
98 return false;
99 }
100
101 private:
102 art::compiler_llvm::CompilationUnit* cunit_;
103};
104
105char UpdateFrameSizePass::ID = 0;
106
107llvm::RegisterPass<UpdateFrameSizePass> reg_update_frame_size_pass_(
108 "update-frame-size", "Update frame size pass", false, false);
109
TDYa127f15b0ab2012-05-11 21:01:36 -0700110
111// TODO: We may need something to manage these passes.
112// TODO: We need high-level IR to analysis and do this at the IRBuilder level.
113class AddSuspendCheckToLoopLatchPass : public llvm::LoopPass {
114 public:
115 static char ID;
116
117 AddSuspendCheckToLoopLatchPass() : llvm::LoopPass(ID), irb_(NULL) {
118 LOG(FATAL) << "Unexpected instantiation of AddSuspendCheckToLoopLatchPass";
119 // NOTE: We have to declare this constructor for llvm::RegisterPass, but
120 // this constructor won't work because we have no information on
121 // IRBuilder. Thus, we should place a LOG(FATAL) here.
122 }
123
124 AddSuspendCheckToLoopLatchPass(art::compiler_llvm::IRBuilder* irb)
125 : llvm::LoopPass(ID), irb_(irb) {
126 }
127
128 virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const {
129 AU.addRequiredID(llvm::LoopSimplifyID);
130
Shih-wei Liaoba67d7d2012-06-23 18:48:04 -0700131 AU.addPreserved<llvm::DominatorTree>();
TDYa127f15b0ab2012-05-11 21:01:36 -0700132 AU.addPreserved<llvm::LoopInfo>();
Shih-wei Liaoba67d7d2012-06-23 18:48:04 -0700133 AU.addPreservedID(llvm::LoopSimplifyID);
TDYa127f15b0ab2012-05-11 21:01:36 -0700134 AU.addPreserved<llvm::ScalarEvolution>();
135 AU.addPreservedID(llvm::BreakCriticalEdgesID);
136 }
137
138 virtual bool runOnLoop(llvm::Loop *loop, llvm::LPPassManager &lpm) {
139 CHECK_EQ(loop->getNumBackEdges(), 1U) << "Loop must be simplified!";
140 llvm::BasicBlock* bb = loop->getLoopLatch();
141 CHECK_NE(bb, static_cast<void*>(NULL)) << "A single loop latch must exist.";
142
Shih-wei Liaoba67d7d2012-06-23 18:48:04 -0700143 irb_->SetInsertPoint(bb->getTerminator());
TDYa127f15b0ab2012-05-11 21:01:36 -0700144
Shih-wei Liaoba67d7d2012-06-23 18:48:04 -0700145 using art::compiler_llvm::runtime_support::TestSuspend;
146 llvm::Value* runtime_func = irb_->GetRuntime(TestSuspend);
147 irb_->CreateCall(runtime_func, irb_->getJNull());
TDYa127f15b0ab2012-05-11 21:01:36 -0700148
149 return true;
150 }
151
152 private:
153 art::compiler_llvm::IRBuilder* irb_;
154};
155
156char AddSuspendCheckToLoopLatchPass::ID = 0;
157
158llvm::RegisterPass<AddSuspendCheckToLoopLatchPass> reg_add_suspend_check_to_loop_latch_pass_(
159 "add-suspend-check-to-loop-latch", "Add suspend check to loop latch pass", false, false);
160
161
Logan Chien110bcba2012-04-16 19:11:28 +0800162} // end anonymous namespace
163
Logan Chien8b977d32012-02-21 19:14:55 +0800164namespace art {
165namespace compiler_llvm {
166
167llvm::Module* makeLLVMModuleContents(llvm::Module* module);
168
169
Logan Chien6546ec52012-03-17 20:08:29 +0800170CompilationUnit::CompilationUnit(InstructionSet insn_set, size_t elf_idx)
Logan Chien8ba2fc52012-04-23 09:10:46 +0800171: cunit_lock_("compilation_unit_lock"), insn_set_(insn_set), elf_idx_(elf_idx),
TDYa127b2eb5c12012-05-24 15:52:10 -0700172 context_(new llvm::LLVMContext()), compiled_methods_map_(new CompiledMethodMap()),
173 mem_usage_(0), num_elf_funcs_(0) {
Logan Chien8b977d32012-02-21 19:14:55 +0800174
175 // Create the module and include the runtime function declaration
176 module_ = new llvm::Module("art", *context_);
177 makeLLVMModuleContents(module_);
178
179 // Create IRBuilder
180 irb_.reset(new IRBuilder(*context_, *module_));
TDYa127d668a062012-04-13 12:36:57 -0700181
182 // We always need a switch case, so just use a normal function.
183 switch(insn_set_) {
TDYa127b08ed122012-06-05 23:51:19 -0700184 default:
185 runtime_support_.reset(new RuntimeSupportBuilder(*context_, *module_, *irb_));
186 break;
TDYa127d668a062012-04-13 12:36:57 -0700187 case kArm:
TDYa127d668a062012-04-13 12:36:57 -0700188 runtime_support_.reset(new RuntimeSupportBuilderARM(*context_, *module_, *irb_));
189 break;
TDYa127b08ed122012-06-05 23:51:19 -0700190 case kThumb2:
191 runtime_support_.reset(new RuntimeSupportBuilderThumb2(*context_, *module_, *irb_));
192 break;
TDYa127d668a062012-04-13 12:36:57 -0700193 case kX86:
194 runtime_support_.reset(new RuntimeSupportBuilderX86(*context_, *module_, *irb_));
195 break;
196 }
197
Shih-wei Liaoba67d7d2012-06-23 18:48:04 -0700198 runtime_support_->OptimizeRuntimeSupport();
199
TDYa127d668a062012-04-13 12:36:57 -0700200 irb_->SetRuntimeSupport(runtime_support_.get());
Logan Chien8b977d32012-02-21 19:14:55 +0800201}
202
203
204CompilationUnit::~CompilationUnit() {
205}
206
207
Logan Chien08e1ba32012-05-08 15:08:51 +0800208bool CompilationUnit::Materialize(size_t thread_count) {
Logan Chien8ba2fc52012-04-23 09:10:46 +0800209 MutexLock GUARD(cunit_lock_);
210
Logan Chienb5eb00c2012-05-18 19:56:46 +0800211 // Materialize the bitcode to elf_image_
212 llvm::raw_string_ostream str_os(elf_image_);
213 bool success = MaterializeToFile(str_os);
214 LOG(INFO) << "Compilation Unit: " << elf_idx_ << (success ? " (done)" : " (failed)");
TDYa127388a83b2012-05-09 18:56:22 -0700215
Logan Chienb5eb00c2012-05-18 19:56:46 +0800216 // Free the resources
217 context_.reset(NULL);
218 irb_.reset(NULL);
219 module_ = NULL;
TDYa127b2eb5c12012-05-24 15:52:10 -0700220 runtime_support_.reset(NULL);
221 compiled_methods_map_.reset(NULL);
TDYa127388a83b2012-05-09 18:56:22 -0700222
Logan Chienb5eb00c2012-05-18 19:56:46 +0800223 return success;
Logan Chien8b977d32012-02-21 19:14:55 +0800224}
225
226
Logan Chien110bcba2012-04-16 19:11:28 +0800227void CompilationUnit::RegisterCompiledMethod(const llvm::Function* func,
228 CompiledMethod* compiled_method) {
Logan Chien8ba2fc52012-04-23 09:10:46 +0800229 MutexLock GUARD(cunit_lock_);
TDYa127b2eb5c12012-05-24 15:52:10 -0700230 compiled_methods_map_->Put(func, compiled_method);
Logan Chien110bcba2012-04-16 19:11:28 +0800231}
232
233
234void CompilationUnit::UpdateFrameSizeInBytes(const llvm::Function* func,
235 size_t frame_size_in_bytes) {
Logan Chien8ba2fc52012-04-23 09:10:46 +0800236 MutexLock GUARD(cunit_lock_);
Logan Chien110bcba2012-04-16 19:11:28 +0800237 SafeMap<const llvm::Function*, CompiledMethod*>::iterator iter =
TDYa127b2eb5c12012-05-24 15:52:10 -0700238 compiled_methods_map_->find(func);
Logan Chien110bcba2012-04-16 19:11:28 +0800239
TDYa127b2eb5c12012-05-24 15:52:10 -0700240 if (iter != compiled_methods_map_->end()) {
Logan Chien110bcba2012-04-16 19:11:28 +0800241 CompiledMethod* compiled_method = iter->second;
242 compiled_method->SetFrameSizeInBytes(frame_size_in_bytes);
243
244 if (frame_size_in_bytes > 1728u) {
245 LOG(WARNING) << "Huge frame size: " << frame_size_in_bytes
246 << " elf_idx=" << compiled_method->GetElfIndex()
247 << " elf_func_idx=" << compiled_method->GetElfFuncIndex();
248 }
249 }
250}
251
Logan Chienb1bab1c2012-05-11 11:05:45 +0800252bool CompilationUnit::MaterializeToFile(llvm::raw_ostream& out_stream) {
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700253 // Lookup the LLVM target
254 char const* target_triple = NULL;
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700255 char const* target_cpu = "";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700256 char const* target_attr = NULL;
257
Logan Chienb1bab1c2012-05-11 11:05:45 +0800258 switch (insn_set_) {
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700259 case kThumb2:
260 target_triple = "thumb-none-linux-gnueabi";
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700261 target_cpu = "cortex-a9";
TDYa127b08ed122012-06-05 23:51:19 -0700262 target_attr = "+thumb2,+neon,+neonfp,+vfp3,+db";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700263 break;
264
265 case kArm:
266 target_triple = "armv7-none-linux-gnueabi";
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700267 // TODO: Fix for Nexus S.
268 target_cpu = "cortex-a9";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700269 // TODO: Fix for Xoom.
TDYa127b08ed122012-06-05 23:51:19 -0700270 target_attr = "+v7,+neon,+neonfp,+vfp3,+db";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700271 break;
272
273 case kX86:
274 target_triple = "i386-pc-linux-gnu";
275 target_attr = "";
276 break;
277
278 case kMips:
279 target_triple = "mipsel-unknown-linux";
280 target_attr = "mips32r2";
281 break;
282
283 default:
Logan Chienb1bab1c2012-05-11 11:05:45 +0800284 LOG(FATAL) << "Unknown instruction set: " << insn_set_;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700285 }
286
287 std::string errmsg;
288 llvm::Target const* target =
289 llvm::TargetRegistry::lookupTarget(target_triple, errmsg);
290
291 CHECK(target != NULL) << errmsg;
292
293 // Target options
294 llvm::TargetOptions target_options;
295 target_options.FloatABIType = llvm::FloatABI::Soft;
296 target_options.NoFramePointerElim = true;
297 target_options.NoFramePointerElimNonLeaf = true;
298 target_options.UseSoftFloat = false;
TDYa1273978da52012-05-19 07:45:39 -0700299 target_options.EnableFastISel = false;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700300
301 // Create the llvm::TargetMachine
Logan Chienb6bed0b2012-05-04 15:03:56 +0800302 llvm::OwningPtr<llvm::TargetMachine> target_machine(
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700303 target->createTargetMachine(target_triple, target_cpu, target_attr, target_options,
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700304 llvm::Reloc::Static, llvm::CodeModel::Small,
Shih-wei Liaodac5eb22012-06-03 14:06:04 -0700305 llvm::CodeGenOpt::Aggressive));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700306
Logan Chienb6bed0b2012-05-04 15:03:56 +0800307 CHECK(target_machine.get() != NULL) << "Failed to create target machine";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700308
309 // Add target data
310 llvm::TargetData const* target_data = target_machine->getTargetData();
311
312 // PassManager for code generation passes
313 llvm::PassManager pm;
314 pm.add(new llvm::TargetData(*target_data));
315
316 // FunctionPassManager for optimization pass
Logan Chien799ef4f2012-04-23 00:17:47 +0800317 llvm::FunctionPassManager fpm(module_);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700318 fpm.add(new llvm::TargetData(*target_data));
319
TDYa127f15b0ab2012-05-11 21:01:36 -0700320 if (bitcode_filename_.empty()) {
321 // If we don't need write the bitcode to file, add the AddSuspendCheckToLoopLatchPass to the
322 // regular FunctionPass.
323 fpm.add(new ::AddSuspendCheckToLoopLatchPass(irb_.get()));
324 } else {
325 // Run AddSuspendCheckToLoopLatchPass before we write the bitcode to file.
326 llvm::FunctionPassManager fpm2(module_);
327 fpm2.add(new ::AddSuspendCheckToLoopLatchPass(irb_.get()));
328 fpm2.doInitialization();
329 for (llvm::Module::iterator F = module_->begin(), E = module_->end();
330 F != E; ++F) {
331 fpm2.run(*F);
332 }
333 fpm2.doFinalization();
334
335
336 // Write bitcode to file
337 std::string errmsg;
338
339 llvm::OwningPtr<llvm::tool_output_file> out_file(
340 new llvm::tool_output_file(bitcode_filename_.c_str(), errmsg,
341 llvm::raw_fd_ostream::F_Binary));
342
343
344 if (!errmsg.empty()) {
345 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
346 return false;
347 }
348
349 llvm::WriteBitcodeToFile(module_, out_file->os());
350 out_file->keep();
351 }
352
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700353 // Add optimization pass
354 llvm::PassManagerBuilder pm_builder;
Shih-wei Liaoe0e40242012-05-08 01:04:03 -0700355 //pm_builder.Inliner = llvm::createFunctionInliningPass();
Shih-wei Liaoba67d7d2012-06-23 18:48:04 -0700356 pm_builder.Inliner = llvm::createAlwaysInlinerPass();
Shih-wei Liao415576b2012-04-23 15:28:53 -0700357 //pm_builder.Inliner = llvm::createPartialInliningPass();
358 pm_builder.OptLevel = 3;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700359 pm_builder.DisableSimplifyLibCalls = 1;
TDYa127e4c2ccc2012-05-13 21:10:36 -0700360 pm_builder.DisableUnitAtATime = 1;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700361 pm_builder.populateFunctionPassManager(fpm);
TDYa127ce9c3172012-05-15 06:09:27 -0700362 pm_builder.populateModulePassManager(pm);
363 pm.add(llvm::createStripDeadPrototypesPass());
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700364
365 // Add passes to emit ELF image
366 {
Logan Chien08e1ba32012-05-08 15:08:51 +0800367 llvm::formatted_raw_ostream formatted_os(out_stream, false);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700368
369 // Ask the target to add backend passes as necessary.
370 if (target_machine->addPassesToEmitFile(pm,
371 formatted_os,
372 llvm::TargetMachine::CGFT_ObjectFile,
373 true)) {
374 LOG(FATAL) << "Unable to generate ELF for this target";
375 return false;
376 }
377
378 // FIXME: Unable to run the UpdateFrameSizePass pass since it tries to
379 // update the value reside in the different address space.
380 // Add pass to update the frame_size_in_bytes_
381 //pm.add(new ::UpdateFrameSizePass(this));
382
383 // Run the per-function optimization
384 fpm.doInitialization();
Logan Chien799ef4f2012-04-23 00:17:47 +0800385 for (llvm::Module::iterator F = module_->begin(), E = module_->end();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700386 F != E; ++F) {
387 fpm.run(*F);
388 }
389 fpm.doFinalization();
390
391 // Run the code generation passes
Logan Chien799ef4f2012-04-23 00:17:47 +0800392 pm.run(*module_);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700393 }
394
395 return true;
396}
Logan Chien110bcba2012-04-16 19:11:28 +0800397
Logan Chien8b977d32012-02-21 19:14:55 +0800398} // namespace compiler_llvm
399} // namespace art