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