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