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