Home | History | Annotate | Download | only in legacy
      1 //===-LTOCodeGenerator.h - LLVM Link Time Optimizer -----------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file declares the LTOCodeGenerator class.
     11 //
     12 //   LTO compilation consists of three phases: Pre-IPO, IPO and Post-IPO.
     13 //
     14 //   The Pre-IPO phase compiles source code into bitcode file. The resulting
     15 // bitcode files, along with object files and libraries, will be fed to the
     16 // linker to through the IPO and Post-IPO phases. By using obj-file extension,
     17 // the resulting bitcode file disguises itself as an object file, and therefore
     18 // obviates the need of writing a special set of the make-rules only for LTO
     19 // compilation.
     20 //
     21 //   The IPO phase perform inter-procedural analyses and optimizations, and
     22 // the Post-IPO consists two sub-phases: intra-procedural scalar optimizations
     23 // (SOPT), and intra-procedural target-dependent code generator (CG).
     24 //
     25 //   As of this writing, we don't separate IPO and the Post-IPO SOPT. They
     26 // are intermingled together, and are driven by a single pass manager (see
     27 // PassManagerBuilder::populateLTOPassManager()).
     28 //
     29 //   The "LTOCodeGenerator" is the driver for the IPO and Post-IPO stages.
     30 // The "CodeGenerator" here is bit confusing. Don't confuse the "CodeGenerator"
     31 // with the machine specific code generator.
     32 //
     33 //===----------------------------------------------------------------------===//
     34 
     35 #ifndef LLVM_LTO_LTOCODEGENERATOR_H
     36 #define LLVM_LTO_LTOCODEGENERATOR_H
     37 
     38 #include "llvm-c/lto.h"
     39 #include "llvm/ADT/SmallPtrSet.h"
     40 #include "llvm/ADT/StringMap.h"
     41 #include "llvm/ADT/StringSet.h"
     42 #include "llvm/IR/GlobalValue.h"
     43 #include "llvm/IR/Module.h"
     44 #include "llvm/Support/Error.h"
     45 #include "llvm/Support/ToolOutputFile.h"
     46 #include "llvm/Target/TargetMachine.h"
     47 #include "llvm/Target/TargetOptions.h"
     48 #include <string>
     49 #include <vector>
     50 
     51 namespace llvm {
     52 template <typename T> class ArrayRef;
     53   class LLVMContext;
     54   class DiagnosticInfo;
     55   class Linker;
     56   class Mangler;
     57   class MemoryBuffer;
     58   class TargetLibraryInfo;
     59   class TargetMachine;
     60   class raw_ostream;
     61   class raw_pwrite_stream;
     62 
     63 //===----------------------------------------------------------------------===//
     64 /// C++ class which implements the opaque lto_code_gen_t type.
     65 ///
     66 struct LTOCodeGenerator {
     67   static const char *getVersionString();
     68 
     69   LTOCodeGenerator(LLVMContext &Context);
     70   ~LTOCodeGenerator();
     71 
     72   /// Merge given module.  Return true on success.
     73   ///
     74   /// Resets \a HasVerifiedInput.
     75   bool addModule(struct LTOModule *);
     76 
     77   /// Set the destination module.
     78   ///
     79   /// Resets \a HasVerifiedInput.
     80   void setModule(std::unique_ptr<LTOModule> M);
     81 
     82   void setAsmUndefinedRefs(struct LTOModule *);
     83   void setTargetOptions(const TargetOptions &Options);
     84   void setDebugInfo(lto_debug_model);
     85   void setCodePICModel(Optional<Reloc::Model> Model) { RelocModel = Model; }
     86 
     87   /// Set the file type to be emitted (assembly or object code).
     88   /// The default is TargetMachine::CGFT_ObjectFile.
     89   void setFileType(TargetMachine::CodeGenFileType FT) { FileType = FT; }
     90 
     91   void setCpu(StringRef MCpu) { this->MCpu = MCpu; }
     92   void setAttr(StringRef MAttr) { this->MAttr = MAttr; }
     93   void setOptLevel(unsigned OptLevel);
     94 
     95   void setShouldInternalize(bool Value) { ShouldInternalize = Value; }
     96   void setShouldEmbedUselists(bool Value) { ShouldEmbedUselists = Value; }
     97 
     98   /// Restore linkage of globals
     99   ///
    100   /// When set, the linkage of globals will be restored prior to code
    101   /// generation. That is, a global symbol that had external linkage prior to
    102   /// LTO will be emitted with external linkage again; and a local will remain
    103   /// local. Note that this option only affects the end result - globals may
    104   /// still be internalized in the process of LTO and may be modified and/or
    105   /// deleted where legal.
    106   ///
    107   /// The default behavior will internalize globals (unless on the preserve
    108   /// list) and, if parallel code generation is enabled, will externalize
    109   /// all locals.
    110   void setShouldRestoreGlobalsLinkage(bool Value) {
    111     ShouldRestoreGlobalsLinkage = Value;
    112   }
    113 
    114   void addMustPreserveSymbol(StringRef Sym) { MustPreserveSymbols[Sym] = 1; }
    115 
    116   /// Pass options to the driver and optimization passes.
    117   ///
    118   /// These options are not necessarily for debugging purpose (the function
    119   /// name is misleading).  This function should be called before
    120   /// LTOCodeGenerator::compilexxx(), and
    121   /// LTOCodeGenerator::writeMergedModules().
    122   void setCodeGenDebugOptions(StringRef Opts);
    123 
    124   /// Parse the options set in setCodeGenDebugOptions.
    125   ///
    126   /// Like \a setCodeGenDebugOptions(), this must be called before
    127   /// LTOCodeGenerator::compilexxx() and
    128   /// LTOCodeGenerator::writeMergedModules().
    129   void parseCodeGenDebugOptions();
    130 
    131   /// Write the merged module to the file specified by the given path.  Return
    132   /// true on success.
    133   ///
    134   /// Calls \a verifyMergedModuleOnce().
    135   bool writeMergedModules(StringRef Path);
    136 
    137   /// Compile the merged module into a *single* output file; the path to output
    138   /// file is returned to the caller via argument "name". Return true on
    139   /// success.
    140   ///
    141   /// \note It is up to the linker to remove the intermediate output file.  Do
    142   /// not try to remove the object file in LTOCodeGenerator's destructor as we
    143   /// don't who (LTOCodeGenerator or the output file) will last longer.
    144   bool compile_to_file(const char **Name, bool DisableVerify,
    145                        bool DisableInline, bool DisableGVNLoadPRE,
    146                        bool DisableVectorization);
    147 
    148   /// As with compile_to_file(), this function compiles the merged module into
    149   /// single output file. Instead of returning the output file path to the
    150   /// caller (linker), it brings the output to a buffer, and returns the buffer
    151   /// to the caller. This function should delete the intermediate file once
    152   /// its content is brought to memory. Return NULL if the compilation was not
    153   /// successful.
    154   std::unique_ptr<MemoryBuffer> compile(bool DisableVerify, bool DisableInline,
    155                                         bool DisableGVNLoadPRE,
    156                                         bool DisableVectorization);
    157 
    158   /// Optimizes the merged module.  Returns true on success.
    159   ///
    160   /// Calls \a verifyMergedModuleOnce().
    161   bool optimize(bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE,
    162                 bool DisableVectorization);
    163 
    164   /// Compiles the merged optimized module into a single output file. It brings
    165   /// the output to a buffer, and returns the buffer to the caller. Return NULL
    166   /// if the compilation was not successful.
    167   std::unique_ptr<MemoryBuffer> compileOptimized();
    168 
    169   /// Compile the merged optimized module into out.size() output files each
    170   /// representing a linkable partition of the module. If out contains more
    171   /// than one element, code generation is done in parallel with out.size()
    172   /// threads.  Output files will be written to members of out. Returns true on
    173   /// success.
    174   ///
    175   /// Calls \a verifyMergedModuleOnce().
    176   bool compileOptimized(ArrayRef<raw_pwrite_stream *> Out);
    177 
    178   /// Enable the Freestanding mode: indicate that the optimizer should not
    179   /// assume builtins are present on the target.
    180   void setFreestanding(bool Enabled) { Freestanding = Enabled; }
    181 
    182   void setDiagnosticHandler(lto_diagnostic_handler_t, void *);
    183 
    184   LLVMContext &getContext() { return Context; }
    185 
    186   void resetMergedModule() { MergedModule.reset(); }
    187 
    188 private:
    189   void initializeLTOPasses();
    190 
    191   /// Verify the merged module on first call.
    192   ///
    193   /// Sets \a HasVerifiedInput on first call and doesn't run again on the same
    194   /// input.
    195   void verifyMergedModuleOnce();
    196 
    197   bool compileOptimizedToFile(const char **Name);
    198   void restoreLinkageForExternals();
    199   void applyScopeRestrictions();
    200   void preserveDiscardableGVs(
    201       Module &TheModule,
    202       llvm::function_ref<bool(const GlobalValue &)> mustPreserveGV);
    203 
    204   bool determineTarget();
    205   std::unique_ptr<TargetMachine> createTargetMachine();
    206 
    207   static void DiagnosticHandler(const DiagnosticInfo &DI, void *Context);
    208 
    209   void DiagnosticHandler2(const DiagnosticInfo &DI);
    210 
    211   void emitError(const std::string &ErrMsg);
    212   void emitWarning(const std::string &ErrMsg);
    213 
    214   void finishOptimizationRemarks();
    215 
    216   LLVMContext &Context;
    217   std::unique_ptr<Module> MergedModule;
    218   std::unique_ptr<Linker> TheLinker;
    219   std::unique_ptr<TargetMachine> TargetMach;
    220   bool EmitDwarfDebugInfo = false;
    221   bool ScopeRestrictionsDone = false;
    222   bool HasVerifiedInput = false;
    223   Optional<Reloc::Model> RelocModel;
    224   StringSet<> MustPreserveSymbols;
    225   StringSet<> AsmUndefinedRefs;
    226   StringMap<GlobalValue::LinkageTypes> ExternalSymbols;
    227   std::vector<std::string> CodegenOptions;
    228   std::string FeatureStr;
    229   std::string MCpu;
    230   std::string MAttr;
    231   std::string NativeObjectPath;
    232   TargetOptions Options;
    233   CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default;
    234   const Target *MArch = nullptr;
    235   std::string TripleStr;
    236   unsigned OptLevel = 2;
    237   lto_diagnostic_handler_t DiagHandler = nullptr;
    238   void *DiagContext = nullptr;
    239   bool ShouldInternalize = true;
    240   bool ShouldEmbedUselists = false;
    241   bool ShouldRestoreGlobalsLinkage = false;
    242   TargetMachine::CodeGenFileType FileType = TargetMachine::CGFT_ObjectFile;
    243   std::unique_ptr<tool_output_file> DiagnosticOutputFile;
    244   bool Freestanding = false;
    245 };
    246 }
    247 #endif
    248