Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 1 | /* |
Stephen Hines | db16918 | 2012-01-05 18:46:36 -0800 | [diff] [blame] | 2 | * Copyright 2010-2012, The Android Open Source Project |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 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 | |
Logan | c439523 | 2010-11-27 18:54:17 +0800 | [diff] [blame] | 17 | #include "Compiler.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 18 | |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 19 | #include "Config.h" |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 20 | #include <bcinfo/MetadataExtractor.h> |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 21 | |
Logan Chien | 4885cf8 | 2011-07-20 10:18:05 +0800 | [diff] [blame] | 22 | #if USE_DISASSEMBLER |
| 23 | #include "Disassembler/Disassembler.h" |
| 24 | #endif |
| 25 | |
Logan | 4dcd679 | 2011-02-28 05:12:00 +0800 | [diff] [blame] | 26 | #include "DebugHelper.h" |
Shih-wei Liao | 6c0c7b0 | 2011-05-21 21:47:14 -0700 | [diff] [blame] | 27 | #include "FileHandle.h" |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 28 | #include "Runtime.h" |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 29 | #include "ScriptCompiled.h" |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 30 | #include "Sha1Helper.h" |
Zonr Chang | 2fcbd02 | 2012-01-06 21:04:31 +0800 | [diff] [blame] | 31 | #include "CompilerOption.h" |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 32 | |
Shih-wei Liao | 320b549 | 2011-06-20 22:53:33 -0700 | [diff] [blame] | 33 | #if USE_MCJIT |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 34 | #include "librsloader.h" |
Shih-wei Liao | 320b549 | 2011-06-20 22:53:33 -0700 | [diff] [blame] | 35 | #endif |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 36 | |
Stephen Hines | db16918 | 2012-01-05 18:46:36 -0800 | [diff] [blame] | 37 | #include "Transforms/BCCTransforms.h" |
| 38 | |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 39 | #include "llvm/ADT/StringRef.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 40 | |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 41 | #include "llvm/Analysis/Passes.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 42 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 43 | #include "llvm/CodeGen/Passes.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 44 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 45 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 46 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 47 | #include "llvm/MC/MCContext.h" |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 48 | #include "llvm/MC/SubtargetFeature.h" |
| 49 | |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 50 | #include "llvm/Transforms/IPO.h" |
| 51 | #include "llvm/Transforms/Scalar.h" |
| 52 | |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 53 | #include "llvm/Target/TargetData.h" |
| 54 | #include "llvm/Target/TargetMachine.h" |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 55 | |
| 56 | #include "llvm/Support/ErrorHandling.h" |
Shih-wei Liao | 898c5a9 | 2011-05-18 07:02:39 -0700 | [diff] [blame] | 57 | #include "llvm/Support/FormattedStream.h" |
Logan Chien | bc9eb8f | 2011-10-21 15:17:45 +0800 | [diff] [blame] | 58 | #include "llvm/Support/TargetRegistry.h" |
| 59 | #include "llvm/Support/TargetSelect.h" |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 60 | #include "llvm/Support/raw_ostream.h" |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 61 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 62 | #include "llvm/Constants.h" |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 63 | #include "llvm/GlobalValue.h" |
| 64 | #include "llvm/Linker.h" |
| 65 | #include "llvm/LLVMContext.h" |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 66 | #include "llvm/Module.h" |
| 67 | #include "llvm/PassManager.h" |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 68 | #include "llvm/Type.h" |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 69 | #include "llvm/Value.h" |
| 70 | |
| 71 | #include <errno.h> |
| 72 | #include <sys/file.h> |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 73 | #include <sys/stat.h> |
| 74 | #include <sys/types.h> |
| 75 | #include <unistd.h> |
| 76 | |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 77 | #include <string.h> |
Logan | 8b77a77 | 2010-12-21 09:11:01 +0800 | [diff] [blame] | 78 | |
Logan Chien | 7890d43 | 2011-08-03 14:55:17 +0800 | [diff] [blame] | 79 | #include <algorithm> |
| 80 | #include <iterator> |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 81 | #include <string> |
| 82 | #include <vector> |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 83 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 84 | extern char* gDebugDumpDirectory; |
| 85 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 86 | namespace bcc { |
| 87 | |
| 88 | ////////////////////////////////////////////////////////////////////////////// |
| 89 | // BCC Compiler Static Variables |
| 90 | ////////////////////////////////////////////////////////////////////////////// |
| 91 | |
| 92 | bool Compiler::GlobalInitialized = false; |
| 93 | |
Andrew Hsieh | 998ec83 | 2011-11-21 02:36:11 -0800 | [diff] [blame] | 94 | |
| 95 | #if !defined(__HOST__) |
| 96 | #define TARGET_TRIPLE_STRING DEFAULT_TARGET_TRIPLE_STRING |
| 97 | #else |
| 98 | // In host TARGET_TRIPLE_STRING is a variable to allow cross-compilation. |
| 99 | #if defined(__cplusplus) |
| 100 | extern "C" { |
| 101 | #endif |
| 102 | char *TARGET_TRIPLE_STRING = (char*)DEFAULT_TARGET_TRIPLE_STRING; |
| 103 | #if defined(__cplusplus) |
| 104 | }; |
| 105 | #endif |
| 106 | #endif |
| 107 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 108 | // Code generation optimization level for the compiler |
| 109 | llvm::CodeGenOpt::Level Compiler::CodeGenOptLevel; |
| 110 | |
| 111 | std::string Compiler::Triple; |
Shih-wei Liao | 4deffde | 2012-01-17 20:38:17 +0800 | [diff] [blame] | 112 | llvm::Triple::ArchType Compiler::ArchType; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 113 | |
| 114 | std::string Compiler::CPU; |
| 115 | |
| 116 | std::vector<std::string> Compiler::Features; |
| 117 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 118 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 119 | ////////////////////////////////////////////////////////////////////////////// |
| 120 | // Compiler |
| 121 | ////////////////////////////////////////////////////////////////////////////// |
| 122 | |
| 123 | void Compiler::GlobalInitialization() { |
Shih-wei Liao | 40bcd66 | 2011-10-22 17:51:01 -0700 | [diff] [blame] | 124 | if (GlobalInitialized) { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 125 | return; |
Shih-wei Liao | 40bcd66 | 2011-10-22 17:51:01 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Shih-wei Liao | 2270591 | 2012-03-06 19:13:26 -0800 | [diff] [blame] | 128 | #if defined(PROVIDE_ARM_CODEGEN) |
| 129 | LLVMInitializeARMAsmPrinter(); |
| 130 | LLVMInitializeARMTargetMC(); |
| 131 | LLVMInitializeARMTargetInfo(); |
| 132 | LLVMInitializeARMTarget(); |
| 133 | #endif |
| 134 | |
| 135 | #if defined(PROVIDE_MIPS_CODEGEN) |
| 136 | LLVMInitializeMipsAsmPrinter(); |
| 137 | LLVMInitializeMipsTargetMC(); |
| 138 | LLVMInitializeMipsTargetInfo(); |
| 139 | LLVMInitializeMipsTarget(); |
| 140 | #endif |
| 141 | |
| 142 | #if defined(PROVIDE_X86_CODEGEN) |
| 143 | LLVMInitializeX86AsmPrinter(); |
| 144 | LLVMInitializeX86TargetMC(); |
| 145 | LLVMInitializeX86TargetInfo(); |
| 146 | LLVMInitializeX86Target(); |
| 147 | #endif |
| 148 | |
| 149 | #if USE_DISASSEMBLER |
| 150 | InitializeDisassembler(); |
| 151 | #endif |
| 152 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 153 | // if (!llvm::llvm_is_multithreaded()) |
| 154 | // llvm::llvm_start_multithreaded(); |
| 155 | |
| 156 | // Set Triple, CPU and Features here |
| 157 | Triple = TARGET_TRIPLE_STRING; |
| 158 | |
Shih-wei Liao | 4deffde | 2012-01-17 20:38:17 +0800 | [diff] [blame] | 159 | // Determine ArchType |
Andrew Hsieh | c0554e2 | 2012-01-13 22:34:34 -0800 | [diff] [blame] | 160 | #if defined(__HOST__) |
Shih-wei Liao | 4deffde | 2012-01-17 20:38:17 +0800 | [diff] [blame] | 161 | { |
| 162 | std::string Err; |
| 163 | llvm::Target const *Target = llvm::TargetRegistry::lookupTarget(Triple, Err); |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 164 | if (Target != NULL) { |
Shih-wei Liao | 4deffde | 2012-01-17 20:38:17 +0800 | [diff] [blame] | 165 | ArchType = llvm::Triple::getArchTypeForLLVMName(Target->getName()); |
| 166 | } else { |
| 167 | ArchType = llvm::Triple::UnknownArch; |
| 168 | ALOGE("%s", Err.c_str()); |
| 169 | } |
Andrew Hsieh | c0554e2 | 2012-01-13 22:34:34 -0800 | [diff] [blame] | 170 | } |
| 171 | #elif defined(DEFAULT_ARM_CODEGEN) |
Shih-wei Liao | 4deffde | 2012-01-17 20:38:17 +0800 | [diff] [blame] | 172 | ArchType = llvm::Triple::arm; |
Andrew Hsieh | c0554e2 | 2012-01-13 22:34:34 -0800 | [diff] [blame] | 173 | #elif defined(DEFAULT_MIPS_CODEGEN) |
Shih-wei Liao | 4deffde | 2012-01-17 20:38:17 +0800 | [diff] [blame] | 174 | ArchType = llvm::Triple::mipsel; |
Andrew Hsieh | c0554e2 | 2012-01-13 22:34:34 -0800 | [diff] [blame] | 175 | #elif defined(DEFAULT_X86_CODEGEN) |
Shih-wei Liao | 4deffde | 2012-01-17 20:38:17 +0800 | [diff] [blame] | 176 | ArchType = llvm::Triple::x86; |
Andrew Hsieh | c0554e2 | 2012-01-13 22:34:34 -0800 | [diff] [blame] | 177 | #elif defined(DEFAULT_X86_64_CODEGEN) |
Shih-wei Liao | 4deffde | 2012-01-17 20:38:17 +0800 | [diff] [blame] | 178 | ArchType = llvm::Triple::x86_64; |
Andrew Hsieh | c0554e2 | 2012-01-13 22:34:34 -0800 | [diff] [blame] | 179 | #else |
Shih-wei Liao | 4deffde | 2012-01-17 20:38:17 +0800 | [diff] [blame] | 180 | ArchType = llvm::Triple::UnknownArch; |
Andrew Hsieh | c0554e2 | 2012-01-13 22:34:34 -0800 | [diff] [blame] | 181 | #endif |
Logan Chien | 3bb7707 | 2011-09-17 16:53:53 +0800 | [diff] [blame] | 182 | |
Shih-wei Liao | 4deffde | 2012-01-17 20:38:17 +0800 | [diff] [blame] | 183 | if ((ArchType == llvm::Triple::arm) || (ArchType == llvm::Triple::thumb)) { |
Shih-wei Liao | fbeb9b6 | 2012-01-14 02:14:31 -0800 | [diff] [blame] | 184 | # if defined(ARCH_ARM_HAVE_VFP) |
Andrew Hsieh | c0554e2 | 2012-01-13 22:34:34 -0800 | [diff] [blame] | 185 | Features.push_back("+vfp3"); |
Shih-wei Liao | fbeb9b6 | 2012-01-14 02:14:31 -0800 | [diff] [blame] | 186 | # if !defined(ARCH_ARM_HAVE_VFP_D32) |
Andrew Hsieh | c0554e2 | 2012-01-13 22:34:34 -0800 | [diff] [blame] | 187 | Features.push_back("+d16"); |
Shih-wei Liao | fbeb9b6 | 2012-01-14 02:14:31 -0800 | [diff] [blame] | 188 | # endif |
| 189 | # endif |
Stephen Hines | a12d2f3 | 2011-09-07 15:30:06 -0700 | [diff] [blame] | 190 | |
Shih-wei Liao | fbeb9b6 | 2012-01-14 02:14:31 -0800 | [diff] [blame] | 191 | # if defined(ARCH_ARM_HAVE_NEON) |
Andrew Hsieh | c0554e2 | 2012-01-13 22:34:34 -0800 | [diff] [blame] | 192 | Features.push_back("+neon"); |
| 193 | Features.push_back("+neonfp"); |
Shih-wei Liao | fbeb9b6 | 2012-01-14 02:14:31 -0800 | [diff] [blame] | 194 | # else |
Andrew Hsieh | c0554e2 | 2012-01-13 22:34:34 -0800 | [diff] [blame] | 195 | Features.push_back("-neon"); |
| 196 | Features.push_back("-neonfp"); |
Shih-wei Liao | fbeb9b6 | 2012-01-14 02:14:31 -0800 | [diff] [blame] | 197 | # endif |
Shih-wei Liao | 40bcd66 | 2011-10-22 17:51:01 -0700 | [diff] [blame] | 198 | |
Stephen Hines | f6b202b | 2012-03-06 17:45:41 -0800 | [diff] [blame] | 199 | // FIXME(all): Turn NEON back on after debugging the rebase. |
| 200 | # if 1 || defined(DISABLE_ARCH_ARM_HAVE_NEON) |
Andrew Hsieh | c0554e2 | 2012-01-13 22:34:34 -0800 | [diff] [blame] | 201 | Features.push_back("-neon"); |
| 202 | Features.push_back("-neonfp"); |
Shih-wei Liao | fbeb9b6 | 2012-01-14 02:14:31 -0800 | [diff] [blame] | 203 | # endif |
Andrew Hsieh | c0554e2 | 2012-01-13 22:34:34 -0800 | [diff] [blame] | 204 | } |
Joseph Wen | 51001b8 | 2011-06-23 18:56:45 -0700 | [diff] [blame] | 205 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 206 | // Register the scheduler |
| 207 | llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler); |
| 208 | |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 209 | #if USE_CACHE |
Ying Wang | 26fea10 | 2011-07-05 15:12:25 -0700 | [diff] [blame] | 210 | // Read in SHA1 checksum of libbcc and libRS. |
| 211 | readSHA1(sha1LibBCC_SHA1, sizeof(sha1LibBCC_SHA1), pathLibBCC_SHA1); |
Joseph Wen | 7691907 | 2011-07-07 23:06:15 -0700 | [diff] [blame] | 212 | |
| 213 | calcFileSHA1(sha1LibRS, pathLibRS); |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 214 | #endif |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 215 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 216 | GlobalInitialized = true; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | void Compiler::LLVMErrorHandler(void *UserData, const std::string &Message) { |
| 221 | std::string *Error = static_cast<std::string*>(UserData); |
| 222 | Error->assign(Message); |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 223 | ALOGE("%s", Message.c_str()); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 224 | exit(1); |
| 225 | } |
| 226 | |
| 227 | |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 228 | Compiler::Compiler(ScriptCompiled *result) |
| 229 | : mpResult(result), |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 230 | #if USE_MCJIT |
| 231 | mRSExecutable(NULL), |
| 232 | #endif |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 233 | mpSymbolLookupFn(NULL), |
| 234 | mpSymbolLookupContext(NULL), |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 235 | mModule(NULL), |
| 236 | mHasLinked(false) /* Turn off linker */ { |
| 237 | llvm::remove_fatal_error_handler(); |
| 238 | llvm::install_fatal_error_handler(LLVMErrorHandler, &mError); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 239 | return; |
| 240 | } |
| 241 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 242 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 243 | int Compiler::linkModule(llvm::Module *moduleWith) { |
Logan Chien | bc9eb8f | 2011-10-21 15:17:45 +0800 | [diff] [blame] | 244 | if (llvm::Linker::LinkModules(mModule, moduleWith, |
Shih-wei Liao | d88c0d1 | 2012-01-17 20:32:58 -0800 | [diff] [blame] | 245 | llvm::Linker::PreserveSource, |
Logan Chien | bc9eb8f | 2011-10-21 15:17:45 +0800 | [diff] [blame] | 246 | &mError) != 0) { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 247 | return hasError(); |
| 248 | } |
| 249 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 250 | // Everything for linking should be settled down here with no error occurs |
| 251 | mHasLinked = true; |
| 252 | return hasError(); |
| 253 | } |
| 254 | |
| 255 | |
Shih-wei Liao | 9e81e37 | 2012-01-17 16:38:40 -0800 | [diff] [blame] | 256 | int Compiler::compile(const CompilerOption &option) { |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 257 | llvm::Target const *Target = NULL; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 258 | llvm::TargetData *TD = NULL; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 259 | llvm::TargetMachine *TM = NULL; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 260 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 261 | std::string FeaturesStr; |
| 262 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 263 | if (mModule == NULL) // No module was loaded |
| 264 | return 0; |
| 265 | |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 266 | bcinfo::MetadataExtractor ME(mModule); |
| 267 | ME.extract(); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 268 | |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 269 | size_t VarCount = ME.getExportVarCount(); |
| 270 | size_t FuncCount = ME.getExportFuncCount(); |
| 271 | size_t ForEachSigCount = ME.getExportForEachSignatureCount(); |
| 272 | size_t ObjectSlotCount = ME.getObjectSlotCount(); |
| 273 | size_t PragmaCount = ME.getPragmaCount(); |
| 274 | |
| 275 | std::vector<std::string> &VarNameList = mpResult->mExportVarsName; |
| 276 | std::vector<std::string> &FuncNameList = mpResult->mExportFuncsName; |
| 277 | std::vector<std::string> &ForEachExpandList = mpResult->mExportForEachName; |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 278 | std::vector<std::string> ForEachNameList; |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 279 | std::vector<uint32_t> ForEachSigList; |
| 280 | std::vector<const char*> ExportSymbols; |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 281 | |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 282 | // Defaults to maximum optimization level from MetadataExtractor. |
| 283 | int OptimizationLevel = ME.getOptimizationLevel(); |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 284 | |
| 285 | if (OptimizationLevel == 0) { |
| 286 | CodeGenOptLevel = llvm::CodeGenOpt::None; |
| 287 | } else if (OptimizationLevel == 1) { |
| 288 | CodeGenOptLevel = llvm::CodeGenOpt::Less; |
| 289 | } else if (OptimizationLevel == 2) { |
| 290 | CodeGenOptLevel = llvm::CodeGenOpt::Default; |
| 291 | } else if (OptimizationLevel == 3) { |
| 292 | CodeGenOptLevel = llvm::CodeGenOpt::Aggressive; |
| 293 | } |
| 294 | |
| 295 | // not the best place for this, but we need to set the register allocation |
| 296 | // policy after we read the optimization_level metadata from the bitcode |
| 297 | |
| 298 | // Register allocation policy: |
| 299 | // createFastRegisterAllocator: fast but bad quality |
| 300 | // createLinearScanRegisterAllocator: not so fast but good quality |
| 301 | llvm::RegisterRegAlloc::setDefault |
| 302 | ((CodeGenOptLevel == llvm::CodeGenOpt::None) ? |
| 303 | llvm::createFastRegisterAllocator : |
Stephen Hines | e0918ac | 2012-03-01 23:28:09 -0800 | [diff] [blame] | 304 | llvm::createGreedyRegisterAllocator); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 305 | |
Logan Chien | be81e10 | 2011-12-16 13:31:39 +0800 | [diff] [blame] | 306 | // Find LLVM Target |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 307 | Target = llvm::TargetRegistry::lookupTarget(Triple, mError); |
| 308 | if (hasError()) |
| 309 | goto on_bcc_compile_error; |
| 310 | |
| 311 | if (!CPU.empty() || !Features.empty()) { |
| 312 | llvm::SubtargetFeatures F; |
Logan | a4994f5 | 2010-11-27 14:06:02 +0800 | [diff] [blame] | 313 | |
| 314 | for (std::vector<std::string>::const_iterator |
| 315 | I = Features.begin(), E = Features.end(); I != E; I++) { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 316 | F.AddFeature(*I); |
Logan | a4994f5 | 2010-11-27 14:06:02 +0800 | [diff] [blame] | 317 | } |
| 318 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 319 | FeaturesStr = F.getString(); |
| 320 | } |
| 321 | |
Logan Chien | be81e10 | 2011-12-16 13:31:39 +0800 | [diff] [blame] | 322 | // Create LLVM Target Machine |
Zonr Chang | 2fcbd02 | 2012-01-06 21:04:31 +0800 | [diff] [blame] | 323 | TM = Target->createTargetMachine(Triple, CPU, FeaturesStr, |
| 324 | option.TargetOpt, |
| 325 | option.RelocModelOpt, |
| 326 | option.CodeModelOpt); |
Logan Chien | be81e10 | 2011-12-16 13:31:39 +0800 | [diff] [blame] | 327 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 328 | if (TM == NULL) { |
| 329 | setError("Failed to create target machine implementation for the" |
| 330 | " specified triple '" + Triple + "'"); |
| 331 | goto on_bcc_compile_error; |
| 332 | } |
| 333 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 334 | // Get target data from Module |
| 335 | TD = new llvm::TargetData(mModule); |
| 336 | |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 337 | // Read pragma information from MetadataExtractor |
| 338 | if (PragmaCount) { |
| 339 | ScriptCompiled::PragmaList &PragmaPairs = mpResult->mPragmas; |
| 340 | const char **PragmaKeys = ME.getPragmaKeyList(); |
| 341 | const char **PragmaValues = ME.getPragmaValueList(); |
| 342 | for (size_t i = 0; i < PragmaCount; i++) { |
| 343 | PragmaPairs.push_back(std::make_pair(PragmaKeys[i], PragmaValues[i])); |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 347 | if (VarCount) { |
| 348 | const char **VarNames = ME.getExportVarNameList(); |
| 349 | for (size_t i = 0; i < VarCount; i++) { |
| 350 | VarNameList.push_back(VarNames[i]); |
| 351 | ExportSymbols.push_back(VarNames[i]); |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 355 | if (FuncCount) { |
| 356 | const char **FuncNames = ME.getExportFuncNameList(); |
| 357 | for (size_t i = 0; i < FuncCount; i++) { |
| 358 | FuncNameList.push_back(FuncNames[i]); |
| 359 | ExportSymbols.push_back(FuncNames[i]); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | if (ForEachSigCount) { |
| 364 | const char **ForEachNames = ME.getExportForEachNameList(); |
| 365 | const uint32_t *ForEachSigs = ME.getExportForEachSignatureList(); |
| 366 | for (size_t i = 0; i < ForEachSigCount; i++) { |
| 367 | std::string Name(ForEachNames[i]); |
| 368 | ForEachNameList.push_back(Name); |
| 369 | ForEachExpandList.push_back(Name + ".expand"); |
| 370 | ForEachSigList.push_back(ForEachSigs[i]); |
| 371 | } |
| 372 | |
| 373 | // Need to wait until ForEachExpandList is fully populated to fill in |
| 374 | // exported symbols. |
| 375 | for (size_t i = 0; i < ForEachSigCount; i++) { |
| 376 | ExportSymbols.push_back(ForEachExpandList[i].c_str()); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | if (ObjectSlotCount) { |
| 381 | ScriptCompiled::ObjectSlotList &objectSlotList = mpResult->mObjectSlots; |
| 382 | const uint32_t *ObjectSlots = ME.getObjectSlotList(); |
| 383 | for (size_t i = 0; i < ObjectSlotCount; i++) { |
| 384 | objectSlotList.push_back(ObjectSlots[i]); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | runInternalPasses(ForEachNameList, ForEachSigList); |
Stephen Hines | db16918 | 2012-01-05 18:46:36 -0800 | [diff] [blame] | 389 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 390 | // Perform link-time optimization if we have multiple modules |
| 391 | if (mHasLinked) { |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 392 | runLTO(new llvm::TargetData(*TD), ExportSymbols, CodeGenOptLevel); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // Perform code generation |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 396 | #if USE_MCJIT |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 397 | if (runMCCodeGen(new llvm::TargetData(*TD), TM) != 0) { |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 398 | goto on_bcc_compile_error; |
| 399 | } |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 400 | |
Zonr Chang | 2fcbd02 | 2012-01-06 21:04:31 +0800 | [diff] [blame] | 401 | if (!option.LoadAfterCompile) |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 402 | return 0; |
| 403 | |
| 404 | // Load the ELF Object |
| 405 | mRSExecutable = |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 406 | rsloaderCreateExec((unsigned char *)&*mEmittedELFExecutable.begin(), |
| 407 | mEmittedELFExecutable.size(), |
| 408 | &resolveSymbolAdapter, this); |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 409 | |
| 410 | if (!mRSExecutable) { |
| 411 | setError("Fail to load emitted ELF relocatable file"); |
| 412 | goto on_bcc_compile_error; |
| 413 | } |
| 414 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 415 | rsloaderUpdateSectionHeaders(mRSExecutable, |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 416 | (unsigned char*) mEmittedELFExecutable.begin()); |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 417 | |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 418 | // Once the ELF object has been loaded, populate the various slots for RS |
| 419 | // with the appropriate relocated addresses. |
| 420 | if (VarCount) { |
| 421 | ScriptCompiled::ExportVarList &VarList = mpResult->mExportVars; |
| 422 | for (size_t i = 0; i < VarCount; i++) { |
| 423 | VarList.push_back(rsloaderGetSymbolAddress(mRSExecutable, |
| 424 | VarNameList[i].c_str())); |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 428 | if (FuncCount) { |
| 429 | ScriptCompiled::ExportFuncList &FuncList = mpResult->mExportFuncs; |
| 430 | for (size_t i = 0; i < FuncCount; i++) { |
| 431 | FuncList.push_back(rsloaderGetSymbolAddress(mRSExecutable, |
| 432 | FuncNameList[i].c_str())); |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 436 | if (ForEachSigCount) { |
| 437 | ScriptCompiled::ExportForEachList &ForEachList = mpResult->mExportForEach; |
| 438 | for (size_t i = 0; i < ForEachSigCount; i++) { |
| 439 | ForEachList.push_back(rsloaderGetSymbolAddress(mRSExecutable, |
| 440 | ForEachExpandList[i].c_str())); |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Logan Chien | 4885cf8 | 2011-07-20 10:18:05 +0800 | [diff] [blame] | 444 | #if DEBUG_MCJIT_DISASSEMBLER |
Shih-wei Liao | 90cd3d1 | 2011-06-20 15:43:34 -0700 | [diff] [blame] | 445 | { |
Shih-wei Liao | d3c551f | 2011-07-01 04:28:27 -0700 | [diff] [blame] | 446 | // Get MC codegen emitted function name list |
| 447 | size_t func_list_size = rsloaderGetFuncCount(mRSExecutable); |
| 448 | std::vector<char const *> func_list(func_list_size, NULL); |
| 449 | rsloaderGetFuncNameList(mRSExecutable, func_list_size, &*func_list.begin()); |
Shih-wei Liao | 320b549 | 2011-06-20 22:53:33 -0700 | [diff] [blame] | 450 | |
Shih-wei Liao | d3c551f | 2011-07-01 04:28:27 -0700 | [diff] [blame] | 451 | // Disassemble each function |
Shih-wei Liao | 90cd3d1 | 2011-06-20 15:43:34 -0700 | [diff] [blame] | 452 | for (size_t i = 0; i < func_list_size; ++i) { |
| 453 | void *func = rsloaderGetSymbolAddress(mRSExecutable, func_list[i]); |
| 454 | if (func) { |
| 455 | size_t size = rsloaderGetSymbolSize(mRSExecutable, func_list[i]); |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 456 | Disassemble(DEBUG_MCJIT_DISASSEMBLER_FILE, |
| 457 | Target, TM, func_list[i], (unsigned char const *)func, size); |
Shih-wei Liao | 90cd3d1 | 2011-06-20 15:43:34 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | } |
| 461 | #endif |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 462 | #endif |
| 463 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 464 | on_bcc_compile_error: |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 465 | // ALOGE("on_bcc_compiler_error"); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 466 | if (TD) { |
| 467 | delete TD; |
| 468 | } |
| 469 | |
| 470 | if (TM) { |
| 471 | delete TM; |
| 472 | } |
| 473 | |
| 474 | if (mError.empty()) { |
| 475 | return 0; |
| 476 | } |
| 477 | |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 478 | // ALOGE(getErrorMessage()); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 479 | return 1; |
| 480 | } |
| 481 | |
| 482 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 483 | #if USE_MCJIT |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 484 | int Compiler::runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM) { |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 485 | // Decorate mEmittedELFExecutable with formatted ostream |
| 486 | llvm::raw_svector_ostream OutSVOS(mEmittedELFExecutable); |
| 487 | |
| 488 | // Relax all machine instructions |
| 489 | TM->setMCRelaxAll(/* RelaxAll= */ true); |
| 490 | |
| 491 | // Create MC code generation pass manager |
| 492 | llvm::PassManager MCCodeGenPasses; |
| 493 | |
| 494 | // Add TargetData to MC code generation pass manager |
| 495 | MCCodeGenPasses.add(TD); |
| 496 | |
| 497 | // Add MC code generation passes to pass manager |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 498 | llvm::MCContext *Ctx = NULL; |
Logan Chien | 4e4485d | 2011-11-25 18:12:33 +0800 | [diff] [blame] | 499 | if (TM->addPassesToEmitMC(MCCodeGenPasses, Ctx, OutSVOS, false)) { |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 500 | setError("Fail to add passes to emit file"); |
| 501 | return 1; |
| 502 | } |
| 503 | |
| 504 | MCCodeGenPasses.run(*mModule); |
| 505 | OutSVOS.flush(); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 506 | return 0; |
| 507 | } |
| 508 | #endif // USE_MCJIT |
| 509 | |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 510 | int Compiler::runInternalPasses(std::vector<std::string>& Names, |
| 511 | std::vector<uint32_t>& Signatures) { |
Stephen Hines | db16918 | 2012-01-05 18:46:36 -0800 | [diff] [blame] | 512 | llvm::PassManager BCCPasses; |
| 513 | |
| 514 | // Expand ForEach on CPU path to reduce launch overhead. |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 515 | BCCPasses.add(createForEachExpandPass(Names, Signatures)); |
Stephen Hines | db16918 | 2012-01-05 18:46:36 -0800 | [diff] [blame] | 516 | |
| 517 | BCCPasses.run(*mModule); |
| 518 | |
| 519 | return 0; |
| 520 | } |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 521 | |
| 522 | int Compiler::runLTO(llvm::TargetData *TD, |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 523 | std::vector<const char*>& ExportSymbols, |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 524 | llvm::CodeGenOpt::Level OptimizationLevel) { |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 525 | // Note: ExportSymbols is a workaround for getting all exported variable, |
| 526 | // function, and kernel names. |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 527 | // We should refine it soon. |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 528 | |
Logan Chien | 7890d43 | 2011-08-03 14:55:17 +0800 | [diff] [blame] | 529 | // TODO(logan): Remove this after we have finished the |
| 530 | // bccMarkExternalSymbol API. |
| 531 | |
Stephen Hines | 6416010 | 2011-09-01 17:30:26 -0700 | [diff] [blame] | 532 | // root(), init(), and .rs.dtor() are born to be exported |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 533 | ExportSymbols.push_back("root"); |
| 534 | ExportSymbols.push_back("init"); |
Stephen Hines | 6416010 | 2011-09-01 17:30:26 -0700 | [diff] [blame] | 535 | ExportSymbols.push_back(".rs.dtor"); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 536 | |
Logan Chien | 7890d43 | 2011-08-03 14:55:17 +0800 | [diff] [blame] | 537 | // User-defined exporting symbols |
| 538 | std::vector<char const *> const &UserDefinedExternalSymbols = |
| 539 | mpResult->getUserDefinedExternalSymbols(); |
| 540 | |
| 541 | std::copy(UserDefinedExternalSymbols.begin(), |
| 542 | UserDefinedExternalSymbols.end(), |
| 543 | std::back_inserter(ExportSymbols)); |
| 544 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 545 | llvm::PassManager LTOPasses; |
| 546 | |
| 547 | // Add TargetData to LTO passes |
| 548 | LTOPasses.add(TD); |
| 549 | |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 550 | // We now create passes list performing LTO. These are copied from |
| 551 | // (including comments) llvm::createStandardLTOPasses(). |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 552 | // Only a subset of these LTO passes are enabled in optimization level 0 |
| 553 | // as they interfere with interactive debugging. |
| 554 | // FIXME: figure out which passes (if any) makes sense for levels 1 and 2 |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 555 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 556 | if (OptimizationLevel != llvm::CodeGenOpt::None) { |
| 557 | // Internalize all other symbols not listed in ExportSymbols |
| 558 | LTOPasses.add(llvm::createInternalizePass(ExportSymbols)); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 559 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 560 | // Propagate constants at call sites into the functions they call. This |
| 561 | // opens opportunities for globalopt (and inlining) by substituting |
| 562 | // function pointers passed as arguments to direct uses of functions. |
| 563 | LTOPasses.add(llvm::createIPSCCPPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 564 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 565 | // Now that we internalized some globals, see if we can hack on them! |
| 566 | LTOPasses.add(llvm::createGlobalOptimizerPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 567 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 568 | // Linking modules together can lead to duplicated global constants, only |
| 569 | // keep one copy of each constant... |
| 570 | LTOPasses.add(llvm::createConstantMergePass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 571 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 572 | // Remove unused arguments from functions... |
| 573 | LTOPasses.add(llvm::createDeadArgEliminationPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 574 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 575 | // Reduce the code after globalopt and ipsccp. Both can open up |
| 576 | // significant simplification opportunities, and both can propagate |
| 577 | // functions through function pointers. When this happens, we often have |
| 578 | // to resolve varargs calls, etc, so let instcombine do this. |
| 579 | LTOPasses.add(llvm::createInstructionCombiningPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 580 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 581 | // Inline small functions |
| 582 | LTOPasses.add(llvm::createFunctionInliningPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 583 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 584 | // Remove dead EH info. |
| 585 | LTOPasses.add(llvm::createPruneEHPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 586 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 587 | // Internalize the globals again after inlining |
| 588 | LTOPasses.add(llvm::createGlobalOptimizerPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 589 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 590 | // Remove dead functions. |
| 591 | LTOPasses.add(llvm::createGlobalDCEPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 592 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 593 | // If we didn't decide to inline a function, check to see if we can |
| 594 | // transform it to pass arguments by value instead of by reference. |
| 595 | LTOPasses.add(llvm::createArgumentPromotionPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 596 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 597 | // The IPO passes may leave cruft around. Clean up after them. |
| 598 | LTOPasses.add(llvm::createInstructionCombiningPass()); |
| 599 | LTOPasses.add(llvm::createJumpThreadingPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 600 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 601 | // Break up allocas |
| 602 | LTOPasses.add(llvm::createScalarReplAggregatesPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 603 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 604 | // Run a few AA driven optimizations here and now, to cleanup the code. |
| 605 | LTOPasses.add(llvm::createFunctionAttrsPass()); // Add nocapture. |
| 606 | LTOPasses.add(llvm::createGlobalsModRefPass()); // IP alias analysis. |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 607 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 608 | // Hoist loop invariants. |
| 609 | LTOPasses.add(llvm::createLICMPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 610 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 611 | // Remove redundancies. |
| 612 | LTOPasses.add(llvm::createGVNPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 613 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 614 | // Remove dead memcpys. |
| 615 | LTOPasses.add(llvm::createMemCpyOptPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 616 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 617 | // Nuke dead stores. |
| 618 | LTOPasses.add(llvm::createDeadStoreEliminationPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 619 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 620 | // Cleanup and simplify the code after the scalar optimizations. |
| 621 | LTOPasses.add(llvm::createInstructionCombiningPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 622 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 623 | LTOPasses.add(llvm::createJumpThreadingPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 624 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 625 | // Delete basic blocks, which optimization passes may have killed. |
| 626 | LTOPasses.add(llvm::createCFGSimplificationPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 627 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 628 | // Now that we have optimized the program, discard unreachable functions. |
| 629 | LTOPasses.add(llvm::createGlobalDCEPass()); |
| 630 | |
| 631 | } else { |
| 632 | LTOPasses.add(llvm::createInternalizePass(ExportSymbols)); |
| 633 | LTOPasses.add(llvm::createGlobalOptimizerPass()); |
| 634 | LTOPasses.add(llvm::createConstantMergePass()); |
| 635 | } |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 636 | |
| 637 | LTOPasses.run(*mModule); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 638 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 639 | #if ANDROID_ENGINEERING_BUILD |
| 640 | if (0 != gDebugDumpDirectory) { |
| 641 | std::string errs; |
| 642 | std::string Filename(gDebugDumpDirectory); |
| 643 | Filename += "/post-lto-module.ll"; |
| 644 | llvm::raw_fd_ostream FS(Filename.c_str(), errs); |
| 645 | mModule->print(FS, 0); |
| 646 | FS.close(); |
| 647 | } |
| 648 | #endif |
| 649 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 650 | return 0; |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 654 | #if USE_MCJIT |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 655 | void *Compiler::getSymbolAddress(char const *name) { |
| 656 | return rsloaderGetSymbolAddress(mRSExecutable, name); |
| 657 | } |
| 658 | #endif |
| 659 | |
| 660 | |
| 661 | #if USE_MCJIT |
| 662 | void *Compiler::resolveSymbolAdapter(void *context, char const *name) { |
| 663 | Compiler *self = reinterpret_cast<Compiler *>(context); |
| 664 | |
| 665 | if (void *Addr = FindRuntimeFunction(name)) { |
| 666 | return Addr; |
| 667 | } |
| 668 | |
| 669 | if (self->mpSymbolLookupFn) { |
| 670 | if (void *Addr = self->mpSymbolLookupFn(self->mpSymbolLookupContext, name)) { |
| 671 | return Addr; |
| 672 | } |
| 673 | } |
| 674 | |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 675 | ALOGE("Unable to resolve symbol: %s\n", name); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 676 | return NULL; |
| 677 | } |
| 678 | #endif |
| 679 | |
| 680 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 681 | Compiler::~Compiler() { |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 682 | #if USE_MCJIT |
| 683 | rsloaderDisposeExec(mRSExecutable); |
| 684 | #endif |
| 685 | |
Logan | a4994f5 | 2010-11-27 14:06:02 +0800 | [diff] [blame] | 686 | // llvm::llvm_shutdown(); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 687 | } |
| 688 | |
Shih-wei Liao | 90cd3d1 | 2011-06-20 15:43:34 -0700 | [diff] [blame] | 689 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 690 | } // namespace bcc |