blob: 741c2d7748a8d2c982ae6bdd2eeb6585cbae610e [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
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070022//
Brian Carlstrom37d48792013-03-22 14:14:45 -070023// 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"
Brian Carlstrom265091e2013-01-30 14:08:26 -080085#include "runtime_support_builder_x86.h"
86#include "utils_llvm.h"
Logan Chien8b977d32012-02-21 19:14:55 +080087
88namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -080089namespace llvm {
Logan Chien8b977d32012-02-21 19:14:55 +080090
Ian Rogers4c1c2832013-03-04 18:30:13 -080091::llvm::FunctionPass*
Ian Rogers76ae4fe2013-02-27 16:03:41 -080092CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Brian Carlstrom265091e2013-01-30 14:08:26 -080093 CompilerDriver* compiler, const DexCompilationUnit* dex_compilation_unit);
Shih-wei Liao21d28f52012-06-12 05:55:00 -070094
Ian Rogers4c1c2832013-03-04 18:30:13 -080095::llvm::Module* makeLLVMModuleContents(::llvm::Module* module);
Logan Chien8b977d32012-02-21 19:14:55 +080096
97
Brian Carlstrom265091e2013-01-30 14:08:26 -080098LlvmCompilationUnit::LlvmCompilationUnit(const CompilerLLVM* compiler_llvm, size_t cunit_id)
99 : compiler_llvm_(compiler_llvm), cunit_id_(cunit_id) {
Ian Rogers1212a022013-03-04 10:48:41 -0800100 driver_ = NULL;
Ian Rogers89756f22013-03-04 16:40:02 -0800101 dex_compilation_unit_ = NULL;
buzbee4df2bbd2012-10-11 14:46:06 -0700102 llvm_info_.reset(new LLVMInfo());
103 context_.reset(llvm_info_->GetLLVMContext());
104 module_ = llvm_info_->GetLLVMModule();
TDYa12755e5e6c2012-09-11 15:14:42 -0700105
106 // Include the runtime function declaration
107 makeLLVMModuleContents(module_);
108
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800109
110 intrinsic_helper_.reset(new IntrinsicHelper(*context_, *module_));
111
Logan Chien8b977d32012-02-21 19:14:55 +0800112 // Create IRBuilder
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800113 irb_.reset(new IRBuilder(*context_, *module_, *intrinsic_helper_));
TDYa127d668a062012-04-13 12:36:57 -0700114
115 // We always need a switch case, so just use a normal function.
Brian Carlstromdf629502013-07-17 22:39:56 -0700116 switch (GetInstructionSet()) {
TDYa127b08ed122012-06-05 23:51:19 -0700117 default:
118 runtime_support_.reset(new RuntimeSupportBuilder(*context_, *module_, *irb_));
119 break;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700120 case kThumb2:
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;
124 case kX86:
125 runtime_support_.reset(new RuntimeSupportBuilderX86(*context_, *module_, *irb_));
126 break;
127 }
128
TDYa127d668a062012-04-13 12:36:57 -0700129 irb_->SetRuntimeSupport(runtime_support_.get());
Logan Chien8b977d32012-02-21 19:14:55 +0800130}
131
132
Brian Carlstrom641ce032013-01-31 15:21:37 -0800133LlvmCompilationUnit::~LlvmCompilationUnit() {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700134 ::llvm::LLVMContext* llvm_context = context_.release(); // Managed by llvm_info_
TDYa12755e5e6c2012-09-11 15:14:42 -0700135 CHECK(llvm_context != NULL);
Logan Chien8b977d32012-02-21 19:14:55 +0800136}
137
138
Brian Carlstrom641ce032013-01-31 15:21:37 -0800139InstructionSet LlvmCompilationUnit::GetInstructionSet() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800140 return compiler_llvm_->GetInstructionSet();
Logan Chien8b977d32012-02-21 19:14:55 +0800141}
142
143
Brian Carlstrom35599732013-03-13 15:15:23 -0700144static std::string DumpDirectory() {
145 if (kIsTargetBuild) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100146 return GetDalvikCacheOrDie("llvm-dump");
Brian Carlstrom35599732013-03-13 15:15:23 -0700147 }
148 return "/tmp";
149}
150
151void LlvmCompilationUnit::DumpBitcodeToFile() {
152 std::string bitcode;
153 DumpBitcodeToString(bitcode);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800154 std::string filename(StringPrintf("%s/Art%zu.bc", DumpDirectory().c_str(), cunit_id_));
Ian Rogers700a4022014-05-19 16:49:03 -0700155 std::unique_ptr<File> output(OS::CreateEmptyFile(filename.c_str()));
Brian Carlstrom35599732013-03-13 15:15:23 -0700156 output->WriteFully(bitcode.data(), bitcode.size());
157 LOG(INFO) << ".bc file written successfully: " << filename;
158}
159
160void LlvmCompilationUnit::DumpBitcodeToString(std::string& str_buffer) {
161 ::llvm::raw_string_ostream str_os(str_buffer);
162 ::llvm::WriteBitcodeToFile(module_, str_os);
163}
164
Brian Carlstrom641ce032013-01-31 15:21:37 -0800165bool LlvmCompilationUnit::Materialize() {
Brian Carlstrom35599732013-03-13 15:15:23 -0700166 const bool kDumpBitcode = false;
167 if (kDumpBitcode) {
168 // Dump the bitcode for debugging
169 DumpBitcodeToFile();
170 }
171
Ian Rogers4c1c2832013-03-04 18:30:13 -0800172 // Compile and prelink ::llvm::Module
Brian Carlstrom265091e2013-01-30 14:08:26 -0800173 if (!MaterializeToString(elf_object_)) {
174 LOG(ERROR) << "Failed to materialize compilation unit " << cunit_id_;
Logan Chien971bf3f2012-05-01 15:47:55 +0800175 return false;
Logan Chien110bcba2012-04-16 19:11:28 +0800176 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800177
Brian Carlstrom35599732013-03-13 15:15:23 -0700178 const bool kDumpELF = false;
179 if (kDumpELF) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800180 // Dump the ELF image for debugging
Ian Rogersef7d42f2014-01-06 12:55:46 -0800181 std::string filename(StringPrintf("%s/Art%zu.o", DumpDirectory().c_str(), cunit_id_));
Ian Rogers700a4022014-05-19 16:49:03 -0700182 std::unique_ptr<File> output(OS::CreateEmptyFile(filename.c_str()));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800183 output->WriteFully(elf_object_.data(), elf_object_.size());
184 LOG(INFO) << ".o file written successfully: " << filename;
Logan Chien971bf3f2012-05-01 15:47:55 +0800185 }
186
Logan Chien971bf3f2012-05-01 15:47:55 +0800187 return true;
Logan Chien110bcba2012-04-16 19:11:28 +0800188}
189
Logan Chien971bf3f2012-05-01 15:47:55 +0800190
Brian Carlstrom641ce032013-01-31 15:21:37 -0800191bool LlvmCompilationUnit::MaterializeToString(std::string& str_buffer) {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800192 ::llvm::raw_string_ostream str_os(str_buffer);
Logan Chien971bf3f2012-05-01 15:47:55 +0800193 return MaterializeToRawOStream(str_os);
194}
195
196
Ian Rogers4c1c2832013-03-04 18:30:13 -0800197bool LlvmCompilationUnit::MaterializeToRawOStream(::llvm::raw_ostream& out_stream) {
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700198 // Lookup the LLVM target
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800199 std::string target_triple;
200 std::string target_cpu;
201 std::string target_attr;
Ian Rogers3d504072014-03-01 09:16:49 -0800202 CompilerDriver::InstructionSetToLLVMTarget(GetInstructionSet(), &target_triple, &target_cpu,
203 &target_attr);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700204
205 std::string errmsg;
Ian Rogers4c1c2832013-03-04 18:30:13 -0800206 const ::llvm::Target* target =
207 ::llvm::TargetRegistry::lookupTarget(target_triple, errmsg);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700208
209 CHECK(target != NULL) << errmsg;
210
211 // Target options
Ian Rogers4c1c2832013-03-04 18:30:13 -0800212 ::llvm::TargetOptions target_options;
213 target_options.FloatABIType = ::llvm::FloatABI::Soft;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700214 target_options.NoFramePointerElim = true;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700215 target_options.UseSoftFloat = false;
TDYa1273978da52012-05-19 07:45:39 -0700216 target_options.EnableFastISel = false;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700217
Ian Rogers4c1c2832013-03-04 18:30:13 -0800218 // Create the ::llvm::TargetMachine
219 ::llvm::OwningPtr< ::llvm::TargetMachine> target_machine(
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700220 target->createTargetMachine(target_triple, target_cpu, target_attr, target_options,
Ian Rogers4c1c2832013-03-04 18:30:13 -0800221 ::llvm::Reloc::Static, ::llvm::CodeModel::Small,
222 ::llvm::CodeGenOpt::Aggressive));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700223
Logan Chienb6bed0b2012-05-04 15:03:56 +0800224 CHECK(target_machine.get() != NULL) << "Failed to create target machine";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700225
226 // Add target data
Brian Carlstrom37d48792013-03-22 14:14:45 -0700227 const ::llvm::DataLayout* data_layout = target_machine->getDataLayout();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700228
229 // PassManager for code generation passes
Ian Rogers4c1c2832013-03-04 18:30:13 -0800230 ::llvm::PassManager pm;
Brian Carlstrom37d48792013-03-22 14:14:45 -0700231 pm.add(new ::llvm::DataLayout(*data_layout));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700232
233 // FunctionPassManager for optimization pass
Ian Rogers4c1c2832013-03-04 18:30:13 -0800234 ::llvm::FunctionPassManager fpm(module_);
Brian Carlstrom37d48792013-03-22 14:14:45 -0700235 fpm.add(new ::llvm::DataLayout(*data_layout));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700236
TDYa127f15b0ab2012-05-11 21:01:36 -0700237 if (bitcode_filename_.empty()) {
238 // If we don't need write the bitcode to file, add the AddSuspendCheckToLoopLatchPass to the
239 // regular FunctionPass.
buzbee4df2bbd2012-10-11 14:46:06 -0700240 fpm.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 } else {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800243 ::llvm::FunctionPassManager fpm2(module_);
buzbee4df2bbd2012-10-11 14:46:06 -0700244 fpm2.add(CreateGBCExpanderPass(*llvm_info_->GetIntrinsicHelper(), *irb_.get(),
Ian Rogers89756f22013-03-04 16:40:02 -0800245 driver_, dex_compilation_unit_));
TDYa127f15b0ab2012-05-11 21:01:36 -0700246 fpm2.doInitialization();
Ian Rogers4c1c2832013-03-04 18:30:13 -0800247 for (::llvm::Module::iterator F = module_->begin(), E = module_->end();
TDYa127f15b0ab2012-05-11 21:01:36 -0700248 F != E; ++F) {
249 fpm2.run(*F);
250 }
251 fpm2.doFinalization();
TDYa127f15b0ab2012-05-11 21:01:36 -0700252
253 // Write bitcode to file
254 std::string errmsg;
255
Ian Rogers4c1c2832013-03-04 18:30:13 -0800256 ::llvm::OwningPtr< ::llvm::tool_output_file> out_file(
257 new ::llvm::tool_output_file(bitcode_filename_.c_str(), errmsg,
Stephen Hines73fbaad2013-08-09 02:14:36 -0700258 ::llvm::sys::fs::F_Binary));
TDYa127f15b0ab2012-05-11 21:01:36 -0700259
260
261 if (!errmsg.empty()) {
262 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
263 return false;
264 }
265
Ian Rogers4c1c2832013-03-04 18:30:13 -0800266 ::llvm::WriteBitcodeToFile(module_, out_file->os());
TDYa127f15b0ab2012-05-11 21:01:36 -0700267 out_file->keep();
268 }
269
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700270 // Add optimization pass
Ian Rogers4c1c2832013-03-04 18:30:13 -0800271 ::llvm::PassManagerBuilder pm_builder;
TDYa1279a129452012-07-19 03:10:08 -0700272 // TODO: Use inliner after we can do IPO.
273 pm_builder.Inliner = NULL;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700274 // pm_builder.Inliner = ::llvm::createFunctionInliningPass();
275 // pm_builder.Inliner = ::llvm::createAlwaysInlinerPass();
276 // pm_builder.Inliner = ::llvm::createPartialInliningPass();
Shih-wei Liao415576b2012-04-23 15:28:53 -0700277 pm_builder.OptLevel = 3;
TDYa127e4c2ccc2012-05-13 21:10:36 -0700278 pm_builder.DisableUnitAtATime = 1;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700279 pm_builder.populateFunctionPassManager(fpm);
TDYa127ce9c3172012-05-15 06:09:27 -0700280 pm_builder.populateModulePassManager(pm);
Ian Rogers4c1c2832013-03-04 18:30:13 -0800281 pm.add(::llvm::createStripDeadPrototypesPass());
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700282
283 // Add passes to emit ELF image
284 {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800285 ::llvm::formatted_raw_ostream formatted_os(out_stream, false);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700286
287 // Ask the target to add backend passes as necessary.
288 if (target_machine->addPassesToEmitFile(pm,
289 formatted_os,
Ian Rogers4c1c2832013-03-04 18:30:13 -0800290 ::llvm::TargetMachine::CGFT_ObjectFile,
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700291 true)) {
292 LOG(FATAL) << "Unable to generate ELF for this target";
293 return false;
294 }
295
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700296 // Run the per-function optimization
297 fpm.doInitialization();
Ian Rogers4c1c2832013-03-04 18:30:13 -0800298 for (::llvm::Module::iterator F = module_->begin(), E = module_->end();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700299 F != E; ++F) {
300 fpm.run(*F);
301 }
302 fpm.doFinalization();
303
304 // Run the code generation passes
Logan Chien799ef4f2012-04-23 00:17:47 +0800305 pm.run(*module_);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700306 }
307
308 return true;
309}
Logan Chien110bcba2012-04-16 19:11:28 +0800310
Logan Chien971bf3f2012-05-01 15:47:55 +0800311// Check whether the align is less than or equal to the code alignment of
312// that architecture. Since the Oat writer only guarantee that the compiled
313// method being aligned to kArchAlignment, we have no way to align the ELf
314// section if the section alignment is greater than kArchAlignment.
Brian Carlstrom641ce032013-01-31 15:21:37 -0800315void LlvmCompilationUnit::CheckCodeAlign(uint32_t align) const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800316 InstructionSet insn_set = GetInstructionSet();
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700317 size_t insn_set_align = GetInstructionSetAlignment(insn_set);
318 CHECK_LE(align, static_cast<uint32_t>(insn_set_align));
Logan Chien971bf3f2012-05-01 15:47:55 +0800319}
320
321
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700322} // namespace llvm
323} // namespace art