blob: 592059e6a6d223803d9fd3f9b947d535929f6948 [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 Carlstrom37d48792013-03-22 14:14:45 -070017// TODO: TargetLibraryInfo is included before sys/... because on Android bionic does #define tricks like:
18//
19// #define stat64 stat
20// #define fstat64 fstat
21// #define lstat64 lstat
22//
23// which causes grief. bionic probably should not do that.
24#include <llvm/Target/TargetLibraryInfo.h>
25
Brian Carlstrom641ce032013-01-31 15:21:37 -080026#include "llvm_compilation_unit.h"
Logan Chien8b977d32012-02-21 19:14:55 +080027
Brian Carlstrom265091e2013-01-30 14:08:26 -080028#include <sys/types.h>
29#include <sys/wait.h>
30#include <unistd.h>
Logan Chien8b977d32012-02-21 19:14:55 +080031
Brian Carlstrom265091e2013-01-30 14:08:26 -080032#include <string>
TDYa127d668a062012-04-13 12:36:57 -070033
Logan Chien8b977d32012-02-21 19:14:55 +080034#include <llvm/ADT/OwningPtr.h>
35#include <llvm/ADT/StringSet.h>
36#include <llvm/ADT/Triple.h>
37#include <llvm/Analysis/CallGraph.h>
Brian Carlstrom37d48792013-03-22 14:14:45 -070038#include <llvm/Analysis/CallGraphSCCPass.h>
TDYa127f15b0ab2012-05-11 21:01:36 -070039#include <llvm/Analysis/Dominators.h>
40#include <llvm/Analysis/LoopInfo.h>
Logan Chien8b977d32012-02-21 19:14:55 +080041#include <llvm/Analysis/LoopPass.h>
42#include <llvm/Analysis/RegionPass.h>
TDYa127f15b0ab2012-05-11 21:01:36 -070043#include <llvm/Analysis/ScalarEvolution.h>
Logan Chien8b977d32012-02-21 19:14:55 +080044#include <llvm/Analysis/Verifier.h>
45#include <llvm/Assembly/PrintModulePass.h>
46#include <llvm/Bitcode/ReaderWriter.h>
Logan Chien110bcba2012-04-16 19:11:28 +080047#include <llvm/CodeGen/MachineFrameInfo.h>
48#include <llvm/CodeGen/MachineFunction.h>
49#include <llvm/CodeGen/MachineFunctionPass.h>
Brian Carlstrombd86bcc2013-03-10 20:26:16 -070050#include <llvm/DebugInfo.h>
Brian Carlstrom37d48792013-03-22 14:14:45 -070051#include <llvm/IR/DataLayout.h>
52#include <llvm/IR/DerivedTypes.h>
53#include <llvm/IR/LLVMContext.h>
54#include <llvm/IR/Module.h>
Logan Chien971bf3f2012-05-01 15:47:55 +080055#include <llvm/Object/ObjectFile.h>
Logan Chien8b977d32012-02-21 19:14:55 +080056#include <llvm/PassManager.h>
57#include <llvm/Support/Debug.h>
Logan Chien971bf3f2012-05-01 15:47:55 +080058#include <llvm/Support/ELF.h>
Logan Chien8b977d32012-02-21 19:14:55 +080059#include <llvm/Support/FormattedStream.h>
60#include <llvm/Support/ManagedStatic.h>
Shih-wei Liaod7726e42012-04-20 15:23:36 -070061#include <llvm/Support/MemoryBuffer.h>
Logan Chien8b977d32012-02-21 19:14:55 +080062#include <llvm/Support/PassNameParser.h>
63#include <llvm/Support/PluginLoader.h>
64#include <llvm/Support/PrettyStackTrace.h>
65#include <llvm/Support/Signals.h>
66#include <llvm/Support/SystemUtils.h>
67#include <llvm/Support/TargetRegistry.h>
68#include <llvm/Support/TargetSelect.h>
69#include <llvm/Support/ToolOutputFile.h>
70#include <llvm/Support/raw_ostream.h>
Shih-wei Liaod7726e42012-04-20 15:23:36 -070071#include <llvm/Support/system_error.h>
Logan Chien8b977d32012-02-21 19:14:55 +080072#include <llvm/Target/TargetMachine.h>
Shih-wei Liaof1cb9a52012-04-20 01:49:18 -070073#include <llvm/Transforms/IPO.h>
Logan Chien8b977d32012-02-21 19:14:55 +080074#include <llvm/Transforms/IPO/PassManagerBuilder.h>
TDYa127f15b0ab2012-05-11 21:01:36 -070075#include <llvm/Transforms/Scalar.h>
Logan Chien8b977d32012-02-21 19:14:55 +080076
Brian Carlstrom265091e2013-01-30 14:08:26 -080077#include "base/logging.h"
78#include "base/unix_file/fd_file.h"
79#include "compiled_method.h"
80#include "compiler_llvm.h"
81#include "instruction_set.h"
82#include "ir_builder.h"
83#include "os.h"
84#include "runtime_support_builder_arm.h"
85#include "runtime_support_builder_thumb2.h"
86#include "runtime_support_builder_x86.h"
87#include "utils_llvm.h"
Logan Chien8b977d32012-02-21 19:14:55 +080088
89namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -080090namespace llvm {
Logan Chien8b977d32012-02-21 19:14:55 +080091
Ian Rogers4c1c2832013-03-04 18:30:13 -080092::llvm::FunctionPass*
Ian Rogers76ae4fe2013-02-27 16:03:41 -080093CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Brian Carlstrom265091e2013-01-30 14:08:26 -080094 CompilerDriver* compiler, const DexCompilationUnit* dex_compilation_unit);
Shih-wei Liao21d28f52012-06-12 05:55:00 -070095
Ian Rogers4c1c2832013-03-04 18:30:13 -080096::llvm::Module* makeLLVMModuleContents(::llvm::Module* module);
Logan Chien8b977d32012-02-21 19:14:55 +080097
98
Brian Carlstrom265091e2013-01-30 14:08:26 -080099LlvmCompilationUnit::LlvmCompilationUnit(const CompilerLLVM* compiler_llvm, size_t cunit_id)
100 : compiler_llvm_(compiler_llvm), cunit_id_(cunit_id) {
Ian Rogers1212a022013-03-04 10:48:41 -0800101 driver_ = NULL;
Ian Rogers89756f22013-03-04 16:40:02 -0800102 dex_compilation_unit_ = NULL;
buzbee4df2bbd2012-10-11 14:46:06 -0700103 llvm_info_.reset(new LLVMInfo());
104 context_.reset(llvm_info_->GetLLVMContext());
105 module_ = llvm_info_->GetLLVMModule();
TDYa12755e5e6c2012-09-11 15:14:42 -0700106
107 // Include the runtime function declaration
108 makeLLVMModuleContents(module_);
109
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800110
111 intrinsic_helper_.reset(new IntrinsicHelper(*context_, *module_));
112
Logan Chien8b977d32012-02-21 19:14:55 +0800113 // Create IRBuilder
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800114 irb_.reset(new IRBuilder(*context_, *module_, *intrinsic_helper_));
TDYa127d668a062012-04-13 12:36:57 -0700115
116 // We always need a switch case, so just use a normal function.
Brian Carlstromdf629502013-07-17 22:39:56 -0700117 switch (GetInstructionSet()) {
TDYa127b08ed122012-06-05 23:51:19 -0700118 default:
119 runtime_support_.reset(new RuntimeSupportBuilder(*context_, *module_, *irb_));
120 break;
TDYa127d668a062012-04-13 12:36:57 -0700121 case kArm:
TDYa127d668a062012-04-13 12:36:57 -0700122 runtime_support_.reset(new RuntimeSupportBuilderARM(*context_, *module_, *irb_));
123 break;
TDYa127b08ed122012-06-05 23:51:19 -0700124 case kThumb2:
125 runtime_support_.reset(new RuntimeSupportBuilderThumb2(*context_, *module_, *irb_));
126 break;
TDYa127d668a062012-04-13 12:36:57 -0700127 case kX86:
128 runtime_support_.reset(new RuntimeSupportBuilderX86(*context_, *module_, *irb_));
129 break;
130 }
131
TDYa127d668a062012-04-13 12:36:57 -0700132 irb_->SetRuntimeSupport(runtime_support_.get());
Logan Chien8b977d32012-02-21 19:14:55 +0800133}
134
135
Brian Carlstrom641ce032013-01-31 15:21:37 -0800136LlvmCompilationUnit::~LlvmCompilationUnit() {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800137 ::llvm::LLVMContext* llvm_context = context_.release(); // Managed by llvm_info_
TDYa12755e5e6c2012-09-11 15:14:42 -0700138 CHECK(llvm_context != NULL);
Logan Chien8b977d32012-02-21 19:14:55 +0800139}
140
141
Brian Carlstrom641ce032013-01-31 15:21:37 -0800142InstructionSet LlvmCompilationUnit::GetInstructionSet() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800143 return compiler_llvm_->GetInstructionSet();
Logan Chien8b977d32012-02-21 19:14:55 +0800144}
145
146
Brian Carlstrom35599732013-03-13 15:15:23 -0700147static std::string DumpDirectory() {
148 if (kIsTargetBuild) {
Brian Carlstrom7675e162013-06-10 16:18:04 -0700149 return GetDalvikCacheOrDie(GetAndroidData());
Brian Carlstrom35599732013-03-13 15:15:23 -0700150 }
151 return "/tmp";
152}
153
154void LlvmCompilationUnit::DumpBitcodeToFile() {
155 std::string bitcode;
156 DumpBitcodeToString(bitcode);
157 std::string filename(StringPrintf("%s/Art%u.bc", DumpDirectory().c_str(), cunit_id_));
158 UniquePtr<File> output(OS::OpenFile(filename.c_str(), true));
159 output->WriteFully(bitcode.data(), bitcode.size());
160 LOG(INFO) << ".bc file written successfully: " << filename;
161}
162
163void LlvmCompilationUnit::DumpBitcodeToString(std::string& str_buffer) {
164 ::llvm::raw_string_ostream str_os(str_buffer);
165 ::llvm::WriteBitcodeToFile(module_, str_os);
166}
167
Brian Carlstrom641ce032013-01-31 15:21:37 -0800168bool LlvmCompilationUnit::Materialize() {
Brian Carlstrom35599732013-03-13 15:15:23 -0700169 const bool kDumpBitcode = false;
170 if (kDumpBitcode) {
171 // Dump the bitcode for debugging
172 DumpBitcodeToFile();
173 }
174
Ian Rogers4c1c2832013-03-04 18:30:13 -0800175 // Compile and prelink ::llvm::Module
Brian Carlstrom265091e2013-01-30 14:08:26 -0800176 if (!MaterializeToString(elf_object_)) {
177 LOG(ERROR) << "Failed to materialize compilation unit " << cunit_id_;
Logan Chien971bf3f2012-05-01 15:47:55 +0800178 return false;
Logan Chien110bcba2012-04-16 19:11:28 +0800179 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800180
Brian Carlstrom35599732013-03-13 15:15:23 -0700181 const bool kDumpELF = false;
182 if (kDumpELF) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800183 // Dump the ELF image for debugging
Brian Carlstrom35599732013-03-13 15:15:23 -0700184 std::string filename(StringPrintf("%s/Art%u.o", DumpDirectory().c_str(), cunit_id_));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800185 UniquePtr<File> output(OS::OpenFile(filename.c_str(), true));
186 output->WriteFully(elf_object_.data(), elf_object_.size());
187 LOG(INFO) << ".o file written successfully: " << filename;
Logan Chien971bf3f2012-05-01 15:47:55 +0800188 }
189
Logan Chien971bf3f2012-05-01 15:47:55 +0800190 return true;
Logan Chien110bcba2012-04-16 19:11:28 +0800191}
192
Logan Chien971bf3f2012-05-01 15:47:55 +0800193
Brian Carlstrom641ce032013-01-31 15:21:37 -0800194bool LlvmCompilationUnit::MaterializeToString(std::string& str_buffer) {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800195 ::llvm::raw_string_ostream str_os(str_buffer);
Logan Chien971bf3f2012-05-01 15:47:55 +0800196 return MaterializeToRawOStream(str_os);
197}
198
199
Ian Rogers4c1c2832013-03-04 18:30:13 -0800200bool LlvmCompilationUnit::MaterializeToRawOStream(::llvm::raw_ostream& out_stream) {
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700201 // Lookup the LLVM target
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800202 std::string target_triple;
203 std::string target_cpu;
204 std::string target_attr;
Ian Rogers1212a022013-03-04 10:48:41 -0800205 CompilerDriver::InstructionSetToLLVMTarget(GetInstructionSet(), target_triple, target_cpu, target_attr);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700206
207 std::string errmsg;
Ian Rogers4c1c2832013-03-04 18:30:13 -0800208 const ::llvm::Target* target =
209 ::llvm::TargetRegistry::lookupTarget(target_triple, errmsg);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700210
211 CHECK(target != NULL) << errmsg;
212
213 // Target options
Ian Rogers4c1c2832013-03-04 18:30:13 -0800214 ::llvm::TargetOptions target_options;
215 target_options.FloatABIType = ::llvm::FloatABI::Soft;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700216 target_options.NoFramePointerElim = true;
217 target_options.NoFramePointerElimNonLeaf = true;
218 target_options.UseSoftFloat = false;
TDYa1273978da52012-05-19 07:45:39 -0700219 target_options.EnableFastISel = false;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700220
Ian Rogers4c1c2832013-03-04 18:30:13 -0800221 // Create the ::llvm::TargetMachine
222 ::llvm::OwningPtr< ::llvm::TargetMachine> target_machine(
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700223 target->createTargetMachine(target_triple, target_cpu, target_attr, target_options,
Ian Rogers4c1c2832013-03-04 18:30:13 -0800224 ::llvm::Reloc::Static, ::llvm::CodeModel::Small,
225 ::llvm::CodeGenOpt::Aggressive));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700226
Logan Chienb6bed0b2012-05-04 15:03:56 +0800227 CHECK(target_machine.get() != NULL) << "Failed to create target machine";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700228
229 // Add target data
Brian Carlstrom37d48792013-03-22 14:14:45 -0700230 const ::llvm::DataLayout* data_layout = target_machine->getDataLayout();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700231
232 // PassManager for code generation passes
Ian Rogers4c1c2832013-03-04 18:30:13 -0800233 ::llvm::PassManager pm;
Brian Carlstrom37d48792013-03-22 14:14:45 -0700234 pm.add(new ::llvm::DataLayout(*data_layout));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700235
236 // FunctionPassManager for optimization pass
Ian Rogers4c1c2832013-03-04 18:30:13 -0800237 ::llvm::FunctionPassManager fpm(module_);
Brian Carlstrom37d48792013-03-22 14:14:45 -0700238 fpm.add(new ::llvm::DataLayout(*data_layout));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700239
TDYa127f15b0ab2012-05-11 21:01:36 -0700240 if (bitcode_filename_.empty()) {
241 // If we don't need write the bitcode to file, add the AddSuspendCheckToLoopLatchPass to the
242 // regular FunctionPass.
buzbee4df2bbd2012-10-11 14:46:06 -0700243 fpm.add(CreateGBCExpanderPass(*llvm_info_->GetIntrinsicHelper(), *irb_.get(),
Ian Rogers89756f22013-03-04 16:40:02 -0800244 driver_, dex_compilation_unit_));
TDYa127f15b0ab2012-05-11 21:01:36 -0700245 } else {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800246 ::llvm::FunctionPassManager fpm2(module_);
buzbee4df2bbd2012-10-11 14:46:06 -0700247 fpm2.add(CreateGBCExpanderPass(*llvm_info_->GetIntrinsicHelper(), *irb_.get(),
Ian Rogers89756f22013-03-04 16:40:02 -0800248 driver_, dex_compilation_unit_));
TDYa127f15b0ab2012-05-11 21:01:36 -0700249 fpm2.doInitialization();
Ian Rogers4c1c2832013-03-04 18:30:13 -0800250 for (::llvm::Module::iterator F = module_->begin(), E = module_->end();
TDYa127f15b0ab2012-05-11 21:01:36 -0700251 F != E; ++F) {
252 fpm2.run(*F);
253 }
254 fpm2.doFinalization();
TDYa127f15b0ab2012-05-11 21:01:36 -0700255
256 // Write bitcode to file
257 std::string errmsg;
258
Ian Rogers4c1c2832013-03-04 18:30:13 -0800259 ::llvm::OwningPtr< ::llvm::tool_output_file> out_file(
260 new ::llvm::tool_output_file(bitcode_filename_.c_str(), errmsg,
261 ::llvm::raw_fd_ostream::F_Binary));
TDYa127f15b0ab2012-05-11 21:01:36 -0700262
263
264 if (!errmsg.empty()) {
265 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
266 return false;
267 }
268
Ian Rogers4c1c2832013-03-04 18:30:13 -0800269 ::llvm::WriteBitcodeToFile(module_, out_file->os());
TDYa127f15b0ab2012-05-11 21:01:36 -0700270 out_file->keep();
271 }
272
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700273 // Add optimization pass
Ian Rogers4c1c2832013-03-04 18:30:13 -0800274 ::llvm::PassManagerBuilder pm_builder;
TDYa1279a129452012-07-19 03:10:08 -0700275 // TODO: Use inliner after we can do IPO.
276 pm_builder.Inliner = NULL;
Ian Rogers4c1c2832013-03-04 18:30:13 -0800277 //pm_builder.Inliner = ::llvm::createFunctionInliningPass();
278 //pm_builder.Inliner = ::llvm::createAlwaysInlinerPass();
279 //pm_builder.Inliner = ::llvm::createPartialInliningPass();
Shih-wei Liao415576b2012-04-23 15:28:53 -0700280 pm_builder.OptLevel = 3;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700281 pm_builder.DisableSimplifyLibCalls = 1;
TDYa127e4c2ccc2012-05-13 21:10:36 -0700282 pm_builder.DisableUnitAtATime = 1;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700283 pm_builder.populateFunctionPassManager(fpm);
TDYa127ce9c3172012-05-15 06:09:27 -0700284 pm_builder.populateModulePassManager(pm);
Ian Rogers4c1c2832013-03-04 18:30:13 -0800285 pm.add(::llvm::createStripDeadPrototypesPass());
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700286
287 // Add passes to emit ELF image
288 {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800289 ::llvm::formatted_raw_ostream formatted_os(out_stream, false);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700290
291 // Ask the target to add backend passes as necessary.
292 if (target_machine->addPassesToEmitFile(pm,
293 formatted_os,
Ian Rogers4c1c2832013-03-04 18:30:13 -0800294 ::llvm::TargetMachine::CGFT_ObjectFile,
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700295 true)) {
296 LOG(FATAL) << "Unable to generate ELF for this target";
297 return false;
298 }
299
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700300 // Run the per-function optimization
301 fpm.doInitialization();
Ian Rogers4c1c2832013-03-04 18:30:13 -0800302 for (::llvm::Module::iterator F = module_->begin(), E = module_->end();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700303 F != E; ++F) {
304 fpm.run(*F);
305 }
306 fpm.doFinalization();
307
308 // Run the code generation passes
Logan Chien799ef4f2012-04-23 00:17:47 +0800309 pm.run(*module_);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700310 }
311
312 return true;
313}
Logan Chien110bcba2012-04-16 19:11:28 +0800314
Logan Chien971bf3f2012-05-01 15:47:55 +0800315// Check whether the align is less than or equal to the code alignment of
316// that architecture. Since the Oat writer only guarantee that the compiled
317// method being aligned to kArchAlignment, we have no way to align the ELf
318// section if the section alignment is greater than kArchAlignment.
Brian Carlstrom641ce032013-01-31 15:21:37 -0800319void LlvmCompilationUnit::CheckCodeAlign(uint32_t align) const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800320 InstructionSet insn_set = GetInstructionSet();
321 switch (insn_set) {
322 case kThumb2:
323 case kArm:
324 CHECK_LE(align, static_cast<uint32_t>(kArmAlignment));
325 break;
326
327 case kX86:
328 CHECK_LE(align, static_cast<uint32_t>(kX86Alignment));
329 break;
330
331 case kMips:
332 CHECK_LE(align, static_cast<uint32_t>(kMipsAlignment));
333 break;
334
335 default:
336 LOG(FATAL) << "Unknown instruction set: " << insn_set;
337 }
338}
339
340
Ian Rogers4c1c2832013-03-04 18:30:13 -0800341} // namespace llvm
Logan Chien8b977d32012-02-21 19:14:55 +0800342} // namespace art