blob: 41c603f349cc504e3f568cb44b6755c1612e0728 [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 Carlstrom641ce032013-01-31 15:21:37 -0800139bool LlvmCompilationUnit::Materialize() {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800140 // Compile and prelink ::llvm::Module
Brian Carlstrom265091e2013-01-30 14:08:26 -0800141 if (!MaterializeToString(elf_object_)) {
142 LOG(ERROR) << "Failed to materialize compilation unit " << cunit_id_;
Logan Chien971bf3f2012-05-01 15:47:55 +0800143 return false;
Logan Chien110bcba2012-04-16 19:11:28 +0800144 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800145
Brian Carlstrom265091e2013-01-30 14:08:26 -0800146 if (false) {
147 // Dump the ELF image for debugging
148 std::string directory;
149 if (kIsTargetBuild) {
150 directory += GetArtCacheOrDie(GetAndroidData());
151 } else {
152 directory += "/tmp";
153 }
154 std::string filename(StringPrintf("%s/Art%u.o", directory.c_str(), cunit_id_));
155 UniquePtr<File> output(OS::OpenFile(filename.c_str(), true));
156 output->WriteFully(elf_object_.data(), elf_object_.size());
157 LOG(INFO) << ".o file written successfully: " << filename;
Logan Chien971bf3f2012-05-01 15:47:55 +0800158 }
159
Logan Chien971bf3f2012-05-01 15:47:55 +0800160 return true;
Logan Chien110bcba2012-04-16 19:11:28 +0800161}
162
Logan Chien971bf3f2012-05-01 15:47:55 +0800163
Brian Carlstrom641ce032013-01-31 15:21:37 -0800164bool LlvmCompilationUnit::MaterializeToString(std::string& str_buffer) {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800165 ::llvm::raw_string_ostream str_os(str_buffer);
Logan Chien971bf3f2012-05-01 15:47:55 +0800166 return MaterializeToRawOStream(str_os);
167}
168
169
Ian Rogers4c1c2832013-03-04 18:30:13 -0800170bool LlvmCompilationUnit::MaterializeToRawOStream(::llvm::raw_ostream& out_stream) {
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700171 // Lookup the LLVM target
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800172 std::string target_triple;
173 std::string target_cpu;
174 std::string target_attr;
Ian Rogers1212a022013-03-04 10:48:41 -0800175 CompilerDriver::InstructionSetToLLVMTarget(GetInstructionSet(), target_triple, target_cpu, target_attr);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700176
177 std::string errmsg;
Ian Rogers4c1c2832013-03-04 18:30:13 -0800178 const ::llvm::Target* target =
179 ::llvm::TargetRegistry::lookupTarget(target_triple, errmsg);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700180
181 CHECK(target != NULL) << errmsg;
182
183 // Target options
Ian Rogers4c1c2832013-03-04 18:30:13 -0800184 ::llvm::TargetOptions target_options;
185 target_options.FloatABIType = ::llvm::FloatABI::Soft;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700186 target_options.NoFramePointerElim = true;
187 target_options.NoFramePointerElimNonLeaf = true;
188 target_options.UseSoftFloat = false;
TDYa1273978da52012-05-19 07:45:39 -0700189 target_options.EnableFastISel = false;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700190
Ian Rogers4c1c2832013-03-04 18:30:13 -0800191 // Create the ::llvm::TargetMachine
192 ::llvm::OwningPtr< ::llvm::TargetMachine> target_machine(
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700193 target->createTargetMachine(target_triple, target_cpu, target_attr, target_options,
Ian Rogers4c1c2832013-03-04 18:30:13 -0800194 ::llvm::Reloc::Static, ::llvm::CodeModel::Small,
195 ::llvm::CodeGenOpt::Aggressive));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700196
Logan Chienb6bed0b2012-05-04 15:03:56 +0800197 CHECK(target_machine.get() != NULL) << "Failed to create target machine";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700198
199 // Add target data
Ian Rogers4c1c2832013-03-04 18:30:13 -0800200 const ::llvm::TargetData* target_data = target_machine->getTargetData();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700201
202 // PassManager for code generation passes
Ian Rogers4c1c2832013-03-04 18:30:13 -0800203 ::llvm::PassManager pm;
204 pm.add(new ::llvm::TargetData(*target_data));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700205
206 // FunctionPassManager for optimization pass
Ian Rogers4c1c2832013-03-04 18:30:13 -0800207 ::llvm::FunctionPassManager fpm(module_);
208 fpm.add(new ::llvm::TargetData(*target_data));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700209
TDYa127f15b0ab2012-05-11 21:01:36 -0700210 if (bitcode_filename_.empty()) {
211 // If we don't need write the bitcode to file, add the AddSuspendCheckToLoopLatchPass to the
212 // regular FunctionPass.
buzbee4df2bbd2012-10-11 14:46:06 -0700213 fpm.add(CreateGBCExpanderPass(*llvm_info_->GetIntrinsicHelper(), *irb_.get(),
Ian Rogers89756f22013-03-04 16:40:02 -0800214 driver_, dex_compilation_unit_));
TDYa127f15b0ab2012-05-11 21:01:36 -0700215 } else {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800216 ::llvm::FunctionPassManager fpm2(module_);
buzbee4df2bbd2012-10-11 14:46:06 -0700217 fpm2.add(CreateGBCExpanderPass(*llvm_info_->GetIntrinsicHelper(), *irb_.get(),
Ian Rogers89756f22013-03-04 16:40:02 -0800218 driver_, dex_compilation_unit_));
TDYa127f15b0ab2012-05-11 21:01:36 -0700219 fpm2.doInitialization();
Ian Rogers4c1c2832013-03-04 18:30:13 -0800220 for (::llvm::Module::iterator F = module_->begin(), E = module_->end();
TDYa127f15b0ab2012-05-11 21:01:36 -0700221 F != E; ++F) {
222 fpm2.run(*F);
223 }
224 fpm2.doFinalization();
TDYa127f15b0ab2012-05-11 21:01:36 -0700225
226 // Write bitcode to file
227 std::string errmsg;
228
Ian Rogers4c1c2832013-03-04 18:30:13 -0800229 ::llvm::OwningPtr< ::llvm::tool_output_file> out_file(
230 new ::llvm::tool_output_file(bitcode_filename_.c_str(), errmsg,
231 ::llvm::raw_fd_ostream::F_Binary));
TDYa127f15b0ab2012-05-11 21:01:36 -0700232
233
234 if (!errmsg.empty()) {
235 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
236 return false;
237 }
238
Ian Rogers4c1c2832013-03-04 18:30:13 -0800239 ::llvm::WriteBitcodeToFile(module_, out_file->os());
TDYa127f15b0ab2012-05-11 21:01:36 -0700240 out_file->keep();
241 }
242
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700243 // Add optimization pass
Ian Rogers4c1c2832013-03-04 18:30:13 -0800244 ::llvm::PassManagerBuilder pm_builder;
TDYa1279a129452012-07-19 03:10:08 -0700245 // TODO: Use inliner after we can do IPO.
246 pm_builder.Inliner = NULL;
Ian Rogers4c1c2832013-03-04 18:30:13 -0800247 //pm_builder.Inliner = ::llvm::createFunctionInliningPass();
248 //pm_builder.Inliner = ::llvm::createAlwaysInlinerPass();
249 //pm_builder.Inliner = ::llvm::createPartialInliningPass();
Shih-wei Liao415576b2012-04-23 15:28:53 -0700250 pm_builder.OptLevel = 3;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700251 pm_builder.DisableSimplifyLibCalls = 1;
TDYa127e4c2ccc2012-05-13 21:10:36 -0700252 pm_builder.DisableUnitAtATime = 1;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700253 pm_builder.populateFunctionPassManager(fpm);
TDYa127ce9c3172012-05-15 06:09:27 -0700254 pm_builder.populateModulePassManager(pm);
Ian Rogers4c1c2832013-03-04 18:30:13 -0800255 pm.add(::llvm::createStripDeadPrototypesPass());
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700256
257 // Add passes to emit ELF image
258 {
Ian Rogers4c1c2832013-03-04 18:30:13 -0800259 ::llvm::formatted_raw_ostream formatted_os(out_stream, false);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700260
261 // Ask the target to add backend passes as necessary.
262 if (target_machine->addPassesToEmitFile(pm,
263 formatted_os,
Ian Rogers4c1c2832013-03-04 18:30:13 -0800264 ::llvm::TargetMachine::CGFT_ObjectFile,
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700265 true)) {
266 LOG(FATAL) << "Unable to generate ELF for this target";
267 return false;
268 }
269
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700270 // Run the per-function optimization
271 fpm.doInitialization();
Ian Rogers4c1c2832013-03-04 18:30:13 -0800272 for (::llvm::Module::iterator F = module_->begin(), E = module_->end();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700273 F != E; ++F) {
274 fpm.run(*F);
275 }
276 fpm.doFinalization();
277
278 // Run the code generation passes
Logan Chien799ef4f2012-04-23 00:17:47 +0800279 pm.run(*module_);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700280 }
281
282 return true;
283}
Logan Chien110bcba2012-04-16 19:11:28 +0800284
Logan Chien971bf3f2012-05-01 15:47:55 +0800285// Check whether the align is less than or equal to the code alignment of
286// that architecture. Since the Oat writer only guarantee that the compiled
287// method being aligned to kArchAlignment, we have no way to align the ELf
288// section if the section alignment is greater than kArchAlignment.
Brian Carlstrom641ce032013-01-31 15:21:37 -0800289void LlvmCompilationUnit::CheckCodeAlign(uint32_t align) const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800290 InstructionSet insn_set = GetInstructionSet();
291 switch (insn_set) {
292 case kThumb2:
293 case kArm:
294 CHECK_LE(align, static_cast<uint32_t>(kArmAlignment));
295 break;
296
297 case kX86:
298 CHECK_LE(align, static_cast<uint32_t>(kX86Alignment));
299 break;
300
301 case kMips:
302 CHECK_LE(align, static_cast<uint32_t>(kMipsAlignment));
303 break;
304
305 default:
306 LOG(FATAL) << "Unknown instruction set: " << insn_set;
307 }
308}
309
310
Ian Rogers4c1c2832013-03-04 18:30:13 -0800311} // namespace llvm
Logan Chien8b977d32012-02-21 19:14:55 +0800312} // namespace art