blob: 94dfe7236c7e7272d213672d332c56ec5f3a5a99 [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
Brian Carlstrom641ce032013-01-31 15:21:37 -080017#include "llvm_compilation_unit.h"
Logan Chien8b977d32012-02-21 19:14:55 +080018
Brian Carlstrom265091e2013-01-30 14:08:26 -080019#include <sys/types.h>
20#include <sys/wait.h>
21#include <unistd.h>
Logan Chien8b977d32012-02-21 19:14:55 +080022
Brian Carlstrom265091e2013-01-30 14:08:26 -080023#include <string>
TDYa127d668a062012-04-13 12:36:57 -070024
Logan Chien8b977d32012-02-21 19:14:55 +080025#include <llvm/ADT/OwningPtr.h>
26#include <llvm/ADT/StringSet.h>
27#include <llvm/ADT/Triple.h>
28#include <llvm/Analysis/CallGraph.h>
TDYa127f15b0ab2012-05-11 21:01:36 -070029#include <llvm/Analysis/Dominators.h>
30#include <llvm/Analysis/LoopInfo.h>
Logan Chien8b977d32012-02-21 19:14:55 +080031#include <llvm/Analysis/LoopPass.h>
32#include <llvm/Analysis/RegionPass.h>
TDYa127f15b0ab2012-05-11 21:01:36 -070033#include <llvm/Analysis/ScalarEvolution.h>
Logan Chien8b977d32012-02-21 19:14:55 +080034#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>
Brian Carlstrombd86bcc2013-03-10 20:26:16 -070041#include <llvm/DebugInfo.h>
Logan Chien8b977d32012-02-21 19:14:55 +080042#include <llvm/DerivedTypes.h>
43#include <llvm/LLVMContext.h>
Logan Chien8b977d32012-02-21 19:14:55 +080044#include <llvm/Module.h>
Logan Chien971bf3f2012-05-01 15:47:55 +080045#include <llvm/Object/ObjectFile.h>
Logan Chien8b977d32012-02-21 19:14:55 +080046#include <llvm/PassManager.h>
47#include <llvm/Support/Debug.h>
Logan Chien971bf3f2012-05-01 15:47:55 +080048#include <llvm/Support/ELF.h>
Logan Chien8b977d32012-02-21 19:14:55 +080049#include <llvm/Support/FormattedStream.h>
50#include <llvm/Support/ManagedStatic.h>
Shih-wei Liaod7726e42012-04-20 15:23:36 -070051#include <llvm/Support/MemoryBuffer.h>
Logan Chien8b977d32012-02-21 19:14:55 +080052#include <llvm/Support/PassNameParser.h>
53#include <llvm/Support/PluginLoader.h>
54#include <llvm/Support/PrettyStackTrace.h>
55#include <llvm/Support/Signals.h>
56#include <llvm/Support/SystemUtils.h>
57#include <llvm/Support/TargetRegistry.h>
58#include <llvm/Support/TargetSelect.h>
59#include <llvm/Support/ToolOutputFile.h>
60#include <llvm/Support/raw_ostream.h>
Shih-wei Liaod7726e42012-04-20 15:23:36 -070061#include <llvm/Support/system_error.h>
Logan Chien8b977d32012-02-21 19:14:55 +080062#include <llvm/Target/TargetData.h>
63#include <llvm/Target/TargetLibraryInfo.h>
64#include <llvm/Target/TargetMachine.h>
Shih-wei Liaof1cb9a52012-04-20 01:49:18 -070065#include <llvm/Transforms/IPO.h>
Logan Chien8b977d32012-02-21 19:14:55 +080066#include <llvm/Transforms/IPO/PassManagerBuilder.h>
TDYa127f15b0ab2012-05-11 21:01:36 -070067#include <llvm/Transforms/Scalar.h>
Logan Chien8b977d32012-02-21 19:14:55 +080068
Brian Carlstrom265091e2013-01-30 14:08:26 -080069#include "base/logging.h"
70#include "base/unix_file/fd_file.h"
71#include "compiled_method.h"
72#include "compiler_llvm.h"
73#include "instruction_set.h"
74#include "ir_builder.h"
75#include "os.h"
76#include "runtime_support_builder_arm.h"
77#include "runtime_support_builder_thumb2.h"
78#include "runtime_support_builder_x86.h"
79#include "utils_llvm.h"
Logan Chien8b977d32012-02-21 19:14:55 +080080
81namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -080082namespace llvm {
Logan Chien8b977d32012-02-21 19:14:55 +080083
Ian Rogers4c1c2832013-03-04 18:30:13 -080084::llvm::FunctionPass*
Ian Rogers76ae4fe2013-02-27 16:03:41 -080085CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Brian Carlstrom265091e2013-01-30 14:08:26 -080086 CompilerDriver* compiler, const DexCompilationUnit* dex_compilation_unit);
Shih-wei Liao21d28f52012-06-12 05:55:00 -070087
Ian Rogers4c1c2832013-03-04 18:30:13 -080088::llvm::Module* makeLLVMModuleContents(::llvm::Module* module);
Logan Chien8b977d32012-02-21 19:14:55 +080089
90
Brian Carlstrom265091e2013-01-30 14:08:26 -080091LlvmCompilationUnit::LlvmCompilationUnit(const CompilerLLVM* compiler_llvm, size_t cunit_id)
92 : compiler_llvm_(compiler_llvm), cunit_id_(cunit_id) {
Ian Rogers1212a022013-03-04 10:48:41 -080093 driver_ = NULL;
Ian Rogers89756f22013-03-04 16:40:02 -080094 dex_compilation_unit_ = NULL;
buzbee4df2bbd2012-10-11 14:46:06 -070095 llvm_info_.reset(new LLVMInfo());
96 context_.reset(llvm_info_->GetLLVMContext());
97 module_ = llvm_info_->GetLLVMModule();
TDYa12755e5e6c2012-09-11 15:14:42 -070098
99 // Include the runtime function declaration
100 makeLLVMModuleContents(module_);
101
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800102
103 intrinsic_helper_.reset(new IntrinsicHelper(*context_, *module_));
104
Logan Chien8b977d32012-02-21 19:14:55 +0800105 // Create IRBuilder
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800106 irb_.reset(new IRBuilder(*context_, *module_, *intrinsic_helper_));
TDYa127d668a062012-04-13 12:36:57 -0700107
108 // We always need a switch case, so just use a normal function.
Logan Chien971bf3f2012-05-01 15:47:55 +0800109 switch(GetInstructionSet()) {
TDYa127b08ed122012-06-05 23:51:19 -0700110 default:
111 runtime_support_.reset(new RuntimeSupportBuilder(*context_, *module_, *irb_));
112 break;
TDYa127d668a062012-04-13 12:36:57 -0700113 case kArm:
TDYa127d668a062012-04-13 12:36:57 -0700114 runtime_support_.reset(new RuntimeSupportBuilderARM(*context_, *module_, *irb_));
115 break;
TDYa127b08ed122012-06-05 23:51:19 -0700116 case kThumb2:
117 runtime_support_.reset(new RuntimeSupportBuilderThumb2(*context_, *module_, *irb_));
118 break;
TDYa127d668a062012-04-13 12:36:57 -0700119 case kX86:
120 runtime_support_.reset(new RuntimeSupportBuilderX86(*context_, *module_, *irb_));
121 break;
122 }
123
TDYa127d668a062012-04-13 12:36:57 -0700124 irb_->SetRuntimeSupport(runtime_support_.get());
Logan Chien8b977d32012-02-21 19:14:55 +0800125}
126
127
Brian Carlstrom641ce032013-01-31 15:21:37 -0800128LlvmCompilationUnit::~LlvmCompilationUnit() {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800129 ::llvm::LLVMContext* llvm_context = context_.release(); // Managed by llvm_info_
TDYa12755e5e6c2012-09-11 15:14:42 -0700130 CHECK(llvm_context != NULL);
Logan Chien8b977d32012-02-21 19:14:55 +0800131}
132
133
Brian Carlstrom641ce032013-01-31 15:21:37 -0800134InstructionSet LlvmCompilationUnit::GetInstructionSet() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800135 return compiler_llvm_->GetInstructionSet();
Logan Chien8b977d32012-02-21 19:14:55 +0800136}
137
138
Brian Carlstrom35599732013-03-13 15:15:23 -0700139static std::string DumpDirectory() {
140 if (kIsTargetBuild) {
141 return GetArtCacheOrDie(GetAndroidData());
142 }
143 return "/tmp";
144}
145
146void LlvmCompilationUnit::DumpBitcodeToFile() {
147 std::string bitcode;
148 DumpBitcodeToString(bitcode);
149 std::string filename(StringPrintf("%s/Art%u.bc", DumpDirectory().c_str(), cunit_id_));
150 UniquePtr<File> output(OS::OpenFile(filename.c_str(), true));
151 output->WriteFully(bitcode.data(), bitcode.size());
152 LOG(INFO) << ".bc file written successfully: " << filename;
153}
154
155void LlvmCompilationUnit::DumpBitcodeToString(std::string& str_buffer) {
156 ::llvm::raw_string_ostream str_os(str_buffer);
157 ::llvm::WriteBitcodeToFile(module_, str_os);
158}
159
Brian Carlstrom641ce032013-01-31 15:21:37 -0800160bool LlvmCompilationUnit::Materialize() {
Brian Carlstrom35599732013-03-13 15:15:23 -0700161
162 const bool kDumpBitcode = false;
163 if (kDumpBitcode) {
164 // Dump the bitcode for debugging
165 DumpBitcodeToFile();
166 }
167
Ian Rogers4c1c2832013-03-04 18:30:13 -0800168 // Compile and prelink ::llvm::Module
Brian Carlstrom265091e2013-01-30 14:08:26 -0800169 if (!MaterializeToString(elf_object_)) {
170 LOG(ERROR) << "Failed to materialize compilation unit " << cunit_id_;
Logan Chien971bf3f2012-05-01 15:47:55 +0800171 return false;
Logan Chien110bcba2012-04-16 19:11:28 +0800172 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800173
Brian Carlstrom35599732013-03-13 15:15:23 -0700174 const bool kDumpELF = false;
175 if (kDumpELF) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800176 // Dump the ELF image for debugging
Brian Carlstrom35599732013-03-13 15:15:23 -0700177 std::string filename(StringPrintf("%s/Art%u.o", DumpDirectory().c_str(), cunit_id_));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800178 UniquePtr<File> output(OS::OpenFile(filename.c_str(), true));
179 output->WriteFully(elf_object_.data(), elf_object_.size());
180 LOG(INFO) << ".o file written successfully: " << filename;
Logan Chien971bf3f2012-05-01 15:47:55 +0800181 }
182
Logan Chien971bf3f2012-05-01 15:47:55 +0800183 return true;
Logan Chien110bcba2012-04-16 19:11:28 +0800184}
185
Logan Chien971bf3f2012-05-01 15:47:55 +0800186
Brian Carlstrom641ce032013-01-31 15:21:37 -0800187bool LlvmCompilationUnit::MaterializeToString(std::string& str_buffer) {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800188 ::llvm::raw_string_ostream str_os(str_buffer);
Logan Chien971bf3f2012-05-01 15:47:55 +0800189 return MaterializeToRawOStream(str_os);
190}
191
192
Ian Rogers4c1c2832013-03-04 18:30:13 -0800193bool LlvmCompilationUnit::MaterializeToRawOStream(::llvm::raw_ostream& out_stream) {
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700194 // Lookup the LLVM target
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800195 std::string target_triple;
196 std::string target_cpu;
197 std::string target_attr;
Ian Rogers1212a022013-03-04 10:48:41 -0800198 CompilerDriver::InstructionSetToLLVMTarget(GetInstructionSet(), target_triple, target_cpu, target_attr);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700199
200 std::string errmsg;
Ian Rogers4c1c2832013-03-04 18:30:13 -0800201 const ::llvm::Target* target =
202 ::llvm::TargetRegistry::lookupTarget(target_triple, errmsg);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700203
204 CHECK(target != NULL) << errmsg;
205
206 // Target options
Ian Rogers4c1c2832013-03-04 18:30:13 -0800207 ::llvm::TargetOptions target_options;
208 target_options.FloatABIType = ::llvm::FloatABI::Soft;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700209 target_options.NoFramePointerElim = true;
210 target_options.NoFramePointerElimNonLeaf = true;
211 target_options.UseSoftFloat = false;
TDYa1273978da52012-05-19 07:45:39 -0700212 target_options.EnableFastISel = false;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700213
Ian Rogers4c1c2832013-03-04 18:30:13 -0800214 // Create the ::llvm::TargetMachine
215 ::llvm::OwningPtr< ::llvm::TargetMachine> target_machine(
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700216 target->createTargetMachine(target_triple, target_cpu, target_attr, target_options,
Ian Rogers4c1c2832013-03-04 18:30:13 -0800217 ::llvm::Reloc::Static, ::llvm::CodeModel::Small,
218 ::llvm::CodeGenOpt::Aggressive));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700219
Logan Chienb6bed0b2012-05-04 15:03:56 +0800220 CHECK(target_machine.get() != NULL) << "Failed to create target machine";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700221
222 // Add target data
Ian Rogers4c1c2832013-03-04 18:30:13 -0800223 const ::llvm::TargetData* target_data = target_machine->getTargetData();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700224
225 // PassManager for code generation passes
Ian Rogers4c1c2832013-03-04 18:30:13 -0800226 ::llvm::PassManager pm;
227 pm.add(new ::llvm::TargetData(*target_data));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700228
229 // FunctionPassManager for optimization pass
Ian Rogers4c1c2832013-03-04 18:30:13 -0800230 ::llvm::FunctionPassManager fpm(module_);
231 fpm.add(new ::llvm::TargetData(*target_data));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700232
TDYa127f15b0ab2012-05-11 21:01:36 -0700233 if (bitcode_filename_.empty()) {
234 // If we don't need write the bitcode to file, add the AddSuspendCheckToLoopLatchPass to the
235 // regular FunctionPass.
buzbee4df2bbd2012-10-11 14:46:06 -0700236 fpm.add(CreateGBCExpanderPass(*llvm_info_->GetIntrinsicHelper(), *irb_.get(),
Ian Rogers89756f22013-03-04 16:40:02 -0800237 driver_, dex_compilation_unit_));
TDYa127f15b0ab2012-05-11 21:01:36 -0700238 } else {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800239 ::llvm::FunctionPassManager fpm2(module_);
buzbee4df2bbd2012-10-11 14:46:06 -0700240 fpm2.add(CreateGBCExpanderPass(*llvm_info_->GetIntrinsicHelper(), *irb_.get(),
Ian Rogers89756f22013-03-04 16:40:02 -0800241 driver_, dex_compilation_unit_));
TDYa127f15b0ab2012-05-11 21:01:36 -0700242 fpm2.doInitialization();
Ian Rogers4c1c2832013-03-04 18:30:13 -0800243 for (::llvm::Module::iterator F = module_->begin(), E = module_->end();
TDYa127f15b0ab2012-05-11 21:01:36 -0700244 F != E; ++F) {
245 fpm2.run(*F);
246 }
247 fpm2.doFinalization();
TDYa127f15b0ab2012-05-11 21:01:36 -0700248
249 // Write bitcode to file
250 std::string errmsg;
251
Ian Rogers4c1c2832013-03-04 18:30:13 -0800252 ::llvm::OwningPtr< ::llvm::tool_output_file> out_file(
253 new ::llvm::tool_output_file(bitcode_filename_.c_str(), errmsg,
254 ::llvm::raw_fd_ostream::F_Binary));
TDYa127f15b0ab2012-05-11 21:01:36 -0700255
256
257 if (!errmsg.empty()) {
258 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
259 return false;
260 }
261
Ian Rogers4c1c2832013-03-04 18:30:13 -0800262 ::llvm::WriteBitcodeToFile(module_, out_file->os());
TDYa127f15b0ab2012-05-11 21:01:36 -0700263 out_file->keep();
264 }
265
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700266 // Add optimization pass
Ian Rogers4c1c2832013-03-04 18:30:13 -0800267 ::llvm::PassManagerBuilder pm_builder;
TDYa1279a129452012-07-19 03:10:08 -0700268 // TODO: Use inliner after we can do IPO.
269 pm_builder.Inliner = NULL;
Ian Rogers4c1c2832013-03-04 18:30:13 -0800270 //pm_builder.Inliner = ::llvm::createFunctionInliningPass();
271 //pm_builder.Inliner = ::llvm::createAlwaysInlinerPass();
272 //pm_builder.Inliner = ::llvm::createPartialInliningPass();
Shih-wei Liao415576b2012-04-23 15:28:53 -0700273 pm_builder.OptLevel = 3;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700274 pm_builder.DisableSimplifyLibCalls = 1;
TDYa127e4c2ccc2012-05-13 21:10:36 -0700275 pm_builder.DisableUnitAtATime = 1;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700276 pm_builder.populateFunctionPassManager(fpm);
TDYa127ce9c3172012-05-15 06:09:27 -0700277 pm_builder.populateModulePassManager(pm);
Ian Rogers4c1c2832013-03-04 18:30:13 -0800278 pm.add(::llvm::createStripDeadPrototypesPass());
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700279
280 // Add passes to emit ELF image
281 {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800282 ::llvm::formatted_raw_ostream formatted_os(out_stream, false);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700283
284 // Ask the target to add backend passes as necessary.
285 if (target_machine->addPassesToEmitFile(pm,
286 formatted_os,
Ian Rogers4c1c2832013-03-04 18:30:13 -0800287 ::llvm::TargetMachine::CGFT_ObjectFile,
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700288 true)) {
289 LOG(FATAL) << "Unable to generate ELF for this target";
290 return false;
291 }
292
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700293 // Run the per-function optimization
294 fpm.doInitialization();
Ian Rogers4c1c2832013-03-04 18:30:13 -0800295 for (::llvm::Module::iterator F = module_->begin(), E = module_->end();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700296 F != E; ++F) {
297 fpm.run(*F);
298 }
299 fpm.doFinalization();
300
301 // Run the code generation passes
Logan Chien799ef4f2012-04-23 00:17:47 +0800302 pm.run(*module_);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700303 }
304
305 return true;
306}
Logan Chien110bcba2012-04-16 19:11:28 +0800307
Logan Chien971bf3f2012-05-01 15:47:55 +0800308// Check whether the align is less than or equal to the code alignment of
309// that architecture. Since the Oat writer only guarantee that the compiled
310// method being aligned to kArchAlignment, we have no way to align the ELf
311// section if the section alignment is greater than kArchAlignment.
Brian Carlstrom641ce032013-01-31 15:21:37 -0800312void LlvmCompilationUnit::CheckCodeAlign(uint32_t align) const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800313 InstructionSet insn_set = GetInstructionSet();
314 switch (insn_set) {
315 case kThumb2:
316 case kArm:
317 CHECK_LE(align, static_cast<uint32_t>(kArmAlignment));
318 break;
319
320 case kX86:
321 CHECK_LE(align, static_cast<uint32_t>(kX86Alignment));
322 break;
323
324 case kMips:
325 CHECK_LE(align, static_cast<uint32_t>(kMipsAlignment));
326 break;
327
328 default:
329 LOG(FATAL) << "Unknown instruction set: " << insn_set;
330 }
331}
332
333
Ian Rogers4c1c2832013-03-04 18:30:13 -0800334} // namespace llvm
Logan Chien8b977d32012-02-21 19:14:55 +0800335} // namespace art