blob: 8a674c0a33304acf94e34160273e012e033b073b [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.
Logan Chien971bf3f2012-05-01 15:47:55 +0800117 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) {
149 return GetArtCacheOrDie(GetAndroidData());
150 }
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
170 const bool kDumpBitcode = false;
171 if (kDumpBitcode) {
172 // Dump the bitcode for debugging
173 DumpBitcodeToFile();
174 }
175
Ian Rogers4c1c2832013-03-04 18:30:13 -0800176 // Compile and prelink ::llvm::Module
Brian Carlstrom265091e2013-01-30 14:08:26 -0800177 if (!MaterializeToString(elf_object_)) {
178 LOG(ERROR) << "Failed to materialize compilation unit " << cunit_id_;
Logan Chien971bf3f2012-05-01 15:47:55 +0800179 return false;
Logan Chien110bcba2012-04-16 19:11:28 +0800180 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800181
Brian Carlstrom35599732013-03-13 15:15:23 -0700182 const bool kDumpELF = false;
183 if (kDumpELF) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800184 // Dump the ELF image for debugging
Brian Carlstrom35599732013-03-13 15:15:23 -0700185 std::string filename(StringPrintf("%s/Art%u.o", DumpDirectory().c_str(), cunit_id_));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800186 UniquePtr<File> output(OS::OpenFile(filename.c_str(), true));
187 output->WriteFully(elf_object_.data(), elf_object_.size());
188 LOG(INFO) << ".o file written successfully: " << filename;
Logan Chien971bf3f2012-05-01 15:47:55 +0800189 }
190
Logan Chien971bf3f2012-05-01 15:47:55 +0800191 return true;
Logan Chien110bcba2012-04-16 19:11:28 +0800192}
193
Logan Chien971bf3f2012-05-01 15:47:55 +0800194
Brian Carlstrom641ce032013-01-31 15:21:37 -0800195bool LlvmCompilationUnit::MaterializeToString(std::string& str_buffer) {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800196 ::llvm::raw_string_ostream str_os(str_buffer);
Logan Chien971bf3f2012-05-01 15:47:55 +0800197 return MaterializeToRawOStream(str_os);
198}
199
200
Ian Rogers4c1c2832013-03-04 18:30:13 -0800201bool LlvmCompilationUnit::MaterializeToRawOStream(::llvm::raw_ostream& out_stream) {
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700202 // Lookup the LLVM target
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800203 std::string target_triple;
204 std::string target_cpu;
205 std::string target_attr;
Ian Rogers1212a022013-03-04 10:48:41 -0800206 CompilerDriver::InstructionSetToLLVMTarget(GetInstructionSet(), target_triple, target_cpu, target_attr);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700207
208 std::string errmsg;
Ian Rogers4c1c2832013-03-04 18:30:13 -0800209 const ::llvm::Target* target =
210 ::llvm::TargetRegistry::lookupTarget(target_triple, errmsg);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700211
212 CHECK(target != NULL) << errmsg;
213
214 // Target options
Ian Rogers4c1c2832013-03-04 18:30:13 -0800215 ::llvm::TargetOptions target_options;
216 target_options.FloatABIType = ::llvm::FloatABI::Soft;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700217 target_options.NoFramePointerElim = true;
218 target_options.NoFramePointerElimNonLeaf = true;
219 target_options.UseSoftFloat = false;
TDYa1273978da52012-05-19 07:45:39 -0700220 target_options.EnableFastISel = false;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700221
Ian Rogers4c1c2832013-03-04 18:30:13 -0800222 // Create the ::llvm::TargetMachine
223 ::llvm::OwningPtr< ::llvm::TargetMachine> target_machine(
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700224 target->createTargetMachine(target_triple, target_cpu, target_attr, target_options,
Ian Rogers4c1c2832013-03-04 18:30:13 -0800225 ::llvm::Reloc::Static, ::llvm::CodeModel::Small,
226 ::llvm::CodeGenOpt::Aggressive));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700227
Logan Chienb6bed0b2012-05-04 15:03:56 +0800228 CHECK(target_machine.get() != NULL) << "Failed to create target machine";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700229
230 // Add target data
Brian Carlstrom37d48792013-03-22 14:14:45 -0700231 const ::llvm::DataLayout* data_layout = target_machine->getDataLayout();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700232
233 // PassManager for code generation passes
Ian Rogers4c1c2832013-03-04 18:30:13 -0800234 ::llvm::PassManager pm;
Brian Carlstrom37d48792013-03-22 14:14:45 -0700235 pm.add(new ::llvm::DataLayout(*data_layout));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700236
237 // FunctionPassManager for optimization pass
Ian Rogers4c1c2832013-03-04 18:30:13 -0800238 ::llvm::FunctionPassManager fpm(module_);
Brian Carlstrom37d48792013-03-22 14:14:45 -0700239 fpm.add(new ::llvm::DataLayout(*data_layout));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700240
TDYa127f15b0ab2012-05-11 21:01:36 -0700241 if (bitcode_filename_.empty()) {
242 // If we don't need write the bitcode to file, add the AddSuspendCheckToLoopLatchPass to the
243 // regular FunctionPass.
buzbee4df2bbd2012-10-11 14:46:06 -0700244 fpm.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 } else {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800247 ::llvm::FunctionPassManager fpm2(module_);
buzbee4df2bbd2012-10-11 14:46:06 -0700248 fpm2.add(CreateGBCExpanderPass(*llvm_info_->GetIntrinsicHelper(), *irb_.get(),
Ian Rogers89756f22013-03-04 16:40:02 -0800249 driver_, dex_compilation_unit_));
TDYa127f15b0ab2012-05-11 21:01:36 -0700250 fpm2.doInitialization();
Ian Rogers4c1c2832013-03-04 18:30:13 -0800251 for (::llvm::Module::iterator F = module_->begin(), E = module_->end();
TDYa127f15b0ab2012-05-11 21:01:36 -0700252 F != E; ++F) {
253 fpm2.run(*F);
254 }
255 fpm2.doFinalization();
TDYa127f15b0ab2012-05-11 21:01:36 -0700256
257 // Write bitcode to file
258 std::string errmsg;
259
Ian Rogers4c1c2832013-03-04 18:30:13 -0800260 ::llvm::OwningPtr< ::llvm::tool_output_file> out_file(
261 new ::llvm::tool_output_file(bitcode_filename_.c_str(), errmsg,
262 ::llvm::raw_fd_ostream::F_Binary));
TDYa127f15b0ab2012-05-11 21:01:36 -0700263
264
265 if (!errmsg.empty()) {
266 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
267 return false;
268 }
269
Ian Rogers4c1c2832013-03-04 18:30:13 -0800270 ::llvm::WriteBitcodeToFile(module_, out_file->os());
TDYa127f15b0ab2012-05-11 21:01:36 -0700271 out_file->keep();
272 }
273
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700274 // Add optimization pass
Ian Rogers4c1c2832013-03-04 18:30:13 -0800275 ::llvm::PassManagerBuilder pm_builder;
TDYa1279a129452012-07-19 03:10:08 -0700276 // TODO: Use inliner after we can do IPO.
277 pm_builder.Inliner = NULL;
Ian Rogers4c1c2832013-03-04 18:30:13 -0800278 //pm_builder.Inliner = ::llvm::createFunctionInliningPass();
279 //pm_builder.Inliner = ::llvm::createAlwaysInlinerPass();
280 //pm_builder.Inliner = ::llvm::createPartialInliningPass();
Shih-wei Liao415576b2012-04-23 15:28:53 -0700281 pm_builder.OptLevel = 3;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700282 pm_builder.DisableSimplifyLibCalls = 1;
TDYa127e4c2ccc2012-05-13 21:10:36 -0700283 pm_builder.DisableUnitAtATime = 1;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700284 pm_builder.populateFunctionPassManager(fpm);
TDYa127ce9c3172012-05-15 06:09:27 -0700285 pm_builder.populateModulePassManager(pm);
Ian Rogers4c1c2832013-03-04 18:30:13 -0800286 pm.add(::llvm::createStripDeadPrototypesPass());
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700287
288 // Add passes to emit ELF image
289 {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800290 ::llvm::formatted_raw_ostream formatted_os(out_stream, false);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700291
292 // Ask the target to add backend passes as necessary.
293 if (target_machine->addPassesToEmitFile(pm,
294 formatted_os,
Ian Rogers4c1c2832013-03-04 18:30:13 -0800295 ::llvm::TargetMachine::CGFT_ObjectFile,
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700296 true)) {
297 LOG(FATAL) << "Unable to generate ELF for this target";
298 return false;
299 }
300
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700301 // Run the per-function optimization
302 fpm.doInitialization();
Ian Rogers4c1c2832013-03-04 18:30:13 -0800303 for (::llvm::Module::iterator F = module_->begin(), E = module_->end();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700304 F != E; ++F) {
305 fpm.run(*F);
306 }
307 fpm.doFinalization();
308
309 // Run the code generation passes
Logan Chien799ef4f2012-04-23 00:17:47 +0800310 pm.run(*module_);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700311 }
312
313 return true;
314}
Logan Chien110bcba2012-04-16 19:11:28 +0800315
Logan Chien971bf3f2012-05-01 15:47:55 +0800316// Check whether the align is less than or equal to the code alignment of
317// that architecture. Since the Oat writer only guarantee that the compiled
318// method being aligned to kArchAlignment, we have no way to align the ELf
319// section if the section alignment is greater than kArchAlignment.
Brian Carlstrom641ce032013-01-31 15:21:37 -0800320void LlvmCompilationUnit::CheckCodeAlign(uint32_t align) const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800321 InstructionSet insn_set = GetInstructionSet();
322 switch (insn_set) {
323 case kThumb2:
324 case kArm:
325 CHECK_LE(align, static_cast<uint32_t>(kArmAlignment));
326 break;
327
328 case kX86:
329 CHECK_LE(align, static_cast<uint32_t>(kX86Alignment));
330 break;
331
332 case kMips:
333 CHECK_LE(align, static_cast<uint32_t>(kMipsAlignment));
334 break;
335
336 default:
337 LOG(FATAL) << "Unknown instruction set: " << insn_set;
338 }
339}
340
341
Ian Rogers4c1c2832013-03-04 18:30:13 -0800342} // namespace llvm
Logan Chien8b977d32012-02-21 19:14:55 +0800343} // namespace art